1616 * specific language governing permissions and limitations
1717 * under the License.
1818 */
19- package org .apache .polaris .service .auth ;
19+ package org .apache .polaris .service .auth . internal . broker ;
2020
2121import com .auth0 .jwt .JWT ;
2222import com .auth0 .jwt .algorithms .Algorithm ;
3232import org .apache .polaris .core .entity .PrincipalEntity ;
3333import org .apache .polaris .core .persistence .PolarisMetaStoreManager ;
3434import org .apache .polaris .core .persistence .dao .entity .EntityResult ;
35- import org .apache .polaris .service .auth .OAuthTokenErrorResponse .Error ;
35+ import org .apache .polaris .core .persistence .dao .entity .PrincipalSecretsResult ;
36+ import org .apache .polaris .service .auth .DefaultAuthenticator ;
37+ import org .apache .polaris .service .auth .PolarisCredential ;
38+ import org .apache .polaris .service .auth .internal .service .OAuthError ;
3639import org .apache .polaris .service .types .TokenType ;
3740import org .slf4j .Logger ;
3841import org .slf4j .LoggerFactory ;
@@ -88,35 +91,35 @@ public TokenResponse generateFromToken(
8891 PolarisCallContext polarisCallContext ,
8992 TokenType requestedTokenType ) {
9093 if (requestedTokenType != null && !TokenType .ACCESS_TOKEN .equals (requestedTokenType )) {
91- return new TokenResponse ( OAuthTokenErrorResponse . Error .invalid_request );
94+ return TokenResponse . of ( OAuthError .invalid_request );
9295 }
9396 if (!TokenType .ACCESS_TOKEN .equals (subjectTokenType )) {
94- return new TokenResponse ( OAuthTokenErrorResponse . Error .invalid_request );
97+ return TokenResponse . of ( OAuthError .invalid_request );
9598 }
9699 if (subjectToken == null || subjectToken .isBlank ()) {
97- return new TokenResponse ( OAuthTokenErrorResponse . Error .invalid_request );
100+ return TokenResponse . of ( OAuthError .invalid_request );
98101 }
99102 InternalPolarisToken decodedToken ;
100103 try {
101104 decodedToken = verifyInternal (subjectToken );
102105 } catch (NotAuthorizedException e ) {
103106 LOGGER .error ("Failed to verify the token" , e .getCause ());
104- return new TokenResponse ( Error .invalid_client );
107+ return TokenResponse . of ( OAuthError .invalid_client );
105108 }
106109 EntityResult principalLookup =
107110 metaStoreManager .loadEntity (
108111 polarisCallContext , 0L , decodedToken .getPrincipalId (), PolarisEntityType .PRINCIPAL );
109112 if (!principalLookup .isSuccess ()
110113 || principalLookup .getEntity ().getType () != PolarisEntityType .PRINCIPAL ) {
111- return new TokenResponse ( OAuthTokenErrorResponse . Error .unauthorized_client );
114+ return TokenResponse . of ( OAuthError .unauthorized_client );
112115 }
113116 String tokenString =
114117 generateTokenString (
115118 decodedToken .getPrincipalName (),
116119 decodedToken .getPrincipalId (),
117120 decodedToken .getClientId (),
118121 decodedToken .getScope ());
119- return new TokenResponse (
122+ return TokenResponse . of (
120123 tokenString , TokenType .ACCESS_TOKEN .getValue (), maxTokenGenerationInSeconds );
121124 }
122125
@@ -130,21 +133,20 @@ public TokenResponse generateFromClientSecrets(
130133 TokenType requestedTokenType ) {
131134 // Initial sanity checks
132135 TokenRequestValidator validator = new TokenRequestValidator ();
133- Optional <OAuthTokenErrorResponse . Error > initialValidationResponse =
136+ Optional <OAuthError > initialValidationResponse =
134137 validator .validateForClientCredentialsFlow (clientId , clientSecret , grantType , scope );
135138 if (initialValidationResponse .isPresent ()) {
136- return new TokenResponse (initialValidationResponse .get ());
139+ return TokenResponse . of (initialValidationResponse .get ());
137140 }
138141
139142 Optional <PrincipalEntity > principal =
140- TokenBroker .findPrincipalEntity (
141- metaStoreManager , clientId , clientSecret , polarisCallContext );
143+ findPrincipalEntity (clientId , clientSecret , polarisCallContext );
142144 if (principal .isEmpty ()) {
143- return new TokenResponse ( OAuthTokenErrorResponse . Error .unauthorized_client );
145+ return TokenResponse . of ( OAuthError .unauthorized_client );
144146 }
145147 String tokenString =
146148 generateTokenString (principal .get ().getName (), principal .get ().getId (), clientId , scope );
147- return new TokenResponse (
149+ return TokenResponse . of (
148150 tokenString , TokenType .ACCESS_TOKEN .getValue (), maxTokenGenerationInSeconds );
149151 }
150152
@@ -177,4 +179,27 @@ public boolean supportsRequestedTokenType(TokenType tokenType) {
177179 private String scopes (String scope ) {
178180 return scope == null || scope .isBlank () ? DefaultAuthenticator .PRINCIPAL_ROLE_ALL : scope ;
179181 }
182+
183+ private Optional <PrincipalEntity > findPrincipalEntity (
184+ String clientId , String clientSecret , PolarisCallContext polarisCallContext ) {
185+ // Validate the principal is present and secrets match
186+ PrincipalSecretsResult principalSecrets =
187+ metaStoreManager .loadPrincipalSecrets (polarisCallContext , clientId );
188+ if (!principalSecrets .isSuccess ()) {
189+ return Optional .empty ();
190+ }
191+ if (!principalSecrets .getPrincipalSecrets ().matchesSecret (clientSecret )) {
192+ return Optional .empty ();
193+ }
194+ EntityResult result =
195+ metaStoreManager .loadEntity (
196+ polarisCallContext ,
197+ 0L ,
198+ principalSecrets .getPrincipalSecrets ().getPrincipalId (),
199+ PolarisEntityType .PRINCIPAL );
200+ if (!result .isSuccess () || result .getEntity ().getType () != PolarisEntityType .PRINCIPAL ) {
201+ return Optional .empty ();
202+ }
203+ return Optional .of (PrincipalEntity .of (result .getEntity ()));
204+ }
180205}
0 commit comments