Search in sources :

Example 1 with ActionNotPossibleException

use of org.niis.xroad.securityserver.restapi.service.ActionNotPossibleException in project X-Road by nordic-institute.

the class KeysApiController method generateCsr.

// squid: see reason below. checkstyle: for readability
@SuppressWarnings({ "squid:S3655", "checkstyle:LineLength" })
@Override
@PreAuthorize("(hasAuthority('GENERATE_AUTH_CERT_REQ') and " + "#csrGenerate.keyUsageType == T(org.niis.xroad.securityserver.restapi.openapi.model.KeyUsageType).AUTHENTICATION)" + " or (hasAuthority('GENERATE_SIGN_CERT_REQ') and " + "#csrGenerate.keyUsageType == T(org.niis.xroad.securityserver.restapi.openapi.model.KeyUsageType).SIGNING)")
@AuditEventMethod(event = RestApiAuditEvent.GENERATE_CSR)
public ResponseEntity<Resource> generateCsr(String keyId, CsrGenerate csrGenerate) {
    // squid:S3655 throwing NoSuchElementException if there is no value present is
    // fine since keyUsageInfo is mandatory parameter
    KeyUsageInfo keyUsageInfo = KeyUsageTypeMapping.map(csrGenerate.getKeyUsageType()).get();
    ClientId memberId = null;
    if (KeyUsageInfo.SIGNING == keyUsageInfo) {
        // memberId not used for authentication csrs
        memberId = clientConverter.convertId(csrGenerate.getMemberId());
    }
    // squid:S3655 throwing NoSuchElementException if there is no value present is
    // fine since csr format is mandatory parameter
    CertificateRequestFormat csrFormat = CsrFormatMapping.map(csrGenerate.getCsrFormat()).get();
    byte[] csr;
    try {
        csr = tokenCertificateService.generateCertRequest(keyId, memberId, keyUsageInfo, csrGenerate.getCaName(), csrGenerate.getSubjectFieldValues(), csrFormat).getCertRequest();
    } catch (WrongKeyUsageException | DnFieldHelper.InvalidDnParameterException | ClientNotFoundException | CertificateAuthorityNotFoundException e) {
        throw new BadRequestException(e);
    } catch (KeyNotFoundException e) {
        throw new ResourceNotFoundException(e);
    } catch (ActionNotPossibleException e) {
        throw new ConflictException(e);
    }
    String filename = csrFilenameCreator.createCsrFilename(keyUsageInfo, csrFormat, memberId, serverConfService.getSecurityServerId());
    return ControllerUtil.createAttachmentResourceResponse(csr, filename);
}
Also used : ClientNotFoundException(org.niis.xroad.securityserver.restapi.service.ClientNotFoundException) ActionNotPossibleException(org.niis.xroad.securityserver.restapi.service.ActionNotPossibleException) CertificateAuthorityNotFoundException(org.niis.xroad.securityserver.restapi.service.CertificateAuthorityNotFoundException) CertificateRequestFormat(ee.ria.xroad.signer.protocol.message.CertificateRequestFormat) WrongKeyUsageException(org.niis.xroad.securityserver.restapi.service.WrongKeyUsageException) ClientId(ee.ria.xroad.common.identifier.ClientId) BadRequestException(org.niis.xroad.restapi.openapi.BadRequestException) ResourceNotFoundException(org.niis.xroad.restapi.openapi.ResourceNotFoundException) KeyUsageInfo(ee.ria.xroad.signer.protocol.dto.KeyUsageInfo) KeyNotFoundException(org.niis.xroad.securityserver.restapi.service.KeyNotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) AuditEventMethod(org.niis.xroad.restapi.config.audit.AuditEventMethod)

Example 2 with ActionNotPossibleException

use of org.niis.xroad.securityserver.restapi.service.ActionNotPossibleException in project X-Road by nordic-institute.

the class KeysApiController method updateKey.

@Override
@PreAuthorize("hasAuthority('EDIT_KEY_FRIENDLY_NAME')")
@AuditEventMethod(event = RestApiAuditEvent.UPDATE_KEY_NAME)
public ResponseEntity<Key> updateKey(String id, KeyName keyName) {
    KeyInfo keyInfo = null;
    try {
        keyInfo = keyService.updateKeyFriendlyName(id, keyName.getName());
    } catch (KeyNotFoundException e) {
        throw new ResourceNotFoundException(e);
    } catch (ActionNotPossibleException e) {
        throw new ConflictException(e);
    }
    Key key = keyConverter.convert(keyInfo);
    return new ResponseEntity<>(key, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) KeyInfo(ee.ria.xroad.signer.protocol.dto.KeyInfo) ActionNotPossibleException(org.niis.xroad.securityserver.restapi.service.ActionNotPossibleException) ResourceNotFoundException(org.niis.xroad.restapi.openapi.ResourceNotFoundException) KeyNotFoundException(org.niis.xroad.securityserver.restapi.service.KeyNotFoundException) Key(org.niis.xroad.securityserver.restapi.openapi.model.Key) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) AuditEventMethod(org.niis.xroad.restapi.config.audit.AuditEventMethod)

Example 3 with ActionNotPossibleException

use of org.niis.xroad.securityserver.restapi.service.ActionNotPossibleException in project X-Road by nordic-institute.

the class TokensApiController method addKey.

@PreAuthorize("hasAuthority('GENERATE_KEY')")
@Override
@AuditEventMethod(event = RestApiAuditEvent.GENERATE_KEY)
public ResponseEntity<Key> addKey(String tokenId, KeyLabel keyLabel) {
    try {
        KeyInfo keyInfo = keyService.addKey(tokenId, keyLabel.getLabel());
        Key key = keyConverter.convert(keyInfo);
        return ControllerUtil.createCreatedResponse("/api/keys/{keyId}", key, key.getId());
    } catch (TokenNotFoundException e) {
        throw new ResourceNotFoundException(e);
    } catch (ActionNotPossibleException e) {
        throw new ConflictException(e);
    }
}
Also used : KeyInfo(ee.ria.xroad.signer.protocol.dto.KeyInfo) ActionNotPossibleException(org.niis.xroad.securityserver.restapi.service.ActionNotPossibleException) ResourceNotFoundException(org.niis.xroad.restapi.openapi.ResourceNotFoundException) Key(org.niis.xroad.securityserver.restapi.openapi.model.Key) TokenNotFoundException(org.niis.xroad.securityserver.restapi.service.TokenNotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) AuditEventMethod(org.niis.xroad.restapi.config.audit.AuditEventMethod)

Example 4 with ActionNotPossibleException

use of org.niis.xroad.securityserver.restapi.service.ActionNotPossibleException in project X-Road by nordic-institute.

the class TokensApiController method loginToken.

@PreAuthorize("hasAuthority('ACTIVATE_DEACTIVATE_TOKEN')")
@Override
@AuditEventMethod(event = RestApiAuditEvent.LOGIN_TOKEN)
public ResponseEntity<Token> loginToken(String id, TokenPassword tokenPassword) {
    if (tokenPassword == null || tokenPassword.getPassword() == null || tokenPassword.getPassword().isEmpty()) {
        throw new BadRequestException("Missing token password");
    }
    char[] password = tokenPassword.getPassword().toCharArray();
    try {
        tokenService.activateToken(id, password);
    } catch (TokenNotFoundException e) {
        throw new ResourceNotFoundException(e);
    } catch (TokenService.PinIncorrectException e) {
        throw new BadRequestException(e);
    } catch (ActionNotPossibleException e) {
        throw new ConflictException(e);
    }
    Token token = getTokenFromService(id);
    return new ResponseEntity<>(token, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ActionNotPossibleException(org.niis.xroad.securityserver.restapi.service.ActionNotPossibleException) BadRequestException(org.niis.xroad.restapi.openapi.BadRequestException) Token(org.niis.xroad.securityserver.restapi.openapi.model.Token) ResourceNotFoundException(org.niis.xroad.restapi.openapi.ResourceNotFoundException) TokenNotFoundException(org.niis.xroad.securityserver.restapi.service.TokenNotFoundException) TokenService(org.niis.xroad.securityserver.restapi.service.TokenService) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) AuditEventMethod(org.niis.xroad.restapi.config.audit.AuditEventMethod)

Example 5 with ActionNotPossibleException

use of org.niis.xroad.securityserver.restapi.service.ActionNotPossibleException in project X-Road by nordic-institute.

the class KeysApiController method downloadCsr.

@Override
@PreAuthorize("hasAnyAuthority('GENERATE_AUTH_CERT_REQ', 'GENERATE_SIGN_CERT_REQ')")
public ResponseEntity<Resource> downloadCsr(String keyId, String csrId, CsrFormat csrFormat) {
    // squid:S3655 throwing NoSuchElementException if there is no value present is
    // fine since csr format is mandatory parameter
    CertificateRequestFormat certificateRequestFormat = CsrFormatMapping.map(csrFormat).get();
    GeneratedCertRequestInfo csrInfo;
    try {
        csrInfo = tokenCertificateService.regenerateCertRequest(keyId, csrId, certificateRequestFormat);
    } catch (KeyNotFoundException | CsrNotFoundException e) {
        throw new ResourceNotFoundException(e);
    } catch (ActionNotPossibleException e) {
        throw new ConflictException(e);
    }
    String filename = csrFilenameCreator.createCsrFilename(csrInfo.getKeyUsage(), certificateRequestFormat, csrInfo.getMemberId(), serverConfService.getSecurityServerId());
    return ControllerUtil.createAttachmentResourceResponse(csrInfo.getCertRequest(), filename);
}
Also used : ActionNotPossibleException(org.niis.xroad.securityserver.restapi.service.ActionNotPossibleException) GeneratedCertRequestInfo(ee.ria.xroad.commonui.SignerProxy.GeneratedCertRequestInfo) CsrNotFoundException(org.niis.xroad.securityserver.restapi.service.CsrNotFoundException) CertificateRequestFormat(ee.ria.xroad.signer.protocol.message.CertificateRequestFormat) ResourceNotFoundException(org.niis.xroad.restapi.openapi.ResourceNotFoundException) KeyNotFoundException(org.niis.xroad.securityserver.restapi.service.KeyNotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

ResourceNotFoundException (org.niis.xroad.restapi.openapi.ResourceNotFoundException)8 ActionNotPossibleException (org.niis.xroad.securityserver.restapi.service.ActionNotPossibleException)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)8 AuditEventMethod (org.niis.xroad.restapi.config.audit.AuditEventMethod)7 BadRequestException (org.niis.xroad.restapi.openapi.BadRequestException)4 KeyNotFoundException (org.niis.xroad.securityserver.restapi.service.KeyNotFoundException)4 TokenNotFoundException (org.niis.xroad.securityserver.restapi.service.TokenNotFoundException)4 ResponseEntity (org.springframework.http.ResponseEntity)4 CertificateRequestFormat (ee.ria.xroad.signer.protocol.message.CertificateRequestFormat)3 Key (org.niis.xroad.securityserver.restapi.openapi.model.Key)3 ClientNotFoundException (org.niis.xroad.securityserver.restapi.service.ClientNotFoundException)3 ClientId (ee.ria.xroad.common.identifier.ClientId)2 KeyInfo (ee.ria.xroad.signer.protocol.dto.KeyInfo)2 KeyUsageInfo (ee.ria.xroad.signer.protocol.dto.KeyUsageInfo)2 Token (org.niis.xroad.securityserver.restapi.openapi.model.Token)2 CertificateAuthorityNotFoundException (org.niis.xroad.securityserver.restapi.service.CertificateAuthorityNotFoundException)2 CsrNotFoundException (org.niis.xroad.securityserver.restapi.service.CsrNotFoundException)2 GeneratedCertRequestInfo (ee.ria.xroad.commonui.SignerProxy.GeneratedCertRequestInfo)1 CertificateInfo (ee.ria.xroad.signer.protocol.dto.CertificateInfo)1 TokenInfo (ee.ria.xroad.signer.protocol.dto.TokenInfo)1