182 CertificateRepresentation info =
new CertificateRepresentation();
183 Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
184 List<InputPart> keystoreFormatPart = uploadForm.get(
"keystoreFormat");
185 if (keystoreFormatPart == null)
throw new BadRequestException();
186 String keystoreFormat = keystoreFormatPart.get(0).getBodyAsString();
187 List<InputPart> inputParts = uploadForm.get(
"file");
189 String pem = StreamUtil.readString(inputParts.get(0).getBody(InputStream.class, null));
191 pem = PemUtils.removeBeginEnd(pem);
194 KeycloakModelUtils.getCertificate(pem);
196 info.setCertificate(pem);
199 String pem = StreamUtil.readString(inputParts.get(0).getBody(InputStream.class, null));
202 KeycloakModelUtils.getPublicKey(pem);
204 info.setPublicKey(pem);
207 InputStream stream = inputParts.get(0).getBody(InputStream.class, null);
208 JSONWebKeySet keySet = JsonSerialization.readValue(stream, JSONWebKeySet.class);
209 JWK publicKeyJwk = JWKSUtils.getKeyForUse(keySet, JWK.Use.SIG);
210 if (publicKeyJwk == null) {
211 throw new IllegalStateException(
"Certificate not found for use sig");
213 PublicKey publicKey = JWKParser.create(publicKeyJwk).toPublicKey();
214 String publicKeyPem = KeycloakModelUtils.getPemFromKey(publicKey);
215 info.setPublicKey(publicKeyPem);
216 info.setKid(publicKeyJwk.getKeyId());
222 String keyAlias = uploadForm.get(
"keyAlias").get(0).getBodyAsString();
223 List<InputPart> keyPasswordPart = uploadForm.get(
"keyPassword");
224 char[] keyPassword = keyPasswordPart != null ? keyPasswordPart.get(0).getBodyAsString().toCharArray() : null;
226 List<InputPart> storePasswordPart = uploadForm.get(
"storePassword");
227 char[] storePassword = storePasswordPart != null ? storePasswordPart.get(0).getBodyAsString().toCharArray() : null;
228 PrivateKey privateKey = null;
229 X509Certificate certificate = null;
231 KeyStore keyStore = null;
232 if (keystoreFormat.equals(
"JKS")) keyStore = KeyStore.getInstance(
"JKS");
233 else keyStore = KeyStore.getInstance(keystoreFormat,
"BC");
234 keyStore.load(inputParts.get(0).getBody(InputStream.class, null), storePassword);
236 privateKey = (PrivateKey)keyStore.getKey(keyAlias, keyPassword);
237 }
catch (Exception e) {
240 certificate = (X509Certificate)keyStore.getCertificate(keyAlias);
241 }
catch (Exception e) {
242 throw new RuntimeException(e);
245 if (privateKey != null) {
246 String privateKeyPem = KeycloakModelUtils.getPemFromKey(privateKey);
247 info.setPrivateKey(privateKeyPem);
250 if (certificate != null) {
251 String certPem = KeycloakModelUtils.getPemFromCertificate(certificate);
252 info.setCertificate(certPem);
AdminPermissionEvaluator auth
Definition: ClientAttributeCertificateResource.java:78
static final String CERTIFICATE_PEM
Definition: ClientAttributeCertificateResource.java:73
ClientModel client
Definition: ClientAttributeCertificateResource.java:79
static final String PUBLIC_KEY_PEM
Definition: ClientAttributeCertificateResource.java:74
static final String JSON_WEB_KEY_SET
Definition: ClientAttributeCertificateResource.java:75
ClientPermissionEvaluator clients()