8e869aca32c26827dd15e7494a5a79cdb5f017c7
[netconf.git] / netconf / aaa-authn-odl-plugin / src / main / java / org / opendaylight / netconf / authprovider / CredentialServiceAuthProvider.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.netconf.authprovider;
9
10 import static java.util.Objects.requireNonNull;
11
12 import javax.inject.Inject;
13 import javax.inject.Singleton;
14 import org.opendaylight.aaa.api.AuthenticationException;
15 import org.opendaylight.aaa.api.Claim;
16 import org.opendaylight.aaa.api.PasswordCredentialAuth;
17 import org.opendaylight.aaa.api.PasswordCredentials;
18 import org.opendaylight.netconf.auth.AuthProvider;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * AuthProvider implementation delegating to a {@link PasswordCredentialAuth} instance.
24  */
25 @Singleton
26 public final class CredentialServiceAuthProvider implements AuthProvider {
27     private static final Logger LOG = LoggerFactory.getLogger(CredentialServiceAuthProvider.class);
28
29     private final PasswordCredentialAuth credService;
30
31     @Inject
32     public CredentialServiceAuthProvider(final PasswordCredentialAuth credService) {
33         this.credService = requireNonNull(credService);
34     }
35
36     /**
37      * Authenticate user. This implementation tracks CredentialAuth<PasswordCredentials>
38      * and delegates the decision to it.
39      */
40     @Override
41     public boolean authenticated(final String username, final String password) {
42         final Claim claim;
43         try {
44             claim = credService.authenticate(new PasswordCredentialsWrapper(username, password));
45         } catch (AuthenticationException e) {
46             LOG.debug("Authentication failed for user '{}'", username, e);
47             return false;
48         }
49
50         LOG.debug("Authentication result for user '{}' : {}", username, claim.domain());
51         return true;
52     }
53
54     private static final class PasswordCredentialsWrapper implements PasswordCredentials {
55         private final String username;
56         private final String password;
57
58         PasswordCredentialsWrapper(final String username, final String password) {
59             this.username = username;
60             this.password = password;
61         }
62
63         @Override
64         public String username() {
65             return username;
66         }
67
68         @Override
69         public String password() {
70             return password;
71         }
72
73         @Override
74         public String domain() {
75             // If this is left null, default "sdn" domain is assumed
76             return null;
77         }
78     }
79 }