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

Add claims parameter to authorize #1045

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/docs/usage/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ See specific example [configurations for your provider](/docs/category/providers
- **clientSecret** - (`string`) client secret to pass to token exchange requests. :warning: Read more about [client secrets](/docs/client-secrets)
- **redirectUrl** - (`string`) _REQUIRED_ the url that links back to your app with the auth code. Depending on your [provider](/docs/category/providers), you may find that you need to add a trailing slash to your redirect URL.
- **scopes** - (`array<string>`) the scopes for your token, e.g. `['email', 'offline_access']`.
- **claims** - (`object`) the requested claims for your token, e.g. ` { "id_token": { "given_name": null } }`.
- **additionalParameters** - (`object`) additional parameters that will be passed in the authorization request.
Must be string values! E.g. setting `additionalParameters: { hello: 'world', foo: 'bar' }` would add
`hello=world&foo=bar` to the authorization request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
import net.openid.appauth.connectivity.ConnectionBuilder;
import net.openid.appauth.connectivity.DefaultConnectionBuilder;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -98,6 +100,7 @@ public void prefetchConfiguration(
final String redirectUrl,
final String clientId,
final ReadableArray scopes,
final ReadableMap claims,
final ReadableMap serviceConfiguration,
final boolean dangerouslyAllowInsecureHttpRequests,
final ReadableMap customHeaders,
Expand Down Expand Up @@ -232,6 +235,7 @@ public void authorize(
final String clientId,
final String clientSecret,
final ReadableArray scopes,
final ReadableMap claims,
final ReadableMap additionalParameters,
final ReadableMap serviceConfiguration,
final Boolean skipCodeExchange,
Expand Down Expand Up @@ -273,6 +277,7 @@ public void authorize(
appAuthConfiguration,
clientId,
scopes,
claims,
redirectUrl,
useNonce,
usePKCE,
Expand Down Expand Up @@ -304,6 +309,7 @@ public void onFetchConfigurationCompleted(
appAuthConfiguration,
clientId,
scopes,
claims,
redirectUrl,
useNonce,
usePKCE,
Expand Down Expand Up @@ -654,11 +660,12 @@ private void authorizeWithConfiguration(
final AppAuthConfiguration appAuthConfiguration,
final String clientId,
final ReadableArray scopes,
final ReadableMap claims,
final String redirectUrl,
final Boolean useNonce,
final Boolean usePKCE,
final Map<String, String> additionalParametersMap,
final Boolean androidTrustedWebActivity) {
final Boolean androidTrustedWebActivity) throws Exception {

String scopesString = null;

Expand All @@ -679,6 +686,14 @@ private void authorizeWithConfiguration(
authRequestBuilder.setScope(scopesString);
}

if (claims != null) {
try {
authRequestBuilder.setClaims(MapUtil.convertMapToJson(claims));
} catch (JSONException ignored) {
throw new Exception("claims passed but contains invalid JSON");
}
}

if (additionalParametersMap != null) {
// handle additional parameters separately to avoid exceptions from AppAuth
if (additionalParametersMap.containsKey("display")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeArray;
Expand Down Expand Up @@ -119,4 +121,59 @@ private static WritableArray convertJsonToArray(JSONArray jsonArray) throws JSON
}
return array;
}

public static JSONObject convertMapToJson(ReadableMap readableMap) throws JSONException {
JSONObject object = new JSONObject();
ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
switch (readableMap.getType(key)) {
case Null:
object.put(key, JSONObject.NULL);
break;
case Boolean:
object.put(key, readableMap.getBoolean(key));
break;
case Number:
object.put(key, readableMap.getDouble(key));
break;
case String:
object.put(key, readableMap.getString(key));
break;
case Map:
object.put(key, convertMapToJson(readableMap.getMap(key)));
break;
case Array:
object.put(key, convertArrayToJson(readableMap.getArray(key)));
break;
}
}
return object;
}

private static JSONArray convertArrayToJson(ReadableArray readableArray) throws JSONException {
JSONArray array = new JSONArray();
for (int i = 0; i < readableArray.size(); i++) {
switch (readableArray.getType(i)) {
case Null:
break;
case Boolean:
array.put(readableArray.getBoolean(i));
break;
case Number:
array.put(readableArray.getDouble(i));
break;
case String:
array.put(readableArray.getString(i));
break;
case Map:
array.put(convertMapToJson(readableArray.getMap(i)));
break;
case Array:
array.put(convertArrayToJson(readableArray.getArray(i)));
break;
}
}
return array;
}
}
1 change: 1 addition & 0 deletions packages/react-native-app-auth/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export type BaseAuthConfiguration = BaseConfiguration & {
export type AuthConfiguration = BaseAuthConfiguration & {
clientSecret?: string;
scopes: string[];
claims?: object,
redirectUrl: string;
additionalParameters?: BuiltInParameters & { [name: string]: string };
clientAuthMethod?: 'basic' | 'post';
Expand Down
4 changes: 4 additions & 0 deletions packages/react-native-app-auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export const prefetchConfiguration = async ({
redirectUrl,
clientId,
scopes,
claims,
serviceConfiguration,
dangerouslyAllowInsecureHttpRequests = false,
customHeaders,
Expand All @@ -118,6 +119,7 @@ export const prefetchConfiguration = async ({
redirectUrl,
clientId,
scopes,
claims,
serviceConfiguration,
dangerouslyAllowInsecureHttpRequests,
customHeaders,
Expand Down Expand Up @@ -200,6 +202,7 @@ export const authorize = ({
clientId,
clientSecret,
scopes,
claims,
useNonce = true,
usePKCE = true,
additionalParameters,
Expand Down Expand Up @@ -229,6 +232,7 @@ export const authorize = ({
clientId,
clientSecret,
scopes,
claims,
additionalParameters,
serviceConfiguration,
skipCodeExchange,
Expand Down