Skip to content

Commit

Permalink
Support requesting specific claims and claims languages (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
agologan committed Aug 13, 2021
1 parent 31f187b commit 766a77a
Show file tree
Hide file tree
Showing 2 changed files with 284 additions and 1 deletion.
117 changes: 116 additions & 1 deletion library/java/net/openid/appauth/AuthorizationRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ public static final class ResponseMode {
@VisibleForTesting
static final String PARAM_NONCE = "nonce";

@VisibleForTesting
static final String PARAM_CLAIMS = "claims";

@VisibleForTesting
static final String PARAM_CLAIMS_LOCALES = "claims_locales";

private static final Set<String> BUILT_IN_PARAMS = builtInParams(
PARAM_CLIENT_ID,
PARAM_CODE_CHALLENGE,
Expand All @@ -317,7 +323,9 @@ public static final class ResponseMode {
PARAM_RESPONSE_MODE,
PARAM_RESPONSE_TYPE,
PARAM_SCOPE,
PARAM_STATE);
PARAM_STATE,
PARAM_CLAIMS,
PARAM_CLAIMS_LOCALES);

private static final String KEY_CONFIGURATION = "configuration";
private static final String KEY_CLIENT_ID = "clientId";
Expand All @@ -334,6 +342,8 @@ public static final class ResponseMode {
private static final String KEY_CODE_VERIFIER_CHALLENGE = "codeVerifierChallenge";
private static final String KEY_CODE_VERIFIER_CHALLENGE_METHOD = "codeVerifierChallengeMethod";
private static final String KEY_RESPONSE_MODE = "responseMode";
private static final String KEY_CLAIMS = "claims";
private static final String KEY_CLAIMS_LOCALES = "claimsLocales";
private static final String KEY_ADDITIONAL_PARAMETERS = "additionalParameters";

/**
Expand Down Expand Up @@ -519,6 +529,26 @@ public static final class ResponseMode {
@Nullable
public final String responseMode;

/**
* Requests that specific Claims be returned.
* The value is a JSON object listing the requested Claims.
*
* @see "OpenID Connect Core 1.0, Section 5.5
* <https://openid.net/specs/openid-connect-core-1_0.html#rfc.section.5.5>"
*/
@Nullable
public final JSONObject claims;

/**
* End-User's preferred languages and scripts for Claims being returned, represented as a
* space-separated list of BCP47 [RFC5646] language tag values, ordered by preference.
*
* @see "OpenID Connect Core 1.0, Section 5.2
* <https://openid.net/specs/openid-connect-core-1_0.html#rfc.section.5.2>"
*/
@Nullable
public final String claimsLocales;

/**
* Additional parameters to be passed as part of the request.
*
Expand Down Expand Up @@ -590,6 +620,12 @@ public static final class Builder {
@Nullable
private String mResponseMode;

@Nullable
private JSONObject mClaims;

@Nullable
private String mClaimsLocales;

@NonNull
private Map<String, String> mAdditionalParameters = new HashMap<>();

Expand Down Expand Up @@ -945,6 +981,63 @@ public Builder setResponseMode(@Nullable String responseMode) {
return this;
}

/**
* Requests that specific Claims be returned.
* The value is a JSON object listing the requested Claims.
*
* @see "OpenID Connect Core 1.0, Section 5.5
* <https://openid.net/specs/openid-connect-core-1_0.html#rfc.section.5.5>"
*/
@NonNull
public Builder setClaims(@Nullable JSONObject claims) {
mClaims = claims;
return this;
}

/**
* End-User's preferred languages and scripts for Claims being returned, represented as a
* space-separated list of BCP47 [RFC5646] language tag values, ordered by preference.
*
* @see "OpenID Connect Core 1.0, Section 5.2
* <https://openid.net/specs/openid-connect-core-1_0.html#rfc.section.5.2>"
*/
public Builder setClaimsLocales(@Nullable String claimsLocales) {
mClaimsLocales = checkNullOrNotEmpty(
claimsLocales,
"claimsLocales must be null or not empty");
return this;
}

/**
* End-User's preferred languages and scripts for Claims being returned, represented as a
* space-separated list of BCP47 [RFC5646] language tag values, ordered by preference.
*
* @see "OpenID Connect Core 1.0, Section 5.2
* <https://openid.net/specs/openid-connect-core-1_0.html#rfc.section.5.2>"
*/
@NonNull
public Builder setClaimsLocalesValues(@Nullable String... claimsLocalesValues) {
if (claimsLocalesValues == null) {
mClaimsLocales = null;
return this;
}

return setClaimsLocalesValues(Arrays.asList(claimsLocalesValues));
}

/**
* End-User's preferred languages and scripts for Claims being returned, represented as a
* space-separated list of BCP47 [RFC5646] language tag values, ordered by preference.
*
* @see "OpenID Connect Core 1.0, Section 5.2
* <https://openid.net/specs/openid-connect-core-1_0.html#rfc.section.5.2>"
*/
@NonNull
public Builder setClaimsLocalesValues(@Nullable Iterable<String> claimsLocalesValues) {
mClaimsLocales = AsciiStringListUtil.iterableToString(claimsLocalesValues);
return this;
}

/**
* Specifies additional parameters. Replaces any previously provided set of parameters.
* Parameter keys and values cannot be null or empty.
Expand Down Expand Up @@ -986,6 +1079,8 @@ public AuthorizationRequest build() {
mCodeVerifierChallenge,
mCodeVerifierChallengeMethod,
mResponseMode,
mClaims,
mClaimsLocales,
Collections.unmodifiableMap(new HashMap<>(mAdditionalParameters)));
}
}
Expand All @@ -1006,6 +1101,8 @@ private AuthorizationRequest(
@Nullable String codeVerifierChallenge,
@Nullable String codeVerifierChallengeMethod,
@Nullable String responseMode,
@Nullable JSONObject claims,
@Nullable String claimsLocales,
@NonNull Map<String, String> additionalParameters) {
// mandatory fields
this.configuration = configuration;
Expand All @@ -1026,6 +1123,8 @@ private AuthorizationRequest(
this.codeVerifierChallenge = codeVerifierChallenge;
this.codeVerifierChallengeMethod = codeVerifierChallengeMethod;
this.responseMode = responseMode;
this.claims = claims;
this.claimsLocales = claimsLocales;
}

/**
Expand Down Expand Up @@ -1062,6 +1161,15 @@ public String getState() {
return state;
}

/**
* Derives the set of claims_locales values from the consolidated, space-separated list of
* BCP47 [RFC5646] language tag values in the {@link #claimsLocales} field. If no claims_locales
* values were specified for this request, the method will return `null`.
*/
public Set<String> getClaimsLocales() {
return AsciiStringListUtil.stringToSet(claimsLocales);
}

/**
* Produces a request URI, that can be used to dispatch the authorization request.
*/
Expand All @@ -1087,6 +1195,9 @@ public Uri toUri() {
.appendQueryParameter(PARAM_CODE_CHALLENGE_METHOD, codeVerifierChallengeMethod);
}

UriUtil.appendQueryParameterIfNotNull(uriBuilder, PARAM_CLAIMS, claims);
UriUtil.appendQueryParameterIfNotNull(uriBuilder, PARAM_CLAIMS_LOCALES, claimsLocales);

for (Entry<String, String> entry : additionalParameters.entrySet()) {
uriBuilder.appendQueryParameter(entry.getKey(), entry.getValue());
}
Expand Down Expand Up @@ -1118,6 +1229,8 @@ public JSONObject jsonSerialize() {
JsonUtil.putIfNotNull(json, KEY_CODE_VERIFIER_CHALLENGE_METHOD,
codeVerifierChallengeMethod);
JsonUtil.putIfNotNull(json, KEY_RESPONSE_MODE, responseMode);
JsonUtil.putIfNotNull(json, KEY_CLAIMS, claims);
JsonUtil.putIfNotNull(json, KEY_CLAIMS_LOCALES, claimsLocales);
JsonUtil.put(json, KEY_ADDITIONAL_PARAMETERS,
JsonUtil.mapToJsonObject(additionalParameters));
return json;
Expand Down Expand Up @@ -1158,6 +1271,8 @@ public static AuthorizationRequest jsonDeserialize(@NonNull JSONObject json)
JsonUtil.getStringIfDefined(json, KEY_CODE_VERIFIER_CHALLENGE),
JsonUtil.getStringIfDefined(json, KEY_CODE_VERIFIER_CHALLENGE_METHOD),
JsonUtil.getStringIfDefined(json, KEY_RESPONSE_MODE),
JsonUtil.getJsonObjectIfDefined(json, KEY_CLAIMS),
JsonUtil.getStringIfDefined(json, KEY_CLAIMS_LOCALES),
JsonUtil.getStringMap(json, KEY_ADDITIONAL_PARAMETERS));
}

Expand Down
Loading

0 comments on commit 766a77a

Please sign in to comment.