Skip to content

Commit

Permalink
test(jans-auth-server): added test to RedirectUri for code-coverage, …
Browse files Browse the repository at this point in the history
…refactor (#751)
  • Loading branch information
jmunozherbas authored Feb 10, 2022
1 parent 737dcf9 commit a4a8784
Show file tree
Hide file tree
Showing 5 changed files with 469 additions and 74 deletions.
8 changes: 8 additions & 0 deletions jans-auth-server/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,13 @@
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-testng</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
import io.jans.as.model.crypto.encryption.BlockEncryptionAlgorithm;
import io.jans.as.model.crypto.encryption.KeyEncryptionAlgorithm;
import io.jans.as.model.crypto.signature.SignatureAlgorithm;
import io.jans.as.model.exception.CryptoProviderException;
import io.jans.as.model.exception.InvalidJweException;
import io.jans.as.model.exception.InvalidJwtException;
import io.jans.as.model.jwe.Jwe;
import io.jans.as.model.jwe.JweEncrypter;
import io.jans.as.model.jwe.JweEncrypterImpl;
import io.jans.as.model.jwt.Jwt;
import io.jans.as.model.jwt.JwtType;
Expand All @@ -29,17 +30,9 @@
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.PublicKey;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import static io.jans.as.model.authorize.AuthorizeResponseParam.AUD;
import static io.jans.as.model.authorize.AuthorizeResponseParam.EXP;
import static io.jans.as.model.authorize.AuthorizeResponseParam.EXPIRES_IN;
import static io.jans.as.model.authorize.AuthorizeResponseParam.ISS;
import static io.jans.as.model.authorize.AuthorizeResponseParam.RESPONSE;
import java.util.*;

import static io.jans.as.model.authorize.AuthorizeResponseParam.*;

/**
* @author Javier Rojas Blum
Expand Down Expand Up @@ -72,7 +65,7 @@ public RedirectUri(String baseRedirectUri) {
this.baseRedirectUri = baseRedirectUri;
this.responseMode = ResponseMode.QUERY;

responseParameters = new HashMap<String, String>();
this.responseParameters = new HashMap<>();
}

public RedirectUri(String baseRedirectUri, List<ResponseType> responseTypes, ResponseMode responseMode) {
Expand Down Expand Up @@ -108,6 +101,10 @@ public String getResponseParameter(@NotNull String key) {
return responseParameters.get(key);
}

public int getResponseParamentersSize() {
return responseParameters != null ? responseParameters.size() : 0;
}

public String getIssuer() {
return issuer;
}
Expand Down Expand Up @@ -267,16 +264,14 @@ public String getQueryString() {
}
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}

return sb.toString();
}

private String getJarmResponse() throws Exception {
private String getJarmResponse() throws InvalidJweException, InvalidJwtException, CryptoProviderException {
if (keyEncryptionAlgorithm != null && blockEncryptionAlgorithm != null) {
if (signatureAlgorithm != null) {
String jws = getJwsResponse(true);
Expand All @@ -293,7 +288,7 @@ private String getJarmResponse() throws Exception {
}
}

private String getJwsResponse(boolean nested) throws Exception {
private String getJwsResponse(boolean nested) throws InvalidJwtException, CryptoProviderException {
Jwt jwt = new Jwt();

// Header
Expand Down Expand Up @@ -325,11 +320,10 @@ private String getJwsResponse(boolean nested) throws Exception {
// Signature
String signature = cryptoProvider.sign(jwt.getSigningInput(), jwt.getHeader().getKeyId(), nested ? nestedSharedSecret : sharedSecret, signatureAlgorithm);
jwt.setEncodedSignature(signature);

return jwt.toString();
}

private String getJweResponse(String nestedJws) throws Exception {
private String getJweResponse(String nestedJws) throws InvalidJweException, InvalidJwtException, CryptoProviderException {
Jwe jwe = new Jwe();

// Header
Expand All @@ -356,25 +350,20 @@ private String getJweResponse(String nestedJws) throws Exception {
jwe.setSignedJWTPayload(jwt);
}

// Encryption
//Encryption
if (keyEncryptionAlgorithm == KeyEncryptionAlgorithm.RSA_OAEP
|| keyEncryptionAlgorithm == KeyEncryptionAlgorithm.RSA1_5) {
PublicKey publicKey = cryptoProvider.getPublicKey(keyId, jsonWebKeys, null);

if (publicKey != null) {
JweEncrypter jweEncrypter = new JweEncrypterImpl(keyEncryptionAlgorithm, blockEncryptionAlgorithm, publicKey);
jwe = jweEncrypter.encrypt(jwe);
} else {
if (publicKey == null) {
throw new InvalidJweException("The public key is not valid");
}

JweEncrypterImpl jweEncrypter = new JweEncrypterImpl(keyEncryptionAlgorithm, blockEncryptionAlgorithm, publicKey);
jwe = jweEncrypter.encrypt(jwe);
} else if (keyEncryptionAlgorithm == KeyEncryptionAlgorithm.A128KW
|| keyEncryptionAlgorithm == KeyEncryptionAlgorithm.A256KW) {
try {
JweEncrypter jweEncrypter = new JweEncrypterImpl(keyEncryptionAlgorithm, blockEncryptionAlgorithm, sharedSymmetricKey);
jwe = jweEncrypter.encrypt(jwe);
} catch (Exception e) {
throw new InvalidJweException(e);
}
JweEncrypterImpl jweEncrypter = new JweEncrypterImpl(keyEncryptionAlgorithm, blockEncryptionAlgorithm, sharedSymmetricKey);
jwe = jweEncrypter.encrypt(jwe);
}

return jwe.toString();
Expand All @@ -399,52 +388,59 @@ private void appendFragmentSymbol(StringBuilder sb) {
@Override
public String toString() {
StringBuilder sb = new StringBuilder(baseRedirectUri);
if (responseParameters.isEmpty()) {
return sb.toString();
}
if (responseMode == ResponseMode.FORM_POST) {
sb = new StringBuilder();
sb.append("<html>");
sb.append("<head><title>Submit This Form</title></head>");
sb.append("<body onload=\"javascript:document.forms[0].submit()\">");
sb.append("<form method=\"post\" action=\"").append(baseRedirectUri).append("\">");
for (Map.Entry<String, String> entry : responseParameters.entrySet()) {
String entryValue = StringEscapeUtils.escapeHtml(entry.getValue());
sb.append("<input type=\"hidden\" name=\"").append(entry.getKey()).append("\" value=\"").append(entryValue).append("\"/>");
}
sb.append("</form>");
sb.append("</body>");
sb.append("</html>");
} else if (responseMode == ResponseMode.FORM_POST_JWT) {
sb = new StringBuilder();
sb.append("<html>");
sb.append("<head><title>Submit This Form</title></head>");
sb.append("<body onload=\"javascript:document.forms[0].submit()\">");
sb.append("<form method=\"post\" action=\"").append(baseRedirectUri).append("\">");
sb.append("<input type=\"hidden\" name=\"response\"").append(" value=\"").append(getQueryString()).append("\"/>");
sb.append("</form>");
sb.append("</body>");
sb.append("</html>");
} else if (responseMode == ResponseMode.QUERY || responseMode == ResponseMode.QUERY_JWT) {
appendQuerySymbol(sb);
sb.append(getQueryString());
} else if (responseMode == ResponseMode.FRAGMENT || responseMode == ResponseMode.FRAGMENT_JWT) {
appendFragmentSymbol(sb);
sb.append(getQueryString());
} else if (responseTypes != null && responseMode == ResponseMode.JWT) {
if (responseTypes.contains(ResponseType.TOKEN)) {
appendFragmentSymbol(sb);
} else if (responseTypes.contains(ResponseType.CODE)) {
appendQuerySymbol(sb);
}
sb.append(getQueryString());
} else {
appendDefaultToString(sb);
}
return sb.toString();
}

if (responseParameters.size() > 0) {
if (responseMode == ResponseMode.FORM_POST) {
sb = new StringBuilder();
sb.append("<html>");
sb.append("<head><title>Submit This Form</title></head>");
sb.append("<body onload=\"javascript:document.forms[0].submit()\">");
sb.append("<form method=\"post\" action=\"").append(baseRedirectUri).append("\">");
for (Map.Entry<String, String> entry : responseParameters.entrySet()) {
String entryValue = StringEscapeUtils.escapeHtml(entry.getValue());
sb.append("<input type=\"hidden\" name=\"").append(entry.getKey()).append("\" value=\"").append(entryValue).append("\"/>");
}
sb.append("</form>");
sb.append("</body>");
sb.append("</html>");
} else if (responseMode == ResponseMode.FORM_POST_JWT) {
sb = new StringBuilder();
sb.append("<html>");
sb.append("<head><title>Submit This Form</title></head>");
sb.append("<body onload=\"javascript:document.forms[0].submit()\">");
sb.append("<form method=\"post\" action=\"").append(baseRedirectUri).append("\">");
sb.append("<input type=\"hidden\" name=\"response\"").append(" value=\"").append(getQueryString()).append("\"/>");
sb.append("</form>");
sb.append("</body>");
sb.append("</html>");
private void appendDefaultToString(StringBuilder sb) {
if (responseMode == null) {
if (responseTypes != null && (responseTypes.contains(ResponseType.TOKEN) || responseTypes.contains(ResponseType.ID_TOKEN))) {
appendFragmentSymbol(sb);
} else {
if (responseMode != null) {
if (responseMode == ResponseMode.QUERY || responseMode == ResponseMode.QUERY_JWT) {
appendQuerySymbol(sb);
} else if (responseMode == ResponseMode.FRAGMENT || responseMode == ResponseMode.FRAGMENT_JWT) {
appendFragmentSymbol(sb);
} else if (responseTypes != null && responseMode == ResponseMode.JWT) {
if (responseTypes.contains(ResponseType.TOKEN)) {
appendFragmentSymbol(sb);
} else if (responseTypes.contains(ResponseType.CODE)) {
appendQuerySymbol(sb);
}
}
} else if (responseTypes != null && (responseTypes.contains(ResponseType.TOKEN) || responseTypes.contains(ResponseType.ID_TOKEN))) {
appendFragmentSymbol(sb);
} else {
appendQuerySymbol(sb);
}
sb.append(getQueryString());
appendQuerySymbol(sb);
}
}
return sb.toString();
sb.append(getQueryString());
}
}
Loading

0 comments on commit a4a8784

Please sign in to comment.