2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 #include <klay/exception.h>
19 #include "password-manager.h"
21 PasswordManager::PasswordManager(uid_t uid) :
24 if (auth_passwd_new_policy(&p_policy) != AUTH_PASSWD_API_SUCCESS) {
25 throw runtime::Exception("Failed to get auth instance");
28 auth_passwd_set_user(p_policy, user);
31 PasswordManager::~PasswordManager()
33 auth_passwd_free_policy(p_policy);
36 void PasswordManager::setQuality(PasswordManager::QualityType quality)
38 if (auth_passwd_set_quality(p_policy, quality) != AUTH_PASSWD_API_SUCCESS) {
39 throw runtime::Exception("Failed to enforce password quality");
43 void PasswordManager::setMinimumLength(int value)
45 if (auth_passwd_set_min_length(p_policy, value) != AUTH_PASSWD_API_SUCCESS) {
46 throw runtime::Exception("Failed to set minimum length");
50 void PasswordManager::setMinimumComplexCharacters(int value)
52 if (auth_passwd_set_min_complex_char_num(p_policy, value) != AUTH_PASSWD_API_SUCCESS) {
53 throw runtime::Exception("Failed to set minimum complex characters");
57 void PasswordManager::setMaximumFailedForWipe(int value)
59 if (auth_passwd_set_max_attempts(p_policy, value) != AUTH_PASSWD_API_SUCCESS) {
60 throw runtime::Exception("Failed to set maximum failed count for wipe");
64 void PasswordManager::setExpires(int value)
66 if (auth_passwd_set_validity(p_policy, value) != AUTH_PASSWD_API_SUCCESS) {
67 throw runtime::Exception("Failed to set expire");
71 void PasswordManager::setHistory(int value)
73 if (auth_passwd_set_history_size(p_policy, value) != AUTH_PASSWD_API_SUCCESS) {
74 throw runtime::Exception("Failed to set history size");
78 void PasswordManager::setPattern(const char* pattern)
80 if (auth_passwd_set_pattern(p_policy, pattern) != AUTH_PASSWD_API_SUCCESS) {
81 throw runtime::Exception("Failed to set pattern");
85 void PasswordManager::deletePatern()
87 if (auth_passwd_set_pattern(p_policy, NULL) != AUTH_PASSWD_API_SUCCESS) {
88 throw runtime::Exception("Failed to delete pattern");
92 void PasswordManager::setMaximumCharacterOccurrences(int value)
94 if (auth_passwd_set_max_char_occurrences(p_policy, value) != AUTH_PASSWD_API_SUCCESS) {
95 throw runtime::Exception("Failed to set maximum character occurrences");
99 void PasswordManager::setMaximumNumericSequenceLength(int value)
101 if (auth_passwd_set_max_num_seq_len(p_policy, value) != AUTH_PASSWD_API_SUCCESS) {
102 throw runtime::Exception("Failed to set maximum numeric sequence length");
106 void PasswordManager::setForbiddenStrings(const std::vector<std::string> &forbiddenStrings)
108 for (const std::string& str : forbiddenStrings) {
109 if (auth_passwd_set_forbidden_passwd(p_policy, str.c_str()) != AUTH_PASSWD_API_SUCCESS) {
110 throw runtime::Exception("Failed to set forbidden strings");
115 void PasswordManager::enforce()
117 if (auth_passwd_set_policy(p_policy) != AUTH_PASSWD_API_SUCCESS) {
118 throw runtime::Exception("Failed to enforce policy");
122 void PasswordManager::resetPassword(const std::string& password)
124 if (auth_passwd_reset_passwd(AUTH_PWD_NORMAL, user, password.c_str()) != AUTH_PASSWD_API_SUCCESS) {
125 throw runtime::Exception("Failed to set reset password");