Split out datastore implementation from aaa-shiro
[aaa.git] / aaa-shiro / impl / src / main / java / org / opendaylight / aaa / AAAShiroProvider.java
1 /*
2  * Copyright © 2017 Brocade Communications Systems and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.aaa;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.concurrent.CompletableFuture;
13 import javax.servlet.ServletException;
14 import org.opendaylight.aaa.api.AuthenticationService;
15 import org.opendaylight.aaa.api.IDMStoreException;
16 import org.opendaylight.aaa.api.IIDMStore;
17 import org.opendaylight.aaa.api.PasswordCredentialAuth;
18 import org.opendaylight.aaa.api.StoreBuilder;
19 import org.opendaylight.aaa.api.TokenStore;
20 import org.opendaylight.aaa.api.password.service.PasswordHashService;
21 import org.opendaylight.aaa.cert.api.ICertificateManager;
22 import org.opendaylight.aaa.datastore.h2.H2TokenStore;
23 import org.opendaylight.aaa.tokenauthrealm.auth.HttpBasicAuth;
24 import org.opendaylight.aaa.tokenauthrealm.auth.TokenAuthenticators;
25 import org.opendaylight.mdsal.binding.api.DataBroker;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.aaa.app.config.rev170619.DatastoreConfig;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.aaa.app.config.rev170619.ShiroConfiguration;
28 import org.osgi.service.http.HttpService;
29 import org.osgi.service.http.NamespaceException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Provider for AAA shiro implementation.
35  */
36 public final class AAAShiroProvider {
37
38     private static final Logger LOG = LoggerFactory.getLogger(AAAShiroProvider.class);
39
40     public static final CompletableFuture<AAAShiroProvider> INSTANCE_FUTURE = new CompletableFuture<>();
41
42     private final DataBroker dataBroker;
43     private final ICertificateManager certificateManager;
44     private final HttpService httpService;
45     private final TokenStore tokenStore;
46     private final ShiroConfiguration shiroConfiguration;
47     private final String moonEndpointPath;
48     private final TokenAuthenticators tokenAuthenticators;
49     private final AuthenticationService authenticationService;
50     private final PasswordHashService passwordHashService;
51
52     /**
53      * Constructor.
54      */
55     public AAAShiroProvider(final DataBroker dataBroker,
56                             final ICertificateManager certificateManager,
57                             final PasswordCredentialAuth credentialAuth,
58                             final ShiroConfiguration shiroConfiguration,
59                             final HttpService httpService,
60                             final String moonEndpointPath,
61                             final String oauth2EndpointPath,
62                             final DatastoreConfig datastoreConfig,
63                             final IIDMStore iidmStore,
64                             final AuthenticationService authenticationService,
65                             final PasswordHashService passwordHashService) {
66         this.dataBroker = dataBroker;
67         this.certificateManager = certificateManager;
68         this.shiroConfiguration = shiroConfiguration;
69         this.httpService = httpService;
70         this.moonEndpointPath = moonEndpointPath;
71         this.authenticationService = authenticationService;
72         this.passwordHashService = passwordHashService;
73
74         if (datastoreConfig == null || !datastoreConfig.getStore().equals(DatastoreConfig.Store.H2DataStore)) {
75             LOG.info("AAA Datastore has not been initialized");
76             tokenStore = null;
77             tokenAuthenticators = new TokenAuthenticators();
78             return;
79         }
80
81         tokenStore = new H2TokenStore(datastoreConfig.getTimeToLive().longValue(),
82                 datastoreConfig.getTimeToWait().longValue());
83
84         initializeIIDMStore(iidmStore);
85
86         tokenAuthenticators = new TokenAuthenticators(new HttpBasicAuth(credentialAuth));
87
88         try {
89             this.registerServletContexts();
90         } catch (final ServletException | NamespaceException e) {
91             LOG.warn("Could not initialize AAA servlet endpoints", e);
92         }
93
94         INSTANCE_FUTURE.complete(this);
95     }
96
97     private void registerServletContexts() throws ServletException, NamespaceException {
98         LOG.info("attempting registration of AAA moon servlet");
99         requireNonNull(httpService, "httpService cannot be null").registerServlet(moonEndpointPath,
100             new org.opendaylight.aaa.shiro.moon.MoonTokenEndpoint(), null, null);
101     }
102
103     private static void initializeIIDMStore(final IIDMStore iidmStore) {
104         try {
105             new StoreBuilder(iidmStore).initWithDefaultUsers(IIDMStore.DEFAULT_DOMAIN);
106         } catch (final IDMStoreException e) {
107             LOG.error("Failed to initialize data in store", e);
108         }
109     }
110
111     /**
112      * Method called when the blueprint container is created.
113      */
114     public void init() {
115         LOG.info("AAAShiroProvider Session Initiated");
116     }
117
118     /**
119      * Method called when the blueprint container is destroyed.
120      */
121     public void close() {
122         LOG.info("AAAShiroProvider Closed");
123         if (httpService != null) {
124             httpService.unregister(moonEndpointPath);
125         }
126     }
127
128     /**
129      * Extract the data broker.
130      *
131      * @return the data broker
132      */
133     public DataBroker getDataBroker() {
134         return this.dataBroker;
135     }
136
137     /**
138      * Extract the certificate manager.
139      *
140      * @return the certificate manager.
141      */
142     public ICertificateManager getCertificateManager() {
143         return certificateManager;
144     }
145
146     /**
147      * Extract Shiro related configuration.
148      *
149      * @return Shiro related configuration.
150      */
151     public ShiroConfiguration getShiroConfiguration() {
152         return this.shiroConfiguration;
153     }
154
155     public TokenStore getTokenStore() {
156         return tokenStore;
157     }
158
159     public TokenAuthenticators getTokenAuthenticators() {
160         return tokenAuthenticators;
161     }
162
163     public AuthenticationService getAuthenticationService() {
164         return authenticationService;
165     }
166
167     public PasswordHashService getPasswordHashService() {
168         return passwordHashService;
169     }
170 }