2 * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. 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 java.util.Dictionary;
11 import java.util.Hashtable;
12 import org.opendaylight.aaa.api.Authentication;
13 import org.opendaylight.aaa.api.AuthenticationService;
14 import org.osgi.service.cm.ConfigurationException;
15 import org.osgi.service.cm.ManagedService;
18 * An {@link InheritableThreadLocal}-based {@link AuthenticationService}.
22 public class AuthenticationManager implements AuthenticationService, ManagedService {
23 private static final String AUTH_ENABLED_ERR = "Error setting authEnabled";
25 static final String AUTH_ENABLED = "authEnabled";
26 static final Dictionary<String, String> defaults = new Hashtable<>();
28 defaults.put(AUTH_ENABLED, Boolean.FALSE.toString());
31 // In non-Karaf environments, authEnabled is set to false by default
32 private static volatile boolean authEnabled = false;
34 private final static AuthenticationManager am = new AuthenticationManager();
35 private final ThreadLocal<Authentication> auth = new InheritableThreadLocal<>();
37 private AuthenticationManager() {
40 static AuthenticationManager instance() {
45 public Authentication get() {
50 public void set(Authentication a) {
60 public boolean isAuthEnabled() {
65 public void updated(Dictionary<String, ?> properties) throws ConfigurationException {
66 if (properties == null) {
70 String propertyValue = (String) properties.get(AUTH_ENABLED);
71 boolean isTrueString = Boolean.parseBoolean(propertyValue);
72 if (!isTrueString && !"false".equalsIgnoreCase(propertyValue)) {
73 throw new ConfigurationException(AUTH_ENABLED, AUTH_ENABLED_ERR);
75 authEnabled = isTrueString;