2 * Copyright © 2017 Brocade Communications Systems and others. All rights reserved.
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
8 package org.opendaylight.aaa;
10 import static java.util.Objects.requireNonNull;
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;
34 * Provider for AAA shiro implementation.
36 public final class AAAShiroProvider {
38 private static final Logger LOG = LoggerFactory.getLogger(AAAShiroProvider.class);
40 public static final CompletableFuture<AAAShiroProvider> INSTANCE_FUTURE = new CompletableFuture<>();
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;
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;
74 if (datastoreConfig == null || !datastoreConfig.getStore().equals(DatastoreConfig.Store.H2DataStore)) {
75 LOG.info("AAA Datastore has not been initialized");
77 tokenAuthenticators = new TokenAuthenticators();
81 tokenStore = new H2TokenStore(datastoreConfig.getTimeToLive().longValue(),
82 datastoreConfig.getTimeToWait().longValue());
84 initializeIIDMStore(iidmStore);
86 tokenAuthenticators = new TokenAuthenticators(new HttpBasicAuth(credentialAuth));
89 this.registerServletContexts();
90 } catch (final ServletException | NamespaceException e) {
91 LOG.warn("Could not initialize AAA servlet endpoints", e);
94 INSTANCE_FUTURE.complete(this);
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);
103 private static void initializeIIDMStore(final IIDMStore iidmStore) {
105 new StoreBuilder(iidmStore).initWithDefaultUsers(IIDMStore.DEFAULT_DOMAIN);
106 } catch (final IDMStoreException e) {
107 LOG.error("Failed to initialize data in store", e);
112 * Method called when the blueprint container is created.
115 LOG.info("AAAShiroProvider Session Initiated");
119 * Method called when the blueprint container is destroyed.
121 public void close() {
122 LOG.info("AAAShiroProvider Closed");
123 if (httpService != null) {
124 httpService.unregister(moonEndpointPath);
129 * Extract the data broker.
131 * @return the data broker
133 public DataBroker getDataBroker() {
134 return this.dataBroker;
138 * Extract the certificate manager.
140 * @return the certificate manager.
142 public ICertificateManager getCertificateManager() {
143 return certificateManager;
147 * Extract Shiro related configuration.
149 * @return Shiro related configuration.
151 public ShiroConfiguration getShiroConfiguration() {
152 return this.shiroConfiguration;
155 public TokenStore getTokenStore() {
159 public TokenAuthenticators getTokenAuthenticators() {
160 return tokenAuthenticators;
163 public AuthenticationService getAuthenticationService() {
164 return authenticationService;
167 public PasswordHashService getPasswordHashService() {
168 return passwordHashService;