• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 Google LLC
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *    * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *    * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *
15  *    * Neither the name of Google LLC nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 package com.google.auth.oauth2;
33 
34 import static com.google.common.base.Preconditions.checkNotNull;
35 
36 import com.google.errorprone.annotations.CanIgnoreReturnValue;
37 import java.util.ArrayList;
38 import java.util.Date;
39 import java.util.List;
40 import javax.annotation.Nullable;
41 
42 /**
43  * Defines an OAuth 2.0 token exchange successful response. Based on
44  * https://tools.ietf.org/html/rfc8693#section-2.2.1.
45  */
46 final class StsTokenExchangeResponse {
47   private final AccessToken accessToken;
48   private final String issuedTokenType;
49   private final String tokenType;
50 
51   @Nullable private final Long expiresInSeconds;
52   @Nullable private final String refreshToken;
53   @Nullable private final List<String> scopes;
54 
StsTokenExchangeResponse( String accessToken, String issuedTokenType, String tokenType, @Nullable Long expiresInSeconds, @Nullable String refreshToken, @Nullable List<String> scopes)55   private StsTokenExchangeResponse(
56       String accessToken,
57       String issuedTokenType,
58       String tokenType,
59       @Nullable Long expiresInSeconds,
60       @Nullable String refreshToken,
61       @Nullable List<String> scopes) {
62     checkNotNull(accessToken);
63 
64     this.expiresInSeconds = expiresInSeconds;
65     Long expiresAtMilliseconds =
66         expiresInSeconds == null ? null : System.currentTimeMillis() + expiresInSeconds * 1000L;
67     Date date = expiresAtMilliseconds == null ? null : new Date(expiresAtMilliseconds);
68     this.accessToken = new AccessToken(accessToken, date);
69 
70     this.issuedTokenType = checkNotNull(issuedTokenType);
71     this.tokenType = checkNotNull(tokenType);
72     this.refreshToken = refreshToken;
73     this.scopes = scopes;
74   }
75 
newBuilder(String accessToken, String issuedTokenType, String tokenType)76   public static Builder newBuilder(String accessToken, String issuedTokenType, String tokenType) {
77     return new Builder(accessToken, issuedTokenType, tokenType);
78   }
79 
getAccessToken()80   public AccessToken getAccessToken() {
81     return accessToken;
82   }
83 
getIssuedTokenType()84   public String getIssuedTokenType() {
85     return issuedTokenType;
86   }
87 
getTokenType()88   public String getTokenType() {
89     return tokenType;
90   }
91 
92   @Nullable
getExpiresInSeconds()93   public Long getExpiresInSeconds() {
94     return expiresInSeconds;
95   }
96 
97   @Nullable
getRefreshToken()98   public String getRefreshToken() {
99     return refreshToken;
100   }
101 
102   @Nullable
getScopes()103   public List<String> getScopes() {
104     if (scopes == null) {
105       return null;
106     }
107     return new ArrayList<>(scopes);
108   }
109 
110   public static class Builder {
111     private final String accessToken;
112     private final String issuedTokenType;
113     private final String tokenType;
114 
115     @Nullable private Long expiresInSeconds;
116     @Nullable private String refreshToken;
117     @Nullable private List<String> scopes;
118 
Builder(String accessToken, String issuedTokenType, String tokenType)119     private Builder(String accessToken, String issuedTokenType, String tokenType) {
120       this.accessToken = accessToken;
121       this.issuedTokenType = issuedTokenType;
122       this.tokenType = tokenType;
123     }
124 
125     @CanIgnoreReturnValue
setExpiresInSeconds(long expiresInSeconds)126     public StsTokenExchangeResponse.Builder setExpiresInSeconds(long expiresInSeconds) {
127       this.expiresInSeconds = expiresInSeconds;
128       return this;
129     }
130 
131     @CanIgnoreReturnValue
setRefreshToken(String refreshToken)132     public StsTokenExchangeResponse.Builder setRefreshToken(String refreshToken) {
133       this.refreshToken = refreshToken;
134       return this;
135     }
136 
137     @CanIgnoreReturnValue
setScopes(List<String> scopes)138     public StsTokenExchangeResponse.Builder setScopes(List<String> scopes) {
139       if (scopes != null) {
140         this.scopes = new ArrayList<>(scopes);
141       }
142       return this;
143     }
144 
build()145     public StsTokenExchangeResponse build() {
146       return new StsTokenExchangeResponse(
147           accessToken, issuedTokenType, tokenType, expiresInSeconds, refreshToken, scopes);
148     }
149   }
150 }
151