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);
}
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);
}
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);
}
}
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);
}
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);
}
Aggregations