Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented new Oauth2Test for JWTs #1246

Merged
merged 8 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@
limitations under the License. */
package com.predic8.membrane.core.interceptor.authentication.session;

import com.google.api.client.util.Base64;
import com.predic8.membrane.annot.MCAttribute;
import com.predic8.membrane.annot.MCChildElement;
import com.predic8.membrane.annot.MCElement;
import com.predic8.membrane.annot.MCOtherAttributes;
import com.predic8.membrane.core.Router;
import org.apache.commons.codec.digest.Crypt;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.*;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -72,11 +67,12 @@ public Map<String, String> verify(Map<String, String> postData) {
String salt = userHashSplit[2];
try {
pw = createPasswdCompatibleHash(algo,postDataPassword,salt);
} catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}else
} else {
pw = postDataPassword;
}
String pw2;
pw2 = userAttributes.getPassword();
if (pw2 == null || !pw2.equals(pw))
Expand All @@ -85,18 +81,18 @@ public Map<String, String> verify(Map<String, String> postData) {
}

private boolean isHashedPassword(String postDataPassword) {
// TODO do a better check here
String[] split = postDataPassword.split(Pattern.quote("$"));
if(split.length != 4)
if (split.length != 4)
return false;
if(!split[0].isEmpty())
if (!split[0].isEmpty())
return false;
if(split[3].length() < 20)
if (split[3].length() < 20)
return false;
return true;
// Check if second part is a valid hex
return Pattern.matches("[a-fA-F0-9]{40}", split[2]);
}

private String createPasswdCompatibleHash(String algo, String password, String salt) throws UnsupportedEncodingException, NoSuchAlgorithmException {
private String createPasswdCompatibleHash(String algo, String password, String salt) {
return Crypt.crypt(password, "$" + algo + "$" + salt);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@MCElement(name="jwks")
public class Jwks {

List<Jwk> jwks;
List<Jwk> jwks = new ArrayList<>();
String jwksUris;
AuthorizationService authorizationService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@
import com.predic8.membrane.core.interceptor.Outcome;
import com.predic8.membrane.core.interceptor.authentication.session.SessionManager;
import com.predic8.membrane.core.interceptor.oauth2.OAuth2AuthorizationServerInterceptor;
import com.predic8.membrane.core.interceptor.oauth2.ParamNames;
import com.predic8.membrane.core.interceptor.oauth2.request.AuthWithSessionRequest;
import com.predic8.membrane.core.interceptor.oauth2.request.AuthWithoutSessionRequest;
import com.predic8.membrane.core.util.URLParamUtil;

import static com.predic8.membrane.core.util.URLParamUtil.DuplicateKeyOrInvalidFormStrategy.ERROR;

public class AuthEndpointProcessor extends EndpointProcessor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@

import com.predic8.membrane.core.beautifier.JSONBeautifier;
import com.predic8.membrane.core.exchange.Exchange;
import com.predic8.membrane.core.http.MimeType;
import com.predic8.membrane.core.http.Response;
import com.predic8.membrane.core.interceptor.Outcome;
import com.predic8.membrane.core.interceptor.oauth2.OAuth2AuthorizationServerInterceptor;

import static com.predic8.membrane.core.http.MimeType.APPLICATION_JSON_UTF8;

public class CertsEndpointProcessor extends EndpointProcessor {

private JSONBeautifier jsonBeautifier = new JSONBeautifier();
private final JSONBeautifier jsonBeautifier = new JSONBeautifier();

public CertsEndpointProcessor(OAuth2AuthorizationServerInterceptor authServer) {
super(authServer);
Expand All @@ -35,7 +36,14 @@ public boolean isResponsible(Exchange exc) {

@Override
public Outcome process(Exchange exc) throws Exception {
exc.setResponse(Response.ok().contentType(MimeType.APPLICATION_JSON_UTF8).body(jsonBeautifier.beautify(authServer.getJwtGenerator().getJwk())).build());
String accessTokenJWKIfAvailable = authServer.getTokenGenerator().getJwkIfAvailable();
String idTokenJWK = authServer.getJwtGenerator().getJwk();

String jwks = "{\"keys\": [ " + idTokenJWK +
(accessTokenJWKIfAvailable != null ? "," + accessTokenJWKIfAvailable : "") +
"]}";

exc.setResponse(Response.ok().contentType(APPLICATION_JSON_UTF8).body(jsonBeautifier.beautify(jwks)).build());
return Outcome.RETURN;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

package com.predic8.membrane.core.interceptor.oauth2.request;

import com.fasterxml.jackson.core.JsonGenerator;
import com.predic8.membrane.core.exchange.Exchange;
import com.predic8.membrane.core.http.MimeType;
import com.predic8.membrane.core.http.Response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.predic8.membrane.core.interceptor.oauth2.tokengenerators.JwtGenerator;

import java.io.IOException;
import java.lang.reflect.Executable;
import java.util.ArrayList;

import org.jose4j.lang.JoseException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
import com.predic8.membrane.core.http.MimeType;
import com.predic8.membrane.core.http.Response;
import com.predic8.membrane.core.interceptor.authentication.session.SessionManager;
import com.predic8.membrane.core.interceptor.oauth2.ClaimRenamer;
import com.predic8.membrane.core.interceptor.oauth2.Client;
import com.predic8.membrane.core.interceptor.oauth2.*;
import com.predic8.membrane.core.interceptor.oauth2.OAuth2AuthorizationServerInterceptor;
import com.predic8.membrane.core.interceptor.oauth2.OAuth2Util;
import com.predic8.membrane.core.interceptor.oauth2.ParamNames;
import com.predic8.membrane.core.interceptor.oauth2.parameter.ClaimsParameter;
import com.predic8.membrane.core.interceptor.oauth2.request.NoResponse;
import com.predic8.membrane.core.interceptor.oauth2.tokengenerators.JwtGenerator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
import com.predic8.membrane.core.http.MimeType;
import com.predic8.membrane.core.http.Response;
import com.predic8.membrane.core.interceptor.authentication.session.SessionManager;
import com.predic8.membrane.core.interceptor.oauth2.ClaimRenamer;
import com.predic8.membrane.core.interceptor.oauth2.Client;
import com.predic8.membrane.core.interceptor.oauth2.*;
import com.predic8.membrane.core.interceptor.oauth2.OAuth2AuthorizationServerInterceptor;
import com.predic8.membrane.core.interceptor.oauth2.OAuth2Util;
import com.predic8.membrane.core.interceptor.oauth2.ParamNames;
import com.predic8.membrane.core.interceptor.oauth2.parameter.ClaimsParameter;
import com.predic8.membrane.core.interceptor.oauth2.request.NoResponse;
import com.predic8.membrane.core.interceptor.oauth2.tokengenerators.JwtGenerator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public void init(Router router) throws Exception {
rsaJsonWebKey = generateKey();
if (warningGeneratedKey)
LOG.warn("bearerJwtToken uses a generated key ('{}'). Sessions of this instance will not be compatible " +
"with sessions of other (e.g. restarted)instances. To solve this, write the JWK into a file and " +
"reference it using <bearerJwtToken><jwk location=\"...\">.",
"with sessions of other (e.g. restarted) instances. To solve this, write the JWK into a file and " +
"reference it using <bearerJwtToken><jwk location=\"...\">.",
rsaJsonWebKey.toJson(JsonWebKey.OutputControlLevel.INCLUDE_PRIVATE));
} else {
rsaJsonWebKey = new RsaJsonWebKey(JsonUtil.parseJson(jwk.get(router.getResolverMap(), router.getBaseLocation())));
Expand Down Expand Up @@ -86,6 +86,7 @@ public String getToken(String username, String clientId, String clientSecret) {
jws.setPayload(claims.toJson());
jws.setKey(rsaJsonWebKey.getRsaPrivateKey());
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
jws.setKeyIdHeaderValue(rsaJsonWebKey.getKeyId());
try {
return jws.getCompactSerialization();
} catch (JoseException e) {
Expand Down Expand Up @@ -165,4 +166,9 @@ public boolean isWarningGeneratedKey() {
public void setWarningGeneratedKey(boolean warningGeneratedKey) {
this.warningGeneratedKey = warningGeneratedKey;
}
}

@Override
public String getJwkIfAvailable() {
return rsaJsonWebKey.toJson();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,9 @@ public boolean supportsRevocation() {
public long getExpiration() {
return 0;
}
}

@Override
public String getJwkIfAvailable() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public class JwtGenerator {

public String getJwk() {
return "{\"keys\": [ " + rsaJsonWebKey.toJson() + "]}";
return rsaJsonWebKey.toJson();
}

public static class Claim{
Expand Down Expand Up @@ -193,4 +193,4 @@ private static JwtClaims processIdTokenToClaims(String idToken, String iss, Stri

return jwtConsumer.processToClaims(idToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,10 @@ public interface TokenGenerator {
* @return token expiration in seconds, or 0 if there is no expiration
*/
long getExpiration();
}

/**
* @return the JWK representation of the key used to sign the tokens or null, if there is no such key (e.g. because
* the tokens are randomly generated strings)
*/
String getJwkIfAvailable();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.predic8.membrane.core.http.*;
import com.predic8.membrane.core.interceptor.authentication.session.*;
import com.predic8.membrane.core.interceptor.oauth2.authorizationservice.*;
import com.predic8.membrane.core.interceptor.oauth2.tokengenerators.BearerJwtTokenGenerator;
import com.predic8.membrane.core.util.*;
import com.predic8.membrane.core.util.functionalInterfaces.*;
import org.junit.jupiter.api.*;
Expand Down
Loading
Loading