2 * Copyright (c) 2016 - 2017 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.aaa.cli.jar;
10 import static java.util.Objects.requireNonNull;
13 import java.io.IOException;
14 import java.util.List;
15 import java.util.Optional;
16 import java.util.stream.Collectors;
17 import org.opendaylight.aaa.api.IDMStoreException;
18 import org.opendaylight.aaa.api.IIDMStore;
19 import org.opendaylight.aaa.api.StoreBuilder;
20 import org.opendaylight.aaa.api.model.User;
21 import org.opendaylight.aaa.api.password.service.PasswordHashService;
22 import org.opendaylight.aaa.datastore.h2.H2Store;
23 import org.opendaylight.aaa.datastore.h2.IdmLightConfig;
24 import org.opendaylight.aaa.datastore.h2.IdmLightConfigBuilder;
25 import org.opendaylight.aaa.datastore.h2.IdmLightSimpleConnectionProvider;
26 import org.opendaylight.aaa.impl.password.service.DefaultPasswordHashService;
30 * This is for a "standalone Java" environment (i.e. plain JSE; non-OSGi, no Karaf).
32 * @author Michael Vorburger.ch
34 public class StandaloneCommandLineInterface {
36 private final IIDMStore identityStore;
37 private final StoreBuilder storeBuilder;
38 private static final String DOMAIN = IIDMStore.DEFAULT_DOMAIN;
39 private final PasswordHashService passwordService;
41 public StandaloneCommandLineInterface(File directoryWithDatabaseFile) throws IOException, IDMStoreException {
42 IdmLightConfigBuilder configBuider = new IdmLightConfigBuilder();
43 configBuider.dbDirectory(directoryWithDatabaseFile.getCanonicalPath()).dbUser("foo").dbPwd("bar");
44 IdmLightConfig config = configBuider.build();
46 passwordService = new DefaultPasswordHashService();
48 H2Store h2Store = new H2Store(new IdmLightSimpleConnectionProvider(config), passwordService);
49 this.identityStore = h2Store;
51 this.storeBuilder = new StoreBuilder(h2Store);
52 storeBuilder.initDomainAndRolesWithoutUsers(DOMAIN);
55 public List<String> getAllUserNames() throws IDMStoreException {
56 List<User> users = identityStore.getUsers().getUsers();
57 return users.stream().map(User::getName).collect(Collectors.toList());
60 public boolean resetPassword(String userIdWithoutDomain, String newPassword) throws IDMStoreException {
61 Optional<User> optUser = getSingleUser(userIdWithoutDomain);
62 if (!optUser.isPresent()) {
65 User user = optUser.orElseThrow();
66 user.setPassword(newPassword);
67 identityStore.updateUser(user);
73 * Check a user's password.
74 * See <a href="https://bugs.opendaylight.org/show_bug.cgi?id=8721">Bug 8721 requirement</a>.
76 public boolean checkUserPassword(String userIdWithoutDomain, String password) throws IDMStoreException {
77 Optional<User> optUser = getSingleUser(userIdWithoutDomain);
78 if (!optUser.isPresent()) {
81 User user = optUser.orElseThrow();
82 return passwordService.passwordsMatch(password, user.getPassword(), user.getSalt());
86 private Optional<User> getSingleUser(String userIdWithoutDomain) throws IDMStoreException {
87 requireNonNull(userIdWithoutDomain, "userIdWithoutDomain == null");
88 List<User> users = identityStore.getUsers(userIdWithoutDomain, DOMAIN).getUsers();
89 if (users.isEmpty()) {
90 return Optional.empty();
92 if (users.size() > 1) {
93 throw new IDMStoreException("More than 1 user found: " + userIdWithoutDomain);
95 return Optional.of(users.get(0));
98 public void createNewUser(String userName, String password, boolean isAdmin) throws IDMStoreException {
99 requireNonNull(userName, "userName == null");
100 storeBuilder.createUser(DOMAIN, userName, password, isAdmin);
103 public boolean deleteUser(String userIdWithoutDomain) throws IDMStoreException {
104 requireNonNull(userIdWithoutDomain, "userIdWithoutDomain == null");
105 return storeBuilder.deleteUser(DOMAIN, userIdWithoutDomain);