Skip to content

Commit

Permalink
Merge pull request #64 from aboutyou/feature/add-nonce-support
Browse files Browse the repository at this point in the history
Add nonce support
  • Loading branch information
Henri authored Apr 28, 2020
2 parents 5e700ce + a1f6f42 commit a25c3ef
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/sign_in_with_apple/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 2.1.0

- Expose `identityToken` to enable Firebase integration (https://github.com/aboutyou/dart_packages/issues/62)
- Add support for passing a `nonce` to the authentication request

## 2.0.0+5

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ class SignInWithAppleAuthorizationController: NSObject, ASAuthorizationControlle
let appleIDProvider = ASAuthorizationAppleIDProvider()
let appleIDRequest = appleIDProvider.createRequest()

if let nonce = requestMap["nonce"] as? String {
appleIDRequest.nonce = nonce;
}

if let scopes = requestMap["scopes"] as? [String] {
appleIDRequest.requestedScopes = []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ enum AppleIDAuthorizationScopes {
class AppleIDAuthorizationRequest implements AuthorizationRequest {
const AppleIDAuthorizationRequest({
this.scopes = const [],
this.nonce,
}) : assert(scopes != null);

/// A list of scopes that can be requested from the user.
Expand All @@ -54,13 +55,20 @@ class AppleIDAuthorizationRequest implements AuthorizationRequest {
/// For more information see: https://forums.developer.apple.com/thread/121496
final List<AppleIDAuthorizationScopes> scopes;

/// The nonce value which was provided when initiating the sign-in.
///
/// Can be `null` if no value was given on the request.
final String nonce;

@override
String toString() => 'AppleIDAuthorizationRequest(scopes: $scopes)';

@override
Map<String, dynamic> toJson() {
return <String, dynamic>{
'type': 'appleid',
if (nonce != null)
'nonce': nonce,
'scopes': [
for (final scope in scopes)
if (scope == AppleIDAuthorizationScopes.email)
Expand Down
19 changes: 17 additions & 2 deletions packages/sign_in_with_apple/lib/src/sign_in_with_apple.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ class SignInWithApple {
///
/// This parameter is required on Android.
WebAuthenticationOptions webAuthenticationOptions,

/// Optional string which, if set, will be be embedded in the resulting `identityToken` field on the [AuthorizationCredentialAppleID].
///
/// This can be used to mitigate replay attacks by using a unique argument per sign-in attempt.
///
/// Can be `null`, in which case no nonce will be passed to the request.
String nonce,
}) async {
assert(scopes != null);

Expand All @@ -95,6 +102,7 @@ class SignInWithApple {
return _signInWithAppleAndroid(
scopes: scopes,
webAuthenticationOptions: webAuthenticationOptions,
nonce: nonce,
);
}

Expand All @@ -111,8 +119,11 @@ class SignInWithApple {
await channel.invokeMethod<Map<dynamic, dynamic>>(
'performAuthorizationRequest',
[
AppleIDAuthorizationRequest(scopes: scopes),
].map((request) => request.toJson()).toList(),
AppleIDAuthorizationRequest(
scopes: scopes,
nonce: nonce,
).toJson(),
],
),
);
} on PlatformException catch (exception) {
Expand Down Expand Up @@ -172,6 +183,7 @@ class SignInWithApple {
static Future<AuthorizationCredentialAppleID> _signInWithAppleAndroid({
@required List<AppleIDAuthorizationScopes> scopes,
@required WebAuthenticationOptions webAuthenticationOptions,
@required String nonce,
}) async {
assert(Platform.isAndroid);

Expand Down Expand Up @@ -199,6 +211,9 @@ class SignInWithApple {
// So the same handling can be used for Apple and 3rd party platforms
'response_type': 'code id_token',
'response_mode': 'form_post',

if (nonce != null)
'nonce': nonce,
},
).toString();

Expand Down

0 comments on commit a25c3ef

Please sign in to comment.