43 X509ClientCertificateLookup provider = context.getSession().getProvider(X509ClientCertificateLookup.class);
44 if (provider == null) {
45 logger.errorv(
"\"{0}\" Spi is not available, did you forget to update the configuration?",
46 X509ClientCertificateLookup.class);
50 X509Certificate[] certs = null;
51 ClientModel client = null;
53 certs = provider.getCertificateChain(context.getHttpRequest());
54 String client_id = null;
55 MediaType mediaType = context.getHttpRequest().getHttpHeaders().getMediaType();
56 boolean hasFormData = mediaType != null && mediaType.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
58 MultivaluedMap<String, String> formData = hasFormData ? context.getHttpRequest().getDecodedFormParameters() : null;
59 MultivaluedMap<String, String> queryParams = context.getHttpRequest().getUri().getQueryParameters();
61 if (formData != null) {
62 client_id = formData.getFirst(OAuth2Constants.CLIENT_ID);
65 if (client_id == null && queryParams != null) {
66 client_id = queryParams.getFirst(OAuth2Constants.CLIENT_ID);
69 if (client_id == null) {
70 client_id = context.getSession().getAttribute(
"client_id", String.class);
73 if (client_id == null) {
74 Response challengeResponse = ClientAuthUtil.errorResponse(Response.Status.BAD_REQUEST.getStatusCode(),
"invalid_client",
"Missing client_id parameter");
75 context.challenge(challengeResponse);
79 client = context.getRealm().getClientByClientId(client_id);
81 context.failure(AuthenticationFlowError.CLIENT_NOT_FOUND, null);
84 context.getEvent().client(client_id);
85 context.setClient(client);
87 if (!client.isEnabled()) {
88 context.failure(AuthenticationFlowError.CLIENT_DISABLED, null);
91 }
catch (GeneralSecurityException e) {
92 logger.errorf(
"[X509ClientCertificateAuthenticator:authenticate] Exception: %s", e.getMessage());
97 if (certs == null || certs.length == 0) {
100 logger.debug(
"[X509ClientCertificateAuthenticator:authenticate] x509 client certificate is not available for mutual SSL.");
106 if (subjectDNRegexp == null || subjectDNRegexp.length() == 0) {
107 logger.errorf(
"[X509ClientCertificateAuthenticator:authenticate] " +
ATTR_SUBJECT_DN +
" is null or empty");
111 Pattern subjectDNPattern = Pattern.compile(subjectDNRegexp);
113 Optional<String> matchedCertificate = Arrays.stream(certs)
114 .map(certificate -> certificate.getSubjectDN().getName())
115 .filter(subjectdn -> subjectDNPattern.matcher(subjectdn).matches())
118 if (!matchedCertificate.isPresent()) {
120 if (
logger.isDebugEnabled()) {
121 logger.debug(
"[X509ClientCertificateAuthenticator:authenticate] Couldn't match any certificate for pattern " + subjectDNRegexp);
122 logger.debug(
"[X509ClientCertificateAuthenticator:authenticate] Available SubjectDNs: " +
124 .map(cert -> cert.getSubjectDN().getName())
125 .collect(Collectors.toList()));
130 logger.debug(
"[X509ClientCertificateAuthenticator:authenticate] Matched " + matchedCertificate.get() +
" certificate.");
static final String ATTR_SUBJECT_DN
Definition: X509ClientAuthenticator.java:31
static ServicesLogger logger
Definition: X509ClientAuthenticator.java:33