2 * Copyright (c) 2014 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
9 package org.opendaylight.netconf.ssh.osgi;
11 import org.opendaylight.netconf.auth.AuthProvider;
12 import org.osgi.framework.BundleContext;
13 import org.osgi.framework.ServiceReference;
14 import org.osgi.util.tracker.ServiceTracker;
15 import org.osgi.util.tracker.ServiceTrackerCustomizer;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 final class AuthProviderTracker implements ServiceTrackerCustomizer<AuthProvider, AuthProvider>, AuthProvider {
20 private static final Logger LOG = LoggerFactory.getLogger(AuthProviderTracker.class);
22 private final BundleContext bundleContext;
24 private final ServiceTracker<AuthProvider, AuthProvider> listenerTracker;
25 private volatile AuthProvider authProvider;
27 public AuthProviderTracker(final BundleContext bundleContext) {
28 this.bundleContext = bundleContext;
29 listenerTracker = new ServiceTracker<>(bundleContext, AuthProvider.class, this);
30 listenerTracker.open();
34 public AuthProvider addingService(final ServiceReference<AuthProvider> reference) {
35 LOG.trace("Service {} added", reference);
36 this.authProvider = bundleContext.getService(reference);
41 public void modifiedService(final ServiceReference<AuthProvider> reference, final AuthProvider service) {
42 final AuthProvider authService = bundleContext.getService(reference);
43 LOG.trace("Replacing modified service {} in netconf SSH.", reference);
44 this.authProvider = authService;
48 public void removedService(final ServiceReference<AuthProvider> reference, final AuthProvider service) {
49 LOG.trace("Removing service {} from netconf SSH. {}", reference,
50 " SSH won't authenticate users until AuthProvider service will be started.");
51 this.authProvider = null;
55 listenerTracker.close();
56 this.authProvider = null;
57 // sshThread should finish normally since sshServer.close stops processing
61 public boolean authenticated(final String username, final String password) {
62 if (authProvider == null) {
63 LOG.warn("AuthProvider is missing, failing authentication");
66 return authProvider.authenticated(username, password);