forked from picketlink/picketlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PasswordCredentialHandler.java
182 lines (154 loc) · 6.57 KB
/
PasswordCredentialHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* JBoss, Home of Professional Open Source
*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.picketlink.idm.credential.internal;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Date;
import java.util.Map;
import org.picketlink.idm.IdentityManagementException;
import org.picketlink.idm.config.SecurityConfigurationException;
import org.picketlink.idm.credential.Credentials.Status;
import org.picketlink.idm.credential.Password;
import org.picketlink.idm.credential.UsernamePasswordCredentials;
import org.picketlink.idm.credential.spi.CredentialHandler;
import org.picketlink.idm.credential.spi.annotations.SupportsCredentials;
import org.picketlink.idm.model.Agent;
import org.picketlink.idm.password.PasswordEncoder;
import org.picketlink.idm.password.internal.EncodedPasswordStorage;
import org.picketlink.idm.password.internal.SHAPasswordEncoder;
import org.picketlink.idm.spi.CredentialStore;
import org.picketlink.idm.spi.SecurityContext;
import static org.picketlink.idm.IDMMessages.MESSAGES;
import static org.picketlink.idm.credential.internal.CredentialUtils.isCredentialExpired;
/**
* <p>
* This particular implementation supports the validation of {@link UsernamePasswordCredentials}, and updating {@link Password}
* credentials.
* </p>
* <p>
* <p/>
* <p>
* How passwords are encoded can be changed by specifying a configuration option using the <code>PASSWORD_ENCODER</code>. By
* default a SHA-512 encoding is performed.
* </p>
* <p/>
* <p>
* Password are always salted before encoding.
* </p>
*
* @author Shane Bryzak
* @author <a href="mailto:psilva@redhat.com">Pedro Silva</a>
*/
@SupportsCredentials({UsernamePasswordCredentials.class, Password.class})
public class PasswordCredentialHandler<S extends CredentialStore<?>, V extends UsernamePasswordCredentials, U extends Password>
implements CredentialHandler<S, V, U> {
private static final String DEFAULT_SALT_ALGORITHM = "SHA1PRNG";
/**
* <p>
* Stores a <b>stateless</b> instance of {@link PasswordEncoder} that should be used to encode passwords.
* </p>
*/
public static final String PASSWORD_ENCODER = "PASSWORD_ENCODER";
private PasswordEncoder passwordEncoder = new SHAPasswordEncoder(512);
@Override
public void setup(S store) {
Map<String, Object> options = store.getConfig().getCredentialHandlerProperties();
if (options != null) {
Object providedEncoder = options.get(PASSWORD_ENCODER);
if (providedEncoder != null) {
if (PasswordEncoder.class.isInstance(providedEncoder)) {
this.passwordEncoder = (PasswordEncoder) providedEncoder;
} else {
throw new SecurityConfigurationException("The password encoder [" + providedEncoder
+ "] must be an instance of " + PasswordEncoder.class.getName());
}
}
}
}
@Override
public void validate(SecurityContext context, V credentials, S store) {
if (!UsernamePasswordCredentials.class.isInstance(credentials)) {
throw MESSAGES.credentialUnsupportedType(credentials.getClass(), this);
}
UsernamePasswordCredentials usernamePassword = (UsernamePasswordCredentials) credentials;
usernamePassword.setStatus(Status.INVALID);
Agent agent = store.getAgent(context, usernamePassword.getUsername());
// If the user for the provided username cannot be found we fail validation
if (agent != null) {
if (agent.isEnabled()) {
EncodedPasswordStorage hash = store.retrieveCurrentCredential(context, agent, EncodedPasswordStorage.class);
// If the stored hash is null we automatically fail validation
if (hash != null) {
if (!isCredentialExpired(hash)) {
String rawPassword = new String(usernamePassword.getPassword().getValue());
String encoded = this.passwordEncoder.encode(saltPassword(rawPassword, hash.getSalt()));
if (hash.getEncodedHash().equals(encoded)) {
usernamePassword.setStatus(Status.VALID);
usernamePassword.setValidatedAgent(agent);
}
} else {
usernamePassword.setStatus(Status.EXPIRED);
}
}
} else {
usernamePassword.setStatus(Status.AGENT_DISABLED);
}
}
}
@Override
public void update(SecurityContext context, Agent agent, U password, S store,
Date effectiveDate, Date expiryDate) {
EncodedPasswordStorage hash = new EncodedPasswordStorage();
String rawPassword = new String(password.getValue());
String passwordSalt = generateSalt();
hash.setSalt(passwordSalt);
hash.setEncodedHash(this.passwordEncoder.encode(saltPassword(rawPassword, passwordSalt)));
hash.setEffectiveDate(effectiveDate);
hash.setExpiryDate(expiryDate);
store.storeCredential(context, agent, hash);
}
/**
* <p>
* Salt the give <code>rawPassword</code> with the specified <code>salt</code> value.
* </p>
*
* @param rawPassword
* @param salt
* @return
*/
private String saltPassword(String rawPassword, String salt) {
return salt + rawPassword;
}
/**
* <p>
* Generates a random string to be used as a salt for passwords.
* </p>
*
* @return
*/
private String generateSalt() {
SecureRandom pseudoRandom = null;
try {
pseudoRandom = SecureRandom.getInstance(DEFAULT_SALT_ALGORITHM);
pseudoRandom.setSeed(1024);
} catch (NoSuchAlgorithmException e) {
throw new IdentityManagementException("Error getting SecureRandom instance: " + DEFAULT_SALT_ALGORITHM, e);
}
return String.valueOf(pseudoRandom.nextLong());
}
}