/
AuthenticationModule.java
357 lines (328 loc) · 17.6 KB
/
AuthenticationModule.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*******************************************************************************
* Copyright (c) 2021, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.service.authentication.shiro;
import com.google.inject.Provides;
import com.google.inject.multibindings.ProvidesIntoSet;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.KapuaRuntimeException;
import org.eclipse.kapua.commons.configuration.CachingServiceConfigRepository;
import org.eclipse.kapua.commons.configuration.RootUserTester;
import org.eclipse.kapua.commons.configuration.ServiceConfigImplJpaRepository;
import org.eclipse.kapua.commons.configuration.ServiceConfigurationManagerCachingWrapper;
import org.eclipse.kapua.commons.core.AbstractKapuaModule;
import org.eclipse.kapua.commons.core.ServiceModule;
import org.eclipse.kapua.commons.event.ServiceEventHouseKeeperFactoryImpl;
import org.eclipse.kapua.commons.jpa.EntityCacheFactory;
import org.eclipse.kapua.commons.jpa.KapuaJpaRepositoryConfiguration;
import org.eclipse.kapua.commons.jpa.KapuaJpaTxManagerFactory;
import org.eclipse.kapua.commons.model.domains.Domains;
import org.eclipse.kapua.commons.service.event.store.api.EventStoreFactory;
import org.eclipse.kapua.commons.service.event.store.api.EventStoreRecordRepository;
import org.eclipse.kapua.commons.service.event.store.internal.EventStoreServiceImpl;
import org.eclipse.kapua.commons.util.qr.QRCodeBuilder;
import org.eclipse.kapua.event.ServiceEventBus;
import org.eclipse.kapua.event.ServiceEventBusException;
import org.eclipse.kapua.model.config.metatype.KapuaTocd;
import org.eclipse.kapua.model.domain.Actions;
import org.eclipse.kapua.model.domain.Domain;
import org.eclipse.kapua.model.domain.DomainEntry;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.service.account.AccountService;
import org.eclipse.kapua.service.authentication.AuthenticationService;
import org.eclipse.kapua.service.authentication.CredentialsFactory;
import org.eclipse.kapua.service.authentication.credential.CredentialFactory;
import org.eclipse.kapua.service.authentication.credential.CredentialRepository;
import org.eclipse.kapua.service.authentication.credential.CredentialService;
import org.eclipse.kapua.service.authentication.credential.cache.CacheMetric;
import org.eclipse.kapua.service.authentication.credential.mfa.MfaOptionFactory;
import org.eclipse.kapua.service.authentication.credential.mfa.MfaOptionRepository;
import org.eclipse.kapua.service.authentication.credential.mfa.MfaOptionService;
import org.eclipse.kapua.service.authentication.credential.mfa.ScratchCodeFactory;
import org.eclipse.kapua.service.authentication.credential.mfa.ScratchCodeRepository;
import org.eclipse.kapua.service.authentication.credential.mfa.ScratchCodeService;
import org.eclipse.kapua.service.authentication.credential.mfa.shiro.MfaOptionFactoryImpl;
import org.eclipse.kapua.service.authentication.credential.mfa.shiro.MfaOptionImplJpaRepository;
import org.eclipse.kapua.service.authentication.credential.mfa.shiro.MfaOptionServiceImpl;
import org.eclipse.kapua.service.authentication.credential.mfa.shiro.ScratchCodeFactoryImpl;
import org.eclipse.kapua.service.authentication.credential.mfa.shiro.ScratchCodeImplJpaRepository;
import org.eclipse.kapua.service.authentication.credential.mfa.shiro.ScratchCodeServiceImpl;
import org.eclipse.kapua.service.authentication.credential.shiro.CredentialFactoryImpl;
import org.eclipse.kapua.service.authentication.credential.shiro.CredentialImplJpaRepository;
import org.eclipse.kapua.service.authentication.credential.shiro.CredentialMapper;
import org.eclipse.kapua.service.authentication.credential.shiro.CredentialMapperImpl;
import org.eclipse.kapua.service.authentication.credential.shiro.CredentialServiceImpl;
import org.eclipse.kapua.service.authentication.credential.shiro.PasswordResetter;
import org.eclipse.kapua.service.authentication.credential.shiro.PasswordValidator;
import org.eclipse.kapua.service.authentication.credential.shiro.PasswordValidatorImpl;
import org.eclipse.kapua.service.authentication.exception.KapuaAuthenticationErrorCodes;
import org.eclipse.kapua.service.authentication.mfa.MfaAuthenticator;
import org.eclipse.kapua.service.authentication.registration.RegistrationService;
import org.eclipse.kapua.service.authentication.shiro.mfa.MfaAuthenticatorImpl;
import org.eclipse.kapua.service.authentication.shiro.realm.AccessTokenCredentialsHandler;
import org.eclipse.kapua.service.authentication.shiro.realm.ApiKeyCredentialsHandler;
import org.eclipse.kapua.service.authentication.shiro.realm.CredentialsHandler;
import org.eclipse.kapua.service.authentication.shiro.realm.JwtCredentialsHandler;
import org.eclipse.kapua.service.authentication.shiro.realm.UserPassCredentialsHandler;
import org.eclipse.kapua.service.authentication.shiro.registration.RegistrationServiceImpl;
import org.eclipse.kapua.service.authentication.shiro.setting.KapuaAuthenticationSetting;
import org.eclipse.kapua.service.authentication.shiro.setting.KapuaAuthenticationSettingKeys;
import org.eclipse.kapua.service.authentication.shiro.setting.KapuaCryptoSetting;
import org.eclipse.kapua.service.authentication.shiro.utils.AuthenticationUtils;
import org.eclipse.kapua.service.authentication.token.AccessTokenFactory;
import org.eclipse.kapua.service.authentication.token.AccessTokenRepository;
import org.eclipse.kapua.service.authentication.token.AccessTokenService;
import org.eclipse.kapua.service.authentication.token.shiro.AccessTokenFactoryImpl;
import org.eclipse.kapua.service.authentication.token.shiro.AccessTokenImplJpaRepository;
import org.eclipse.kapua.service.authentication.token.shiro.AccessTokenServiceImpl;
import org.eclipse.kapua.service.authorization.AuthorizationService;
import org.eclipse.kapua.service.authorization.permission.PermissionFactory;
import org.eclipse.kapua.service.user.UserService;
import org.eclipse.kapua.storage.TxContext;
import javax.inject.Named;
import javax.inject.Singleton;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Map;
import java.util.Optional;
public class AuthenticationModule extends AbstractKapuaModule {
@Override
protected void configureModule() {
bind(KapuaAuthenticationSetting.class).in(Singleton.class);
bind(AuthenticationService.class).to(AuthenticationServiceShiroImpl.class).in(Singleton.class);
bind(CredentialFactory.class).to(CredentialFactoryImpl.class).in(Singleton.class);
bind(CredentialsFactory.class).to(CredentialsFactoryImpl.class).in(Singleton.class);
bind(MfaOptionFactory.class).to(MfaOptionFactoryImpl.class).in(Singleton.class);
bind(ScratchCodeFactory.class).to(ScratchCodeFactoryImpl.class).in(Singleton.class);
bind(AccessTokenFactory.class).to(AccessTokenFactoryImpl.class).in(Singleton.class);
bind(RegistrationService.class).to(RegistrationServiceImpl.class).in(Singleton.class);
bind(MfaAuthenticator.class).to(MfaAuthenticatorImpl.class).in(Singleton.class);
bind(KapuaCryptoSetting.class).in(Singleton.class);
bind(CacheMetric.class).in(Singleton.class);
}
@Provides
@Singleton
AuthenticationUtils authenticationUtils(KapuaCryptoSetting kapuaCryptoSetting) {
final SecureRandom random;
try {
random = SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e) {
throw new KapuaRuntimeException(KapuaAuthenticationErrorCodes.CREDENTIAL_CRYPT_ERROR, e);
}
return new AuthenticationUtils(random, kapuaCryptoSetting);
}
@ProvidesIntoSet
public Domain accessTokenDomain() {
return new DomainEntry(Domains.ACCESS_TOKEN, AccessTokenService.class.getName(), false, Actions.read, Actions.delete, Actions.write);
}
@ProvidesIntoSet
public Domain credentialDomain() {
return new DomainEntry(Domains.CREDENTIAL, CredentialService.class.getName(), false, Actions.read, Actions.delete, Actions.write);
}
@ProvidesIntoSet
public ServiceModule authenticationServiceModule(AccessTokenService accessTokenService,
CredentialService credentialService,
AuthorizationService authorizationService,
PermissionFactory permissionFactory,
KapuaJpaTxManagerFactory txManagerFactory,
EventStoreFactory eventStoreFactory,
EventStoreRecordRepository eventStoreRecordRepository,
ServiceEventBus serviceEventBus,
KapuaAuthenticationSetting kapuaAuthenticationSetting,
@Named("eventsModuleName") String eventModuleName
) throws ServiceEventBusException {
return new AuthenticationServiceModule(
credentialService,
accessTokenService,
kapuaAuthenticationSetting,
new ServiceEventHouseKeeperFactoryImpl(
new EventStoreServiceImpl(
authorizationService,
permissionFactory,
txManagerFactory.create("kapua-authentication"),
eventStoreFactory,
eventStoreRecordRepository
),
txManagerFactory.create("kapua-authentication"),
serviceEventBus
),
serviceEventBus,
eventModuleName);
}
@ProvidesIntoSet
public CredentialsHandler usernamePasswordCredentialsHandler() {
return new UserPassCredentialsHandler();
}
@ProvidesIntoSet
public CredentialsHandler apiKeyCredentialsHandler() {
return new ApiKeyCredentialsHandler();
}
@ProvidesIntoSet
public CredentialsHandler jwtCredentialsHandler() {
return new JwtCredentialsHandler();
}
@ProvidesIntoSet
public CredentialsHandler accessTokenCredentialsHandler() {
return new AccessTokenCredentialsHandler();
}
@Provides
@Singleton
PasswordValidator passwordValidator(CredentialServiceConfigurationManager credentialServiceConfigurationManager) {
return new PasswordValidatorImpl(credentialServiceConfigurationManager);
}
@Provides
@Singleton
CredentialMapper credentialMapper(CredentialFactory credentialFactory, KapuaAuthenticationSetting kapuaAuthenticationSetting,
AuthenticationUtils authenticationUtils) {
return new CredentialMapperImpl(credentialFactory, kapuaAuthenticationSetting, authenticationUtils);
}
@Provides
@Singleton
AccessTokenService accessTokenService(
AuthorizationService authorizationService,
PermissionFactory permissionFactory,
AccessTokenRepository accessTokenRepository,
AccessTokenFactory accessTokenFactory,
KapuaJpaTxManagerFactory jpaTxManagerFactory) {
return new AccessTokenServiceImpl(
authorizationService,
permissionFactory,
jpaTxManagerFactory.create("kapua-authentication"),
accessTokenRepository,
accessTokenFactory);
}
@Provides
@Singleton
MfaOptionService mfaOptionService(
MfaAuthenticator mfaAuthenticator,
MfaOptionRepository mfaOptionRepository,
AccountService accountService,
ScratchCodeRepository scratchCodeRepository,
AuthorizationService authorizationService,
PermissionFactory permissionFactory,
UserService userService,
KapuaJpaTxManagerFactory jpaTxManagerFactory,
KapuaAuthenticationSetting kapuaAuthenticationSetting,
AuthenticationUtils authenticationUtils,
QRCodeBuilder qrCodeBuilder) {
int trustKeyDuration = kapuaAuthenticationSetting.getInt(KapuaAuthenticationSettingKeys.AUTHENTICATION_MFA_TRUST_KEY_DURATION);
return new MfaOptionServiceImpl(
trustKeyDuration,
mfaAuthenticator,
jpaTxManagerFactory.create("kapua-authentication"),
mfaOptionRepository,
accountService,
scratchCodeRepository,
authorizationService,
permissionFactory,
userService,
authenticationUtils,
qrCodeBuilder
);
}
@Provides
@Singleton
ScratchCodeService scratchCodeService(
AuthorizationService authorizationService,
PermissionFactory permissionFactory,
ScratchCodeRepository scratchCodeRepository,
KapuaJpaTxManagerFactory jpaTxManagerFactory,
AuthenticationUtils authenticationUtils) {
return new ScratchCodeServiceImpl(
authorizationService,
permissionFactory,
jpaTxManagerFactory.create("kapua-authentication"),
scratchCodeRepository);
}
@Provides
@Singleton
public AccessTokenRepository accessTokenRepository(KapuaJpaRepositoryConfiguration jpaRepoConfig) {
return new AccessTokenImplJpaRepository(jpaRepoConfig);
}
@Provides
@Singleton
public MfaOptionRepository mfaOptionRepository(KapuaJpaRepositoryConfiguration jpaRepoConfig) {
return new MfaOptionImplJpaRepository(jpaRepoConfig);
}
@Provides
@Singleton
public ScratchCodeRepository scratchCodeRepository(KapuaJpaRepositoryConfiguration jpaRepoConfig) {
return new ScratchCodeImplJpaRepository(jpaRepoConfig);
}
@Provides
@Singleton
public CredentialService credentialService(
CredentialServiceConfigurationManager serviceConfigurationManager,
AuthorizationService authorizationService,
PermissionFactory permissionFactory,
CredentialRepository credentialRepository,
CredentialFactory credentialFactory,
KapuaJpaTxManagerFactory jpaTxManagerFactory,
CredentialMapper credentialMapper,
PasswordValidator passwordValidator,
KapuaAuthenticationSetting kapuaAuthenticationSetting,
PasswordResetter passwordResetter) {
return new CredentialServiceImpl(serviceConfigurationManager,
authorizationService,
permissionFactory,
jpaTxManagerFactory.create("kapua-authentication"),
credentialRepository,
credentialFactory,
credentialMapper,
passwordValidator,
kapuaAuthenticationSetting,
passwordResetter);
}
@Provides
@Singleton
CredentialRepository credentialRepository(KapuaJpaRepositoryConfiguration jpaRepoConfig) {
return new CredentialImplJpaRepository(jpaRepoConfig);
}
@Provides
@Singleton
public CredentialServiceConfigurationManager credentialServiceConfigurationManager(
RootUserTester rootUserTester,
KapuaJpaRepositoryConfiguration jpaRepoConfig,
KapuaAuthenticationSetting kapuaAuthenticationSetting,
EntityCacheFactory entityCacheFactory) {
final CredentialServiceConfigurationManagerImpl credentialServiceConfigurationManager = new CredentialServiceConfigurationManagerImpl(
new CachingServiceConfigRepository(
new ServiceConfigImplJpaRepository(jpaRepoConfig),
entityCacheFactory.createCache("AbstractKapuaConfigurableServiceCacheId")
),
rootUserTester,
kapuaAuthenticationSetting);
final ServiceConfigurationManagerCachingWrapper cached = new ServiceConfigurationManagerCachingWrapper(credentialServiceConfigurationManager);
return new CredentialServiceConfigurationManager() {
@Override
public int getSystemMinimumPasswordLength() {
return credentialServiceConfigurationManager.getSystemMinimumPasswordLength();
}
@Override
public void checkAllowedEntities(TxContext txContext, KapuaId scopeId, String entityType) throws KapuaException {
cached.checkAllowedEntities(txContext, scopeId, entityType);
}
@Override
public void setConfigValues(TxContext txContext, KapuaId scopeId, Optional<KapuaId> parentId, Map<String, Object> values) throws KapuaException {
cached.setConfigValues(txContext, scopeId, parentId, values);
}
@Override
public Map<String, Object> getConfigValues(TxContext txContext, KapuaId scopeId, boolean excludeDisabled) throws KapuaException {
return cached.getConfigValues(txContext, scopeId, excludeDisabled);
}
@Override
public KapuaTocd getConfigMetadata(TxContext txContext, KapuaId scopeId, boolean excludeDisabled) throws KapuaException {
return cached.getConfigMetadata(txContext, scopeId, excludeDisabled);
}
};
}
}