2 * Copyright (c) 2013 Cisco Systems, Inc. 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.controller.netconf.ssh.authentication;
10 import static com.google.common.base.Preconditions.checkNotNull;
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;
22 public class AuthProvider {
23 private static final Logger logger = LoggerFactory.getLogger(AuthProvider.class);
25 private final String pem;
26 private IUserManager nullableUserManager;
28 public AuthProvider(String pemCertificate, final BundleContext bundleContext) {
29 checkNotNull(pemCertificate, "Parameter 'pemCertificate' is null");
32 ServiceTrackerCustomizer<IUserManager, IUserManager> customizer = new ServiceTrackerCustomizer<IUserManager, IUserManager>() {
34 public IUserManager addingService(final ServiceReference<IUserManager> reference) {
35 logger.trace("Service {} added", reference);
36 nullableUserManager = bundleContext.getService(reference);
37 return nullableUserManager;
41 public void modifiedService(final ServiceReference<IUserManager> reference, final IUserManager service) {
42 logger.trace("Replacing modified service {} in netconf SSH.", reference);
43 nullableUserManager = service;
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;
55 ServiceTracker<IUserManager, IUserManager> listenerTracker = new ServiceTracker<>(bundleContext, IUserManager.class, customizer);
56 listenerTracker.open();
60 * Authenticate user. This implementation tracks IUserManager and delegates the decision to it. If the service is not
61 * available, IllegalStateException is thrown.
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");
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);
73 public char[] getPEMAsCharArray() {
74 return pem.toCharArray();
78 void setNullableUserManager(IUserManager nullableUserManager) {
79 this.nullableUserManager = nullableUserManager;