1
18 package io.undertow.security.impl;
19
20 import io.undertow.security.api.AuthenticatedSessionManager;
21 import io.undertow.security.api.AuthenticatedSessionManager.AuthenticatedSession;
22 import io.undertow.security.api.AuthenticationMechanism;
23 import io.undertow.security.api.SecurityContext;
24 import io.undertow.security.idm.Account;
25 import io.undertow.security.idm.IdentityManager;
26 import io.undertow.server.HttpServerExchange;
27
28
33 public class CachedAuthenticatedSessionMechanism implements AuthenticationMechanism {
34
35 private final IdentityManager identityManager;
36
37 public CachedAuthenticatedSessionMechanism() {
38 this(null);
39 }
40
41 public CachedAuthenticatedSessionMechanism(final IdentityManager identityManager) {
42 this.identityManager = identityManager;
43 }
44
45 @SuppressWarnings("deprecation")
46 private IdentityManager getIdentityManager(SecurityContext securityContext) {
47 return identityManager != null ? identityManager : securityContext.getIdentityManager();
48 }
49
50 @Override
51 public AuthenticationMechanismOutcome authenticate(HttpServerExchange exchange, SecurityContext securityContext) {
52 AuthenticatedSessionManager sessionManager = exchange.getAttachment(AuthenticatedSessionManager.ATTACHMENT_KEY);
53 if (sessionManager != null) {
54 return runCached(exchange, securityContext, sessionManager);
55 } else {
56 return AuthenticationMechanismOutcome.NOT_ATTEMPTED;
57 }
58 }
59
60 public AuthenticationMechanismOutcome runCached(final HttpServerExchange exchange, final SecurityContext securityContext, final AuthenticatedSessionManager sessionManager) {
61 AuthenticatedSession authSession = sessionManager.lookupSession(exchange);
62 if (authSession != null) {
63 Account account = getIdentityManager(securityContext).verify(authSession.getAccount());
64 if (account != null) {
65 securityContext.authenticationComplete(account, authSession.getMechanism(), false);
66 return AuthenticationMechanismOutcome.AUTHENTICATED;
67 } else {
68 sessionManager.clearSession(exchange);
69
70
71 return AuthenticationMechanismOutcome.NOT_ATTEMPTED;
72 }
73 } else {
74
75
76 return AuthenticationMechanismOutcome.NOT_ATTEMPTED;
77 }
78
79 }
80
81 @Override
82 public ChallengeResult sendChallenge(HttpServerExchange exchange, SecurityContext securityContext) {
83
84 return ChallengeResult.NOT_SENT;
85 }
86
87 }
88