Skip to content

Commit

Permalink
Merge pull request #32833 from sberyozkin/oidc_userinfo_property_null
Browse files Browse the repository at this point in the history
Fix OIDC UserInfo to better handle null, array, map
  • Loading branch information
gastaldi committed Apr 24, 2023
2 parents 7cd9c4e + 57c8dc5 commit 485d870
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
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"));
}
}

0 comments on commit 485d870

Please sign in to comment.