Split out DefaultPasswordCredentials
[netconf.git] / plugins / netconf-auth-aaa / src / main / java / org / opendaylight / netconf / auth / aaa / 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.auth.aaa;
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.netconf.auth.AuthProvider;
18 import org.osgi.service.component.annotations.Activate;
19 import org.osgi.service.component.annotations.Component;
20 import org.osgi.service.component.annotations.Reference;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * AuthProvider implementation delegating to a {@link PasswordCredentialAuth} instance.
26  */
27 @Singleton
28 @Component(immediate = true, property = "type=netconf-auth-provider")
29 public final class CredentialServiceAuthProvider implements AuthProvider {
30     private static final Logger LOG = LoggerFactory.getLogger(CredentialServiceAuthProvider.class);
31
32     private final PasswordCredentialAuth credService;
33
34     @Inject
35     @Activate
36     public CredentialServiceAuthProvider(final @Reference PasswordCredentialAuth credService) {
37         this.credService = requireNonNull(credService);
38     }
39
40     /**
41      * Authenticate user. This implementation tracks CredentialAuth<PasswordCredentials>
42      * and delegates the decision to it.
43      */
44     @Override
45     public boolean authenticated(final String username, final String password) {
46         final var credentials = new DefaultPasswordCredentials(username, password);
47
48         final Claim claim;
49         try {
50             claim = credService.authenticate(credentials);
51         } catch (AuthenticationException e) {
52             LOG.debug("Authentication failed for user '{}'", username, e);
53             return false;
54         }
55
56         LOG.debug("Authentication result for user '{}' : {}", username, claim.domain());
57         return true;
58     }
59 }