1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2014 Red Hat, Inc., and individual contributors
4  * as indicated by the @author tags.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */

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 /**
29  * An {@link AuthenticationMechanism} which uses any cached {@link AuthenticatedSession}s.
30  *
31  * @author <a href="mailto:darran.lofthouse@jboss.com">Darran Lofthouse</a>
32  */

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                 // We know we had a previously authenticated account but for some reason the IdentityManager is no longer
70                 // accepting it, we now
71                 return AuthenticationMechanismOutcome.NOT_ATTEMPTED;
72             }
73         } else {
74             // It is possible an AuthenticatedSessionManager could have been available even if there was no chance of it
75             // loading a session.
76             return AuthenticationMechanismOutcome.NOT_ATTEMPTED;
77         }
78
79     }
80
81     @Override
82     public ChallengeResult sendChallenge(HttpServerExchange exchange, SecurityContext securityContext) {
83         // This mechanism can only use what is already available and can not send a challenge of it's own.
84         return ChallengeResult.NOT_SENT;
85     }
86
87 }
88