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 ;
2830import org .springframework .security .oauth2 .jwt .Jwt ;
2931
@@ -41,7 +43,7 @@ public class HttpAuthorizationProvider implements AuthorizationProvider {
4143 * The default subject claim is the key within the Authentication object where the user's identity
4244 * can be found
4345 */
44- private final String DEFAULT_SUBJECT_CLAIM = "email" ;
46+ private final String subjectClaim ;
4547
4648 /**
4749 * Initializes the HTTPAuthorizationProvider
@@ -58,26 +60,29 @@ public HttpAuthorizationProvider(Map<String, String> options) {
5860 ApiClient apiClient = new ApiClient ();
5961 apiClient .setBasePath (options .get ("authorizationUrl" ));
6062 this .defaultApiClient = new DefaultApi (apiClient );
63+ subjectClaim = options .get ("subjectClaim" );
6164 }
6265
6366 /**
64- * Validates whether a user has access to a project
67+ * Validates whether a user has access to a project. @Cacheable is using {@link
68+ * CacheConfiguration} settings to cache output of the method {@link AuthorizationResult} for a
69+ * specified duration set in cache settings.
6570 *
6671 * @param projectId Name of the Feast project
6772 * @param authentication Spring Security Authentication object
6873 * @return AuthorizationResult result of authorization query
6974 */
75+ @ Cacheable (value = CacheConfiguration .AUTHORIZATION_CACHE , keyGenerator = "authKeyGenerator" )
7076 public AuthorizationResult checkAccessToProject (String projectId , Authentication authentication ) {
7177
7278 CheckAccessRequest checkAccessRequest = new CheckAccessRequest ();
7379 Object context = getContext (authentication );
74- String subject = getSubjectFromAuth (authentication , DEFAULT_SUBJECT_CLAIM );
80+ String subject = AuthUtils . getSubjectFromAuth (authentication , subjectClaim );
7581 String resource = "projects:" + projectId ;
7682 checkAccessRequest .setAction ("ALL" );
7783 checkAccessRequest .setContext (context );
7884 checkAccessRequest .setResource (resource );
7985 checkAccessRequest .setSubject (subject );
80-
8186 try {
8287 Jwt credentials = ((Jwt ) authentication .getCredentials ());
8388 // Make authorization request to external service
@@ -114,31 +119,4 @@ private Object getContext(Authentication authentication) {
114119 // Not implemented yet, left empty
115120 return new Object ();
116121 }
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- }
144122}
0 commit comments