-
-
Notifications
You must be signed in to change notification settings - Fork 4
Implement Native Google OAuth for Apple targets #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
275c78b
Set up spm4kmp
hieuwu 1004420
Implement google auth in swift
hieuwu 7532483
Call swift code in google auth
hieuwu 70d25bc
Add flag for spm4kmp
hieuwu 3311ef2
Bump kotlin for xcode 26 compatibility
hieuwu 4b2f67c
Upgrade gradle & move swift code
hieuwu aecbd94
Rename exported ios package
hieuwu b98c3b3
Revert rename
hieuwu 136d7d3
Revert Add file for package
hieuwu de94221
Remove unused imports and add dep
hieuwu 4b8374a
Update README.md
hieuwu ab47a30
Update README with GoogleSignIn integration steps
hieuwu e6c9188
Update README.md
hieuwu bb77754
Update README.md
hieuwu 49e4103
Update supabase-version to 3.2.7-SNAPSHOT
jan-tennert 55fc3d8
Merge branch 'main' into main
jan-tennert 70531ed
Use nonce for SignIn
hieuwu c5bde0f
Merge remote-tracking branch 'origin/main'
hieuwu 2ac027e
Remove unnecessary nonce extraction and hash nonce for now
hieuwu 320115b
More expressive logs
hieuwu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // swift-tools-version: 5.9 | ||
| import PackageDescription | ||
|
|
||
| let package = Package( | ||
| name: "exportedNativeBridge", | ||
| platforms: [.iOS("12.0"), .macOS("10.13"), .tvOS("12.0"), .watchOS("4.0")], | ||
| products: [ | ||
| .library( | ||
| name: "exportedNativeBridge", | ||
| type: .static, | ||
| targets: ["exportedNativeBridge"]) | ||
| ], | ||
| dependencies: [ | ||
| .package(url: "https://github.com/google/GoogleSignIn-iOS.git", exact: "9.0.0") | ||
| ], | ||
| targets: [ | ||
| .target( | ||
| name: "exportedNativeBridge", | ||
| dependencies: [ | ||
| .product(name: "GoogleSignIn", package: "GoogleSignIn-iOS") | ||
| ], | ||
| path: "Sources" | ||
|
|
||
| ) | ||
|
|
||
| ] | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // This file has been generated by Spm4Kmp plugin | ||
| // DO NO EDIT THIS FILE AS IT WILL BE OVERWRITTEN ON EACH BUILD | ||
| import Foundation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
ComposeAuth/src/swift/nativeBridge/GoogleSignInController.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import Foundation | ||
| import GoogleSignIn | ||
| import UIKit // Needed for UIViewController | ||
|
|
||
| // Define a public typealias for the completion handler closure | ||
| public typealias GoogleSignInCompletionHandler = (String?, String?, Bool) -> Void | ||
|
|
||
| @objcMembers public class GoogleSignInController: NSObject { | ||
|
|
||
| public override init() { | ||
| super.init() | ||
| } | ||
|
|
||
| public func signIn( | ||
| completion: @escaping GoogleSignInCompletionHandler, | ||
| nonce: String? = nil | ||
| ) { | ||
| guard let presentingViewController = UIApplication.shared.keyWindow?.rootViewController else { | ||
| completion(nil, "No root view controller found", false) | ||
| return | ||
| } | ||
| GoogleSignIn.GIDSignIn.sharedInstance.signIn( | ||
| withPresenting: presentingViewController, | ||
| hint: nil, | ||
| additionalScopes: nil, | ||
| nonce: nonce | ||
| ) { result, error in | ||
| if let error = error { | ||
| if let nsError = error as NSError?, nsError.code == -5 { | ||
| completion(nil, nil, true) | ||
| } else { | ||
| completion(nil, error.localizedDescription, false) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| guard let idToken = result?.user.idToken?.tokenString else { | ||
| completion(nil, "No ID token returned", false) | ||
| return | ||
| } | ||
|
|
||
| if let nonceFromToken = self.decodeNonce(fromJWT: idToken) { | ||
| if nonceFromToken != nonce { | ||
| print("Nonce from idToken does not match, random nonce is used from AppAuth") | ||
| completion(idToken, nil, false) | ||
| return | ||
| } | ||
| } else { | ||
| completion(nil, "Unknown sign-in error or no ID token", false) | ||
| } | ||
hieuwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| completion(idToken, nil, false) | ||
| } | ||
| } | ||
|
|
||
| @objc public static func signOutGoogle() { | ||
| GoogleSignIn.GIDSignIn.sharedInstance.signOut() | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Private JWT Decoding Helpers | ||
| private extension GoogleSignInController { | ||
|
|
||
| func decodeNonce(fromJWT jwt: String) -> String? { | ||
| let segments = jwt.components(separatedBy: ".") | ||
| guard segments.count > 1 else { return nil } | ||
| return decodeJWTSegment(segments[1])?["nonce"] as? String | ||
| } | ||
|
|
||
| func decodeJWTSegment(_ segment: String) -> [String: Any]? { | ||
| guard let data = base64UrlDecode(segment), | ||
| let json = try? JSONSerialization.jsonObject(with: data, options: []), | ||
| let payload = json as? [String: Any] else { | ||
| return nil | ||
| } | ||
| return payload | ||
| } | ||
|
|
||
| func base64UrlDecode(_ value: String) -> Data? { | ||
| var base64 = value | ||
| .replacingOccurrences(of: "-", with: "+") | ||
| .replacingOccurrences(of: "_", with: "/") | ||
|
|
||
| let length = Double(base64.lengthOfBytes(using: .utf8)) | ||
| let requiredLength = 4 * ceil(length / 4.0) | ||
| let paddingLength = requiredLength - length | ||
| if paddingLength > 0 { | ||
| let padding = String(repeating: "=", count: Int(paddingLength)) | ||
| base64 += padding | ||
| } | ||
| return Data(base64Encoded: base64, options: .ignoreUnknownCharacters) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.