use of io.gravitee.am.gateway.handler.root.service.user.UserService in project gravitee-access-management by gravitee-io.
the class UserServiceImpl method confirmRegistration.
@Override
public Single<RegistrationResponse> confirmRegistration(Client client, User user, io.gravitee.am.identityprovider.api.User principal) {
// user has completed his account, add it to the idp
return identityProviderManager.getUserProvider(user.getSource()).switchIfEmpty(Maybe.error(new UserProviderNotFoundException(user.getSource()))).flatMapSingle(userProvider -> userProvider.findByUsername(user.getUsername()).switchIfEmpty(Maybe.error(new UserNotFoundException(user.getUsername()))).flatMapSingle(idpUser -> userProvider.update(idpUser.getId(), convert(user))).onErrorResumeNext(ex -> {
if (ex instanceof UserNotFoundException) {
// idp user not found, create its account
return userProvider.create(convert(user));
}
return Single.error(ex);
})).flatMap(idpUser -> {
// update 'users' collection for management and audit purpose
user.setPassword(null);
user.setRegistrationCompleted(true);
user.setEnabled(true);
user.setExternalId(idpUser.getId());
user.setUpdatedAt(new Date());
// additional information
extractAdditionalInformation(user, idpUser.getAdditionalInformation());
// set login information
AccountSettings accountSettings = AccountSettings.getInstance(domain, client);
if (accountSettings != null && accountSettings.isAutoLoginAfterRegistration()) {
user.setLoggedAt(new Date());
user.setLoginsCount(1l);
}
return userService.update(user);
}).flatMap(userService::enhance).map(user1 -> {
AccountSettings accountSettings = AccountSettings.getInstance(domain, client);
return new RegistrationResponse(user1, accountSettings != null ? accountSettings.getRedirectUriAfterRegistration() : null, accountSettings != null ? accountSettings.isAutoLoginAfterRegistration() : false);
}).doOnSuccess(response -> auditService.report(AuditBuilder.builder(UserAuditBuilder.class).domain(domain.getId()).client(user.getClient()).principal(principal).type(EventType.REGISTRATION_CONFIRMATION))).doOnError(throwable -> auditService.report(AuditBuilder.builder(UserAuditBuilder.class).domain(domain.getId()).client(user.getClient()).principal(principal).type(EventType.REGISTRATION_CONFIRMATION).throwable(throwable)));
}
use of io.gravitee.am.gateway.handler.root.service.user.UserService in project gravitee-access-management by gravitee-io.
the class EmailFactorProvider method generateCodeAndSendEmail.
private Completable generateCodeAndSendEmail(FactorContext context, EmailSenderProvider provider, EnrolledFactor enrolledFactor) {
logger.debug("Generating factor code of {} digits", configuration.getReturnDigits());
try {
UserService userService = context.getComponent(UserService.class);
EmailService emailService = context.getComponent(EmailService.class);
// register mfa code to make it available into the TemplateEngine values
Map<String, Object> params = context.getTemplateValues();
params.put(FactorContext.KEY_CODE, generateOTP(enrolledFactor));
final String recipient = enrolledFactor.getChannel().getTarget();
EmailService.EmailWrapper emailWrapper = emailService.createEmail(Template.MFA_CHALLENGE, context.getClient(), asList(recipient), params);
return provider.sendMessage(emailWrapper.getEmail()).andThen(Single.just(enrolledFactor).flatMap(ef -> {
ef.setPrimary(true);
ef.setStatus(FactorStatus.ACTIVATED);
ef.getSecurity().putData(FactorDataKeys.KEY_EXPIRE_AT, emailWrapper.getExpireAt());
return userService.addFactor(context.getUser().getId(), ef, new DefaultUser(context.getUser()));
}).ignoreElement());
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
logger.error("Code generation fails", e);
return Completable.error(new TechnicalException("Code can't be sent"));
} catch (Exception e) {
logger.error("Email templating fails", e);
return Completable.error(new TechnicalException("Email can't be sent"));
}
}
use of io.gravitee.am.gateway.handler.root.service.user.UserService in project gravitee-access-management by gravitee-io.
the class RecoveryCodeFactorProvider method addRecoveryCodeFactor.
private Completable addRecoveryCodeFactor(FactorContext context, EnrolledFactor enrolledFactor) {
try {
final UserService userService = context.getComponent(UserService.class);
enrolledFactor.setSecurity(createEnrolledFactorSecurity());
return userService.addFactor(context.getUser().getId(), enrolledFactor, new DefaultUser(context.getUser())).ignoreElement();
} catch (Exception ex) {
return Completable.error(ex);
}
}
use of io.gravitee.am.gateway.handler.root.service.user.UserService in project gravitee-access-management by gravitee-io.
the class MFARecoveryCodeEndpointTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
client = new Client();
client.setClientId(UUID.randomUUID().toString());
final User user = new User();
setFactorsFor(user);
mfaRecoveryCodeEndpoint = new MFARecoveryCodeEndpoint(templateEngine, domain, userService);
router.route().handler(ctx -> {
ctx.setUser(io.vertx.reactivex.ext.auth.User.newInstance(new io.gravitee.am.gateway.handler.common.vertx.web.auth.user.User(user)));
ctx.put(ConstantKeys.CLIENT_CONTEXT_KEY, client);
ctx.next();
}).handler(BodyHandler.create());
}
use of io.gravitee.am.gateway.handler.root.service.user.UserService in project gravitee-access-management by gravitee-io.
the class EmailFactorProviderTest method shouldSendEmailAndGenerateCode.
@Test
public void shouldSendEmailAndGenerateCode() throws Exception {
EmailSenderProvider smtpProvider = mock(EmailSenderProvider.class);
when(resourceManager.getResourceProvider(any())).thenReturn(smtpProvider);
Email template = new Email();
template.setTemplate("mfa_challenge.html");
template.setSubject("Some Subject");
template.setExpiresAfter(600);
io.gravitee.am.common.email.Email generatedEmail = new io.gravitee.am.common.email.Email();
generatedEmail.setTo(new String[] { RECIPIENT });
when(emailService.createEmail(any(), any(), any(), any())).thenReturn(new EmailService.EmailWrapper(generatedEmail));
EnrolledFactor enrolled = new EnrolledFactor();
enrolled.setUpdatedAt(new Date());
Map<String, Object> additionalData = new Maps.MapBuilder(new HashMap()).put(FactorDataKeys.KEY_MOVING_FACTOR, 0).put(FactorDataKeys.KEY_EXPIRE_AT, System.currentTimeMillis() + 600).build();
enrolled.setSecurity(new EnrolledFactorSecurity(FactorSecurityType.SHARED_SECRET, SHARED_SECRET, additionalData));
enrolled.setChannel(new EnrolledFactorChannel(EnrolledFactorChannel.Type.EMAIL, RECIPIENT));
when(factorContext.getData(FactorContext.KEY_ENROLLED_FACTOR, EnrolledFactor.class)).thenReturn(enrolled);
when(factorContext.getTemplateValues()).thenReturn(new HashMap<>());
User user = mock(User.class);
when(user.getId()).thenReturn("id");
when(factorContext.getUser()).thenReturn(user);
when(userService.addFactor(any(), any(), any())).thenReturn(Single.just(user));
when(smtpProvider.sendMessage(any())).thenReturn(Completable.complete());
TestObserver<Void> test = cut.sendChallenge(factorContext).test();
test.awaitTerminalEvent();
test.assertNoValues();
test.assertNoErrors();
verify(smtpProvider).sendMessage(argThat(m -> m.getTo()[0].equals(RECIPIENT)));
verify(userService).addFactor(any(), any(), any());
}
Aggregations