Merge "Bug 809: Enhancements to the toaster example"
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / ssh / authentication / AuthProvider.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.controller.netconf.ssh.authentication;
9
10 import java.io.IOException;
11 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
12 import org.opendaylight.controller.usermanager.IUserManager;
13 import static com.google.common.base.Preconditions.checkNotNull;
14
15 public class AuthProvider implements AuthProviderInterface {
16
17     private IUserManager um;
18     private final String pem;
19
20     public AuthProvider(IUserManager ium, String pemCertificate) throws IllegalArgumentException, IOException {
21         checkNotNull(pemCertificate, "Parameter 'pemCertificate' is null");
22         checkNotNull(ium, "No user manager service available.");
23         this.um = ium;
24         pem = pemCertificate;
25     }
26
27     @Override
28     public boolean authenticated(String username, String password) {
29         AuthResultEnum authResult = this.um.authenticate(username, password);
30         return authResult.equals(AuthResultEnum.AUTH_ACCEPT) || authResult.equals(AuthResultEnum.AUTH_ACCEPT_LOC);
31     }
32
33     @Override
34     public char[] getPEMAsCharArray() {
35         return pem.toCharArray();
36     }
37
38     @Override
39     public void removeUserManagerService() {
40         this.um = null;
41     }
42
43     @Override
44     public void addUserManagerService(IUserManager userManagerService) {
45         this.um = userManagerService;
46     }
47 }