Skip to content

Commit 78fa5f9

Browse files
committed
Started to write ScopeClaimTranslationService
1 parent cb449c2 commit 78fa5f9

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.mitre.openid.connect.service;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import com.google.common.collect.ArrayListMultimap;
7+
import com.google.common.collect.Maps;
8+
9+
/**
10+
* Service to map scopes to claims, and claims to Java field names
11+
*
12+
* @author Amanda Anganes
13+
*
14+
*/
15+
public class ScopeClaimTranslationService {
16+
17+
private ArrayListMultimap<String, String> scopesToClaims = ArrayListMultimap.create();
18+
private Map<String, String> claimsToFields = Maps.newHashMap();
19+
20+
/**
21+
* Default constructor; initializes scopesToClaims map
22+
*/
23+
public ScopeClaimTranslationService() {
24+
25+
scopesToClaims.put("openid", "sub");
26+
27+
scopesToClaims.put("profile", "name");
28+
scopesToClaims.put("profile", "preferred_username");
29+
scopesToClaims.put("profile", "given_name");
30+
scopesToClaims.put("profile", "family_name");
31+
scopesToClaims.put("profile", "middle_name");
32+
scopesToClaims.put("profile", "nickname");
33+
scopesToClaims.put("profile", "profile");
34+
scopesToClaims.put("profile", "picture");
35+
scopesToClaims.put("profile", "website");
36+
scopesToClaims.put("profile", "gender");
37+
scopesToClaims.put("profile", "zone_info");
38+
scopesToClaims.put("profile", "locale");
39+
scopesToClaims.put("profile", "updated_time");
40+
scopesToClaims.put("profile", "birthdate");
41+
42+
scopesToClaims.put("email", "email");
43+
scopesToClaims.put("email", "email_verified");
44+
45+
scopesToClaims.put("phone", "phone_number");
46+
scopesToClaims.put("phone", "phone_number_verified");
47+
48+
scopesToClaims.put("address", "address.formatted");
49+
scopesToClaims.put("address", "address.street_address");
50+
scopesToClaims.put("address", "address.locality");
51+
scopesToClaims.put("address", "address.region");
52+
scopesToClaims.put("address", "address.postal_code");
53+
scopesToClaims.put("address", "address.country");
54+
55+
claimsToFields.put("sub", "sub");
56+
57+
claimsToFields.put("name", "name");
58+
claimsToFields.put("preferred_username", "preferredUsername");
59+
claimsToFields.put("given_name", "givenName");
60+
claimsToFields.put("family_name", "familyName");
61+
claimsToFields.put("middle_name", "middleName");
62+
claimsToFields.put("nickname", "nickname");
63+
claimsToFields.put("profile", "profile");
64+
claimsToFields.put("picture", "picture");
65+
claimsToFields.put("website", "website");
66+
claimsToFields.put("gender", "gender");
67+
claimsToFields.put("zone_info", "zoneinfo");
68+
claimsToFields.put("locale", "locale");
69+
claimsToFields.put("updated_time", "updatedTime");
70+
claimsToFields.put("birthdate", "birthdate");
71+
72+
claimsToFields.put("email", "email");
73+
claimsToFields.put("email_verified", "emailVerified");
74+
75+
claimsToFields.put("phone_number", "phoneNumber");
76+
claimsToFields.put("phone_number_verified", "phoneNumberVerified");
77+
78+
//TODO: how to handle compound fields?
79+
claimsToFields.put("address.formatted", "");
80+
claimsToFields.put("address.street_address", "");
81+
claimsToFields.put("address.locality", "");
82+
claimsToFields.put("address.region", "");
83+
claimsToFields.put("address.postal_code", "");
84+
claimsToFields.put("address.country", "");
85+
86+
}
87+
88+
public List<String> getClaimsForScope(String scope) {
89+
return scopesToClaims.get(scope);
90+
}
91+
92+
public String getFieldNameForClaim(String claim) {
93+
return claimsToFields.get(claim);
94+
}
95+
96+
}

openid-connect-common/src/main/java/org/mitre/openid/connect/view/UserInfoSerializer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Set;
77

88
import org.mitre.openid.connect.model.UserInfo;
9+
import org.mitre.openid.connect.service.ScopeClaimTranslationService;
910
import org.slf4j.Logger;
1011
import org.slf4j.LoggerFactory;
1112

@@ -18,6 +19,8 @@ public class UserInfoSerializer {
1819

1920
private static Logger logger = LoggerFactory.getLogger(UserInfoSerializer.class);
2021

22+
private ScopeClaimTranslationService translator = new ScopeClaimTranslationService();
23+
2124
/**
2225
* Build a JSON response according to the request object received.
2326
*
@@ -61,6 +64,9 @@ public static JsonObject toJsonFromRequestObj(UserInfo ui, Set<String> scope, Js
6164

6265
}
6366

67+
//TODO: is there a way to use bean processors to do bean.getfield(name)?
68+
//Method reflection is OK, but need a service to translate scopes into claim names => field names
69+
6470
// TODO: this method is likely to be fragile if the data model changes at all
6571

6672
//For each claim found, add it if not already present

0 commit comments

Comments
 (0)