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

feat: replace jwt token with reference token to access config-api (admin ui plugin) #6562 #6587

Merged
merged 4 commits into from Nov 18, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -9,6 +9,7 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public class OIDCClientSettings {

private String introspectionEndpoint;
private String tokenEndpoint;
private String redirectUri;
private String postLogoutUri;
Expand All @@ -30,12 +31,13 @@ public OIDCClientSettings(String opHost, String clientId, String clientSecret) {
this.clientSecret = clientSecret;
}

public OIDCClientSettings(String opHost, String clientId, String clientSecret, String tokenEndpoint) {
public OIDCClientSettings(String opHost, String clientId, String clientSecret, String tokenEndpoint, String introspectionEndpoint) {

this.opHost = opHost;
this.clientId = clientId;
this.clientSecret = clientSecret;
this.tokenEndpoint = tokenEndpoint;
this.introspectionEndpoint = introspectionEndpoint;
}

@JsonInclude(JsonInclude.Include.NON_EMPTY)
Expand Down Expand Up @@ -97,18 +99,27 @@ public void setFrontchannelLogoutUri(String frontchannelLogoutUri) {
this.frontchannelLogoutUri = frontchannelLogoutUri;
}

public String getIntrospectionEndpoint() {
return introspectionEndpoint;
}

public void setIntrospectionEndpoint(String introspectionEndpoint) {
this.introspectionEndpoint = introspectionEndpoint;
}

@Override
public String toString() {
return "OIDCClientSettings{" +
"opHost='" + opHost + '\'' +
", clientId='" + clientId + '\'' +
", clientSecret='" + clientSecret + '\'' +
"introspectionEndpoint='" + introspectionEndpoint + '\'' +
", tokenEndpoint='" + tokenEndpoint + '\'' +
", redirectUri='" + redirectUri + '\'' +
", postLogoutUri='" + postLogoutUri + '\'' +
", frontchannelLogoutUri='" + frontchannelLogoutUri + '\'' +
", scopes=" + scopes +
", acrValues=" + acrValues +
", opHost='" + opHost + '\'' +
", clientId='" + clientId + '\'' +
", clientSecret='" + clientSecret + '\'' +
'}';
}
}
Expand Up @@ -186,4 +186,24 @@ else if (jwtClaims.getClaim(key) instanceof JSONArray) {
});
return claims;
}

public Optional<Map<String, Object>> introspectToken(String accessToken, String introspectionEndpoint) {
log.info("Token introspection from auth-server.");
Invocation.Builder request = ClientFactory.instance().getClientBuilder(introspectionEndpoint);
request.header("Authorization", "Bearer " + accessToken);

MultivaluedMap<String, String> body = new MultivaluedHashMap<>();
body.putSingle("token", accessToken);

Response response = request.post(Entity.form(body));

log.info("Introspection response status code: {}", response.getStatus());

if (response.getStatus() == 200) {
Optional<Map<String, Object>> entity = Optional.of(response.readEntity(Map.class));
log.info("Introspection response entity: {}", entity.get().toString());
return entity;
}
return Optional.empty();
}
}
Expand Up @@ -5,7 +5,6 @@
import io.jans.as.client.TokenRequest;
import io.jans.as.common.service.common.EncryptionService;
import io.jans.as.model.common.GrantType;
import io.jans.as.model.jwt.Jwt;
import io.jans.ca.plugin.adminui.model.auth.ApiTokenRequest;
import io.jans.ca.plugin.adminui.model.auth.TokenResponse;
import io.jans.ca.plugin.adminui.model.config.AUIConfiguration;
Expand All @@ -21,10 +20,7 @@
import org.slf4j.Logger;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

@Singleton
public class OAuth2Service extends BaseService {
Expand Down Expand Up @@ -64,17 +60,27 @@ public TokenResponse getApiProtectionToken(ApiTokenRequest apiTokenRequest, Stri
tokenResponse = getToken(tokenRequest, auiConfiguration.getAuiBackendApiServerTokenEndpoint(), apiTokenRequest.getUjwt(), apiTokenRequest.getPermissionTag());
}

final Jwt tokenJwt = Jwt.parse(tokenResponse.getAccessToken());
Map<String, Object> claims = getClaims(tokenJwt);
Optional<Map<String, Object>> introspectionResponse = introspectToken(tokenResponse.getAccessToken(), auiConfiguration.getAuiBackendApiServerIntrospectionEndpoint());


TokenResponse tokenResp = new TokenResponse();
tokenResp.setAccessToken(tokenResponse.getAccessToken());
tokenResp.setIdToken(tokenResponse.getIdToken());
tokenResp.setRefreshToken(tokenResponse.getRefreshToken());

if (!introspectionResponse.isPresent()) {
return tokenResp;
}
final String SCOPE = "scope";
if (claims.get(SCOPE) instanceof List) {
tokenResp.setScopes((List) claims.get(SCOPE));
Map<String, Object> claims = introspectionResponse.get();
if (claims.get(SCOPE) != null) {
if (claims.get(SCOPE) instanceof List) {
tokenResp.setScopes((List) claims.get(SCOPE));
}
if (claims.get(SCOPE) instanceof String) {
tokenResp.setScopes(Arrays.asList(((String) claims.get(SCOPE)).split(" ")));
}
}

if (claims.get("iat") != null) {
tokenResp.setIat(Long.valueOf(claims.get("iat").toString()));
}
Expand Down
Expand Up @@ -114,6 +114,7 @@ private AUIConfiguration addPropertiesToAUIConfiguration(String appType, AdminCo
auiConfig.setAuiBackendApiServerClientSecret(appConf.getMainSettings().getOidcConfig().getAuiBackendApiClient().getClientSecret());
auiConfig.setAuiBackendApiServerScope(StringUtils.join(appConf.getMainSettings().getOidcConfig().getAuiBackendApiClient().getScopes(), "+"));
auiConfig.setAuiBackendApiServerTokenEndpoint(appConf.getMainSettings().getOidcConfig().getAuiBackendApiClient().getTokenEndpoint());
auiConfig.setAuiBackendApiServerIntrospectionEndpoint(appConf.getMainSettings().getOidcConfig().getAuiBackendApiClient().getIntrospectionEndpoint());

return auiConfig;
}
Expand Down