Skip to content

Commit

Permalink
[firebase_auth_platform_interface] Add AppleAuthCredential class (fir…
Browse files Browse the repository at this point in the history
…ebase#1549)

* Added Apple auth provider

Co-Authored-By: Collin Jackson <[email protected]>
  • Loading branch information
zariweyo and collinjackson committed Dec 9, 2019
1 parent 901b33c commit a766672
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.0

- Added type `PlatformOAuthCredential` for generic OAuth providers.

## 1.0.0

- Initial open-source release.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,54 @@ class GoogleAuthCredential extends AuthCredential {
};
}

/// An [OAuthCredential] for authenticating via custom providerId.
/// Example: For Apple you can do it with
/// PlatformOAuthCredential _credential = PlatformOAuthCredential(
/// providerId: "apple.com",
/// idToken: appleIdToken,
/// accessToken: appleAccessToken
/// )
/// Optionally you can provide a rawNonce param
/// More info in https://firebase.google.com/docs/auth/ios/apple
class PlatformOAuthCredential extends OAuthCredential {
const PlatformOAuthCredential(
{@required String providerId,
@required String idToken,
String accessToken,
String rawNonce})
: super(providerId, idToken, accessToken, rawNonce);
}

/// Abstract class to implement [OAuthCredential] authentications
abstract class OAuthCredential extends AuthCredential {
/// The ID Token associated with this credential.
final String idToken;

/// The OAuth access token.
final String accessToken;

/// The OAuth raw nonce.
final String rawNonce;

/// The OAuth raw nonce.
final String providerId;

const OAuthCredential(
this.providerId,
this.idToken,
this.accessToken,
this.rawNonce,
) : super(providerId);

@override
Map<String, String> _asMap() => <String, String>{
'idToken': idToken,
'accessToken': accessToken,
'providerId': providerId,
'rawNonce': rawNonce,
};
}

/// An [AuthCredential] for authenticating via facebook.com.
class FacebookAuthCredential extends AuthCredential {
const FacebookAuthCredential({@required this.accessToken})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ author: Flutter Team <[email protected]>
homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_auth/firebase_auth_platform_interface
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 1.0.0
version: 1.1.0

dependencies:
flutter:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,35 @@ void main() {
);
});

test('PlatformOAuthProvider signInWithCredential', () async {
const AuthCredential credential = PlatformOAuthCredential(
providerId: "generic_provider.com",
idToken: kMockIdToken,
accessToken: kMockAccessToken,
);
final PlatformAuthResult result =
await auth.signInWithCredential(appName, credential);
verifyAuthResult(result);
expect(
log,
<Matcher>[
isMethodCall(
'signInWithCredential',
arguments: <String, dynamic>{
'app': appName,
'provider': 'generic_provider.com',
'data': <String, String>{
'idToken': kMockIdToken,
'accessToken': kMockAccessToken,
'providerId': "generic_provider.com",
'rawNonce': null
},
},
),
],
);
});

test('PhoneAuthProvider signInWithCredential', () async {
const AuthCredential credential = PhoneAuthCredential(
verificationId: kMockVerificationId,
Expand Down Expand Up @@ -628,6 +657,35 @@ void main() {
);
});

test('PlatformOAuthProvider reauthenticateWithCredential', () async {
const AuthCredential credential = PlatformOAuthCredential(
providerId: "generic_provider.com",
idToken: kMockIdToken,
accessToken: kMockAccessToken,
);
final PlatformAuthResult result =
await auth.reauthenticateWithCredential(appName, credential);
verifyAuthResult(result);
expect(
log,
<Matcher>[
isMethodCall(
'reauthenticateWithCredential',
arguments: <String, dynamic>{
'app': appName,
'provider': 'generic_provider.com',
'data': <String, String>{
'idToken': kMockIdToken,
'accessToken': kMockAccessToken,
'providerId': "generic_provider.com",
'rawNonce': null
},
},
),
],
);
});

test('FacebookAuthProvider reauthenticateWithCredential', () async {
const AuthCredential credential = FacebookAuthCredential(
accessToken: kMockAccessToken,
Expand Down Expand Up @@ -728,6 +786,35 @@ void main() {
);
});

test('PlatformOAuthProvider linkWithCredential', () async {
const AuthCredential credential = PlatformOAuthCredential(
providerId: "generic_provider.com",
idToken: kMockIdToken,
accessToken: kMockAccessToken,
);
final PlatformAuthResult result =
await auth.linkWithCredential(appName, credential);
verifyAuthResult(result);
expect(
log,
<Matcher>[
isMethodCall(
'linkWithCredential',
arguments: <String, dynamic>{
'app': appName,
'provider': 'generic_provider.com',
'data': <String, String>{
'idToken': kMockIdToken,
'accessToken': kMockAccessToken,
'providerId': "generic_provider.com",
'rawNonce': null
},
},
),
],
);
});

test('FacebookAuthProvider linkWithCredential', () async {
const AuthCredential credential = FacebookAuthCredential(
accessToken: kMockAccessToken,
Expand Down Expand Up @@ -1059,6 +1146,24 @@ void main() {
]);
});

test('PlatformOAuthProvider unlinkFromProvider', () async {
const PlatformOAuthCredential oAuthCredential = PlatformOAuthCredential(
providerId: "generic_provider.com",
idToken: kMockIdToken,
accessToken: kMockAccessToken,
);
await auth.unlinkFromProvider(appName, oAuthCredential.providerId);
expect(log, <Matcher>[
isMethodCall(
'unlinkFromProvider',
arguments: <String, String>{
'app': appName,
'provider': 'generic_provider.com',
},
),
]);
});

test('FacebookAuthProvider unlinkFromProvider', () async {
const FacebookAuthCredential facebookCredential =
FacebookAuthCredential(accessToken: kMockAccessToken);
Expand Down

0 comments on commit a766672

Please sign in to comment.