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