-
Notifications
You must be signed in to change notification settings - Fork 828
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 federated client authentication #3219
base: develop
Are you sure you want to change the base?
Changes from all commits
f182f9d
736fd35
7b10318
66ec6ec
c3a4aed
3b9b6f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.cloudfoundry.identity.uaa.oauth.client; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import org.cloudfoundry.identity.uaa.util.JsonUtils; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.List; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
@Builder(toBuilder = true) | ||
@Data | ||
public class ClientJwtCredential { | ||
|
||
@JsonProperty("sub") | ||
private String subject; | ||
@JsonProperty("iss") | ||
private String issuer; | ||
@JsonProperty("aud") | ||
private String audience; | ||
|
||
public ClientJwtCredential() { | ||
} | ||
|
||
public ClientJwtCredential(String subject, String issuer, String audience) { | ||
this.subject = subject; | ||
this.issuer = issuer; | ||
this.audience = audience; | ||
} | ||
|
||
@JsonIgnore | ||
public boolean isValid() { | ||
return StringUtils.hasText(subject) && StringUtils.hasText(issuer); | ||
} | ||
|
||
public static List<ClientJwtCredential> parse(String clientJwtCredentials) { | ||
try { | ||
return JsonUtils.readValue(clientJwtCredentials, new TypeReference<>() {}); | ||
} catch (JsonUtils.JsonUtilException e) { | ||
throw new IllegalArgumentException("Client jwt configuration cannot be parsed", e); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.cloudfoundry.identity.uaa.oauth.client; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatNoException; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class ClientJwtCredentialTest { | ||
|
||
@Test | ||
void parse() { | ||
assertThatNoException().isThrownBy(() -> ClientJwtCredential.parse("[{\"iss\":\"http://localhost:8080/uaa\",\"sub\":\"client_with_jwks_trust\"}]")); | ||
List<ClientJwtCredential> federationList = ClientJwtCredential.parse("[{\"iss\":\"http://localhost:8080/uaa\",\"sub\":\"client_with_jwks_trust\"},{\"iss\":\"http://localhost:8080/uaa\"}]"); | ||
assertThat(federationList).hasSize(2); | ||
} | ||
|
||
@Test | ||
void constructor() { | ||
ClientJwtCredential jwtCredential = new ClientJwtCredential("subject", "issuer", "audience"); | ||
assertThat(jwtCredential) | ||
.returns("subject", ClientJwtCredential::getSubject) | ||
.returns("issuer", ClientJwtCredential::getIssuer) | ||
.returns("audience", ClientJwtCredential::getAudience) | ||
.returns(true, ClientJwtCredential::isValid); | ||
jwtCredential = new ClientJwtCredential(); | ||
assertThat(jwtCredential.isValid()).isFalse(); | ||
} | ||
|
||
@Test | ||
void testDeserializer() { | ||
assertThat(ClientJwtCredential.parse("[{\"iss\":\"issuer\"}]").iterator().next().isValid()).isFalse(); | ||
} | ||
|
||
@Test | ||
void deserializerException() { | ||
assertThatThrownBy(() -> ClientJwtCredential.parse("[\"iss\":\"issuer\"]")) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package org.cloudfoundry.identity.uaa.client; | ||
|
||
import static java.util.Optional.ofNullable; | ||
import static org.cloudfoundry.identity.uaa.client.ClientJwtConfiguration.JWT_CREDS; | ||
import static org.cloudfoundry.identity.uaa.client.ClientJwtConfiguration.JWKS; | ||
import static org.cloudfoundry.identity.uaa.client.ClientJwtConfiguration.JWKS_URI; | ||
import static org.cloudfoundry.identity.uaa.oauth.token.TokenConstants.GRANT_TYPE_AUTHORIZATION_CODE; | ||
|
@@ -20,6 +21,7 @@ | |
import org.cloudfoundry.identity.uaa.audit.event.EntityDeletedEvent; | ||
import org.cloudfoundry.identity.uaa.authentication.SystemAuthentication; | ||
import org.cloudfoundry.identity.uaa.oauth.client.ClientConstants; | ||
import org.cloudfoundry.identity.uaa.oauth.client.ClientJwtCredential; | ||
import org.cloudfoundry.identity.uaa.provider.ClientAlreadyExistsException; | ||
import org.cloudfoundry.identity.uaa.provider.NoSuchClientException; | ||
import org.cloudfoundry.identity.uaa.user.UaaAuthority; | ||
|
@@ -202,22 +204,27 @@ private void addNewClients() { | |
client.getAuthorizedGrantTypes().add(GRANT_TYPE_REFRESH_TOKEN); | ||
} | ||
for (String key : Arrays.asList("resource-ids", "scope", "authorized-grant-types", "authorities", | ||
"redirect-uri", "secret", "id", "override", "access-token-validity", | ||
"refresh-token-validity", "show-on-homepage", "app-launch-url", "app-icon", JWKS, JWKS_URI)) { | ||
"redirect-uri", "secret", "id", "override", "access-token-validity", "refresh-token-validity", | ||
"show-on-homepage", "app-launch-url", "app-icon", JWKS, JWKS_URI, JWT_CREDS)) { | ||
info.remove(key); | ||
} | ||
|
||
client.setAdditionalInformation(info); | ||
|
||
ClientJwtConfiguration keyConfig = null; | ||
if (map.get(JWKS_URI) instanceof String || map.get(JWKS) instanceof String) { | ||
String jwksUri = (String) map.get(JWKS_URI); | ||
String jwks = (String) map.get(JWKS); | ||
ClientJwtConfiguration keyConfig = ClientJwtConfiguration.parse(jwksUri, jwks); | ||
if (keyConfig != null && keyConfig.getCleanString() != null) { | ||
keyConfig.writeValue(client); | ||
} else { | ||
throw new InvalidClientDetailsException("Client jwt configuration invalid syntax. ClientID: " + client.getClientId()); | ||
keyConfig = ClientJwtConfiguration.parse(jwksUri, jwks); | ||
writeJwtClientConfiguration(keyConfig, client); | ||
} | ||
|
||
if (map.get(JWT_CREDS) instanceof String jwtCredential) { | ||
if (keyConfig == null) { | ||
keyConfig = new ClientJwtConfiguration(); | ||
} | ||
keyConfig.addJwtCredentials(ClientJwtCredential.parse(jwtCredential)); | ||
writeJwtClientConfiguration(keyConfig, client); | ||
} | ||
|
||
try { | ||
|
@@ -251,6 +258,14 @@ private void addNewClients() { | |
} | ||
} | ||
|
||
private static void writeJwtClientConfiguration(ClientJwtConfiguration keyConfig, UaaClientDetails client) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [suggestion, non-blocking] I like re-using the method here, but I think in the case of The is JWT credentials are parsed with Therefore, I would not use |
||
if (keyConfig != null && keyConfig.getCleanString() != null) { | ||
keyConfig.writeValue(client); | ||
} else { | ||
throw new InvalidClientDetailsException("Client jwt configuration invalid syntax. ClientID: " + client.getClientId()); | ||
} | ||
} | ||
|
||
private boolean isMissingRedirectUris(UaaClientDetails client) { | ||
return client.getRegisteredRedirectUri() == null || client.getRegisteredRedirectUri().isEmpty(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[question, non-blocking] Is the order important here? If so, why?