-
-
Notifications
You must be signed in to change notification settings - Fork 69
feat: add Spotify authentication #375
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c7ef4cf
feat: add Spotify login to 3rd party login providers
rocxteady f99fd7a
feat: add optional parameters to Spotify login
rocxteady 15f4423
test: add test cases for Spotify login
rocxteady 213964e
Update CHANGELOG.md
rocxteady 31944f3
Update CHANGELOG.md
rocxteady db0b7e0
fix: Remove clientId from authentication data
rocxteady 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
Large diffs are not rendered by default.
Oops, something went wrong.
99 changes: 99 additions & 0 deletions
99
Sources/ParseSwift/Authentication/3rd Party/ParseSpotify/ParseSpotify+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,99 @@ | ||
// | ||
// ParseSpotify+async.swift | ||
// ParseSwift | ||
// | ||
// Created by Ulaş Sancak on 06/20/22. | ||
// Copyright © 2022 Parse Community. All rights reserved. | ||
// | ||
|
||
#if compiler(>=5.5.2) && canImport(_Concurrency) | ||
import Foundation | ||
|
||
public extension ParseSpotify { | ||
// MARK: Async/Await | ||
|
||
/** | ||
Login a `ParseUser` *asynchronously* using Spotify authentication. | ||
- parameter id: The **Spotify profile id** from **Spotify**. | ||
- parameter accessToken: Required **access_token** from **Spotify**. | ||
- parameter expiresIn: Optional **expires_in** in seconds from **Spotify**. | ||
- parameter refreshToken: Optional **refresh_token** from **Spotify**. | ||
- 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, | ||
expiresIn: Int? = nil, | ||
refreshToken: String? = nil, | ||
options: API.Options = []) async throws -> AuthenticatedUser { | ||
try await withCheckedThrowingContinuation { continuation in | ||
self.login(id: id, | ||
accessToken: accessToken, | ||
expiresIn: expiresIn, | ||
refreshToken: refreshToken, | ||
options: options, | ||
completion: continuation.resume) | ||
} | ||
} | ||
|
||
/** | ||
Login a `ParseUser` *asynchronously* using Spotify 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) | ||
} | ||
} | ||
} | ||
|
||
public extension ParseSpotify { | ||
|
||
/** | ||
Link the *current* `ParseUser` *asynchronously* using Spotify authentication. | ||
- parameter id: The **Spotify profile id** from **Spotify**. | ||
- parameter accessToken: Required **access_token** from **Spotify**. | ||
- parameter expiresIn: Optional **expires_in** in seconds from **Spotify**. | ||
- parameter refreshToken: Optional **refresh_token** from **Spotify**. | ||
- 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, | ||
expiresIn: Int? = nil, | ||
refreshToken: String? = nil, | ||
options: API.Options = []) async throws -> AuthenticatedUser { | ||
try await withCheckedThrowingContinuation { continuation in | ||
self.link(id: id, | ||
accessToken: accessToken, | ||
expiresIn: expiresIn, | ||
refreshToken: refreshToken, | ||
options: options, | ||
completion: continuation.resume) | ||
} | ||
} | ||
|
||
/** | ||
Link the *current* `ParseUser` *asynchronously* using Spotify 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) | ||
} | ||
} | ||
} | ||
#endif |
96 changes: 96 additions & 0 deletions
96
Sources/ParseSwift/Authentication/3rd Party/ParseSpotify/ParseSpotify+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,96 @@ | ||
// | ||
// ParseSpotify+combine.swift | ||
// ParseSwift | ||
// | ||
// Created by Ulaş Sancak on 06/20/22. | ||
// Copyright © 2022 Parse Community. All rights reserved. | ||
// | ||
|
||
#if canImport(Combine) | ||
import Foundation | ||
import Combine | ||
|
||
public extension ParseSpotify { | ||
// MARK: Combine | ||
/** | ||
Login a `ParseUser` *asynchronously* using Spotify authentication. Publishes when complete. | ||
- parameter id: The **Spotify profile id** from **Spotify**. | ||
- parameter accessToken: Required **access_token** from **Spotify**. | ||
- parameter expiresIn: Optional **expires_in** in seconds from **Spotify**. | ||
- parameter refreshToken: Optional **refresh_token** from **Spotify**. | ||
- 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, | ||
expiresIn: Int? = nil, | ||
refreshToken: String? = nil, | ||
options: API.Options = []) -> Future<AuthenticatedUser, ParseError> { | ||
Future { promise in | ||
self.login(id: id, | ||
accessToken: accessToken, | ||
expiresIn: expiresIn, | ||
refreshToken: refreshToken, | ||
options: options, | ||
completion: promise) | ||
} | ||
} | ||
|
||
/** | ||
Login a `ParseUser` *asynchronously* using Spotify 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 ParseSpotify { | ||
/** | ||
Link the *current* `ParseUser` *asynchronously* using Spotify authentication. | ||
Publishes when complete. | ||
- parameter id: The **Spotify profile id** from **Spotify**. | ||
- parameter accessToken: Required **access_token** from **Spotify**. | ||
- parameter expiresIn: Optional **expires_in** in seconds from **Spotify**. | ||
- parameter refreshToken: Optional **refresh_token** from **Spotify**. | ||
- 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, | ||
expiresIn: Int? = nil, | ||
refreshToken: String? = nil, | ||
options: API.Options = []) -> Future<AuthenticatedUser, ParseError> { | ||
Future { promise in | ||
self.link(id: id, | ||
accessToken: accessToken, | ||
expiresIn: expiresIn, | ||
refreshToken: refreshToken, | ||
options: options, | ||
completion: promise) | ||
} | ||
} | ||
|
||
/** | ||
Link the *current* `ParseUser` *asynchronously* using Spotify 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 |
186 changes: 186 additions & 0 deletions
186
Sources/ParseSwift/Authentication/3rd Party/ParseSpotify/ParseSpotify.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,186 @@ | ||
// | ||
// ParseSpotify.swift | ||
// ParseSwift | ||
// | ||
// Created by Ulaş Sancak on 06/20/22. | ||
// Copyright © 2022 Parse Community. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
// swiftlint:disable line_length | ||
|
||
/** | ||
Provides utility functions for working with Spotify User Authentication and `ParseUser`'s. | ||
Be sure your Parse Server is configured for [sign in with Spotify](https://docs.parseplatform.org/parse-server/guide/#spotify-authdata) | ||
For information on acquiring Spotify sign-in credentials to use with `ParseSpotify`, refer to [Spotify's Documentation](https://developer.spotify.com/documentation/general/guides/authorization/) | ||
*/ | ||
public struct ParseSpotify<AuthenticatedUser: ParseUser>: ParseAuthentication { | ||
|
||
/// Authentication keys required for Spotify authentication. | ||
enum AuthenticationKeys: String, Codable { | ||
case id | ||
case accessToken = "access_token" | ||
case expirationDate = "expiration_date" | ||
case refreshToken = "refresh_token" | ||
/// Properly makes an authData dictionary with the required keys. | ||
/// - parameter id: Required id for the user. | ||
/// - parameter accessToken: Required access token for Spotify. | ||
/// - parameter expiresIn: Optional expiration in seconds for Spotify. | ||
/// - parameter refreshToken: Optional refresh token for Spotify. | ||
/// - returns: authData dictionary. | ||
func makeDictionary(id: String, | ||
accessToken: String, | ||
expiresIn: Int? = nil, | ||
refreshToken: String? = nil) -> [String: String] { | ||
|
||
var returnDictionary = [ | ||
AuthenticationKeys.id.rawValue: id, | ||
AuthenticationKeys.accessToken.rawValue: accessToken | ||
] | ||
if let expiresIn = expiresIn, | ||
let expirationDate = Calendar.current.date(byAdding: .second, | ||
value: expiresIn, | ||
to: Date()) { | ||
let dateString = ParseCoding.dateFormatter.string(from: expirationDate) | ||
returnDictionary[AuthenticationKeys.expirationDate.rawValue] = dateString | ||
} | ||
if let refreshToken = refreshToken { | ||
returnDictionary[AuthenticationKeys.refreshToken.rawValue] = refreshToken | ||
} | ||
return returnDictionary | ||
} | ||
|
||
/// 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 | ||
"spotify" | ||
} | ||
|
||
public init() { } | ||
} | ||
|
||
// MARK: Login | ||
public extension ParseSpotify { | ||
|
||
/** | ||
Login a `ParseUser` *asynchronously* using Spotify authentication. | ||
- parameter id: The **Spotify profile id** from **Spotify**. | ||
- parameter accessToken: Required **access_token** from **Spotify**. | ||
- parameter expiresIn: Optional **expires_in** in seconds from **Spotify**. | ||
- parameter refreshToken: Optional **refresh_token** from **Spotify**. | ||
- 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, | ||
expiresIn: Int? = nil, | ||
refreshToken: String? = nil, | ||
options: API.Options = [], | ||
callbackQueue: DispatchQueue = .main, | ||
completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) { | ||
|
||
let spotifyAuthData = AuthenticationKeys.id | ||
.makeDictionary(id: id, | ||
accessToken: accessToken, | ||
expiresIn: expiresIn, | ||
refreshToken: refreshToken) | ||
login(authData: spotifyAuthData, | ||
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 in consisting of keys \"id\" and \"accessToken\"."))) | ||
} | ||
return | ||
} | ||
AuthenticatedUser.login(Self.__type, | ||
authData: authData, | ||
options: options, | ||
callbackQueue: callbackQueue, | ||
completion: completion) | ||
} | ||
} | ||
|
||
// MARK: Link | ||
public extension ParseSpotify { | ||
|
||
/** | ||
Link the *current* `ParseUser` *asynchronously* using Spotify authentication. | ||
- parameter id: The **Spotify profile id** from **Spotify**. | ||
- parameter accessToken: Required **access_token** from **Spotify**. | ||
- parameter expiresIn: Optional **expires_in** in seconds from **Spotify**. | ||
- parameter refreshToken: Optional **refresh_token** from **Spotify**. | ||
- 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, | ||
expiresIn: Int? = nil, | ||
refreshToken: String? = nil, | ||
options: API.Options = [], | ||
callbackQueue: DispatchQueue = .main, | ||
completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) { | ||
let spotifyAuthData = AuthenticationKeys.id | ||
.makeDictionary(id: id, | ||
accessToken: accessToken, | ||
expiresIn: expiresIn, | ||
refreshToken: refreshToken) | ||
link(authData: spotifyAuthData, | ||
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 in consisting of keys \"id\" and \"accessToken\"."))) | ||
} | ||
return | ||
} | ||
AuthenticatedUser.link(Self.__type, | ||
authData: authData, | ||
options: options, | ||
callbackQueue: callbackQueue, | ||
completion: completion) | ||
} | ||
} | ||
|
||
// MARK: 3rd Party Authentication - ParseSpotify | ||
public extension ParseUser { | ||
|
||
/// A Spotify `ParseUser`. | ||
static var spotify: ParseSpotify<Self> { | ||
ParseSpotify<Self>() | ||
} | ||
|
||
/// An Spotify `ParseUser`. | ||
var spotify: ParseSpotify<Self> { | ||
Self.spotify | ||
} | ||
} |
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.