1616 */
1717package feast .auth .authorization ;
1818
19+ import feast .auth .config .CacheConfiguration ;
1920import feast .auth .generated .client .api .DefaultApi ;
2021import feast .auth .generated .client .invoker .ApiClient ;
2122import feast .auth .generated .client .invoker .ApiException ;
2223import feast .auth .generated .client .model .CheckAccessRequest ;
24+ import feast .auth .utils .AuthUtils ;
2325import java .util .Map ;
24- import org .hibernate .validator .internal .constraintvalidators .bv .EmailValidator ;
2526import org .slf4j .Logger ;
2627import org .slf4j .LoggerFactory ;
28+ import org .springframework .cache .annotation .Cacheable ;
2729import org .springframework .security .core .Authentication ;
28- import org .springframework .security .oauth2 .jwt .Jwt ;
2930
3031/**
3132 * HTTPAuthorizationProvider uses an external HTTP service for authorizing requests. Please see
@@ -41,7 +42,7 @@ public class HttpAuthorizationProvider implements AuthorizationProvider {
4142 * The default subject claim is the key within the Authentication object where the user's identity
4243 * can be found
4344 */
44- private final String DEFAULT_SUBJECT_CLAIM = "email" ;
45+ private final String subjectClaim ;
4546
4647 /**
4748 * Initializes the HTTPAuthorizationProvider
@@ -58,26 +59,29 @@ public HttpAuthorizationProvider(Map<String, String> options) {
5859 ApiClient apiClient = new ApiClient ();
5960 apiClient .setBasePath (options .get ("authorizationUrl" ));
6061 this .defaultApiClient = new DefaultApi (apiClient );
62+ subjectClaim = options .get ("subjectClaim" );
6163 }
6264
6365 /**
64- * Validates whether a user has access to a project
66+ * Validates whether a user has access to a project. @Cacheable is using {@link
67+ * CacheConfiguration} settings to cache output of the method {@link AuthorizationResult} for a
68+ * specified duration set in cache settings.
6569 *
6670 * @param projectId Name of the Feast project
6771 * @param authentication Spring Security Authentication object
6872 * @return AuthorizationResult result of authorization query
6973 */
74+ @ Cacheable (value = CacheConfiguration .AUTHORIZATION_CACHE )
7075 public AuthorizationResult checkAccessToProject (String projectId , Authentication authentication ) {
7176
7277 CheckAccessRequest checkAccessRequest = new CheckAccessRequest ();
7378 Object context = getContext (authentication );
74- String subject = getSubjectFromAuth (authentication , DEFAULT_SUBJECT_CLAIM );
79+ String subject = AuthUtils . getSubjectFromAuth (authentication , subjectClaim );
7580 String resource = "projects:" + projectId ;
7681 checkAccessRequest .setAction ("ALL" );
7782 checkAccessRequest .setContext (context );
7883 checkAccessRequest .setResource (resource );
7984 checkAccessRequest .setSubject (subject );
80-
8185 try {
8286 Jwt credentials = ((Jwt ) authentication .getCredentials ());
8387 // Make authorization request to external service
@@ -114,31 +118,4 @@ private Object getContext(Authentication authentication) {
114118 // Not implemented yet, left empty
115119 return new Object ();
116120 }
117-
118- /**
119- * Get user email from their authentication object.
120- *
121- * @param authentication Spring Security Authentication object, used to extract user details
122- * @param subjectClaim Indicates the claim where the subject can be found
123- * @return String user email
124- */
125- private String getSubjectFromAuth (Authentication authentication , String subjectClaim ) {
126- Jwt principle = ((Jwt ) authentication .getPrincipal ());
127- Map <String , Object > claims = principle .getClaims ();
128- String subjectValue = (String ) claims .get (subjectClaim );
129-
130- if (subjectValue .isEmpty ()) {
131- throw new IllegalStateException (
132- String .format ("JWT does not have a valid claim %s." , subjectClaim ));
133- }
134-
135- if (subjectClaim .equals ("email" )) {
136- boolean validEmail = (new EmailValidator ()).isValid (subjectValue , null );
137- if (!validEmail ) {
138- throw new IllegalStateException ("JWT contains an invalid email address" );
139- }
140- }
141-
142- return subjectValue ;
143- }
144121}
0 commit comments