LCOV - code coverage report
Current view: top level - extensions/auth/oauth - OAuthToken.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 18 30 60.0 %
Date: 2022-11-19 15:00:39 Functions: 7 11 63.6 %

          Line data    Source code
       1             : // Copyright (C) 2015 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.extensions.auth.oauth;
      16             : 
      17             : import static java.util.Objects.requireNonNull;
      18             : 
      19             : import com.google.common.base.MoreObjects;
      20             : import com.google.common.base.Strings;
      21             : import com.google.gerrit.common.Nullable;
      22             : import java.io.Serializable;
      23             : import java.util.Objects;
      24             : 
      25             : /**
      26             :  * OAuth token.
      27             :  *
      28             :  * <p>Only implements {@link Serializable} for backwards compatibility; new extensions should not
      29             :  * depend on the serialized format.
      30             :  */
      31             : public class OAuthToken implements Serializable {
      32             : 
      33             :   private static final long serialVersionUID = 1L;
      34             : 
      35             :   private final String token;
      36             :   private final String secret;
      37             :   private final String raw;
      38             : 
      39             :   /**
      40             :    * Time of expiration of this token, or {@code Long#MAX_VALUE} if this token never expires, or
      41             :    * time of expiration is unknown.
      42             :    */
      43             :   private final long expiresAt;
      44             : 
      45             :   /**
      46             :    * The identifier of the OAuth provider that issued this token in the form {@code
      47             :    * "plugin-name:provider-name"}, or {@code null}. The empty string {@code ""} is treated the same
      48             :    * as {@code null}.
      49             :    */
      50             :   private final String providerId;
      51             : 
      52             :   public OAuthToken(String token, String secret, String raw) {
      53           0 :     this(token, secret, raw, Long.MAX_VALUE, null);
      54           0 :   }
      55             : 
      56             :   public OAuthToken(
      57           1 :       String token, String secret, String raw, long expiresAt, @Nullable String providerId) {
      58           1 :     this.token = requireNonNull(token, "token");
      59           1 :     this.secret = requireNonNull(secret, "secret");
      60           1 :     this.raw = requireNonNull(raw, "raw");
      61           1 :     this.expiresAt = expiresAt;
      62           1 :     this.providerId = Strings.emptyToNull(providerId);
      63           1 :   }
      64             : 
      65             :   public String getToken() {
      66           1 :     return token;
      67             :   }
      68             : 
      69             :   public String getSecret() {
      70           1 :     return secret;
      71             :   }
      72             : 
      73             :   public String getRaw() {
      74           1 :     return raw;
      75             :   }
      76             : 
      77             :   public long getExpiresAt() {
      78           1 :     return expiresAt;
      79             :   }
      80             : 
      81             :   public boolean isExpired() {
      82           0 :     return System.currentTimeMillis() > expiresAt;
      83             :   }
      84             : 
      85             :   @Nullable
      86             :   public String getProviderId() {
      87           1 :     return providerId;
      88             :   }
      89             : 
      90             :   @Override
      91             :   public boolean equals(Object o) {
      92           1 :     if (!(o instanceof OAuthToken)) {
      93           0 :       return false;
      94             :     }
      95           1 :     OAuthToken t = (OAuthToken) o;
      96           1 :     return token.equals(t.token)
      97           1 :         && secret.equals(t.secret)
      98           1 :         && raw.equals(t.raw)
      99             :         && expiresAt == t.expiresAt
     100           1 :         && Objects.equals(providerId, t.providerId);
     101             :   }
     102             : 
     103             :   @Override
     104             :   public int hashCode() {
     105           0 :     return Objects.hash(token, secret, raw, expiresAt, providerId);
     106             :   }
     107             : 
     108             :   @Override
     109             :   public String toString() {
     110           0 :     return MoreObjects.toStringHelper(this)
     111           0 :         .add("token", token)
     112           0 :         .add("secret", secret)
     113           0 :         .add("raw", raw)
     114           0 :         .add("expiresAt", expiresAt)
     115           0 :         .add("providerId", providerId)
     116           0 :         .toString();
     117             :   }
     118             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750