Skip to content

Commit 6cf59be

Browse files
authored
Bugfix/nonce touchup (#231)
* Revet sha265 -> sha256 * Wrap app in a SafeAreaView * Add useNonce param at the end of the the args instead of in the middle * Add a test for the nonce parameter * Add typescript definition for useNonce * Update readme to add useNonce
1 parent a83ce89 commit 6cf59be

File tree

9 files changed

+99
-19
lines changed

9 files changed

+99
-19
lines changed
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
// @flow
22

3+
import React from 'react';
34
import styled from 'styled-components/native';
45

5-
export default styled.ImageBackground.attrs({
6-
source: require('../assets/background.jpg')
6+
const SafeArea = styled.SafeAreaView`
7+
flex: 1;
8+
`;
9+
10+
const Background = styled.ImageBackground.attrs({
11+
source: require('../assets/background.jpg'),
712
})`
813
flex: 1;
914
background-color: white;
1015
padding: 40px 10px 10px 10px;
1116
`;
17+
18+
export default ({ children }) => (
19+
<Background>
20+
<SafeArea>{children}</SafeArea>
21+
</Background>
22+
);

Example/Latest/components/Page.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
// @flow
22

3+
import React from 'react';
34
import styled from 'styled-components/native';
45

5-
export default styled.ImageBackground.attrs({
6+
const SafeArea = styled.SafeAreaView`
7+
flex: 1;
8+
`;
9+
10+
const Background = styled.ImageBackground.attrs({
611
source: require('../assets/background.jpg')
712
})`
813
flex: 1;
914
background-color: white;
1015
padding: 40px 10px 10px 10px;
1116
`;
17+
18+
export default ({ children }) => (
19+
<Background>
20+
<SafeArea>{children}</SafeArea>
21+
</Background>
22+
);
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
// @flow
22

3+
import React from 'react';
34
import styled from 'styled-components/native';
45

5-
export default styled.ImageBackground.attrs({
6-
source: require('../assets/background.jpg')
6+
const SafeArea = styled.SafeAreaView`
7+
flex: 1;
8+
`;
9+
10+
const Background = styled.ImageBackground.attrs({
11+
source: require('../assets/background.jpg'),
712
})`
813
flex: 1;
914
background-color: white;
1015
padding: 40px 10px 10px 10px;
1116
`;
17+
18+
export default ({ children }) => (
19+
<Background>
20+
<SafeArea>{children}</SafeArea>
21+
</Background>
22+
);
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
// @flow
22

3+
import React from 'react';
34
import styled from 'styled-components/native';
45

5-
export default styled.ImageBackground.attrs({
6-
source: require('../assets/background.jpg')
6+
const SafeArea = styled.SafeAreaView`
7+
flex: 1;
8+
`;
9+
10+
const Background = styled.ImageBackground.attrs({
11+
source: require('../assets/background.jpg'),
712
})`
813
flex: 1;
914
background-color: white;
1015
padding: 40px 10px 10px 10px;
1116
`;
17+
18+
export default ({ children }) => (
19+
<Background>
20+
<SafeArea>{children}</SafeArea>
21+
</Background>
22+
);

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
Past documentation: [`3.1`](https://github.com/FormidableLabs/react-native-app-auth/tree/v3.1.0) [`3.0`](https://github.com/FormidableLabs/react-native-app-auth/tree/v3.0.0) [`2.x`](https://github.com/FormidableLabs/react-native-app-auth/tree/v2.0.0) [`1.x`](https://github.com/FormidableLabs/react-native-app-auth/tree/v1.0.1).
1212

13-
1413
React Native bridge for [AppAuth-iOS](https://github.com/openid/AppAuth-iOS) and
1514
[AppAuth-Android](https://github.com/openid/AppAuth-Android) SDKS for communicating with
1615
[OAuth 2.0](https://tools.ietf.org/html/rfc6749) and
@@ -97,6 +96,7 @@ with optional overrides.
9796
Must be string values! E.g. setting `additionalParameters: { hello: 'world', foo: 'bar' }` would add
9897
`hello=world&foo=bar` to the authorization request.
9998
* **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.
99+
* **useNonce** - (`boolean`) _IOS_ (default: true) optionally allows not sending the nonce parameter, to support non-compliant providers
100100

101101
#### result
102102

@@ -682,12 +682,12 @@ First, set up a your user pool in [the AWS console](https://eu-west-1.console.aw
682682
Now you need to set up your domain name. This will be on the left menu in your pool details page, under App Integration -> Domain Name. What this is depends on your preference. E.g. for AppAuth demo, mine is `https://app-auth-test.auth.eu-west-1.amazoncognito.com` as I chose `app-auth-test` as the domain and `eu-west-1` as the region.
683683

684684
Finally, you need to configure your app client. Go to App Integration -> App Client Settings.
685+
685686
1. Enable your newly created user pool under Enabled Identity Providers.
686687
2. Add the callback url (must be same as in your config, e.g. `com.myclientapp://myclient/redirect`)
687688
3. Enable the Authorization code grant
688689
4. Enable openid scope
689690

690-
691691
```js
692692
const config = {
693693
clientId: '<YOUR_CLIENT_ID>',

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export type AuthConfiguration = BaseAuthConfiguration & {
2929
redirectUrl: string;
3030
additionalParameters?: BuiltInParameters & { [name: string]: string };
3131
dangerouslyAllowInsecureHttpRequests?: boolean;
32+
useNonce?: boolean;
3233
};
3334

3435
export interface AuthorizeResult {

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ export const authorize = ({
4747
additionalParameters,
4848
serviceConfiguration,
4949
];
50+
5051
if (Platform.OS === 'android') {
5152
nativeMethodArguments.push(dangerouslyAllowInsecureHttpRequests);
52-
} else {
53-
// add a new useNonce param on iOS to support making it optional
54-
const nonceParamIndex = 5;
55-
nativeMethodArguments.splice(nonceParamIndex, 0, useNonce);
53+
}
54+
55+
if (Platform.OS === 'ios') {
56+
nativeMethodArguments.push(useNonce);
5657
}
5758

5859
return RNAppAuth.authorize(...nativeMethodArguments);

index.spec.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ describe('AppAuth', () => {
8787
config.clientId,
8888
config.clientSecret,
8989
config.scopes,
90-
config.useNonce,
9190
config.additionalParameters,
92-
config.serviceConfiguration
91+
config.serviceConfiguration,
92+
config.useNonce
9393
);
9494
});
9595

@@ -270,5 +270,39 @@ describe('AppAuth', () => {
270270
);
271271
});
272272
});
273+
274+
describe('iOS-specific useNonce parameter', () => {
275+
beforeEach(() => {
276+
require('react-native').Platform.OS = 'ios';
277+
});
278+
279+
it('calls the native wrapper with default value `true`', () => {
280+
authorize(config, { refreshToken: 'such-token' });
281+
expect(mockAuthorize).toHaveBeenCalledWith(
282+
config.issuer,
283+
config.redirectUrl,
284+
config.clientId,
285+
config.clientSecret,
286+
config.scopes,
287+
config.additionalParameters,
288+
config.serviceConfiguration,
289+
true
290+
);
291+
});
292+
293+
it('calls the native wrapper with passed value `false`', () => {
294+
authorize({ ...config, useNonce: false }, { refreshToken: 'such-token' });
295+
expect(mockAuthorize).toHaveBeenCalledWith(
296+
config.issuer,
297+
config.redirectUrl,
298+
config.clientId,
299+
config.clientSecret,
300+
config.scopes,
301+
config.additionalParameters,
302+
config.serviceConfiguration,
303+
false
304+
);
305+
});
306+
});
273307
});
274308
});

ios/RNAppAuth.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ - (dispatch_queue_t)methodQueue
3636
clientId: (NSString *) clientId
3737
clientSecret: (NSString *) clientSecret
3838
scopes: (NSArray *) scopes
39-
useNonce: (BOOL *) useNonce
4039
additionalParameters: (NSDictionary *_Nullable) additionalParameters
4140
serviceConfiguration: (NSDictionary *_Nullable) serviceConfiguration
41+
useNonce: (BOOL *) useNonce
4242
resolve: (RCTPromiseResolveBlock) resolve
4343
reject: (RCTPromiseRejectBlock) reject)
4444
{
@@ -152,8 +152,8 @@ + (nullable NSString *)codeChallengeS256ForVerifier:(NSString *)codeVerifier {
152152
// generates the code_challenge per spec https://tools.ietf.org/html/rfc7636#section-4.2
153153
// code_challenge = BASE64URL-ENCODE(SHA256(ASCII(code_verifier)))
154154
// NB. the ASCII conversion on the code_verifier entropy was done at time of generation.
155-
NSData *sha256Verifier = [OIDTokenUtilities sha256:codeVerifier];
156-
return [OIDTokenUtilities encodeBase64urlNoPadding:sha256Verifier];
155+
NSData *sha265Verifier = [OIDTokenUtilities sha265:codeVerifier];
156+
return [OIDTokenUtilities encodeBase64urlNoPadding:sha265Verifier];
157157
}
158158

159159
/*
@@ -277,7 +277,7 @@ - (NSDictionary*)formatResponse: (OIDTokenResponse*) response
277277
dateFormat.timeZone = [NSTimeZone timeZoneWithAbbreviation: @"UTC"];
278278
[dateFormat setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
279279
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
280-
280+
281281
return @{@"accessToken": response.accessToken ? response.accessToken : @"",
282282
@"accessTokenExpirationDate": response.accessTokenExpirationDate ? [dateFormat stringFromDate:response.accessTokenExpirationDate] : @"",
283283
@"additionalParameters": params,

0 commit comments

Comments
 (0)