Skip to content

Commit 039a2e7

Browse files
committed
4.x: UPN claim should be optional (#5151)
1 parent 3c4bbd8 commit 039a2e7

File tree

2 files changed

+24
-13
lines changed
  • security/jwt/src

2 files changed

+24
-13
lines changed

security/jwt/src/main/java/io/helidon/security/jwt/Jwt.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2024 Oracle and/or its affiliates.
2+
* Copyright (c) 2018, 2025 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -307,9 +307,7 @@ public class Jwt {
307307
this.cHash = JwtUtil.getByteArray(payloadJson, C_HASH, "c_hash value");
308308
this.nonce = JwtUtil.getString(payloadJson, NONCE);
309309
this.scopes = JwtUtil.toScopes(payloadJson);
310-
this.userPrincipal = JwtUtil.getString(payloadJson, USER_PRINCIPAL)
311-
.or(() -> preferredUsername)
312-
.or(() -> subject);
310+
this.userPrincipal = JwtUtil.getString(payloadJson, USER_PRINCIPAL);
313311
}
314312

315313
private Jwt(Builder builder) {
@@ -357,9 +355,7 @@ private Jwt(Builder builder) {
357355
this.scopes = builder.scopes;
358356

359357
this.userPrincipal = builder.userPrincipal
360-
.or(() -> toOptionalString(builder.payloadClaims, USER_PRINCIPAL))
361-
.or(() -> preferredUsername)
362-
.or(() -> subject);
358+
.or(() -> toOptionalString(builder.payloadClaims, USER_PRINCIPAL));
363359

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

664660
/**
665661
* User principal claim ("upn" from microprofile specification).
662+
* Falls back "preferred_username" then "sub" claim.
666663
*
667-
* @return user principal or empty if claim is not defined
664+
* @return user principal or empty if claim and fallbacks are not defined
668665
*/
669666
public Optional<String> userPrincipal() {
670-
return userPrincipal;
667+
return userPrincipal
668+
.or(() -> preferredUsername)
669+
.or(() -> subject);
671670
}
672671

673672
/**
@@ -1682,8 +1681,7 @@ public Builder subject(String subject) {
16821681
* User principal claim as defined by Microprofile JWT Auth spec.
16831682
* Uses "upn" claim.
16841683
*
1685-
* @param principal name of the principal, falls back to {@link #preferredUsername(String)} and then to
1686-
* {@link #subject(String)}
1684+
* @param principal name of the principal
16871685
* @return updated builder instance
16881686
*/
16891687
public Builder userPrincipal(String principal) {

security/jwt/src/test/java/io/helidon/security/jwt/JwtTest.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2024 Oracle and/or its affiliates.
2+
* Copyright (c) 2018, 2025 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@
2020
import java.time.temporal.ChronoUnit;
2121
import java.util.List;
2222
import java.util.Optional;
23-
import java.util.Set;
2423
import java.util.UUID;
2524

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

3130
import static org.hamcrest.CoreMatchers.is;
31+
import static org.hamcrest.CoreMatchers.containsString;
32+
import static org.hamcrest.CoreMatchers.not;
3233
import static org.hamcrest.MatcherAssert.assertThat;
3334

3435
/**
@@ -111,4 +112,16 @@ public void testOidcJwt() {
111112
errors.log(LOGGER);
112113
errors.checkValid();
113114
}
115+
116+
@Test
117+
public void testUpnNotAddedAutomatically() {
118+
String json = Jwt.builder().subject("a").build().payloadJson().toString();
119+
assertThat(json, not(containsString("\"upn\"")));
120+
}
121+
122+
@Test
123+
public void testUserPrincipalFallsBackToSub() {
124+
Jwt jwt = Jwt.builder().subject("a").build();
125+
assertThat(jwt.userPrincipal(), is(Optional.of("a")));
126+
}
114127
}

0 commit comments

Comments
 (0)