Skip to content

Commit

Permalink
feat(authentication): return authorizationCode on Apple Sign-In (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz authored Oct 1, 2022
1 parent 24b54be commit 29ac6cc
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-queens-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@capacitor-firebase/authentication": minor
---

feat(ios): return `authorizationCode` on Apple Sign-In
15 changes: 8 additions & 7 deletions packages/authentication/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1267,13 +1267,14 @@ Remove all listeners for this plugin.

#### AuthCredential

| Prop | Type | Description | Since |
| ----------------- | ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | ----- |
| **`providerId`** | <code>string</code> | The authentication provider ID for the credential. | 0.1.0 |
| **`accessToken`** | <code>string</code> | The OAuth access token associated with the credential if it belongs to an OAuth provider. | 0.1.0 |
| **`idToken`** | <code>string</code> | The OAuth ID token associated with the credential if it belongs to an OIDC provider. | 0.1.0 |
| **`secret`** | <code>string</code> | The OAuth access token secret associated with the credential if it belongs to an OAuth 1.0 provider. | 0.1.0 |
| **`nonce`** | <code>string</code> | The random string used to make sure that the ID token you get was granted specifically in response to your app's authentication request. | 0.1.0 |
| Prop | Type | Description | Since |
| ----------------------- | ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | ----- |
| **`accessToken`** | <code>string</code> | The OAuth access token associated with the credential if it belongs to an OAuth provider. | 0.1.0 |
| **`authorizationCode`** | <code>string</code> | A token that the app uses to interact with the server. Only available for Apple Sign-in on iOS. | 1.2.0 |
| **`idToken`** | <code>string</code> | The OAuth ID token associated with the credential if it belongs to an OIDC provider. | 0.1.0 |
| **`nonce`** | <code>string</code> | The random string used to make sure that the ID token you get was granted specifically in response to your app's authentication request. | 0.1.0 |
| **`providerId`** | <code>string</code> | The authentication provider ID for the credential. | 0.1.0 |
| **`secret`** | <code>string</code> | The OAuth access token secret associated with the credential if it belongs to an OAuth 1.0 provider. | 0.1.0 |


#### AdditionalUserInfo
Expand Down
14 changes: 7 additions & 7 deletions packages/authentication/ios/Plugin/FirebaseAuthentication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -401,17 +401,17 @@ public typealias AuthStateChangedObserver = () -> Void
}

func handleSuccessfulSignIn(credential: AuthCredential, idToken: String?, nonce: String?, accessToken: String?) {
self.handleSuccessfulSignIn(credential: credential, idToken: idToken, nonce: nonce, accessToken: accessToken, displayName: nil)
self.handleSuccessfulSignIn(credential: credential, idToken: idToken, nonce: nonce, accessToken: accessToken, displayName: nil, authorizationCode: nil)
}

func handleSuccessfulSignIn(credential: AuthCredential, idToken: String?, nonce: String?, accessToken: String?, displayName: String?) {
func handleSuccessfulSignIn(credential: AuthCredential, idToken: String?, nonce: String?, accessToken: String?, displayName: String?, authorizationCode: String?) {
guard let savedCall = self.savedCall else {
return
}
let skipNativeAuth = savedCall.getBool("skipNativeAuth", config.skipNativeAuth)
if skipNativeAuth == true {
let result = FirebaseAuthenticationHelper.createSignInResult(credential: credential, user: nil, idToken: idToken, nonce: nonce,
accessToken: accessToken, additionalUserInfo: nil, displayName: displayName)
accessToken: accessToken, additionalUserInfo: nil, displayName: displayName, authorizationCode: authorizationCode)
savedCall.resolve(result)
return
}
Expand All @@ -424,7 +424,7 @@ public typealias AuthStateChangedObserver = () -> Void
return
}
let result = FirebaseAuthenticationHelper.createSignInResult(credential: authResult?.credential, user: authResult?.user, idToken: idToken, nonce: nonce, accessToken: accessToken,
additionalUserInfo: authResult?.additionalUserInfo, displayName: displayName)
additionalUserInfo: authResult?.additionalUserInfo, displayName: displayName, authorizationCode: authorizationCode)
savedCall.resolve(result)
}
}
Expand All @@ -438,10 +438,10 @@ public typealias AuthStateChangedObserver = () -> Void
}

func handleSuccessfulLink(credential: AuthCredential, idToken: String?, nonce: String?, accessToken: String?) {
self.handleSuccessfulLink(credential: credential, idToken: idToken, nonce: nonce, accessToken: accessToken, displayName: nil)
self.handleSuccessfulLink(credential: credential, idToken: idToken, nonce: nonce, accessToken: accessToken, displayName: nil, authorizationCode: nil)
}

func handleSuccessfulLink(credential: AuthCredential, idToken: String?, nonce: String?, accessToken: String?, displayName: String?) {
func handleSuccessfulLink(credential: AuthCredential, idToken: String?, nonce: String?, accessToken: String?, displayName: String?, authorizationCode: String?) {
guard let user = getCurrentUser() else {
self.handleFailedLink(message: plugin.errorNoUserSignedIn, error: nil)
return
Expand All @@ -455,7 +455,7 @@ public typealias AuthStateChangedObserver = () -> Void
return
}
let result = FirebaseAuthenticationHelper.createSignInResult(credential: authResult?.credential, user: authResult?.user, idToken: idToken, nonce: nonce, accessToken: accessToken,
additionalUserInfo: authResult?.additionalUserInfo, displayName: displayName)
additionalUserInfo: authResult?.additionalUserInfo, displayName: displayName, authorizationCode: authorizationCode)
savedCall.resolve(result)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ public class FirebaseAuthenticationHelper {
}

public static func createSignInResult(credential: AuthCredential?, user: User?, idToken: String?, nonce: String?, accessToken: String?, additionalUserInfo: AdditionalUserInfo?, displayName: String?) -> JSObject {
return createSignInResult(credential: credential, user: user, idToken: idToken, nonce: nonce, accessToken: accessToken, additionalUserInfo: additionalUserInfo, displayName: nil,
authorizationCode: nil)
}

public static func createSignInResult(credential: AuthCredential?, user: User?, idToken: String?, nonce: String?, accessToken: String?, additionalUserInfo: AdditionalUserInfo?, displayName: String?, authorizationCode: String?) -> JSObject {
let userResult = self.createUserResult(user, displayName: displayName)
let credentialResult = self.createCredentialResult(credential, idToken: idToken, nonce: nonce, accessToken: accessToken)
let credentialResult = self.createCredentialResult(credential, idToken: idToken, nonce: nonce, accessToken: accessToken, authorizationCode: authorizationCode)
let additionalUserInfoResult = self.createAdditionalUserInfoResult(additionalUserInfo)
var result = JSObject()
result["user"] = userResult
Expand Down Expand Up @@ -59,8 +64,8 @@ public class FirebaseAuthenticationHelper {
return result
}

public static func createCredentialResult(_ credential: AuthCredential?, idToken: String?, nonce: String?, accessToken: String?) -> JSObject? {
if credential == nil && idToken == nil && nonce == nil && accessToken == nil {
public static func createCredentialResult(_ credential: AuthCredential?, idToken: String?, nonce: String?, accessToken: String?, authorizationCode: String?) -> JSObject? {
if credential == nil && idToken == nil && nonce == nil && accessToken == nil && authorizationCode == nil {
return nil
}
var result = JSObject()
Expand Down Expand Up @@ -90,6 +95,9 @@ public class FirebaseAuthenticationHelper {
if let accessToken = accessToken {
result["accessToken"] = accessToken
}
if let authorizationCode = authorizationCode {
result["authorizationCode"] = authorizationCode
}
return result
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ extension AppleAuthProviderHandler: ASAuthorizationControllerDelegate, ASAuthori
print("Unable to fetch identity token")
return
}
var authorizationCode: String?
if let authorizationCodeData = appleIDCredential.authorizationCode {
authorizationCode = String(data: authorizationCodeData, encoding: .utf8)
}
guard let idTokenString = String(data: appleIDToken, encoding: .utf8) else {
print("Unable to serialize token string from data: \(appleIDToken.debugDescription)")
return
Expand All @@ -125,9 +129,11 @@ extension AppleAuthProviderHandler: ASAuthorizationControllerDelegate, ASAuthori
return
}
if isLink == true {
self.pluginImplementation.handleSuccessfulLink(credential: credential, idToken: idTokenString, nonce: nonce, accessToken: nil, displayName: displayName)
self.pluginImplementation.handleSuccessfulLink(credential: credential, idToken: idTokenString, nonce: nonce,
accessToken: nil, displayName: displayName, authorizationCode: authorizationCode)
} else {
self.pluginImplementation.handleSuccessfulSignIn(credential: credential, idToken: idTokenString, nonce: nonce, accessToken: nil, displayName: displayName)
self.pluginImplementation.handleSuccessfulSignIn(credential: credential, idToken: idTokenString, nonce: nonce,
accessToken: nil, displayName: displayName, authorizationCode: authorizationCode)
}
}

Expand Down
28 changes: 18 additions & 10 deletions packages/authentication/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -922,36 +922,44 @@ export interface User {
*/
export interface AuthCredential {
/**
* The authentication provider ID for the credential.
* The OAuth access token associated with the credential if it belongs to an OAuth provider.
*
* @example "google.com"
* @since 0.1.0
*/
providerId: string;
accessToken?: string;
/**
* The OAuth access token associated with the credential if it belongs to an OAuth provider.
* A token that the app uses to interact with the server.
*
* @since 0.1.0
* Only available for Apple Sign-in on iOS.
*
* @since 1.2.0
*/
accessToken?: string;
authorizationCode?: string;
/**
* The OAuth ID token associated with the credential if it belongs to an OIDC provider.
*
* @since 0.1.0
*/
idToken?: string;
/**
* The OAuth access token secret associated with the credential if it belongs to an OAuth 1.0 provider.
* The random string used to make sure that the ID token you get was granted specifically in response to your app's authentication request.
*
* @since 0.1.0
*/
secret?: string;
nonce?: string;
/**
* The random string used to make sure that the ID token you get was granted specifically in response to your app's authentication request.
* The authentication provider ID for the credential.
*
* @example "google.com"
* @since 0.1.0
*/
nonce?: string;
providerId: string;
/**
* The OAuth access token secret associated with the credential if it belongs to an OAuth 1.0 provider.
*
* @since 0.1.0
*/
secret?: string;
}

/**
Expand Down

0 comments on commit 29ac6cc

Please sign in to comment.