@@ -43,7 +43,6 @@ public class DefaultOAuth2ApiService implements IcebergRestOAuth2ApiService {
43
43
44
44
private static final Logger LOGGER = LoggerFactory .getLogger (DefaultOAuth2ApiService .class );
45
45
46
- private static final String CLIENT_CREDENTIALS = "client_credentials" ;
47
46
private static final String BEARER = "bearer" ;
48
47
49
48
private final TokenBrokerFactory tokenBrokerFactory ;
@@ -75,43 +74,39 @@ public Response getToken(
75
74
if (!tokenBroker .supportsRequestedTokenType (requestedTokenType )) {
76
75
return OAuthUtils .getResponseFromError (OAuthTokenErrorResponse .Error .invalid_request );
77
76
}
78
- if (authHeader == null && clientId == null ) {
77
+ if (authHeader == null && clientSecret == null ) {
79
78
return OAuthUtils .getResponseFromError (OAuthTokenErrorResponse .Error .invalid_client );
80
79
}
81
- if (authHeader != null && clientId == null && authHeader .startsWith ("Basic " )) {
80
+ // token exchange with client id and client secret in the authorization header means the client
81
+ // has previously attempted to refresh an access token, but refreshing was not supported by the
82
+ // token broker. Accept the client id and secret and treat it as a new token request
83
+ if (authHeader != null && clientSecret == null && authHeader .startsWith ("Basic " )) {
82
84
String credentials = new String (Base64 .decodeBase64 (authHeader .substring (6 )), UTF_8 );
83
85
if (!credentials .contains (":" )) {
84
- return OAuthUtils .getResponseFromError (OAuthTokenErrorResponse .Error .invalid_client );
86
+ return OAuthUtils .getResponseFromError (OAuthTokenErrorResponse .Error .invalid_request );
85
87
}
86
88
LOGGER .debug ("Found credentials in auth header - treating as client_credentials" );
87
89
String [] parts = credentials .split (":" , 2 );
88
- clientId = parts [0 ];
89
- clientSecret = parts [1 ];
90
+ if (parts .length == 2 ) {
91
+ clientId = parts [0 ];
92
+ clientSecret = parts [1 ];
93
+ } else {
94
+ LOGGER .debug ("Don't know how to parse Basic auth header" );
95
+ return OAuthUtils .getResponseFromError (OAuthTokenErrorResponse .Error .invalid_request );
96
+ }
97
+ }
98
+ TokenResponse tokenResponse ;
99
+ if (clientSecret != null ) {
100
+ tokenResponse =
101
+ tokenBroker .generateFromClientSecrets (
102
+ clientId , clientSecret , grantType , scope , requestedTokenType );
103
+ } else if (subjectToken != null ) {
104
+ tokenResponse =
105
+ tokenBroker .generateFromToken (
106
+ subjectTokenType , subjectToken , grantType , scope , requestedTokenType );
107
+ } else {
108
+ return OAuthUtils .getResponseFromError (OAuthTokenErrorResponse .Error .invalid_request );
90
109
}
91
- TokenResponse tokenResponse =
92
- switch (subjectTokenType ) {
93
- case TokenType .ID_TOKEN ,
94
- TokenType .REFRESH_TOKEN ,
95
- TokenType .JWT ,
96
- TokenType .SAML1 ,
97
- TokenType .SAML2 ->
98
- new TokenResponse (OAuthTokenErrorResponse .Error .invalid_request );
99
- case TokenType .ACCESS_TOKEN -> {
100
- // token exchange with client id and client secret means the client has previously
101
- // attempted to refresh
102
- // an access token, but refreshing was not supported by the token broker. Accept the
103
- // client id and
104
- // secret and treat it as a new token request
105
- if (clientId != null && clientSecret != null ) {
106
- yield tokenBroker .generateFromClientSecrets (
107
- clientId , clientSecret , CLIENT_CREDENTIALS , scope );
108
- } else {
109
- yield tokenBroker .generateFromToken (subjectTokenType , subjectToken , grantType , scope );
110
- }
111
- }
112
- case null ->
113
- tokenBroker .generateFromClientSecrets (clientId , clientSecret , grantType , scope );
114
- };
115
110
if (tokenResponse == null ) {
116
111
return OAuthUtils .getResponseFromError (OAuthTokenErrorResponse .Error .unsupported_grant_type );
117
112
}
0 commit comments