CustomAuthorizer implements Authorizer { @Override public boolean authorize(Session session, Operation operation, Resource resource) { try { log.info("Starting Authorization."); log.info("Operation request Info: {}", operation.toString()); log.info("Resource request Info: {}", resource.toString()); if (!(session.principal() instanceof CustomPrincipal)) { log.error("Session Principal is not using the proper class. Should be instance of CustomPrincipal."); return false; } CustomPrincipal principal = (CustomPrincipal) session.principal(); if (principal.getOauthBearerTokenJwt() == null) { log.error("Custom Principal does not contain token information."); return false; } OAuthBearerTokenJwt jwt = principal.getOauthBearerTokenJwt(); if (jwt.scope() == null || jwt.scope().isEmpty()) { log.error("No scopes provided in JWT. Unable to Authorize."); return false; } java.util.Set<String> scopes = jwt.scope(); List<OAuthScope> scopeInfo = parseScopes(scopes); String operationStr = operation.toJava().toString(); return checkAuthorization(scopeInfo, resource, operationStr); } catch (Exception e) { log.error("Error in authorization. ", e); } return false; } CustomAuthorizer(); @Override boolean authorize(Session session, Operation operation, Resource resource); @Override void addAcls(Set<Acl> acls, Resource resource); @Override boolean removeAcls(Set<Acl> acls, Resource resource); @Override boolean removeAcls(Resource resource); @Override Set<Acl> getAcls(Resource resource); @Override Map<Resource, Set<Acl>> getAcls(KafkaPrincipal principal); @Override Map<Resource, Set<Acl>> getAcls(); @Override void close(); @Override void configure(java.util.Map<String, ?> map);  }
@Test public void authorize() { Mockito.when(session.principal()).thenReturn(customPrincipal); Mockito.when(customPrincipal.getOauthBearerTokenJwt()).thenReturn(jwt); Set<String> set = new HashSet<>(); set.add("urn:kafka:topic:test:write"); Mockito.when(jwt.scope()).thenReturn(set); Mockito.when(resource.name()).thenReturn("test"); Mockito.when(resource.resourceType()).thenReturn(ResourceType.fromString("topic")); Mockito.when(operation.toJava()).thenReturn(AclOperation.fromString("write")); boolean result = customAuthorizer.authorize(session, operation, resource); assertTrue(result); }