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 AuthProviderImpl implements AuthProvider {
23 private static final Logger logger = LoggerFactory.getLogger(AuthProviderImpl.class);
25 private final String pem;
26 private IUserManager nullableUserManager;
28 public AuthProviderImpl(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 (AuthProviderImpl.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.
64 public synchronized boolean authenticated(String username, String password) {
65 if (nullableUserManager == null) {
66 logger.warn("Cannot authenticate user '{}', user manager service is missing", username);
67 throw new IllegalStateException("User manager service is not available");
69 AuthResultEnum authResult = nullableUserManager.authenticate(username, password);
70 logger.debug("Authentication result for user '{}' : {}", username, authResult);
71 return authResult.equals(AuthResultEnum.AUTH_ACCEPT) || authResult.equals(AuthResultEnum.AUTH_ACCEPT_LOC);
75 public char[] getPEMAsCharArray() {
76 return pem.toCharArray();
80 void setNullableUserManager(IUserManager nullableUserManager) {
81 this.nullableUserManager = nullableUserManager;