2121import java .util .Optional ;
2222import java .util .Set ;
2323
24+ import static com .microsoft .azure .spring .autoconfigure .aad .AADOAuth2ErrorCode .CONDITIONAL_ACCESS_POLICY ;
25+ import static com .microsoft .azure .spring .autoconfigure .aad .AADOAuth2ErrorCode .INVALID_REQUEST ;
26+ import static com .microsoft .azure .spring .autoconfigure .aad .AADOAuth2ErrorCode .SERVER_SERVER ;
27+
2428/**
2529 * This implementation will retrieve group info of user from Microsoft Graph and map groups to {@link GrantedAuthority}.
2630 */
2731public class AADOAuth2UserService implements OAuth2UserService <OidcUserRequest , OidcUser > {
28- private static final String CONDITIONAL_ACCESS_POLICY = "conditional_access_policy" ;
29- private static final String INVALID_REQUEST = "invalid_request" ;
30- private static final String SERVER_ERROR = "server_error" ;
31- private static final String DEFAULT_USERNAME_ATTR_NAME = "name" ;
32-
33- private AADAuthenticationProperties aadAuthProps ;
34- private ServiceEndpointsProperties serviceEndpointsProps ;
35- private OidcUserService oidcUserService ;
32+ private final AADAuthenticationProperties aadAuthenticationProperties ;
33+ private final ServiceEndpointsProperties serviceEndpointsProperties ;
34+ private final OidcUserService oidcUserService ;
3635
37- public AADOAuth2UserService (AADAuthenticationProperties aadAuthProps ,
38- ServiceEndpointsProperties serviceEndpointsProps ) {
39- this .aadAuthProps = aadAuthProps ;
40- this .serviceEndpointsProps = serviceEndpointsProps ;
36+ public AADOAuth2UserService (AADAuthenticationProperties aadAuthenticationProperties ,
37+ ServiceEndpointsProperties serviceEndpointsProperties ) {
38+ this .aadAuthenticationProperties = aadAuthenticationProperties ;
39+ this .serviceEndpointsProperties = serviceEndpointsProperties ;
4140 this .oidcUserService = new OidcUserService ();
4241 }
4342
@@ -50,25 +49,28 @@ public OidcUser loadUser(OidcUserRequest userRequest) throws OAuth2Authenticatio
5049 // https://github.com/MicrosoftDocs/azure-docs/issues/8121#issuecomment-387090099
5150 // In AAD App Registration configure oauth2AllowImplicitFlow to true
5251 final ClientRegistration registration = userRequest .getClientRegistration ();
53- final AzureADGraphClient graphClient = new AzureADGraphClient (
52+ final AzureADGraphClient azureADGraphClient = new AzureADGraphClient (
5453 registration .getClientId (),
5554 registration .getClientSecret (),
56- aadAuthProps ,
57- serviceEndpointsProps
55+ aadAuthenticationProperties ,
56+ serviceEndpointsProperties
5857 );
59- String graphApiToken = graphClient
60- .acquireTokenForGraphApi (userRequest .getIdToken ().getTokenValue (), aadAuthProps .getTenantId ())
58+ String graphApiToken = azureADGraphClient
59+ .acquireTokenForGraphApi (
60+ userRequest .getIdToken ().getTokenValue (),
61+ aadAuthenticationProperties .getTenantId ()
62+ )
6163 .accessToken ();
62- mappedAuthorities = graphClient .getGrantedAuthorities (graphApiToken );
64+ mappedAuthorities = azureADGraphClient .getGrantedAuthorities (graphApiToken );
6365 } catch (MalformedURLException e ) {
64- throw wrapException (INVALID_REQUEST , "Failed to acquire token for Graph API." , null , e );
66+ throw toOAuth2AuthenticationException (INVALID_REQUEST , "Failed to acquire token for Graph API." , e );
6567 } catch (ServiceUnavailableException e ) {
66- throw wrapException ( SERVER_ERROR , "Failed to acquire token for Graph API." , null , e );
68+ throw toOAuth2AuthenticationException ( SERVER_SERVER , "Failed to acquire token for Graph API." , e );
6769 } catch (IOException e ) {
68- throw wrapException ( SERVER_ERROR , "Failed to map group to authorities." , null , e );
70+ throw toOAuth2AuthenticationException ( SERVER_SERVER , "Failed to map group to authorities." , e );
6971 } catch (MsalServiceException e ) {
7072 if (e .claims () != null && !e .claims ().isEmpty ()) {
71- throw wrapException (CONDITIONAL_ACCESS_POLICY , "Handle conditional access policy" , null , e );
73+ throw toOAuth2AuthenticationException (CONDITIONAL_ACCESS_POLICY , "Handle conditional access policy" , e );
7274 } else {
7375 throw e ;
7476 }
@@ -79,13 +81,15 @@ public OidcUser loadUser(OidcUserRequest userRequest) throws OAuth2Authenticatio
7981 .map (ClientRegistration .ProviderDetails ::getUserInfoEndpoint )
8082 .map (ClientRegistration .ProviderDetails .UserInfoEndpoint ::getUserNameAttributeName )
8183 .filter (s -> !s .isEmpty ())
82- .orElse (DEFAULT_USERNAME_ATTR_NAME );
84+ .orElse (AADAccessTokenClaim . NAME );
8385 // Create a copy of oidcUser but use the mappedAuthorities instead
8486 return new DefaultOidcUser (mappedAuthorities , oidcUser .getIdToken (), nameAttributeKey );
8587 }
8688
87- private OAuth2AuthenticationException wrapException (String errorCode , String errDesc , String uri , Exception e ) {
88- final OAuth2Error oAuth2Error = new OAuth2Error (errorCode , errDesc , uri );
89- throw new OAuth2AuthenticationException (oAuth2Error , e );
89+ private OAuth2AuthenticationException toOAuth2AuthenticationException (String errorCode ,
90+ String description ,
91+ Exception cause ) {
92+ OAuth2Error oAuth2Error = new OAuth2Error (errorCode , description , null );
93+ return new OAuth2AuthenticationException (oAuth2Error , cause );
9094 }
9195}
0 commit comments