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

JWT decoding for both base64url and base64 #12083

Merged
merged 3 commits into from
Jul 31, 2023
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 @@ -34,6 +34,7 @@
private String jwtHeader = "X-JWT-Assertion";
private String consumerDialectUri = "http://wso2.org/claims";
private String signatureAlgorithm = "SHA256withRSA";
private String jwtDecoding = "base64";

Check warning on line 37 in components/apimgt/org.wso2.carbon.apimgt.common.gateway/src/main/java/org/wso2/carbon/apimgt/common/gateway/dto/JWTConfigurationDto.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.common.gateway/src/main/java/org/wso2/carbon/apimgt/common/gateway/dto/JWTConfigurationDto.java#L37

Added line #L37 was not covered by tests
private boolean enableUserClaims;
private String gatewayJWTGeneratorImpl;
private Map<String, TokenIssuerDto> tokenIssuerDtoMap = new HashMap();
Expand All @@ -58,6 +59,7 @@
this.jwtHeader = jwtConfigurationDto.jwtHeader;
this.consumerDialectUri = jwtConfigurationDto.consumerDialectUri;
this.signatureAlgorithm = jwtConfigurationDto.signatureAlgorithm;
this.jwtDecoding = jwtConfigurationDto.jwtDecoding;

Check warning on line 62 in components/apimgt/org.wso2.carbon.apimgt.common.gateway/src/main/java/org/wso2/carbon/apimgt/common/gateway/dto/JWTConfigurationDto.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.common.gateway/src/main/java/org/wso2/carbon/apimgt/common/gateway/dto/JWTConfigurationDto.java#L62

Added line #L62 was not covered by tests
this.enableUserClaims = jwtConfigurationDto.enableUserClaims;
this.gatewayJWTGeneratorImpl = jwtConfigurationDto.gatewayJWTGeneratorImpl;
this.tokenIssuerDtoMap = jwtConfigurationDto.tokenIssuerDtoMap;
Expand Down Expand Up @@ -140,6 +142,14 @@
this.jwtExcludedClaims = jwtClaims;
}

public String getJwtDecoding() {
return jwtDecoding;

Check warning on line 146 in components/apimgt/org.wso2.carbon.apimgt.common.gateway/src/main/java/org/wso2/carbon/apimgt/common/gateway/dto/JWTConfigurationDto.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.common.gateway/src/main/java/org/wso2/carbon/apimgt/common/gateway/dto/JWTConfigurationDto.java#L146

Added line #L146 was not covered by tests
}

public void setJwtDecoding(String jwtDecoding) {
this.jwtDecoding = jwtDecoding;
}

Check warning on line 151 in components/apimgt/org.wso2.carbon.apimgt.common.gateway/src/main/java/org/wso2/carbon/apimgt/common/gateway/dto/JWTConfigurationDto.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.common.gateway/src/main/java/org/wso2/carbon/apimgt/common/gateway/dto/JWTConfigurationDto.java#L150-L151

Added lines #L150 - L151 were not covered by tests

public boolean isEnableUserClaims() {

return enableUserClaims;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,12 @@
if (token != null) {
endUserToken = (String) token;
String[] splitToken = ((String) token).split("\\.");
JSONObject payload = new JSONObject(new String(Base64.getUrlDecoder().decode(splitToken[1])));
JSONObject payload;
if (APIConstants.JwtTokenConstants.DECODING_ALGORITHM_BASE64URL.equals(jwtConfigurationDto.getJwtDecoding())) {
payload = new JSONObject(new String(Base64.getUrlDecoder().decode(splitToken[1])));

Check warning on line 520 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/apikey/ApiKeyAuthenticator.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/apikey/ApiKeyAuthenticator.java#L520

Added line #L520 was not covered by tests
} else {
payload = new JSONObject(new String(Base64.getDecoder().decode(splitToken[1])));

Check warning on line 522 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/apikey/ApiKeyAuthenticator.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/apikey/ApiKeyAuthenticator.java#L522

Added line #L522 was not covered by tests
}
long exp = payload.getLong("exp");
long timestampSkew = OAuthServerConfiguration.getInstance().getTimeStampSkewInSeconds() * 1000;
valid = (exp - System.currentTimeMillis() > timestampSkew);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,12 @@
if (token != null) {
endUserToken = (String) token;
String[] splitToken = ((String) token).split("\\.");
JSONObject payload = new JSONObject(new String(Base64.getUrlDecoder().decode(splitToken[1])));
JSONObject payload;
if (APIConstants.JwtTokenConstants.DECODING_ALGORITHM_BASE64URL.equals(jwtConfigurationDto.getJwtDecoding())) {
payload = new JSONObject(new String(Base64.getUrlDecoder().decode(splitToken[1])));

Check warning on line 295 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/jwt/JWTValidator.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/jwt/JWTValidator.java#L295

Added line #L295 was not covered by tests
} else {
payload = new JSONObject(new String(Base64.getDecoder().decode(splitToken[1])));

Check warning on line 297 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/jwt/JWTValidator.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/jwt/JWTValidator.java#L297

Added line #L297 was not covered by tests
}
long exp = payload.getLong("exp") * 1000L;
long timestampSkew = getTimeStampSkewInSeconds() * 1000;
valid = (exp - System.currentTimeMillis() > timestampSkew);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ public final class APIConstants {
public static final String JWT_DEFAULT_AUDIENCE = "http://org.wso2.apimgt/gateway";
public static final String JWT_CONFIGS = "JWTConfiguration";
public static final String JWT_HEADER = "JWTHeader";
public static final String JWT_DECODING = "JWTDecoding";
public static final String ENABLE_USER_CLAIMS = "EnableUserClaims";
public static final String BINDING_FEDERATED_USER_CLAIMS = "EnableBindingFederatedUserClaims";
public static final String TOKEN_GENERATOR_IMPL = "JWTGeneratorImpl";
Expand Down Expand Up @@ -2100,6 +2101,7 @@ public static class JwtTokenConstants {
public static final String INTERNAL_KEY_TOKEN_TYPE = "InternalKey";
public static final String TOKEN_TYPE = "token_type";
public static final String API_KEY_TOKEN_TYPE = "apiKey";
public static final String DECODING_ALGORITHM_BASE64URL = "base64url";
}

public static final String SIGNATURE_ALGORITHM_RS256 = "RS256";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,11 @@
if (jwtHeaderElement != null) {
jwtConfigurationDto.setJwtHeader(jwtHeaderElement.getText());
}
OMElement jwtDecoding =
omElement.getFirstChildWithName(new QName(APIConstants.JWT_DECODING));

Check warning on line 1607 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIManagerConfiguration.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIManagerConfiguration.java#L1606-L1607

Added lines #L1606 - L1607 were not covered by tests
if (jwtDecoding != null) {
jwtConfigurationDto.setJwtDecoding(jwtDecoding.getText());

Check warning on line 1609 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIManagerConfiguration.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIManagerConfiguration.java#L1609

Added line #L1609 was not covered by tests
}
OMElement jwtUserClaimsElement =
omElement.getFirstChildWithName(new QName(APIConstants.ENABLE_USER_CLAIMS));
if (jwtUserClaimsElement != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@
<!-- This parameter specifies which implementation should be used for generating the Token. For URL safe JWT
Token generation the implementation is provided in URLSafeJWTGenerator -->
<!--<JWTGeneratorImpl>org.wso2.carbon.apimgt.keymgt.token.URLSafeJWTGenerator</JWTGeneratorImpl>-->

<!-- Set the JWT decoding method. Options are "base64url" or "base64". The default value is "base64". -->
{% if apim.jwt.decoding %}
<JWTDecoding>{{apim.jwt.decoding}}</JWTDecoding>
{% elif apim.jwt.encoding %}
<JWTDecoding>{{apim.jwt.encoding}}</JWTDecoding>
{% endif %}

{% if apim.jwt.enable_tenant_based_signing is defined %}
<EnableTenantBasedSigning>{{apim.jwt.enable_tenant_based_signing}}</EnableTenantBasedSigning>
{% endif %}
Expand Down
Loading