Skip to content

Commit 5bdf151

Browse files
kpeleliskadikraman
authored andcommitted
[Help Needed] Configure client authentication strategy (#272)
* android: Add ClientAuthentication strategy flag This patch introduces a `clientAuthMethod` flag in both `authroize` and `refresh` methods that utilizes both ClientSecretBasic, a class used for secret exchange with basic authentication (default), and ClientSecretPost, which send authentication credentials via the HTTP POST body. * Update README.md * Update tests
1 parent 0134521 commit 5bdf151

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ with optional overrides.
9797
* **additionalParameters** - (`object`) additional parameters that will be passed in the authorization request.
9898
Must be string values! E.g. setting `additionalParameters: { hello: 'world', foo: 'bar' }` would add
9999
`hello=world&foo=bar` to the authorization request.
100+
* **clientAuthMethod** - (string) Client Authentication Method. Can be either `basic` (default) for [Basic Authentication](https://github.com/openid/AppAuth-Android/blob/master/library/java/net/openid/appauth/ClientSecretBasic.java) or `post` for [HTTP POST body Authentication](https://github.com/openid/AppAuth-Android/blob/master/library/java/net/openid/appauth/ClientSecretPost.java)
100101
* **dangerouslyAllowInsecureHttpRequests** - (`boolean`) _ANDROID_ whether to allow requests over plain HTTP or with self-signed SSL certificates. :warning: Can be useful for testing against local server, _should not be used in production._ This setting has no effect on iOS; to enable insecure HTTP requests, add a [NSExceptionAllowsInsecureHTTPLoads exception](https://cocoacasts.com/how-to-add-app-transport-security-exception-domains) to your App Transport Security settings.
101102
* **customHeaders** - (`object`) _ANDROID_ you can specify custom headers to pass during authorize request and/or token request.
102103
* **authorize** - (`{ [key: string]: value }`) headers to be passed during authorization request.

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import net.openid.appauth.AuthorizationServiceConfiguration;
3131
import net.openid.appauth.ClientAuthentication;
3232
import net.openid.appauth.ClientSecretBasic;
33+
import net.openid.appauth.ClientSecretPost;
3334
import net.openid.appauth.ResponseTypeValues;
3435
import net.openid.appauth.TokenResponse;
3536
import net.openid.appauth.TokenRequest;
@@ -44,6 +45,7 @@ public class RNAppAuthModule extends ReactContextBaseJavaModule implements Activ
4445
private final ReactApplicationContext reactContext;
4546
private Promise promise;
4647
private Boolean dangerouslyAllowInsecureHttpRequests;
48+
private String clientAuthMethod = "basic";
4749
private Map<String, String> authorizationRequestHeaders = null;
4850
private Map<String, String> tokenRequestHeaders = null;
4951
private Map<String, String> additionalParametersMap;
@@ -64,6 +66,7 @@ public void authorize(
6466
final ReadableArray scopes,
6567
final ReadableMap additionalParameters,
6668
final ReadableMap serviceConfiguration,
69+
final String clientAuthMethod,
6770
final Boolean dangerouslyAllowInsecureHttpRequests,
6871
final ReadableMap headers,
6972
final Promise promise
@@ -82,6 +85,7 @@ public void authorize(
8285
this.dangerouslyAllowInsecureHttpRequests = dangerouslyAllowInsecureHttpRequests;
8386
this.additionalParametersMap = additionalParametersMap;
8487
this.clientSecret = clientSecret;
88+
this.clientAuthMethod = clientAuthMethod;
8589

8690
// when serviceConfiguration is provided, we don't need to hit up the OpenID well-known id endpoint
8791
if (serviceConfiguration != null) {
@@ -139,6 +143,7 @@ public void refresh(
139143
final ReadableArray scopes,
140144
final ReadableMap additionalParameters,
141145
final ReadableMap serviceConfiguration,
146+
final String clientAuthMethod,
142147
final Boolean dangerouslyAllowInsecureHttpRequests,
143148
final ReadableMap headers,
144149
final Promise promise
@@ -167,6 +172,7 @@ public void refresh(
167172
scopes,
168173
redirectUrl,
169174
additionalParametersMap,
175+
clientAuthMethod,
170176
clientSecret,
171177
promise
172178
);
@@ -195,6 +201,7 @@ public void onFetchConfigurationCompleted(
195201
scopes,
196202
redirectUrl,
197203
additionalParametersMap,
204+
clientAuthMethod,
198205
clientSecret,
199206
promise
200207
);
@@ -242,7 +249,7 @@ public void onTokenRequestCompleted(
242249
};
243250

244251
if (this.clientSecret != null) {
245-
ClientAuthentication clientAuth = new ClientSecretBasic(this.clientSecret);
252+
ClientAuthentication clientAuth = this.getClientAuthentication(this.clientSecret, this.clientAuthMethod);
246253
authService.performTokenRequest(tokenRequest, clientAuth, tokenResponseCallback);
247254

248255
} else {
@@ -330,6 +337,7 @@ private void refreshWithConfiguration(
330337
final ReadableArray scopes,
331338
final String redirectUrl,
332339
final Map<String, String> additionalParametersMap,
340+
final String clientAuthMethod,
333341
final String clientSecret,
334342
final Promise promise
335343
) {
@@ -376,7 +384,7 @@ public void onTokenRequestCompleted(@Nullable TokenResponse response, @Nullable
376384

377385

378386
if (clientSecret != null) {
379-
ClientAuthentication clientAuth = new ClientSecretBasic(clientSecret);
387+
ClientAuthentication clientAuth = this.getClientAuthentication(clientSecret, clientAuthMethod);
380388
authService.performTokenRequest(tokenRequest, clientAuth, tokenResponseCallback);
381389

382390
} else {
@@ -397,6 +405,14 @@ private void parseHeaderMap (ReadableMap headerMap) {
397405

398406
}
399407

408+
private ClientAuthentication getClientAuthentication(String clientSecret, String clientAuthMethod) {
409+
if (clientAuthMethod.equals("post")) {
410+
return new ClientSecretPost(clientSecret);
411+
}
412+
413+
return new ClientSecretBasic(clientSecret);
414+
}
415+
400416
/*
401417
* Create a space-delimited string from an array
402418
*/

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export type AuthConfiguration = BaseAuthConfiguration & {
3333
scopes: string[];
3434
redirectUrl: string;
3535
additionalParameters?: BuiltInParameters & { [name: string]: string };
36+
clientAuthMethod?: 'basic' | 'post';
3637
dangerouslyAllowInsecureHttpRequests?: boolean;
3738
customHeaders?: CustomHeaders;
3839
useNonce?: boolean;

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export const authorize = ({
5858
usePKCE = true,
5959
additionalParameters,
6060
serviceConfiguration,
61+
clientAuthMethod = 'basic',
6162
dangerouslyAllowInsecureHttpRequests = false,
6263
customHeaders,
6364
}) => {
@@ -78,6 +79,7 @@ export const authorize = ({
7879
];
7980

8081
if (Platform.OS === 'android') {
82+
nativeMethodArguments.push(clientAuthMethod);
8183
nativeMethodArguments.push(dangerouslyAllowInsecureHttpRequests);
8284
nativeMethodArguments.push(customHeaders);
8385
}
@@ -99,6 +101,7 @@ export const refresh = (
99101
scopes,
100102
additionalParameters,
101103
serviceConfiguration,
104+
clientAuthMethod = 'basic',
102105
dangerouslyAllowInsecureHttpRequests = false,
103106
customHeaders,
104107
},
@@ -123,6 +126,7 @@ export const refresh = (
123126
];
124127

125128
if (Platform.OS === 'android') {
129+
nativeMethodArguments.push(clientAuthMethod);
126130
nativeMethodArguments.push(dangerouslyAllowInsecureHttpRequests);
127131
nativeMethodArguments.push(customHeaders);
128132
}

index.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe('AppAuth', () => {
3030
clientId: 'test-clientId',
3131
clientSecret: 'test-clientSecret',
3232
additionalParameters: { hello: 'world' },
33+
clientAuthMethod: 'post',
3334
serviceConfiguration: null,
3435
scopes: ['my-scope'],
3536
useNonce: true,
@@ -164,6 +165,7 @@ describe('AppAuth', () => {
164165
config.scopes,
165166
config.additionalParameters,
166167
config.serviceConfiguration,
168+
config.clientAuthMethod,
167169
false,
168170
config.customHeaders
169171
);
@@ -179,6 +181,7 @@ describe('AppAuth', () => {
179181
config.scopes,
180182
config.additionalParameters,
181183
config.serviceConfiguration,
184+
config.clientAuthMethod,
182185
false,
183186
config.customHeaders
184187
);
@@ -194,6 +197,7 @@ describe('AppAuth', () => {
194197
config.scopes,
195198
config.additionalParameters,
196199
config.serviceConfiguration,
200+
config.clientAuthMethod,
197201
true,
198202
config.customHeaders
199203
);
@@ -213,6 +217,7 @@ describe('AppAuth', () => {
213217
config.scopes,
214218
config.additionalParameters,
215219
config.serviceConfiguration,
220+
config.clientAuthMethod,
216221
false,
217222
customHeaders
218223
);
@@ -305,6 +310,7 @@ describe('AppAuth', () => {
305310
config.scopes,
306311
config.additionalParameters,
307312
config.serviceConfiguration,
313+
config.clientAuthMethod,
308314
false,
309315
config.customHeaders
310316
);
@@ -324,6 +330,7 @@ describe('AppAuth', () => {
324330
config.scopes,
325331
config.additionalParameters,
326332
config.serviceConfiguration,
333+
config.clientAuthMethod,
327334
false,
328335
config.customHeaders
329336
);
@@ -343,6 +350,7 @@ describe('AppAuth', () => {
343350
config.scopes,
344351
config.additionalParameters,
345352
config.serviceConfiguration,
353+
config.clientAuthMethod,
346354
true,
347355
config.customHeaders
348356
);
@@ -362,6 +370,7 @@ describe('AppAuth', () => {
362370
config.scopes,
363371
config.additionalParameters,
364372
config.serviceConfiguration,
373+
config.clientAuthMethod,
365374
false,
366375
customHeaders
367376
);

0 commit comments

Comments
 (0)