adjust to use password-service
[aaa.git] / aaa-cli-jar / src / main / java / org / opendaylight / aaa / cli / jar / StandaloneCommandLineInterface.java
1 /*
2  * Copyright (c) 2016 - 2017 Red Hat, 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.cli.jar;
9
10 import com.google.common.base.Preconditions;
11 import java.io.File;
12 import java.io.IOException;
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.stream.Collectors;
16 import org.opendaylight.aaa.api.IDMStoreException;
17 import org.opendaylight.aaa.api.IIDMStore;
18 import org.opendaylight.aaa.api.StoreBuilder;
19 import org.opendaylight.aaa.api.model.User;
20 import org.opendaylight.aaa.api.password.service.PasswordHashService;
21 import org.opendaylight.aaa.datastore.h2.H2Store;
22 import org.opendaylight.aaa.datastore.h2.IdmLightConfig;
23 import org.opendaylight.aaa.datastore.h2.IdmLightConfigBuilder;
24 import org.opendaylight.aaa.datastore.h2.IdmLightSimpleConnectionProvider;
25 import org.opendaylight.aaa.impl.password.service.DefaultPasswordHashService;
26
27 /**
28  * AAA CLI interface.
29  * This is for a "standalone Java" environment (i.e. plain JSE; non-OSGi, no Karaf).
30  *
31  * @author Michael Vorburger.ch
32  */
33 public class StandaloneCommandLineInterface {
34
35     private final IIDMStore identityStore;
36     private final StoreBuilder storeBuilder;
37     private static final String DOMAIN = IIDMStore.DEFAULT_DOMAIN;
38     private final PasswordHashService passwordService;
39
40     public StandaloneCommandLineInterface(File directoryWithDatabaseFile) throws IOException, IDMStoreException {
41         IdmLightConfigBuilder configBuider = new IdmLightConfigBuilder();
42         configBuider.dbDirectory(directoryWithDatabaseFile.getCanonicalPath()).dbUser("foo").dbPwd("bar");
43         IdmLightConfig config = configBuider.build();
44
45         passwordService = new DefaultPasswordHashService();
46
47         H2Store h2Store = new H2Store(new IdmLightSimpleConnectionProvider(config), passwordService);
48         this.identityStore = h2Store;
49
50         this.storeBuilder = new StoreBuilder(h2Store);
51         storeBuilder.initDomainAndRolesWithoutUsers(DOMAIN);
52     }
53
54     public List<String> getAllUserNames() throws IDMStoreException {
55         List<User> users = identityStore.getUsers().getUsers();
56         return users.stream().map(User::getName).collect(Collectors.toList());
57     }
58
59     public boolean resetPassword(String userIdWithoutDomain, String newPassword) throws IDMStoreException {
60         Optional<User> optUser = getSingleUser(userIdWithoutDomain);
61         if (!optUser.isPresent()) {
62             return false;
63         } else {
64             User user = optUser.get();
65             user.setPassword(newPassword);
66             identityStore.updateUser(user);
67             return true;
68         }
69     }
70
71     /**
72      * Check a user's password.
73      * See <a href="https://bugs.opendaylight.org/show_bug.cgi?id=8721">Bug 8721 requirement</a>.
74      */
75     public boolean checkUserPassword(String userIdWithoutDomain, String password) throws IDMStoreException {
76         Optional<User> optUser = getSingleUser(userIdWithoutDomain);
77         if (!optUser.isPresent()) {
78             return false;
79         } else {
80             User user = optUser.get();
81             return passwordService.passwordsMatch(password, user.getPassword(), user.getSalt());
82         }
83     }
84
85     private Optional<User> getSingleUser(String userIdWithoutDomain) throws IDMStoreException {
86         Preconditions.checkNotNull(userIdWithoutDomain, "userIdWithoutDomain == null");
87         List<User> users = identityStore.getUsers(userIdWithoutDomain, DOMAIN).getUsers();
88         if (users.isEmpty()) {
89             return Optional.empty();
90         }
91         if (users.size() > 1) {
92             throw new IDMStoreException("More than 1 user found: " + userIdWithoutDomain);
93         }
94         return Optional.of(users.get(0));
95     }
96
97     public void createNewUser(String userName, String password, boolean isAdmin) throws IDMStoreException {
98         Preconditions.checkNotNull(userName, "userName == null");
99         storeBuilder.createUser(DOMAIN, userName, password, isAdmin);
100     }
101
102     public boolean deleteUser(String userIdWithoutDomain) throws IDMStoreException {
103         Preconditions.checkNotNull(userIdWithoutDomain, "userIdWithoutDomain == null");
104         return storeBuilder.deleteUser(DOMAIN, userIdWithoutDomain);
105     }
106 }