-
-
Notifications
You must be signed in to change notification settings - Fork 79
feat: Add PhantAuth authentication adapter #474
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
Open
DENGXUELIN
wants to merge
2
commits into
parse-community:main
Choose a base branch
from
DENGXUELIN:add-phantauth-authentication
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
87 changes: 87 additions & 0 deletions
87
Sources/ParseSwift/Authentication/3rd Party/ParsePhantAuth/ParsePhantAuth+async.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,87 @@ | ||
| // | ||
| // ParsePhantAuth+async.swift | ||
| // ParseSwift | ||
| // | ||
| // Created by DENGXUELIN on 05/13/26. | ||
| // Copyright © 2022 Parse Community. All rights reserved. | ||
| // | ||
|
|
||
| #if compiler(>=5.5.2) && canImport(_Concurrency) | ||
| import Foundation | ||
|
|
||
| public extension ParsePhantAuth { | ||
| // MARK: Async/Await | ||
|
|
||
| /** | ||
| Login a `ParseUser` *asynchronously* using PhantAuth authentication. | ||
| - parameter id: The **PhantAuth profile id** from **PhantAuth**. | ||
| - parameter accessToken: Required **access_token** from **PhantAuth**. | ||
| - parameter options: A set of header options sent to the server. Defaults to an empty set. | ||
| - returns: An instance of the logged in `ParseUser`. | ||
| - throws: An error of type `ParseError`. | ||
| */ | ||
| func login(id: String, | ||
| accessToken: String, | ||
| options: API.Options = []) async throws -> AuthenticatedUser { | ||
| try await withCheckedThrowingContinuation { continuation in | ||
| self.login(id: id, | ||
| accessToken: accessToken, | ||
| options: options, | ||
| completion: continuation.resume(with:)) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| Login a `ParseUser` *asynchronously* using PhantAuth authentication. | ||
| - parameter authData: Dictionary containing key/values. | ||
| - returns: An instance of the logged in `ParseUser`. | ||
| - throws: An error of type `ParseError`. | ||
| */ | ||
| func login(authData: [String: String], | ||
| options: API.Options = []) async throws -> AuthenticatedUser { | ||
| try await withCheckedThrowingContinuation { continuation in | ||
| self.login(authData: authData, | ||
| options: options, | ||
| completion: continuation.resume(with:)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public extension ParsePhantAuth { | ||
|
|
||
| /** | ||
| Link the *current* `ParseUser` *asynchronously* using PhantAuth authentication. | ||
| - parameter id: The **PhantAuth profile id** from **PhantAuth**. | ||
| - parameter accessToken: Required **access_token** from **PhantAuth**. | ||
| - parameter options: A set of header options sent to the server. Defaults to an empty set. | ||
| - returns: An instance of the logged in `ParseUser`. | ||
| - throws: An error of type `ParseError`. | ||
| */ | ||
| func link(id: String, | ||
| accessToken: String, | ||
| options: API.Options = []) async throws -> AuthenticatedUser { | ||
| try await withCheckedThrowingContinuation { continuation in | ||
| self.link(id: id, | ||
| accessToken: accessToken, | ||
| options: options, | ||
| completion: continuation.resume(with:)) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| Link the *current* `ParseUser` *asynchronously* using PhantAuth authentication. | ||
| - parameter authData: Dictionary containing key/values. | ||
| - parameter options: A set of header options sent to the server. Defaults to an empty set. | ||
| - returns: An instance of the logged in `ParseUser`. | ||
| - throws: An error of type `ParseError`. | ||
| */ | ||
| func link(authData: [String: String], | ||
| options: API.Options = []) async throws -> AuthenticatedUser { | ||
| try await withCheckedThrowingContinuation { continuation in | ||
| self.link(authData: authData, | ||
| options: options, | ||
| completion: continuation.resume(with:)) | ||
| } | ||
| } | ||
| } | ||
| #endif |
84 changes: 84 additions & 0 deletions
84
Sources/ParseSwift/Authentication/3rd Party/ParsePhantAuth/ParsePhantAuth+combine.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,84 @@ | ||
| // | ||
| // ParsePhantAuth+combine.swift | ||
| // ParseSwift | ||
| // | ||
| // Created by DENGXUELIN on 05/13/26. | ||
| // Copyright © 2022 Parse Community. All rights reserved. | ||
| // | ||
|
|
||
| #if canImport(Combine) | ||
| import Foundation | ||
| import Combine | ||
|
|
||
| public extension ParsePhantAuth { | ||
| // MARK: Combine | ||
| /** | ||
| Login a `ParseUser` *asynchronously* using PhantAuth authentication. Publishes when complete. | ||
| - parameter id: The **PhantAuth profile id** from **PhantAuth**. | ||
| - parameter accessToken: Required **access_token** from **PhantAuth**. | ||
| - parameter options: A set of header options sent to the server. Defaults to an empty set. | ||
| - returns: A publisher that eventually produces a single value and then finishes or fails. | ||
| */ | ||
| func loginPublisher(id: String, | ||
| accessToken: String, | ||
| options: API.Options = []) -> Future<AuthenticatedUser, ParseError> { | ||
| Future { promise in | ||
| self.login(id: id, | ||
| accessToken: accessToken, | ||
| options: options, | ||
| completion: promise) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| Login a `ParseUser` *asynchronously* using PhantAuth authentication. Publishes when complete. | ||
| - parameter authData: Dictionary containing key/values. | ||
| - returns: A publisher that eventually produces a single value and then finishes or fails. | ||
| */ | ||
| func loginPublisher(authData: [String: String], | ||
| options: API.Options = []) -> Future<AuthenticatedUser, ParseError> { | ||
| Future { promise in | ||
| self.login(authData: authData, | ||
| options: options, | ||
| completion: promise) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public extension ParsePhantAuth { | ||
| /** | ||
| Link the *current* `ParseUser` *asynchronously* using PhantAuth authentication. | ||
| Publishes when complete. | ||
| - parameter id: The **PhantAuth profile id** from **PhantAuth**. | ||
| - parameter accessToken: Required **access_token** from **PhantAuth**. | ||
| - parameter options: A set of header options sent to the server. Defaults to an empty set. | ||
| - returns: A publisher that eventually produces a single value and then finishes or fails. | ||
| */ | ||
| func linkPublisher(id: String, | ||
| accessToken: String, | ||
| options: API.Options = []) -> Future<AuthenticatedUser, ParseError> { | ||
| Future { promise in | ||
| self.link(id: id, | ||
| accessToken: accessToken, | ||
| options: options, | ||
| completion: promise) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| Link the *current* `ParseUser` *asynchronously* using PhantAuth authentication. | ||
| Publishes when complete. | ||
| - parameter authData: Dictionary containing key/values. | ||
| - returns: A publisher that eventually produces a single value and then finishes or fails. | ||
| */ | ||
| func linkPublisher(authData: [String: String], | ||
| options: API.Options = []) -> Future<AuthenticatedUser, ParseError> { | ||
| Future { promise in | ||
| self.link(authData: authData, | ||
| options: options, | ||
| completion: promise) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #endif |
157 changes: 157 additions & 0 deletions
157
Sources/ParseSwift/Authentication/3rd Party/ParsePhantAuth/ParsePhantAuth.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,157 @@ | ||
| // | ||
| // ParsePhantAuth.swift | ||
| // ParseSwift | ||
| // | ||
| // Created by DENGXUELIN on 05/13/26. | ||
| // Copyright © 2022 Parse Community. All rights reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| // swiftlint:disable line_length | ||
|
|
||
| /** | ||
| Provides utility functions for working with PhantAuth User Authentication and `ParseUser`'s. | ||
| Be sure your Parse Server is configured for [sign in with PhantAuth](https://docs.parseplatform.org/parse-server/guide/#phantauth-authdata) | ||
| For information on PhantAuth test identities, refer to [PhantAuth's Documentation](https://www.phantauth.net) | ||
| */ | ||
| public struct ParsePhantAuth<AuthenticatedUser: ParseUser>: ParseAuthentication { | ||
|
|
||
| /// Authentication keys required for PhantAuth authentication. | ||
| enum AuthenticationKeys: String, Codable { | ||
| case id | ||
| case accessToken = "access_token" | ||
|
|
||
| /// Properly makes an authData dictionary with the required keys. | ||
| /// - parameter id: Required id for the user. | ||
| /// - parameter accessToken: Required access token for PhantAuth. | ||
| /// - returns: authData dictionary. | ||
| func makeDictionary(id: String, | ||
| accessToken: String) -> [String: String] { | ||
| [ | ||
| AuthenticationKeys.id.rawValue: id, | ||
| AuthenticationKeys.accessToken.rawValue: accessToken | ||
| ] | ||
| } | ||
|
|
||
| /// Verifies all mandatory keys are in authData. | ||
| /// - parameter authData: Dictionary containing key/values. | ||
| /// - returns: **true** if all the mandatory keys are present, **false** otherwise. | ||
| func verifyMandatoryKeys(authData: [String: String]) -> Bool { | ||
| guard authData[AuthenticationKeys.id.rawValue] != nil, | ||
| authData[AuthenticationKeys.accessToken.rawValue] != nil else { | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| public static var __type: String { // swiftlint:disable:this identifier_name | ||
| "phantauth" | ||
| } | ||
|
|
||
| public init() { } | ||
| } | ||
|
|
||
| // MARK: Login | ||
| public extension ParsePhantAuth { | ||
|
|
||
| /** | ||
| Login a `ParseUser` *asynchronously* using PhantAuth authentication. | ||
| - parameter id: The **PhantAuth profile id** from **PhantAuth**. | ||
| - parameter accessToken: Required **access_token** from **PhantAuth**. | ||
| - parameter options: A set of header options sent to the server. Defaults to an empty set. | ||
| - parameter callbackQueue: The queue to return to after completion. Default value of .main. | ||
| - parameter completion: The block to execute. | ||
| */ | ||
| func login(id: String, | ||
| accessToken: String, | ||
| options: API.Options = [], | ||
| callbackQueue: DispatchQueue = .main, | ||
| completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) { | ||
|
|
||
| let phantauthAuthData = AuthenticationKeys.id | ||
| .makeDictionary(id: id, | ||
| accessToken: accessToken) | ||
| login(authData: phantauthAuthData, | ||
| options: options, | ||
| callbackQueue: callbackQueue, | ||
| completion: completion) | ||
| } | ||
|
|
||
| func login(authData: [String: String], | ||
| options: API.Options = [], | ||
| callbackQueue: DispatchQueue = .main, | ||
| completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) { | ||
| guard AuthenticationKeys.id.verifyMandatoryKeys(authData: authData) else { | ||
| callbackQueue.async { | ||
| completion(.failure(.init(code: .unknownError, | ||
| message: "Should have authData consisting of keys \"id\" and \"access_token\"."))) | ||
| } | ||
| return | ||
| } | ||
| AuthenticatedUser.login(Self.__type, | ||
| authData: authData, | ||
| options: options, | ||
| callbackQueue: callbackQueue, | ||
| completion: completion) | ||
| } | ||
| } | ||
|
|
||
| // MARK: Link | ||
| public extension ParsePhantAuth { | ||
|
|
||
| /** | ||
| Link the *current* `ParseUser` *asynchronously* using PhantAuth authentication. | ||
| - parameter id: The **PhantAuth profile id** from **PhantAuth**. | ||
| - parameter accessToken: Required **access_token** from **PhantAuth**. | ||
| - parameter options: A set of header options sent to the server. Defaults to an empty set. | ||
| - parameter callbackQueue: The queue to return to after completion. Default value of .main. | ||
| - parameter completion: The block to execute. | ||
| */ | ||
| func link(id: String, | ||
| accessToken: String, | ||
| options: API.Options = [], | ||
| callbackQueue: DispatchQueue = .main, | ||
| completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) { | ||
| let phantauthAuthData = AuthenticationKeys.id | ||
| .makeDictionary(id: id, | ||
| accessToken: accessToken) | ||
| link(authData: phantauthAuthData, | ||
| options: options, | ||
| callbackQueue: callbackQueue, | ||
| completion: completion) | ||
| } | ||
|
|
||
| func link(authData: [String: String], | ||
| options: API.Options = [], | ||
| callbackQueue: DispatchQueue = .main, | ||
| completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) { | ||
| guard AuthenticationKeys.id.verifyMandatoryKeys(authData: authData) else { | ||
| callbackQueue.async { | ||
| completion(.failure(.init(code: .unknownError, | ||
| message: "Should have authData consisting of keys \"id\" and \"access_token\"."))) | ||
| } | ||
| return | ||
| } | ||
| AuthenticatedUser.link(Self.__type, | ||
| authData: authData, | ||
| options: options, | ||
| callbackQueue: callbackQueue, | ||
| completion: completion) | ||
| } | ||
| } | ||
|
|
||
| // MARK: 3rd Party Authentication - ParsePhantAuth | ||
| public extension ParseUser { | ||
|
|
||
| /// A PhantAuth `ParseUser`. | ||
| static var phantauth: ParsePhantAuth<Self> { | ||
| ParsePhantAuth<Self>() | ||
| } | ||
|
|
||
| /// A PhantAuth `ParseUser`. | ||
| var phantauth: ParsePhantAuth<Self> { | ||
| Self.phantauth | ||
| } | ||
| } | ||
Oops, something went wrong.
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.