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.handlers;
19
20 import io.undertow.security.api.AuthenticationMode;
21 import io.undertow.security.api.SecurityContext;
22 import io.undertow.security.api.SecurityContextFactory;
23 import io.undertow.security.idm.IdentityManager;
24 import io.undertow.security.impl.SecurityContextFactoryImpl;
25 import io.undertow.server.HttpHandler;
26 import io.undertow.server.HttpServerExchange;
27
28 /**
29  * The security handler responsible for attaching the SecurityContext to the current {@link HttpServerExchange}.
30  *
31  * This handler is called early in the processing of the incoming request, subsequently supported authentication mechanisms will
32  * be added to the context, a decision will then be made if authentication is required or optional and the associated mechanisms
33  * will be called.
34  *
35  * In addition to the HTTPExchange authentication state can also be associated with the
36  * {@link io.undertow.server.protocol.http.HttpServerConnection} and with the {@link io.undertow.server.session.Session} however this is
37  * mechanism specific so it is down to the actual mechanisms to decide if there is state that can be re-used.
38  *
39  * @author <a href="mailto:darran.lofthouse@jboss.com">Darran Lofthouse</a>
40  */

41 @SuppressWarnings("deprecation")
42 public class SecurityInitialHandler extends AbstractSecurityContextAssociationHandler {
43
44     private final AuthenticationMode authenticationMode;
45     private final IdentityManager identityManager;
46     private final String programaticMechName;
47     private final SecurityContextFactory contextFactory;
48
49     public SecurityInitialHandler(final AuthenticationMode authenticationMode, final IdentityManager identityManager,
50             final String programaticMechName, final SecurityContextFactory contextFactory, final HttpHandler next) {
51         super(next);
52         this.authenticationMode = authenticationMode;
53         this.identityManager = identityManager;
54         this.programaticMechName = programaticMechName;
55         this.contextFactory = contextFactory;
56     }
57
58     public SecurityInitialHandler(final AuthenticationMode authenticationMode, final IdentityManager identityManager,
59             final String programaticMechName, final HttpHandler next) {
60         this(authenticationMode, identityManager, programaticMechName, SecurityContextFactoryImpl.INSTANCE, next);
61     }
62
63     public SecurityInitialHandler(final AuthenticationMode authenticationMode, final IdentityManager identityManager,
64             final HttpHandler next) {
65         this(authenticationMode, identityManager, null, SecurityContextFactoryImpl.INSTANCE, next);
66     }
67
68     /**
69      * @see io.undertow.security.handlers.AbstractSecurityContextAssociationHandler#createSecurityContext
70      */

71     @Override
72     public SecurityContext createSecurityContext(final HttpServerExchange exchange) {
73         return contextFactory.createSecurityContext(exchange, authenticationMode, identityManager, programaticMechName);
74     }
75
76
77 }
78