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

Fix OIDC UserInfo to better handle null, array, map #32833

Merged
merged 1 commit into from
Apr 24, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ public Boolean getBoolean(String name) {
}

public Long getLong(String name) {
JsonNumber number = json.getJsonNumber(name);
JsonNumber number = contains(name) ? json.getJsonNumber(name) : null;
return number != null ? number.longValue() : null;
}

public JsonArray getArray(String name) {
return json.getJsonArray(name);
return contains(name) ? json.getJsonArray(name) : null;
}

public JsonObject getObject(String name) {
return json.getJsonObject(name);
return contains(name) ? json.getJsonObject(name) : null;
}

public JsonObject getJsonObject() {
Expand All @@ -58,7 +58,7 @@ public Object get(String name) {
}

public boolean contains(String propertyName) {
return json.containsKey(propertyName);
return json.containsKey(propertyName) && !json.isNull(propertyName);
}

public Set<String> getPropertyNames() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package io.quarkus.oidc.runtime;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import jakarta.json.JsonArray;
import jakarta.json.JsonObject;

import org.junit.jupiter.api.Test;

import io.quarkus.oidc.UserInfo;
Expand All @@ -12,7 +16,11 @@ public class UserInfoTest {
UserInfo userInfo = new UserInfo(
"{"
+ "\"name\": \"alice\","
+ "\"admin\": true"
+ "\"admin\": true,"
+ "\"email\": null,"
+ "\"id\": 1234,"
+ "\"permissions\": [\"read\", \"write\"],"
+ "\"scopes\": {\"scope\": \"see\"}"
+ "}");

@Test
Expand All @@ -26,4 +34,34 @@ public void testGetBoolean() {
assertTrue(userInfo.getBoolean("admin"));
assertNull(userInfo.getBoolean("admins"));
}

@Test
public void testGetLong() {
assertEquals(1234, userInfo.getLong("id"));
assertNull(userInfo.getLong("ids"));
}

@Test
public void testGetArray() {
JsonArray array = userInfo.getArray("permissions");
assertNotNull(array);
assertEquals(2, array.size());
assertEquals("read", array.getString(0));
assertEquals("write", array.getString(1));
assertNull(userInfo.getArray("permit"));
}

@Test
public void testGetObject() {
JsonObject map = userInfo.getObject("scopes");
assertNotNull(map);
assertEquals(1, map.size());
assertEquals("see", map.getString("scope"));
assertNull(userInfo.getObject("scope"));
}

@Test
public void testGetNullProperty() {
assertNull(userInfo.getString("email"));
}
}