Search in sources :

Example 11 with EntityNotFoundException

use of eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException in project CzechIdMng by bcvsolutions.

the class AbstractRequestDtoController method delete.

/**
 * Deletes DTO by given id
 *
 * @param requestId
 * @param backendId
 * @return
 */
@ApiOperation(value = "Delete record", authorizations = { @Authorization(SwaggerConfig.AUTHENTICATION_BASIC), @Authorization(SwaggerConfig.AUTHENTICATION_CIDMST) })
public // 
ResponseEntity<?> delete(// 
@ApiParam(value = "Request ID", required = true) String requestId, @ApiParam(value = "Record's uuid identifier or unique code.", required = true) String backendId) {
    // 
    DTO dto = getDto(requestId, backendId);
    if (dto == null) {
        throw new EntityNotFoundException(getService().getEntityClass(), backendId);
    }
    Requestable resultDto = requestManager.delete(requestId, dto, IdmBasePermission.DELETE);
    @SuppressWarnings("unchecked") ResourceSupport resource = toResource(requestId, (DTO) resultDto);
    if (resource == null) {
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
    return new ResponseEntity<>(resource, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Requestable(eu.bcvsolutions.idm.core.api.domain.Requestable) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) ResourceSupport(org.springframework.hateoas.ResourceSupport) ApiOperation(io.swagger.annotations.ApiOperation)

Example 12 with EntityNotFoundException

use of eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException in project CzechIdMng by bcvsolutions.

the class IdmNotificationAttachmentController method download.

@RequestMapping(value = "/{backendId}/download", method = RequestMethod.GET)
@ResponseBody
@PreAuthorize("hasAuthority('" + NotificationGroupPermission.NOTIFICATION_READ + "')")
@ApiOperation(value = "Download notification attachment", nickname = "downloadNotificationAttachment", tags = { IdmNotificationAttachmentController.TAG }, notes = "Returns input stream to notification attachment.", authorizations = { @Authorization(value = SwaggerConfig.AUTHENTICATION_BASIC, scopes = { @AuthorizationScope(scope = NotificationGroupPermission.NOTIFICATION_READ, description = "") }), @Authorization(value = SwaggerConfig.AUTHENTICATION_CIDMST, scopes = { @AuthorizationScope(scope = NotificationGroupPermission.NOTIFICATION_READ, description = "") }) })
public ResponseEntity<InputStreamResource> download(@ApiParam(value = "Notification attachment uuid identifier.", required = true) @PathVariable String backendId) {
    IdmNotificationAttachmentDto dto = getDto(backendId);
    if (dto == null) {
        throw new EntityNotFoundException(getService().getEntityClass(), backendId);
    }
    // 
    UUID attachmentId = dto.getAttachment();
    IdmAttachmentDto attachment = attachmentManager.get(attachmentId);
    if (attachment == null) {
        throw new EntityNotFoundException(attachmentManager.getEntityClass(), attachmentId);
    }
    // 
    InputStream is = attachmentManager.getAttachmentData(attachment.getId());
    // 
    try {
        BodyBuilder response = ResponseEntity.ok().contentLength(is.available()).header(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=\"%s\"", attachment.getName()));
        // append media type, if it's filled
        String mimetype = attachment.getMimetype();
        if (StringUtils.isNotBlank(mimetype)) {
            response = response.contentType(MediaType.valueOf(attachment.getMimetype()));
        }
        // 
        return response.body(new InputStreamResource(is));
    } catch (IOException e) {
        throw new ResultCodeException(CoreResultCode.INTERNAL_SERVER_ERROR, e);
    }
}
Also used : IdmAttachmentDto(eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto) IdmNotificationAttachmentDto(eu.bcvsolutions.idm.core.notification.api.dto.IdmNotificationAttachmentDto) InputStream(java.io.InputStream) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) IOException(java.io.IOException) UUID(java.util.UUID) BodyBuilder(org.springframework.http.ResponseEntity.BodyBuilder) InputStreamResource(org.springframework.core.io.InputStreamResource) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 13 with EntityNotFoundException

use of eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException in project CzechIdMng by bcvsolutions.

the class DefaultLoginService method switchUserLogout.

@Override
public LoginDto switchUserLogout() {
    IdmTokenDto currentToken = tokenManager.getCurrentToken();
    ConfigurationMap properties = currentToken.getProperties();
    String originalUsername = properties.getString(JwtAuthenticationMapper.PROPERTY_ORIGINAL_USERNAME);
    UUID originalId = properties.getUuid(JwtAuthenticationMapper.PROPERTY_ORIGINAL_IDENTITY_ID);
    String loggedAction = siemLogger.buildAction(SiemLoggerManager.LOGIN_LEVEL_KEY, SiemLoggerManager.SWITCH_SUBLEVEL_KEY);
    String subjectUsername = securityService.getCurrentUsername();
    String subjectUuid = Objects.toString(securityService.getCurrentId(), "");
    String targetUuid = Objects.toString(originalId, "");
    try {
        // 
        if (originalId == null) {
            throw new ResultCodeException(CoreResultCode.NULL_ATTRIBUTE, ImmutableMap.of("attribute", "originalUsername"));
        }
        // change logged token authorities
        IdmIdentityDto identity = identityService.get(originalId);
        if (identity == null) {
            throw new EntityNotFoundException(IdmIdentity.class, originalId);
        }
        // 
        // Preserve the first original user => switch is available repetitively, but original user is preserved.
        properties.remove(JwtAuthenticationMapper.PROPERTY_ORIGINAL_USERNAME);
        properties.remove(JwtAuthenticationMapper.PROPERTY_ORIGINAL_IDENTITY_ID);
        currentToken.setProperties(properties);
        IdmTokenDto switchedToken = jwtTokenMapper.createToken(identity, currentToken);
        // 
        // login by updated token
        LOG.info("Identity with username [{}] - logout from switched user [{}].", originalUsername, securityService.getCurrentUsername());
        // 
        LoginDto login = login(identity, switchedToken);
        siemLogger.log(loggedAction, SiemLoggerManager.SUCCESS_ACTION_STATUS, originalUsername, targetUuid, subjectUsername, subjectUuid, null, null);
        return login;
    } catch (Exception e) {
        siemLogger.log(loggedAction, SiemLoggerManager.FAILED_ACTION_STATUS, originalUsername, targetUuid, subjectUsername, subjectUuid, null, e.getMessage());
        throw e;
    }
}
Also used : IdmTokenDto(eu.bcvsolutions.idm.core.api.dto.IdmTokenDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) ConfigurationMap(eu.bcvsolutions.idm.core.api.domain.ConfigurationMap) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) UUID(java.util.UUID) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) LoginDto(eu.bcvsolutions.idm.core.security.api.dto.LoginDto) IdentityNotFoundException(eu.bcvsolutions.idm.core.security.api.exception.IdentityNotFoundException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) IdmAuthenticationException(eu.bcvsolutions.idm.core.security.api.exception.IdmAuthenticationException) IdentityDisabledException(eu.bcvsolutions.idm.core.security.api.exception.IdentityDisabledException)

Example 14 with EntityNotFoundException

use of eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException in project CzechIdMng by bcvsolutions.

the class DefaultTwoFactorAuthenticationManager method confirm.

@Override
@Transactional
public boolean confirm(UUID identityId, TwoFactorRegistrationConfirmDto registrationConfirm) {
    Assert.notNull(identityId, "Identity identifier is required.");
    Assert.notNull(registrationConfirm, "Two factor confirm request is required.");
    // 
    // support two factor authentication, even when identity is not authenticated by IdM (secret is required to persist only)
    IdmPasswordDto password = passwordService.findOrCreateByIdentity(identityId);
    if (password == null) {
        throw new EntityNotFoundException(IdmIdentityDto.class, identityId);
    }
    // 
    GuardedString verificationSecret = registrationConfirm.getVerificationSecret();
    GuardedString verificationCode = registrationConfirm.getVerificationCode();
    // 
    if (!verifyCode(verificationSecret, verificationCode)) {
        throw new ResultCodeException(CoreResultCode.TWO_FACTOR_VERIFICATION_CODE_FAILED);
    }
    // 
    password.setVerificationSecret(verificationSecret.asString());
    passwordService.save(password);
    // 
    IdmProfileDto profile = profileService.findOrCreateByIdentity(identityId);
    profile.setTwoFactorAuthenticationType(registrationConfirm.getTwoFactorAuthenticationType());
    profileService.save(profile);
    // 
    return true;
}
Also used : IdmProfileDto(eu.bcvsolutions.idm.core.api.dto.IdmProfileDto) IdmPasswordDto(eu.bcvsolutions.idm.core.api.dto.IdmPasswordDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) Transactional(org.springframework.transaction.annotation.Transactional)

Example 15 with EntityNotFoundException

use of eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException in project CzechIdMng by bcvsolutions.

the class DefaultTwoFactorAuthenticationManager method requireTwoFactorAuthentication.

@Override
@Transactional
public boolean requireTwoFactorAuthentication(UUID identityId, UUID tokenId) {
    // check two factor authentication is enabled
    TwoFactorAuthenticationType twoFactorAuthenticationType = getTwoFactorAuthenticationType(identityId);
    if (twoFactorAuthenticationType == null) {
        return false;
    }
    // 
    IdmTokenDto token = tokenManager.getToken(tokenId);
    if (token.isSecretVerified()) {
        // token was already verified
        return false;
    }
    // 
    if (TwoFactorAuthenticationType.NOTIFICATION == twoFactorAuthenticationType) {
        IdmPasswordDto password = passwordService.findOneByIdentity(identityId);
        if (password == null) {
            throw new EntityNotFoundException(IdmIdentityDto.class, identityId);
        }
        sendVerificationCode(identityService.get(identityId), generateCode(new GuardedString(password.getVerificationSecret())));
    }
    // 
    return true;
}
Also used : IdmTokenDto(eu.bcvsolutions.idm.core.api.dto.IdmTokenDto) TwoFactorAuthenticationType(eu.bcvsolutions.idm.core.security.api.domain.TwoFactorAuthenticationType) IdmPasswordDto(eu.bcvsolutions.idm.core.api.dto.IdmPasswordDto) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

EntityNotFoundException (eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException)30 ApiOperation (io.swagger.annotations.ApiOperation)15 ResponseEntity (org.springframework.http.ResponseEntity)12 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)10 UUID (java.util.UUID)10 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)9 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)9 IdmLongRunningTaskDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto)7 ResourceSupport (org.springframework.hateoas.ResourceSupport)7 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)6 Transactional (org.springframework.transaction.annotation.Transactional)5 IdmAttachmentDto (eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto)4 IOException (java.io.IOException)4 IdmPasswordDto (eu.bcvsolutions.idm.core.api.dto.IdmPasswordDto)3 IdmProfileDto (eu.bcvsolutions.idm.core.api.dto.IdmProfileDto)3 IdmRoleRequestDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto)3 IdmTokenDto (eu.bcvsolutions.idm.core.api.dto.IdmTokenDto)3 AcceptedException (eu.bcvsolutions.idm.core.api.exception.AcceptedException)3 IdmLongRunningTaskFilter (eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter)3