Skip to content

Commit 906a4e8

Browse files
authored
Merge pull request #33 from FormidableLabs/feature/additional-parameters
Feature/additional parameters
2 parents 237eda0 + 311c6d8 commit 906a4e8

File tree

6 files changed

+83
-20
lines changed

6 files changed

+83
-20
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ const result = await appAuth.authorize(scopes);
5454
// returns accessToken, accessTokenExpirationDate and refreshToken
5555
```
5656

57+
#### config
58+
59+
This is your configuration object for the client
60+
- **issuer**: (`string`) *REQUIRED* the url of the auth server
61+
- **clientId**: (`string`) *REQUIRED* your client id on the auth server
62+
- **redirectUrl**: (`string`) *REQUIRED* the url that links back to your app with the auth code
63+
- **additionalParameters**: (`object` | `null`) additional parameters that will be passed in the authorization request.
64+
Must be string values! E.g. setting `additionalParameters: { hello: 'world', foo: 'bar' }` would add
65+
`hello=world&foo=bar` to the authorization request.
66+
5767
### `refresh`
5868

5969
This method will refresh the accessToken using the refreshToken. Some auth providers will also give
@@ -280,7 +290,7 @@ import AppAuth from 'react-native-app-auth';
280290
// initialise the client with your configuration
281291
const appAuth = new AppAuth({
282292
issuer: '<YOUR_ISSUER_URL>',
283-
clientId: '<YOUR_CLIENT_ID',
293+
clientId: '<YOUR_CLIENT_ID>',
284294
redirectUrl: '<YOUR_REDIRECT_URL>',
285295
});
286296

android/src/main/java/com/reactlibrary/RNAppAuthModule.java

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import com.facebook.react.bridge.ReactMethod;
1414
import com.facebook.react.bridge.Promise;
1515
import com.facebook.react.bridge.ReadableArray;
16+
import com.facebook.react.bridge.ReadableMap;
17+
import com.facebook.react.bridge.ReadableMapKeySetIterator;
1618
import com.facebook.react.bridge.WritableMap;
1719

1820
import net.openid.appauth.AuthorizationException;
@@ -26,6 +28,7 @@
2628

2729
import java.text.SimpleDateFormat;
2830
import java.util.Date;
31+
import java.util.HashMap;
2932

3033
public class RNAppAuthModule extends ReactContextBaseJavaModule implements ActivityEventListener {
3134

@@ -63,8 +66,29 @@ private WritableMap tokenResponseToMap(TokenResponse response) {
6366
return map;
6467
}
6568

69+
private HashMap<String, String> additionalParametersToMap(ReadableMap additionalParameters) {
70+
71+
HashMap<String, String> additionalParametersHash = new HashMap<>();
72+
73+
ReadableMapKeySetIterator iterator = additionalParameters.keySetIterator();
74+
75+
while (iterator.hasNextKey()) {
76+
String nextKey = iterator.nextKey();
77+
additionalParametersHash.put(nextKey, additionalParameters.getString(nextKey));
78+
}
79+
80+
return additionalParametersHash;
81+
}
82+
6683
@ReactMethod
67-
public void authorize(String issuer, final String redirectUrl, final String clientId, final ReadableArray scopes, final Promise promise) {
84+
public void authorize(
85+
String issuer,
86+
final String redirectUrl,
87+
final String clientId,
88+
final ReadableArray scopes,
89+
final ReadableMap additionalParameters,
90+
final Promise promise
91+
) {
6892

6993
final Context context = this.reactContext;
7094
this.promise = promise;
@@ -88,11 +112,16 @@ public void onFetchConfigurationCompleted(
88112
serviceConfiguration,
89113
clientId,
90114
ResponseTypeValues.CODE,
91-
Uri.parse(redirectUrl));
115+
Uri.parse(redirectUrl)
116+
)
117+
.setScope(scopesString);
118+
119+
if (additionalParameters != null) {
120+
authRequestBuilder.setAdditionalParameters(additionalParametersToMap(additionalParameters));
121+
}
122+
123+
AuthorizationRequest authRequest = authRequestBuilder.build();
92124

93-
AuthorizationRequest authRequest = authRequestBuilder
94-
.setScope(scopesString)
95-
.build();
96125
AuthorizationService authService = new AuthorizationService(context);
97126
Intent authIntent = authService.getAuthorizationRequestIntent(authRequest);
98127
currentActivity.startActivityForResult(authIntent, 0);
@@ -103,7 +132,15 @@ public void onFetchConfigurationCompleted(
103132
}
104133

105134
@ReactMethod
106-
public void refresh(String issuer, final String redirectUrl, final String clientId, final String refreshToken, final ReadableArray scopes, final Promise promise) {
135+
public void refresh(
136+
String issuer,
137+
final String redirectUrl,
138+
final String clientId,
139+
final String refreshToken,
140+
final ReadableArray scopes,
141+
final ReadableMap additionalParameters,
142+
final Promise promise
143+
) {
107144
final Context context = this.reactContext;
108145

109146
final String scopesString = this.arrayToString(scopes);
@@ -123,13 +160,17 @@ public void onFetchConfigurationCompleted(
123160
new TokenRequest.Builder(
124161
serviceConfiguration,
125162
clientId
126-
);
163+
)
164+
.setScope(scopesString)
165+
.setRefreshToken(refreshToken)
166+
.setRedirectUri(Uri.parse(redirectUrl));
167+
168+
if (additionalParameters != null) {
169+
tokenRequestBuilder.setAdditionalParameters(additionalParametersToMap(additionalParameters));
170+
}
171+
172+
TokenRequest tokenRequest = tokenRequestBuilder.build();
127173

128-
TokenRequest tokenRequest = tokenRequestBuilder
129-
.setScope(scopesString)
130-
.setRefreshToken(refreshToken)
131-
.setRedirectUri(Uri.parse(redirectUrl))
132-
.build();
133174

134175
AuthorizationService authService = new AuthorizationService(context);
135176

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export default class AppAuth {
2323
this.config.issuer,
2424
this.config.redirectUrl,
2525
this.config.clientId,
26-
scopes
26+
scopes,
27+
this.config.additionalParameters
2728
);
2829
}
2930

@@ -36,7 +37,8 @@ export default class AppAuth {
3637
this.config.redirectUrl,
3738
this.config.clientId,
3839
refreshToken,
39-
scopes
40+
scopes,
41+
this.config.additionalParameters
4042
);
4143
}
4244

index.spec.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('AppAuth', () => {
1717
issuer: 'test-issuer',
1818
redirectUrl: 'test-redirectUrl',
1919
clientId: 'test-clientId',
20+
additionalParameters: { hello: 'world' },
2021
};
2122

2223
beforeAll(() => {
@@ -33,6 +34,11 @@ describe('AppAuth', () => {
3334
expect(appAuth.getConfig()).toEqual(config);
3435
});
3536

37+
it('saves the additionalParameters correctly if they are empty', () => {
38+
const appAuth = new AppAuth({ ...config, additionalParameters: undefined });
39+
expect(appAuth.getConfig()).toEqual({ ...config, additionalParameters: undefined });
40+
});
41+
3642
it('throws an error when issuer is not a string', () => {
3743
expect(() => {
3844
new AppAuth({ ...config, issuer: () => ({}) }); // eslint-disable-line no-new
@@ -81,7 +87,8 @@ describe('AppAuth', () => {
8187
config.issuer,
8288
config.redirectUrl,
8389
config.clientId,
84-
scopes
90+
scopes,
91+
config.additionalParameters
8592
);
8693
});
8794
});
@@ -124,7 +131,8 @@ describe('AppAuth', () => {
124131
config.redirectUrl,
125132
config.clientId,
126133
refreshToken,
127-
scopes
134+
scopes,
135+
config.additionalParameters
128136
);
129137
});
130138
});

ios/RNAppAuth.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ - (dispatch_queue_t)methodQueue
1818
redirectUrl: (NSString *) redirectUrl
1919
clientId: (NSString *) clientId
2020
scopes: (NSArray *) scopes
21+
additionalParameters: (NSDictionary *_Nullable) additionalParameters
2122
resolve:(RCTPromiseResolveBlock) resolve
2223
reject: (RCTPromiseRejectBlock) reject)
2324
{
@@ -35,7 +36,7 @@ - (dispatch_queue_t)methodQueue
3536
scopes:scopes
3637
redirectURL:[NSURL URLWithString:redirectUrl]
3738
responseType:OIDResponseTypeCode
38-
additionalParameters:nil];
39+
additionalParameters:additionalParameters];
3940

4041

4142
// performs authentication request
@@ -79,6 +80,7 @@ - (dispatch_queue_t)methodQueue
7980
clientId: (NSString *) clientId
8081
refreshToken: (NSString *) refreshToken
8182
scopes: (NSArray *) scopes
83+
additionalParameters: (NSDictionary *_Nullable) additionalParameters
8284
resolve:(RCTPromiseResolveBlock) resolve
8385
reject: (RCTPromiseRejectBlock) reject)
8486
{
@@ -99,7 +101,7 @@ - (dispatch_queue_t)methodQueue
99101
scopes:scopes
100102
refreshToken:refreshToken
101103
codeVerifier:nil
102-
additionalParameters:nil];
104+
additionalParameters:additionalParameters];
103105

104106
[OIDAuthorizationService performTokenRequest:tokenRefreshRequest
105107
callback:^(OIDTokenResponse *_Nullable response,

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4076,7 +4076,7 @@ preserve@^0.2.0:
40764076
version "0.2.0"
40774077
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"
40784078

4079-
prettier@^1.10.2:
4079+
prettier@1.10.2:
40804080
version "1.10.2"
40814081
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.10.2.tgz#1af8356d1842276a99a5b5529c82dd9e9ad3cc93"
40824082

0 commit comments

Comments
 (0)