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

4.x: UPN claim should be optional (#5151) #9640

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 9 additions & 11 deletions security/jwt/src/main/java/io/helidon/security/jwt/Jwt.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2024 Oracle and/or its affiliates.
* Copyright (c) 2018, 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -307,9 +307,7 @@ public class Jwt {
this.cHash = JwtUtil.getByteArray(payloadJson, C_HASH, "c_hash value");
this.nonce = JwtUtil.getString(payloadJson, NONCE);
this.scopes = JwtUtil.toScopes(payloadJson);
this.userPrincipal = JwtUtil.getString(payloadJson, USER_PRINCIPAL)
.or(() -> preferredUsername)
.or(() -> subject);
this.userPrincipal = JwtUtil.getString(payloadJson, USER_PRINCIPAL);
}

private Jwt(Builder builder) {
Expand Down Expand Up @@ -357,9 +355,7 @@ private Jwt(Builder builder) {
this.scopes = builder.scopes;

this.userPrincipal = builder.userPrincipal
.or(() -> toOptionalString(builder.payloadClaims, USER_PRINCIPAL))
.or(() -> preferredUsername)
.or(() -> subject);
.or(() -> toOptionalString(builder.payloadClaims, USER_PRINCIPAL));

this.userGroups = builder.userGroups;
}
Expand Down Expand Up @@ -663,11 +659,14 @@ public Optional<String> subject() {

/**
* User principal claim ("upn" from microprofile specification).
* Falls back "preferred_username" then "sub" claim.
*
* @return user principal or empty if claim is not defined
* @return user principal or empty if claim and fallbacks are not defined
*/
public Optional<String> userPrincipal() {
return userPrincipal;
return userPrincipal
.or(() -> preferredUsername)
.or(() -> subject);
}

/**
Expand Down Expand Up @@ -1682,8 +1681,7 @@ public Builder subject(String subject) {
* User principal claim as defined by Microprofile JWT Auth spec.
* Uses "upn" claim.
*
* @param principal name of the principal, falls back to {@link #preferredUsername(String)} and then to
* {@link #subject(String)}
* @param principal name of the principal
* @return updated builder instance
*/
public Builder userPrincipal(String principal) {
Expand Down
17 changes: 15 additions & 2 deletions security/jwt/src/test/java/io/helidon/security/jwt/JwtTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2024 Oracle and/or its affiliates.
* Copyright (c) 2018, 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,7 +20,6 @@
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

import io.helidon.common.Errors;
Expand All @@ -29,6 +28,8 @@
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;

/**
Expand Down Expand Up @@ -111,4 +112,16 @@ public void testOidcJwt() {
errors.log(LOGGER);
errors.checkValid();
}

@Test
public void testUpnNotAddedAutomatically() {
String json = Jwt.builder().subject("a").build().payloadJson().toString();
assertThat(json, not(containsString("\"upn\"")));
}

@Test
public void testUserPrincipalFallsBackToSub() {
Jwt jwt = Jwt.builder().subject("a").build();
assertThat(jwt.userPrincipal(), is(Optional.of("a")));
}
}