|
| 1 | +// |
| 2 | +// ParseSpotify.swift |
| 3 | +// ParseSwift |
| 4 | +// |
| 5 | +// Created by Ulaş Sancak on 06/20/22. |
| 6 | +// Copyright © 2022 Parse Community. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +// swiftlint:disable line_length |
| 12 | + |
| 13 | +/** |
| 14 | + Provides utility functions for working with Spotify User Authentication and `ParseUser`'s. |
| 15 | + Be sure your Parse Server is configured for [sign in with Spotify](https://docs.parseplatform.org/parse-server/guide/#spotify-authdata) |
| 16 | + 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/) |
| 17 | + */ |
| 18 | +public struct ParseSpotify<AuthenticatedUser: ParseUser>: ParseAuthentication { |
| 19 | + |
| 20 | + /// Authentication keys required for Spotify authentication. |
| 21 | + enum AuthenticationKeys: String, Codable { |
| 22 | + case id |
| 23 | + case accessToken = "access_token" |
| 24 | + case expirationDate = "expiration_date" |
| 25 | + case refreshToken = "refresh_token" |
| 26 | + /// Properly makes an authData dictionary with the required keys. |
| 27 | + /// - parameter id: Required id for the user. |
| 28 | + /// - parameter accessToken: Required access token for Spotify. |
| 29 | + /// - parameter expiresIn: Optional expiration in seconds for Spotify. |
| 30 | + /// - parameter refreshToken: Optional refresh token for Spotify. |
| 31 | + /// - returns: authData dictionary. |
| 32 | + func makeDictionary(id: String, |
| 33 | + accessToken: String, |
| 34 | + expiresIn: Int? = nil, |
| 35 | + refreshToken: String? = nil) -> [String: String] { |
| 36 | + |
| 37 | + var returnDictionary = [ |
| 38 | + AuthenticationKeys.id.rawValue: id, |
| 39 | + AuthenticationKeys.accessToken.rawValue: accessToken |
| 40 | + ] |
| 41 | + if let expiresIn = expiresIn, |
| 42 | + let expirationDate = Calendar.current.date(byAdding: .second, |
| 43 | + value: expiresIn, |
| 44 | + to: Date()) { |
| 45 | + let dateString = ParseCoding.dateFormatter.string(from: expirationDate) |
| 46 | + returnDictionary[AuthenticationKeys.expirationDate.rawValue] = dateString |
| 47 | + } |
| 48 | + if let refreshToken = refreshToken { |
| 49 | + returnDictionary[AuthenticationKeys.refreshToken.rawValue] = refreshToken |
| 50 | + } |
| 51 | + return returnDictionary |
| 52 | + } |
| 53 | + |
| 54 | + /// Verifies all mandatory keys are in authData. |
| 55 | + /// - parameter authData: Dictionary containing key/values. |
| 56 | + /// - returns: **true** if all the mandatory keys are present, **false** otherwise. |
| 57 | + func verifyMandatoryKeys(authData: [String: String]) -> Bool { |
| 58 | + guard authData[AuthenticationKeys.id.rawValue] != nil, |
| 59 | + authData[AuthenticationKeys.accessToken.rawValue] != nil else { |
| 60 | + return false |
| 61 | + } |
| 62 | + return true |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public static var __type: String { // swiftlint:disable:this identifier_name |
| 67 | + "spotify" |
| 68 | + } |
| 69 | + |
| 70 | + public init() { } |
| 71 | +} |
| 72 | + |
| 73 | +// MARK: Login |
| 74 | +public extension ParseSpotify { |
| 75 | + |
| 76 | + /** |
| 77 | + Login a `ParseUser` *asynchronously* using Spotify authentication. |
| 78 | + - parameter id: The **Spotify profile id** from **Spotify**. |
| 79 | + - parameter accessToken: Required **access_token** from **Spotify**. |
| 80 | + - parameter expiresIn: Optional **expires_in** in seconds from **Spotify**. |
| 81 | + - parameter refreshToken: Optional **refresh_token** from **Spotify**. |
| 82 | + - parameter options: A set of header options sent to the server. Defaults to an empty set. |
| 83 | + - parameter callbackQueue: The queue to return to after completion. Default value of .main. |
| 84 | + - parameter completion: The block to execute. |
| 85 | + */ |
| 86 | + func login(id: String, |
| 87 | + accessToken: String, |
| 88 | + expiresIn: Int? = nil, |
| 89 | + refreshToken: String? = nil, |
| 90 | + options: API.Options = [], |
| 91 | + callbackQueue: DispatchQueue = .main, |
| 92 | + completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) { |
| 93 | + |
| 94 | + let spotifyAuthData = AuthenticationKeys.id |
| 95 | + .makeDictionary(id: id, |
| 96 | + accessToken: accessToken, |
| 97 | + expiresIn: expiresIn, |
| 98 | + refreshToken: refreshToken) |
| 99 | + login(authData: spotifyAuthData, |
| 100 | + options: options, |
| 101 | + callbackQueue: callbackQueue, |
| 102 | + completion: completion) |
| 103 | + } |
| 104 | + |
| 105 | + func login(authData: [String: String], |
| 106 | + options: API.Options = [], |
| 107 | + callbackQueue: DispatchQueue = .main, |
| 108 | + completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) { |
| 109 | + guard AuthenticationKeys.id.verifyMandatoryKeys(authData: authData) else { |
| 110 | + callbackQueue.async { |
| 111 | + completion(.failure(.init(code: .unknownError, |
| 112 | + message: "Should have authData in consisting of keys \"id\" and \"accessToken\"."))) |
| 113 | + } |
| 114 | + return |
| 115 | + } |
| 116 | + AuthenticatedUser.login(Self.__type, |
| 117 | + authData: authData, |
| 118 | + options: options, |
| 119 | + callbackQueue: callbackQueue, |
| 120 | + completion: completion) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// MARK: Link |
| 125 | +public extension ParseSpotify { |
| 126 | + |
| 127 | + /** |
| 128 | + Link the *current* `ParseUser` *asynchronously* using Spotify authentication. |
| 129 | + - parameter id: The **Spotify profile id** from **Spotify**. |
| 130 | + - parameter accessToken: Required **access_token** from **Spotify**. |
| 131 | + - parameter expiresIn: Optional **expires_in** in seconds from **Spotify**. |
| 132 | + - parameter refreshToken: Optional **refresh_token** from **Spotify**. |
| 133 | + - parameter options: A set of header options sent to the server. Defaults to an empty set. |
| 134 | + - parameter callbackQueue: The queue to return to after completion. Default value of .main. |
| 135 | + - parameter completion: The block to execute. |
| 136 | + */ |
| 137 | + func link(id: String, |
| 138 | + accessToken: String, |
| 139 | + expiresIn: Int? = nil, |
| 140 | + refreshToken: String? = nil, |
| 141 | + options: API.Options = [], |
| 142 | + callbackQueue: DispatchQueue = .main, |
| 143 | + completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) { |
| 144 | + let spotifyAuthData = AuthenticationKeys.id |
| 145 | + .makeDictionary(id: id, |
| 146 | + accessToken: accessToken, |
| 147 | + expiresIn: expiresIn, |
| 148 | + refreshToken: refreshToken) |
| 149 | + link(authData: spotifyAuthData, |
| 150 | + options: options, |
| 151 | + callbackQueue: callbackQueue, |
| 152 | + completion: completion) |
| 153 | + } |
| 154 | + |
| 155 | + func link(authData: [String: String], |
| 156 | + options: API.Options = [], |
| 157 | + callbackQueue: DispatchQueue = .main, |
| 158 | + completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) { |
| 159 | + guard AuthenticationKeys.id.verifyMandatoryKeys(authData: authData) else { |
| 160 | + callbackQueue.async { |
| 161 | + completion(.failure(.init(code: .unknownError, |
| 162 | + message: "Should have authData in consisting of keys \"id\" and \"accessToken\"."))) |
| 163 | + } |
| 164 | + return |
| 165 | + } |
| 166 | + AuthenticatedUser.link(Self.__type, |
| 167 | + authData: authData, |
| 168 | + options: options, |
| 169 | + callbackQueue: callbackQueue, |
| 170 | + completion: completion) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +// MARK: 3rd Party Authentication - ParseSpotify |
| 175 | +public extension ParseUser { |
| 176 | + |
| 177 | + /// A Spotify `ParseUser`. |
| 178 | + static var spotify: ParseSpotify<Self> { |
| 179 | + ParseSpotify<Self>() |
| 180 | + } |
| 181 | + |
| 182 | + /// An Spotify `ParseUser`. |
| 183 | + var spotify: ParseSpotify<Self> { |
| 184 | + Self.spotify |
| 185 | + } |
| 186 | +} |
0 commit comments