12
12
*******************************************************************************/
13
13
package org .eclipse .kapua .service .authentication .credential .shiro ;
14
14
15
+ import java .security .NoSuchAlgorithmException ;
16
+ import java .security .SecureRandom ;
17
+ import java .util .concurrent .atomic .AtomicReference ;
18
+
19
+ import javax .inject .Singleton ;
20
+
15
21
import org .apache .shiro .codec .Base64 ;
16
22
import org .eclipse .kapua .KapuaEntityNotFoundException ;
17
23
import org .eclipse .kapua .KapuaException ;
18
24
import org .eclipse .kapua .KapuaIllegalArgumentException ;
19
25
import org .eclipse .kapua .KapuaRuntimeException ;
20
26
import org .eclipse .kapua .commons .configuration .KapuaConfigurableServiceBase ;
27
+ import org .eclipse .kapua .commons .configuration .ServiceConfigurationManager ;
21
28
import org .eclipse .kapua .commons .model .domains .Domains ;
22
29
import org .eclipse .kapua .commons .util .ArgumentValidator ;
23
30
import org .eclipse .kapua .event .ServiceEvent ;
39
46
import org .eclipse .kapua .service .authentication .credential .CredentialService ;
40
47
import org .eclipse .kapua .service .authentication .credential .CredentialType ;
41
48
import org .eclipse .kapua .service .authentication .exception .DuplicatedPasswordCredentialException ;
42
- import org .eclipse .kapua .service .authentication .shiro .CredentialServiceConfigurationManager ;
43
49
import org .eclipse .kapua .service .authentication .shiro .setting .KapuaAuthenticationSetting ;
44
50
import org .eclipse .kapua .service .authentication .shiro .setting .KapuaAuthenticationSettingKeys ;
45
51
import org .eclipse .kapua .service .authentication .user .PasswordResetRequest ;
49
55
import org .slf4j .Logger ;
50
56
import org .slf4j .LoggerFactory ;
51
57
52
- import javax .inject .Singleton ;
53
- import java .security .NoSuchAlgorithmException ;
54
- import java .security .SecureRandom ;
55
- import java .util .concurrent .atomic .AtomicReference ;
56
-
57
58
/**
58
59
* {@link CredentialService} implementation.
59
60
*
@@ -69,11 +70,12 @@ public class CredentialServiceImpl extends KapuaConfigurableServiceBase implemen
69
70
private final CredentialFactory credentialFactory ;
70
71
private final KapuaAuthenticationSetting kapuaAuthenticationSetting ;
71
72
private final CredentialMapper credentialMapper ;
73
+ private final AccountPasswordLengthProvider accountPasswordLengthProvider ;
72
74
private final PasswordValidator passwordValidator ;
73
75
private final PasswordResetter passwordResetter ;
74
76
75
77
public CredentialServiceImpl (
76
- CredentialServiceConfigurationManager serviceConfigurationManager ,
78
+ ServiceConfigurationManager serviceConfigurationManager ,
77
79
AuthorizationService authorizationService ,
78
80
PermissionFactory permissionFactory ,
79
81
TxManager txManager ,
@@ -82,11 +84,13 @@ public CredentialServiceImpl(
82
84
CredentialMapper credentialMapper ,
83
85
PasswordValidator passwordValidator ,
84
86
KapuaAuthenticationSetting kapuaAuthenticationSetting ,
87
+ AccountPasswordLengthProvider accountPasswordLengthProvider ,
85
88
PasswordResetter passwordResetter ) {
86
89
super (txManager , serviceConfigurationManager , Domains .CREDENTIAL , authorizationService , permissionFactory );
87
90
this .credentialRepository = credentialRepository ;
88
91
this .credentialFactory = credentialFactory ;
89
92
this .kapuaAuthenticationSetting = kapuaAuthenticationSetting ;
93
+ this .accountPasswordLengthProvider = accountPasswordLengthProvider ;
90
94
this .passwordResetter = passwordResetter ;
91
95
try {
92
96
random = SecureRandom .getInstance ("SHA1PRNG" );
@@ -134,31 +138,31 @@ public Credential create(CredentialCreator credentialCreatorer)
134
138
// Do create
135
139
// Do pre persist magic on key values
136
140
switch (credentialCreator .getCredentialType ()) {
137
- case API_KEY : // Generate new api key
138
- int preLength = kapuaAuthenticationSetting .getInt (KapuaAuthenticationSettingKeys .AUTHENTICATION_CREDENTIAL_APIKEY_PRE_LENGTH );
139
- int keyLength = kapuaAuthenticationSetting .getInt (KapuaAuthenticationSettingKeys .AUTHENTICATION_CREDENTIAL_APIKEY_KEY_LENGTH );
140
-
141
- byte [] bPre = new byte [preLength ];
142
- random .nextBytes (bPre );
143
- String pre = Base64 .encodeToString (bPre ).substring (0 , preLength );
144
-
145
- byte [] bKey = new byte [keyLength ];
146
- random .nextBytes (bKey );
147
- String key = Base64 .encodeToString (bKey );
148
-
149
- fullKey .set (pre + key );
150
-
151
- credentialCreator = new CredentialCreatorImpl (credentialCreator .getScopeId (),
152
- credentialCreator .getUserId (),
153
- credentialCreator .getCredentialType (),
154
- fullKey .get (),
155
- credentialCreator .getCredentialStatus (),
156
- credentialCreator .getExpirationDate ());
157
- break ;
158
- case PASSWORD :
159
- default :
160
- // Don't do anything special
161
- break ;
141
+ case API_KEY : // Generate new api key
142
+ int preLength = kapuaAuthenticationSetting .getInt (KapuaAuthenticationSettingKeys .AUTHENTICATION_CREDENTIAL_APIKEY_PRE_LENGTH );
143
+ int keyLength = kapuaAuthenticationSetting .getInt (KapuaAuthenticationSettingKeys .AUTHENTICATION_CREDENTIAL_APIKEY_KEY_LENGTH );
144
+
145
+ byte [] bPre = new byte [preLength ];
146
+ random .nextBytes (bPre );
147
+ String pre = Base64 .encodeToString (bPre ).substring (0 , preLength );
148
+
149
+ byte [] bKey = new byte [keyLength ];
150
+ random .nextBytes (bKey );
151
+ String key = Base64 .encodeToString (bKey );
152
+
153
+ fullKey .set (pre + key );
154
+
155
+ credentialCreator = new CredentialCreatorImpl (credentialCreator .getScopeId (),
156
+ credentialCreator .getUserId (),
157
+ credentialCreator .getCredentialType (),
158
+ fullKey .get (),
159
+ credentialCreator .getCredentialStatus (),
160
+ credentialCreator .getExpirationDate ());
161
+ break ;
162
+ case PASSWORD :
163
+ default :
164
+ // Don't do anything special
165
+ break ;
162
166
}
163
167
// Create Credential
164
168
Credential newCredential = credentialMapper .map (credentialCreator );
@@ -325,7 +329,7 @@ public void unlock(KapuaId scopeId, KapuaId credentialId) throws KapuaException
325
329
326
330
@ Override
327
331
public int getMinimumPasswordLength (KapuaId scopeId ) throws KapuaException {
328
- return txManager .execute (tx -> passwordValidator .getMinimumPasswordLength (tx , scopeId ));
332
+ return txManager .execute (tx -> accountPasswordLengthProvider .getMinimumPasswordLength (tx , scopeId ));
329
333
}
330
334
331
335
private long countExistingCredentials (CredentialType credentialType , KapuaId scopeId , KapuaId userId ) throws KapuaException {
0 commit comments