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 com.google.common.base.Preconditions;
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;
29 * This is for a "standalone Java" environment (i.e. plain JSE; non-OSGi, no Karaf).
31 * @author Michael Vorburger.ch
33 public class StandaloneCommandLineInterface {
35 private final IIDMStore identityStore;
36 private final StoreBuilder storeBuilder;
37 private static final String DOMAIN = IIDMStore.DEFAULT_DOMAIN;
38 private final PasswordHashService passwordService;
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();
45 passwordService = new DefaultPasswordHashService();
47 H2Store h2Store = new H2Store(new IdmLightSimpleConnectionProvider(config), passwordService);
48 this.identityStore = h2Store;
50 this.storeBuilder = new StoreBuilder(h2Store);
51 storeBuilder.initDomainAndRolesWithoutUsers(DOMAIN);
54 public List<String> getAllUserNames() throws IDMStoreException {
55 List<User> users = identityStore.getUsers().getUsers();
56 return users.stream().map(User::getName).collect(Collectors.toList());
59 public boolean resetPassword(String userIdWithoutDomain, String newPassword) throws IDMStoreException {
60 Optional<User> optUser = getSingleUser(userIdWithoutDomain);
61 if (!optUser.isPresent()) {
64 User user = optUser.get();
65 user.setPassword(newPassword);
66 identityStore.updateUser(user);
72 * Check a user's password.
73 * See <a href="https://bugs.opendaylight.org/show_bug.cgi?id=8721">Bug 8721 requirement</a>.
75 public boolean checkUserPassword(String userIdWithoutDomain, String password) throws IDMStoreException {
76 Optional<User> optUser = getSingleUser(userIdWithoutDomain);
77 if (!optUser.isPresent()) {
80 User user = optUser.get();
81 return passwordService.passwordsMatch(password, user.getPassword(), user.getSalt());
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();
91 if (users.size() > 1) {
92 throw new IDMStoreException("More than 1 user found: " + userIdWithoutDomain);
94 return Optional.of(users.get(0));
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);
102 public boolean deleteUser(String userIdWithoutDomain) throws IDMStoreException {
103 Preconditions.checkNotNull(userIdWithoutDomain, "userIdWithoutDomain == null");
104 return storeBuilder.deleteUser(DOMAIN, userIdWithoutDomain);