Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.keycloak.adapters.tomcat7;
  
  import java.util.Set;
  

Author(s):
Davide Ungari
Version:
$Revision: 1 $
 
     private static final Logger log = Logger.getLogger(""+CatalinaRequestAuthenticator.class);
     protected KeycloakAuthenticatorValve valve;
     protected Request request;
 
     public CatalinaRequestAuthenticator(KeycloakDeployment deployment,
                                         KeycloakAuthenticatorValve valveCatalinaUserSessionManagement userSessionManagement,
                                         CatalinaHttpFacade facade,
                                         Request request) {
         super(facadedeploymentrequest.getConnector().getRedirectPort());
         this. = valve;
         this. = userSessionManagement;
         this. = request;
     }
 
     @Override
         return new OAuthRequestAuthenticator() {
             @Override
             protected void saveRequest() {
                 try {
                     .keycloakSaveRequest();
                 } catch (IOException e) {
                     throw new RuntimeException(e);
                 }
             }
         };
     }
 
     @Override
     protected void completeOAuthAuthentication(KeycloakPrincipal skpRefreshableKeycloakSecurityContext securityContext) {
         Set<Stringroles = getRolesFromToken(securityContext);
         GenericPrincipal principal = new CatalinaSecurityContextHelper().createPrincipal(.getContext().getRealm(), skprolessecurityContext);
         Session session = .getSessionInternal(true);
         session.setPrincipal(principal);
         session.setAuthType("OAUTH");
         session.setNote(KeycloakSecurityContext.class.getName(), securityContext);
         String username = securityContext.getToken().getSubject();
         .finer("userSessionManage.login: " + username);
         .login(sessionusernamesecurityContext.getToken().getSessionState());
     }
 
     @Override
     protected void completeBearerAuthentication(KeycloakPrincipal principalRefreshableKeycloakSecurityContext securityContext) {
         Set<Stringroles = getRolesFromToken(securityContext);
         for (String role : roles) {
             .info("Bearer role: " + role);
         }
         Principal generalPrincipal = new CatalinaSecurityContextHelper().createPrincipal(.getContext().getRealm(), principalrolessecurityContext);
         .setUserPrincipal(generalPrincipal);
         .setAuthType("KEYCLOAK");
         .setAttribute(KeycloakSecurityContext.class.getName(), securityContext);
     }
 
         Set<Stringroles = null;
         if (.isUseResourceRoleMappings()) {
             AccessToken.Access access = session.getToken().getResourceAccess(.getResourceName());
             if (access != nullroles = access.getRoles();
         } else {
             AccessToken.Access access =  session.getToken().getRealmAccess();
             if (access != nullroles = access.getRoles();
         }
         if (roles == nullroles = Collections.emptySet();
         return roles;
     }
 
     @Override
     protected boolean isCached() {
         if (.getSessionInternal(false) == null || .getSessionInternal().getPrincipal() == null)
             return false;
         .finer("remote logged in already");
         .setUserPrincipal(principal);
        .setAuthType("KEYCLOAK");
        Session session = .getSessionInternal();
        if (session != null) {
            if (securityContext != null) {
                securityContext.setDeployment();
                .setAttribute(KeycloakSecurityContext.class.getName(), securityContext);
            }
        }
        restoreRequest();
        return true;
    }
    protected void restoreRequest() {
        if (.getSessionInternal().getNote(.) != null) {
            if (.keycloakRestoreRequest()) {
                .finer("restoreRequest");
            } else {
                .finer("Restore of original request failed");
                throw new RuntimeException("Restore of original request failed");
            }
        }
    }
New to GrepCode? Check out our FAQ X