Merge "BUG-625: migrate InventoryAndReadAdapter"
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / ssh / authentication / AuthProvider.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.controller.netconf.ssh.authentication;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
14 import org.opendaylight.controller.usermanager.IUserManager;
15 import org.osgi.framework.BundleContext;
16 import org.osgi.framework.ServiceReference;
17 import org.osgi.util.tracker.ServiceTracker;
18 import org.osgi.util.tracker.ServiceTrackerCustomizer;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class AuthProvider {
23     private static final Logger logger = LoggerFactory.getLogger(AuthProvider.class);
24
25     private final String pem;
26     private IUserManager nullableUserManager;
27
28     public AuthProvider(String pemCertificate, final BundleContext bundleContext) {
29         checkNotNull(pemCertificate, "Parameter 'pemCertificate' is null");
30         pem = pemCertificate;
31
32         ServiceTrackerCustomizer<IUserManager, IUserManager> customizer = new ServiceTrackerCustomizer<IUserManager, IUserManager>() {
33             @Override
34             public IUserManager addingService(final ServiceReference<IUserManager> reference) {
35                 logger.trace("Service {} added", reference);
36                 nullableUserManager = bundleContext.getService(reference);
37                 return nullableUserManager;
38             }
39
40             @Override
41             public void modifiedService(final ServiceReference<IUserManager> reference, final IUserManager service) {
42                 logger.trace("Replacing modified service {} in netconf SSH.", reference);
43                 nullableUserManager = service;
44             }
45
46             @Override
47             public void removedService(final ServiceReference<IUserManager> reference, final IUserManager service) {
48                 logger.trace("Removing service {} from netconf SSH. " +
49                         "SSH won't authenticate users until IUserManager service will be started.", reference);
50                 synchronized (AuthProvider.this) {
51                     nullableUserManager = null;
52                 }
53             }
54         };
55         ServiceTracker<IUserManager, IUserManager> listenerTracker = new ServiceTracker<>(bundleContext, IUserManager.class, customizer);
56         listenerTracker.open();
57     }
58
59     /**
60      * Authenticate user. This implementation tracks IUserManager and delegates the decision to it. If the service is not
61      * available, IllegalStateException is thrown.
62      */
63     public synchronized boolean authenticated(String username, String password) {
64         if (nullableUserManager == null) {
65             logger.warn("Cannot authenticate user '{}', user manager service is missing", username);
66             throw new IllegalStateException("User manager service is not available");
67         }
68         AuthResultEnum authResult = nullableUserManager.authenticate(username, password);
69         logger.debug("Authentication result for user '{}' : {}", username, authResult);
70         return authResult.equals(AuthResultEnum.AUTH_ACCEPT) || authResult.equals(AuthResultEnum.AUTH_ACCEPT_LOC);
71     }
72
73     public char[] getPEMAsCharArray() {
74         return pem.toCharArray();
75     }
76
77     @VisibleForTesting
78     void setNullableUserManager(IUserManager nullableUserManager) {
79         this.nullableUserManager = nullableUserManager;
80     }
81 }