Search in sources :

Example 16 with CertificateRepresentation

use of org.keycloak.representations.idm.CertificateRepresentation in project keycloak by keycloak.

the class ClientAttributeCertificateResource method uploadJks.

/**
 * Upload certificate and eventually private key
 *
 * @param input
 * @return
 * @throws IOException
 */
@POST
@Path("upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public CertificateRepresentation uploadJks(MultipartFormDataInput input) throws IOException {
    auth.clients().requireConfigure(client);
    try {
        CertificateRepresentation info = getCertFromRequest(input);
        CertificateInfoHelper.updateClientModelCertificateInfo(client, info, attributePrefix);
        adminEvent.operation(OperationType.ACTION).resourcePath(session.getContext().getUri()).representation(info).success();
        return info;
    } catch (IllegalStateException ise) {
        throw new ErrorResponseException("certificate-not-found", "Certificate or key with given alias not found in the keystore", Response.Status.BAD_REQUEST);
    }
}
Also used : CertificateRepresentation(org.keycloak.representations.idm.CertificateRepresentation) ErrorResponseException(org.keycloak.services.ErrorResponseException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 17 with CertificateRepresentation

use of org.keycloak.representations.idm.CertificateRepresentation in project keycloak by keycloak.

the class ClientAttributeCertificateResource method getKeystore.

/**
 * Get a keystore file for the client, containing private key and public certificate
 *
 * @param config Keystore configuration as JSON
 * @return
 */
@POST
@NoCache
@Path("/download")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Consumes(MediaType.APPLICATION_JSON)
public byte[] getKeystore(final KeyStoreConfig config) {
    auth.clients().requireView(client);
    if (config.getFormat() != null && !config.getFormat().equals("JKS") && !config.getFormat().equals("PKCS12")) {
        throw new NotAcceptableException("Only support jks or pkcs12 format.");
    }
    CertificateRepresentation info = CertificateInfoHelper.getCertificateFromClient(client, attributePrefix);
    String privatePem = info.getPrivateKey();
    String certPem = info.getCertificate();
    if (privatePem == null && certPem == null) {
        throw new NotFoundException("keypair not generated for client");
    }
    if (privatePem != null && config.getKeyPassword() == null) {
        throw new ErrorResponseException("password-missing", "Need to specify a key password for jks download", Response.Status.BAD_REQUEST);
    }
    if (config.getStorePassword() == null) {
        throw new ErrorResponseException("password-missing", "Need to specify a store password for jks download", Response.Status.BAD_REQUEST);
    }
    byte[] rtn = getKeystore(config, privatePem, certPem);
    return rtn;
}
Also used : NotAcceptableException(javax.ws.rs.NotAcceptableException) CertificateRepresentation(org.keycloak.representations.idm.CertificateRepresentation) NotFoundException(javax.ws.rs.NotFoundException) ErrorResponseException(org.keycloak.services.ErrorResponseException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) NoCache(org.jboss.resteasy.annotations.cache.NoCache)

Aggregations

CertificateRepresentation (org.keycloak.representations.idm.CertificateRepresentation)17 Produces (javax.ws.rs.Produces)6 POST (javax.ws.rs.POST)5 Path (javax.ws.rs.Path)5 Test (org.junit.Test)5 ErrorResponseException (org.keycloak.services.ErrorResponseException)5 X509Certificate (java.security.cert.X509Certificate)4 Consumes (javax.ws.rs.Consumes)4 NoCache (org.jboss.resteasy.annotations.cache.NoCache)4 ClientAttributeCertificateResource (org.keycloak.admin.client.resource.ClientAttributeCertificateResource)4 KeyStore (java.security.KeyStore)3 NotAcceptableException (javax.ws.rs.NotAcceptableException)3 JSONWebKeySet (org.keycloak.jose.jwk.JSONWebKeySet)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 Key (java.security.Key)2 PublicKey (java.security.PublicKey)2 Certificate (java.security.cert.Certificate)2 NotFoundException (javax.ws.rs.NotFoundException)2 JWK (org.keycloak.jose.jwk.JWK)2