Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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\".")))
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
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
}
}
Loading