-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(auth): fix remaining nullability in UserAuthorizer and Builder #14158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
04d869c
5473d28
aa5cdf9
4415d89
aaf1235
9d91637
7fcbf4a
c74668d
670cc5e
4483370
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,8 +40,8 @@ | |
| import com.google.api.client.json.JsonObjectParser; | ||
| import com.google.api.client.util.GenericData; | ||
| import com.google.api.client.util.Joiner; | ||
| import com.google.api.client.util.Preconditions; | ||
| import com.google.auth.http.HttpTransportFactory; | ||
| import com.google.common.base.Preconditions; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
| import java.io.IOException; | ||
|
|
@@ -76,8 +76,8 @@ public enum ClientAuthenticationType { | |
|
|
||
| static final URI DEFAULT_CALLBACK_URI = URI.create("/oauth2callback"); | ||
|
|
||
| private final String TOKEN_STORE_ERROR = "Error parsing stored token data."; | ||
| private final String FETCH_TOKEN_ERROR = "Error reading result of Token API:"; | ||
| private static final String TOKEN_STORE_ERROR = "Error parsing stored token data."; | ||
| private static final String FETCH_TOKEN_ERROR = "Error reading result of Token API:"; | ||
|
|
||
| private final ClientId clientId; | ||
| private final Collection<String> scopes; | ||
|
|
@@ -249,7 +249,7 @@ public URL getAuthorizationUrl( | |
| GenericJson tokenJson = OAuth2Utils.parseJson(tokenData); | ||
| String accessTokenValue = | ||
| OAuth2Utils.validateString(tokenJson, "access_token", TOKEN_STORE_ERROR); | ||
| Long expirationMillis = | ||
| long expirationMillis = | ||
| OAuth2Utils.validateLong(tokenJson, "expiration_time_millis", TOKEN_STORE_ERROR); | ||
| Date expirationTime = new Date(expirationMillis); | ||
|
lqiu96 marked this conversation as resolved.
|
||
| List<String> scopes = | ||
|
|
@@ -414,21 +414,23 @@ public void revokeAuthorization(String userId) throws IOException { | |
| */ | ||
| public void storeCredentials(String userId, UserCredentials credentials) throws IOException { | ||
| AccessToken accessToken = credentials.getAccessToken(); | ||
| String acessTokenValue = null; | ||
| String accessTokenValue = null; | ||
| Date expiresBy = null; | ||
| List<String> grantedScopes = new ArrayList<>(); | ||
|
|
||
| if (accessToken != null) { | ||
| acessTokenValue = accessToken.getTokenValue(); | ||
| accessTokenValue = accessToken.getTokenValue(); | ||
| expiresBy = accessToken.getExpirationTime(); | ||
| grantedScopes = accessToken.getScopes(); | ||
| } | ||
| String refreshToken = credentials.getRefreshToken(); | ||
| GenericJson tokenStateJson = new GenericJson(); | ||
| tokenStateJson.setFactory(OAuth2Utils.JSON_FACTORY); | ||
| tokenStateJson.put("access_token", acessTokenValue); | ||
| tokenStateJson.put("access_token", accessTokenValue); | ||
| tokenStateJson.put(OAuth2Utils.TOKEN_RESPONSE_SCOPE, grantedScopes); | ||
| tokenStateJson.put("expiration_time_millis", expiresBy.getTime()); | ||
| if (expiresBy != null) { | ||
| tokenStateJson.put("expiration_time_millis", expiresBy.getTime()); | ||
| } | ||
|
Comment on lines
+431
to
+433
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can't ensure that expiresBy is non-null because accessToken may null here |
||
| if (refreshToken != null) { | ||
| tokenStateJson.put("refresh_token", refreshToken); | ||
| } | ||
|
|
@@ -498,7 +500,7 @@ private TokenResponseWithConfig getCredentialsFromCodeInternal( | |
| String accessTokenValue = | ||
| OAuth2Utils.validateString(parsedTokens, "access_token", FETCH_TOKEN_ERROR); | ||
| int expiresInSecs = OAuth2Utils.validateInt32(parsedTokens, "expires_in", FETCH_TOKEN_ERROR); | ||
| Date expirationTime = new Date(new Date().getTime() + expiresInSecs * 1000); | ||
| Date expirationTime = new Date(new Date().getTime() + expiresInSecs * 1000L); | ||
| String scopes = | ||
| OAuth2Utils.validateOptionalString( | ||
| parsedTokens, OAuth2Utils.TOKEN_RESPONSE_SCOPE, FETCH_TOKEN_ERROR); | ||
|
|
@@ -551,15 +553,15 @@ public Builder toBuilder() { | |
|
|
||
| public static class Builder { | ||
|
|
||
| private ClientId clientId; | ||
| private TokenStore tokenStore; | ||
| private URI callbackUri; | ||
| private URI tokenServerUri; | ||
| private URI userAuthUri; | ||
| private Collection<String> scopes; | ||
| private HttpTransportFactory transportFactory; | ||
| private @Nullable ClientId clientId; | ||
| private @Nullable TokenStore tokenStore; | ||
| private @Nullable URI callbackUri; | ||
| private @Nullable URI tokenServerUri; | ||
| private @Nullable URI userAuthUri; | ||
| private @Nullable Collection<String> scopes; | ||
| private @Nullable HttpTransportFactory transportFactory; | ||
| private @Nullable PKCEProvider pkce; | ||
| private ClientAuthenticationType clientAuthenticationType; | ||
| private @Nullable ClientAuthenticationType clientAuthenticationType; | ||
|
|
||
| protected Builder() {} | ||
|
|
||
|
|
@@ -674,7 +676,6 @@ public Builder setPKCEProvider(@Nullable PKCEProvider pkce) { | |
| if (pkce.getCodeChallenge() == null | ||
| || pkce.getCodeVerifier() == null | ||
| || pkce.getCodeChallengeMethod() == null) { | ||
|
|
||
| throw new IllegalArgumentException( | ||
| "PKCE provider contained null implementations. PKCE object must implement all" | ||
| + " PKCEProvider methods."); | ||
|
|
@@ -698,39 +699,39 @@ public Builder setClientAuthenticationType(ClientAuthenticationType clientAuthen | |
| return this; | ||
| } | ||
|
|
||
| public ClientId getClientId() { | ||
| public @Nullable ClientId getClientId() { | ||
| return clientId; | ||
| } | ||
|
|
||
| public TokenStore getTokenStore() { | ||
| public @Nullable TokenStore getTokenStore() { | ||
| return tokenStore; | ||
| } | ||
|
|
||
| public Collection<String> getScopes() { | ||
| public @Nullable Collection<String> getScopes() { | ||
| return scopes; | ||
| } | ||
|
|
||
| public URI getTokenServerUri() { | ||
| public @Nullable URI getTokenServerUri() { | ||
| return tokenServerUri; | ||
| } | ||
|
|
||
| public URI getCallbackUri() { | ||
| public @Nullable URI getCallbackUri() { | ||
| return callbackUri; | ||
| } | ||
|
|
||
| public URI getUserAuthUri() { | ||
| public @Nullable URI getUserAuthUri() { | ||
| return userAuthUri; | ||
| } | ||
|
|
||
| public HttpTransportFactory getHttpTransportFactory() { | ||
| public @Nullable HttpTransportFactory getHttpTransportFactory() { | ||
| return transportFactory; | ||
| } | ||
|
|
||
| public @Nullable PKCEProvider getPKCEProvider() { | ||
| return pkce; | ||
| } | ||
|
|
||
| public ClientAuthenticationType getClientAuthenticationType() { | ||
| public @Nullable ClientAuthenticationType getClientAuthenticationType() { | ||
| return clientAuthenticationType; | ||
| } | ||
|
|
||
|
|
@@ -771,18 +772,18 @@ public UserAuthorizer build() { | |
| public static class TokenResponseWithConfig { | ||
|
|
||
| private final String clientId; | ||
| private final String clientSecret; | ||
| private final String refreshToken; | ||
| private final @Nullable String clientSecret; | ||
| private final @Nullable String refreshToken; | ||
| private final AccessToken accessToken; | ||
| private URI tokenServerUri; | ||
| private final URI tokenServerUri; | ||
| private final HttpTransportFactory httpTransportFactory; | ||
|
|
||
| private TokenResponseWithConfig(Builder builder) { | ||
| this.clientId = builder.clientId; | ||
| this.clientId = Preconditions.checkNotNull(builder.clientId); | ||
| this.clientSecret = builder.clientSecret; | ||
| this.accessToken = builder.accessToken; | ||
| this.httpTransportFactory = builder.httpTransportFactory; | ||
| this.tokenServerUri = builder.tokenServerUri; | ||
| this.accessToken = Preconditions.checkNotNull(builder.accessToken); | ||
| this.httpTransportFactory = Preconditions.checkNotNull(builder.httpTransportFactory); | ||
| this.tokenServerUri = Preconditions.checkNotNull(builder.tokenServerUri); | ||
|
Comment on lines
+782
to
+786
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these are passed in from UserAuthorizer which enforces them to be non-null
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TokenResponseWithConfig#Builder's setters are all package-private and internal usages of this ensures that clientId, accessToken, httpTransportFactory, and tokenServerUri are non-null (default value is created) |
||
| this.refreshToken = builder.refreshToken; | ||
| } | ||
|
|
||
|
|
@@ -800,7 +801,7 @@ public String getClientId() { | |
| * | ||
| * @return The client secret. | ||
| */ | ||
| public String getClientSecret() { | ||
| public @Nullable String getClientSecret() { | ||
| return clientSecret; | ||
| } | ||
|
|
||
|
|
@@ -836,8 +837,7 @@ public URI getTokenServerUri() { | |
| * | ||
| * @return The refresh token, or null if not granted. | ||
| */ | ||
| @Nullable | ||
| public String getRefreshToken() { | ||
| public @Nullable String getRefreshToken() { | ||
|
Comment on lines
804
to
+840
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turns out Oauth2Utils#validateOptionalString can return null if it doesn't exist. So these two fields use it and it can be null |
||
| return refreshToken; | ||
| } | ||
|
|
||
|
|
@@ -846,12 +846,12 @@ static Builder newBuilder() { | |
| } | ||
|
|
||
| static class Builder { | ||
| private String clientId; | ||
| private String clientSecret; | ||
| private String refreshToken; | ||
| private AccessToken accessToken; | ||
| private URI tokenServerUri; | ||
| private HttpTransportFactory httpTransportFactory; | ||
| private @Nullable String clientId; | ||
| private @Nullable String clientSecret; | ||
| private @Nullable String refreshToken; | ||
| private @Nullable AccessToken accessToken; | ||
| private @Nullable URI tokenServerUri; | ||
| private @Nullable HttpTransportFactory httpTransportFactory; | ||
|
|
||
| @CanIgnoreReturnValue | ||
| Builder setClientId(String clientId) { | ||
|
|
@@ -860,13 +860,13 @@ Builder setClientId(String clientId) { | |
| } | ||
|
|
||
| @CanIgnoreReturnValue | ||
| Builder setClientSecret(String clientSecret) { | ||
| Builder setClientSecret(@Nullable String clientSecret) { | ||
| this.clientSecret = clientSecret; | ||
| return this; | ||
| } | ||
|
|
||
| @CanIgnoreReturnValue | ||
| Builder setRefreshToken(String refreshToken) { | ||
| Builder setRefreshToken(@Nullable String refreshToken) { | ||
| this.refreshToken = refreshToken; | ||
| return this; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OAuth2Utils.validateString ensures that it's non-null