1   /*
2    * Copyright 2018 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.internal.api.v1;
18  
19  import static com.google.common.base.Preconditions.checkArgument;
20  import static java.util.Objects.requireNonNull;
21  
22  import java.time.Instant;
23  import java.time.temporal.ChronoUnit;
24  
25  import com.fasterxml.jackson.annotation.JsonCreator;
26  import com.fasterxml.jackson.annotation.JsonProperty;
27  import com.google.common.base.Ascii;
28  import com.google.common.base.MoreObjects;
29  
30  /**
31   * An OAuth 2.0 access token.
32   *
33   * @see <a href="https://tools.ietf.org/html/rfc6749#section-4.2.2">Access Token Response</a>
34   */
35  public class AccessToken {
36  
37      private static final String BEARER = "Bearer";
38  
39      private final String accessToken;
40  
41      /**
42       * The lifetime in seconds of the access token. For example, the value "3600" denotes that the access
43       * token will expire in one hour from the time the response was generated. If omitted, the authorization
44       * server SHOULD provide the expiration time via other means or document the default value.
45       */
46      private final long expiresIn;
47  
48      private final String refreshToken;
49  
50      private final Instant deadline;
51  
52      //TODO(minwoox) Add scope if needed.
53  
54      public AccessToken(String accessToken, long expiresIn) {
55          this(BEARER, accessToken, expiresIn, "");
56      }
57  
58      @JsonCreator
59      public AccessToken(@JsonProperty("token_type") String tokenType,
60                         @JsonProperty("access_token") String accessToken,
61                         @JsonProperty("expires_in") long expiresIn,
62                         @JsonProperty("refresh_token") String refreshToken) {
63          requireNonNull(tokenType, "tokenType");
64          checkArgument(Ascii.equalsIgnoreCase(tokenType, BEARER),
65                        "tokenType: %s (expected: %s)", tokenType, BEARER);
66          this.accessToken = requireNonNull(accessToken, "accessToken");
67          this.expiresIn = expiresIn;
68          this.refreshToken = requireNonNull(refreshToken, "refreshToken");
69  
70          deadline = Instant.now().plus(expiresIn, ChronoUnit.SECONDS);
71      }
72  
73      @JsonProperty("access_token")
74      public String accessToken() {
75          return accessToken;
76      }
77  
78      @JsonProperty("expires_in")
79      public long expiresIn() {
80          return expiresIn;
81      }
82  
83      @JsonProperty("token_type")
84      public String tokenType() {
85          return BEARER;
86      }
87  
88      @JsonProperty("refresh_token")
89      public String refreshToken() {
90          return refreshToken;
91      }
92  
93      public Instant deadline() {
94          return deadline;
95      }
96  
97      @Override
98      public String toString() {
99          return MoreObjects.toStringHelper(this)
100                           .add("accessToken", accessToken)
101                           .add("expiresIn", expiresIn)
102                           .add("tokenType", BEARER)
103                           .add("deadline", deadline)
104                           .toString();
105     }
106 }