From f2f1b0c165e4444d0b4361b9c6ae0616a30f36c4 Mon Sep 17 00:00:00 2001 From: Sergey Beryozkin Date: Wed, 19 Apr 2023 16:23:26 +0100 Subject: [PATCH] Prevent NPE for UserInfo string and boolean properties --- .../runtime/AbstractJsonObjectResponse.java | 4 +-- .../io/quarkus/oidc/runtime/UserInfoTest.java | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 extensions/oidc/runtime/src/test/java/io/quarkus/oidc/runtime/UserInfoTest.java diff --git a/extensions/oidc/runtime/src/main/java/io/quarkus/oidc/runtime/AbstractJsonObjectResponse.java b/extensions/oidc/runtime/src/main/java/io/quarkus/oidc/runtime/AbstractJsonObjectResponse.java index d8e7361970eb7..c01494cc941c5 100644 --- a/extensions/oidc/runtime/src/main/java/io/quarkus/oidc/runtime/AbstractJsonObjectResponse.java +++ b/extensions/oidc/runtime/src/main/java/io/quarkus/oidc/runtime/AbstractJsonObjectResponse.java @@ -29,11 +29,11 @@ public AbstractJsonObjectResponse(JsonObject json) { } public String getString(String name) { - return json.getString(name); + return contains(name) ? json.getString(name) : null; } public Boolean getBoolean(String name) { - return json.getBoolean(name); + return contains(name) ? json.getBoolean(name) : null; } public Long getLong(String name) { diff --git a/extensions/oidc/runtime/src/test/java/io/quarkus/oidc/runtime/UserInfoTest.java b/extensions/oidc/runtime/src/test/java/io/quarkus/oidc/runtime/UserInfoTest.java new file mode 100644 index 0000000000000..83d4f6ffba34f --- /dev/null +++ b/extensions/oidc/runtime/src/test/java/io/quarkus/oidc/runtime/UserInfoTest.java @@ -0,0 +1,29 @@ +package io.quarkus.oidc.runtime; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +import io.quarkus.oidc.UserInfo; + +public class UserInfoTest { + UserInfo userInfo = new UserInfo( + "{" + + "\"name\": \"alice\"," + + "\"admin\": true" + + "}"); + + @Test + public void testGetString() { + assertEquals("alice", userInfo.getString("name")); + assertNull(userInfo.getString("names")); + } + + @Test + public void testGetBoolean() { + assertTrue(userInfo.getBoolean("admin")); + assertNull(userInfo.getBoolean("admins")); + } +}