Skip to content
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 @@ -47,7 +47,8 @@ public final class TokenSecurityRealm implements SecurityRealm {
private final String principalClaimName;
/** A function that maps the set of token claims to a Principal. */
private final Function<Attributes, Principal> claimToPrincipal;

/** A function that transofrms Principal **/
private final Function<Principal, Principal> principalTransformer;
/**
* Returns a {@link Builder} instance that can be used to configure and create a {@link TokenSecurityRealm}.
*
Expand All @@ -72,6 +73,8 @@ public static Builder builder() {
this.claimToPrincipal = configuration.claimToPrincipal;
}

this.principalTransformer = configuration.principalTransformer;

this.strategy = Assert.checkNotNullParam("tokenValidationStrategy", configuration.strategy);
}

Expand Down Expand Up @@ -134,12 +137,17 @@ final class TokenRealmIdentity implements RealmIdentity {
this.evidence = null;
}
}

@Override
public Principal getRealmIdentityPrincipal() {
Principal principal = null;
try {
if (exists()) {
principal = claimToPrincipal.apply(this.claims);

if (principalTransformer != null) {
principal = principalTransformer.apply(principal);
}
}
} catch (Exception e) {
throw ElytronMessages.log.tokenRealmFailedToObtainPrincipal(e);
Expand Down Expand Up @@ -223,7 +231,7 @@ public static class Builder {
private String principalClaimName = "username";
private Function<Attributes, Principal> claimToPrincipal;
private TokenValidator strategy;

private Function<Principal, Principal> principalTransformer;
/**
* Construct a new instance.
*/
Expand Down Expand Up @@ -262,6 +270,15 @@ public Builder validator(TokenValidator strategy) {
return this;
}

/**
* Defines a {@link TokenValidator} that will be used to validate tokens.
*
* @return this instance
*/
public Builder principalTransformer(Function<Principal, Principal> func) {
this.principalTransformer = func;
return this;
}
/**
* Creates a {@link TokenSecurityRealm} instance with all the given configuration.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.security.auth.realm.token.validator.OAuth2IntrospectValidator;
import org.wildfly.security.auth.server.NameRewriter;
import org.wildfly.security.auth.server.RealmIdentity;
import org.wildfly.security.auth.server.RealmUnavailableException;
import org.wildfly.security.authz.Attributes;
Expand All @@ -42,6 +43,7 @@
import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.util.Arrays;
import java.util.Locale;
import java.util.function.Function;

import static org.junit.Assert.*;
Expand Down Expand Up @@ -103,6 +105,60 @@ public void testUserDefinedPrincipalClaimName() throws Exception {
assertEquals("elytron@jboss.org", realmIdentityPrincipal.getName());
}

@Test
public void testPrincipalTransformer() throws Exception {
configureReplayTokenIntrospection();

Function<Principal, Principal> principalTransformer = new CaseRewriter().asPrincipalRewriter();
TokenSecurityRealm securityRealm = TokenSecurityRealm.builder()
.validator(OAuth2IntrospectValidator.builder()
.clientId("wildfly-elytron")
.clientSecret("dont_tell_me")
.tokenIntrospectionUrl(new URL("http://as.test.org/oauth2/token/introspect")).build())
.principalTransformer(principalTransformer)
.build();

JsonObjectBuilder tokenBuilder = Json.createObjectBuilder();

tokenBuilder.add("active", true);
tokenBuilder.add("username", "elytron@jboss.org");

RealmIdentity realmIdentity = securityRealm.getRealmIdentity(new BearerTokenEvidence(tokenBuilder.build().toString()));

assertTrue(realmIdentity.exists());

Principal realmIdentityPrincipal = realmIdentity.getRealmIdentityPrincipal();

assertEquals("ELYTRON@JBOSS.ORG", realmIdentityPrincipal.getName());
}

@Test
public void testNullPrincipalTransformer() throws Exception {
configureReplayTokenIntrospection();

Function<Principal, Principal> principalTransformer = null;
TokenSecurityRealm securityRealm = TokenSecurityRealm.builder()
.validator(OAuth2IntrospectValidator.builder()
.clientId("wildfly-elytron")
.clientSecret("dont_tell_me")
.tokenIntrospectionUrl(new URL("http://as.test.org/oauth2/token/introspect")).build())
.principalTransformer(principalTransformer)
.build();

JsonObjectBuilder tokenBuilder = Json.createObjectBuilder();

tokenBuilder.add("active", true);
tokenBuilder.add("username", "elytron@jboss.org");

RealmIdentity realmIdentity = securityRealm.getRealmIdentity(new BearerTokenEvidence(tokenBuilder.build().toString()));

assertTrue(realmIdentity.exists());

Principal realmIdentityPrincipal = realmIdentity.getRealmIdentityPrincipal();

assertEquals("elytron@jboss.org", realmIdentityPrincipal.getName());
}

@Test
public void testNotActiveToken() throws Exception {
configureReplayTokenIntrospection();
Expand Down Expand Up @@ -218,4 +274,13 @@ public JsonObject introspectAccessToken(URL tokenIntrospectionUrl, String client
}
};
}

/*
* Function to convert string to all caps
*/
private class CaseRewriter implements NameRewriter {
public String rewriteName(String original) {
return (original == null) ? null : original.toUpperCase(Locale.ROOT);
}
}
}
Loading