Search in sources :

Example 1 with REALM

use of org.sonatype.nexus.security.JwtHelper.REALM in project nexus-public by sonatype.

the class JwtSecurityFilter method createSubject.

@Override
protected WebSubject createSubject(final ServletRequest request, final ServletResponse response) {
    Cookie[] cookies = ((HttpServletRequest) request).getCookies();
    if (cookies != null) {
        Optional<Cookie> jwtCookie = stream(cookies).filter(cookie -> cookie.getName().equals(JWT_COOKIE_NAME)).findFirst();
        if (jwtCookie.isPresent()) {
            Cookie cookie = jwtCookie.get();
            SimpleSession session = new SimpleSession(request.getRemoteHost());
            DecodedJWT decodedJwt;
            String jwt = cookie.getValue();
            if (!Strings2.isEmpty(jwt)) {
                try {
                    decodedJwt = jwtHelper.verifyJwt(jwt);
                } catch (JwtVerificationException e) {
                    log.debug("Expire and reset the JWT cookie due to the error: {}", e.getMessage());
                    cookie.setValue("");
                    cookie.setMaxAge(0);
                    WebUtils.toHttp(response).addCookie(cookie);
                    return super.createSubject(request, response);
                }
                Claim user = decodedJwt.getClaim(USER);
                Claim realm = decodedJwt.getClaim(REALM);
                PrincipalCollection principals = new SimplePrincipalCollection(user.asString(), realm.asString());
                session.setTimeout(TimeUnit.SECONDS.toMillis(jwtHelper.getExpirySeconds()));
                session.setAttribute(JWT_COOKIE_NAME, jwt);
                return new WebDelegatingSubject(principals, true, request.getRemoteHost(), session, true, request, response, getSecurityManager());
            }
        }
    }
    return super.createSubject(request, response);
}
Also used : Cookie(javax.servlet.http.Cookie) REALM(org.sonatype.nexus.security.JwtHelper.REALM) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) JwtVerificationException(org.sonatype.nexus.security.jwt.JwtVerificationException) WebSecurityManager(org.apache.shiro.web.mgt.WebSecurityManager) WebSubject(org.apache.shiro.web.subject.WebSubject) LoggerFactory(org.slf4j.LoggerFactory) WebDelegatingSubject(org.apache.shiro.web.subject.support.WebDelegatingSubject) Singleton(javax.inject.Singleton) JWT_COOKIE_NAME(org.sonatype.nexus.security.JwtHelper.JWT_COOKIE_NAME) Inject(javax.inject.Inject) HttpServletRequest(javax.servlet.http.HttpServletRequest) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) Cookie(javax.servlet.http.Cookie) Claim(com.auth0.jwt.interfaces.Claim) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimpleSession(org.apache.shiro.session.mgt.SimpleSession) ServletRequest(javax.servlet.ServletRequest) Logger(org.slf4j.Logger) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) FilterChainResolver(org.apache.shiro.web.filter.mgt.FilterChainResolver) WebUtils(org.apache.shiro.web.util.WebUtils) TimeUnit(java.util.concurrent.TimeUnit) Strings2(org.sonatype.nexus.common.text.Strings2) ServletResponse(javax.servlet.ServletResponse) USER(org.sonatype.nexus.security.JwtHelper.USER) Optional(java.util.Optional) Arrays.stream(java.util.Arrays.stream) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) JwtVerificationException(org.sonatype.nexus.security.jwt.JwtVerificationException) WebDelegatingSubject(org.apache.shiro.web.subject.support.WebDelegatingSubject) HttpServletRequest(javax.servlet.http.HttpServletRequest) SimpleSession(org.apache.shiro.session.mgt.SimpleSession) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Claim(com.auth0.jwt.interfaces.Claim)

Aggregations

Claim (com.auth0.jwt.interfaces.Claim)1 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Arrays.stream (java.util.Arrays.stream)1 Optional (java.util.Optional)1 TimeUnit (java.util.concurrent.TimeUnit)1 Inject (javax.inject.Inject)1 Singleton (javax.inject.Singleton)1 ServletRequest (javax.servlet.ServletRequest)1 ServletResponse (javax.servlet.ServletResponse)1 Cookie (javax.servlet.http.Cookie)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 SimpleSession (org.apache.shiro.session.mgt.SimpleSession)1 PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)1 SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)1 FilterChainResolver (org.apache.shiro.web.filter.mgt.FilterChainResolver)1 WebSecurityManager (org.apache.shiro.web.mgt.WebSecurityManager)1 WebSubject (org.apache.shiro.web.subject.WebSubject)1 WebDelegatingSubject (org.apache.shiro.web.subject.support.WebDelegatingSubject)1 WebUtils (org.apache.shiro.web.util.WebUtils)1