|
| 1 | +// |
| 2 | +// ParseInstagram.swift |
| 3 | +// ParseSwift |
| 4 | +// |
| 5 | +// Created by Ulaş Sancak on 06/19/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 Instagram User Authentication and `ParseUser`'s. |
| 15 | + Be sure your Parse Server is configured for [sign in with Instagram](https://docs.parseplatform.org/parse-server/guide/#instagram-authdata). |
| 16 | + For information on acquiring Instagram sign-in credentials to use with `ParseInstagram`, refer to [Facebook's Documentation](https://developers.facebook.com/docs/instagram-basic-display-api/overview). |
| 17 | + */ |
| 18 | +public struct ParseInstagram<AuthenticatedUser: ParseUser>: ParseAuthentication { |
| 19 | + |
| 20 | + public static var graphAPIBaseURL: String { |
| 21 | + "https://graph.instagram.com/" |
| 22 | + } |
| 23 | + |
| 24 | + /// Authentication keys required for Instagram authentication. |
| 25 | + enum AuthenticationKeys: String, Codable { |
| 26 | + case id |
| 27 | + case accessToken = "access_token" |
| 28 | + case apiURL |
| 29 | + |
| 30 | + /// Properly makes an authData dictionary with the required keys. |
| 31 | + /// - parameter id: Required id for the user. |
| 32 | + /// - parameter accessToken: Required access token for Instagram. |
| 33 | + /// - returns: authData dictionary. |
| 34 | + func makeDictionary(id: String, |
| 35 | + accessToken: String, |
| 36 | + apiURL: String = ParseInstagram.graphAPIBaseURL) -> [String: String] { |
| 37 | + |
| 38 | + let returnDictionary = [ |
| 39 | + AuthenticationKeys.id.rawValue: id, |
| 40 | + AuthenticationKeys.accessToken.rawValue: accessToken, |
| 41 | + AuthenticationKeys.apiURL.rawValue: apiURL |
| 42 | + ] |
| 43 | + return returnDictionary |
| 44 | + } |
| 45 | + |
| 46 | + /// Verifies all mandatory keys are in authData. |
| 47 | + /// - parameter authData: Dictionary containing key/values. |
| 48 | + /// - returns: **true** if all the mandatory keys are present, **false** otherwise. |
| 49 | + func verifyMandatoryKeys(authData: [String: String]) -> Bool { |
| 50 | + guard authData[AuthenticationKeys.id.rawValue] != nil, |
| 51 | + authData[AuthenticationKeys.accessToken.rawValue] != nil, |
| 52 | + authData[AuthenticationKeys.apiURL.rawValue] != nil else { |
| 53 | + return false |
| 54 | + } |
| 55 | + return true |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public static var __type: String { // swiftlint:disable:this identifier_name |
| 60 | + "instagram" |
| 61 | + } |
| 62 | + |
| 63 | + public init() { } |
| 64 | +} |
| 65 | + |
| 66 | +// MARK: Login |
| 67 | +public extension ParseInstagram { |
| 68 | + |
| 69 | + /** |
| 70 | + Login a `ParseUser` *asynchronously* using Instagram authentication. |
| 71 | + - parameter id: The **Instagram profile id** from **Instagram**. |
| 72 | + - parameter accessToken: Required **access_token** from **Instagram**. |
| 73 | + - parameter apiURL: The `Instagram's most recent graph api url` from **Instagram**. |
| 74 | + - parameter options: A set of header options sent to the server. Defaults to an empty set. |
| 75 | + - parameter callbackQueue: The queue to return to after completion. Default value of .main. |
| 76 | + - parameter completion: The block to execute. |
| 77 | + */ |
| 78 | + func login(id: String, |
| 79 | + accessToken: String, |
| 80 | + apiURL: String = Self.graphAPIBaseURL, |
| 81 | + options: API.Options = [], |
| 82 | + callbackQueue: DispatchQueue = .main, |
| 83 | + completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) { |
| 84 | + |
| 85 | + let instagramAuthData = AuthenticationKeys.id |
| 86 | + .makeDictionary(id: id, |
| 87 | + accessToken: accessToken, |
| 88 | + apiURL: apiURL) |
| 89 | + print(instagramAuthData) |
| 90 | + login(authData: instagramAuthData, |
| 91 | + options: options, |
| 92 | + callbackQueue: callbackQueue, |
| 93 | + completion: completion) |
| 94 | + } |
| 95 | + |
| 96 | + func login(authData: [String: String], |
| 97 | + options: API.Options = [], |
| 98 | + callbackQueue: DispatchQueue = .main, |
| 99 | + completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) { |
| 100 | + guard AuthenticationKeys.id.verifyMandatoryKeys(authData: authData) else { |
| 101 | + callbackQueue.async { |
| 102 | + completion(.failure(.init(code: .unknownError, |
| 103 | + message: "Should have authData in consisting of keys \"id\", \"accessToken\", and \"isMobileSDK\"."))) |
| 104 | + } |
| 105 | + return |
| 106 | + } |
| 107 | + AuthenticatedUser.login(Self.__type, |
| 108 | + authData: authData, |
| 109 | + options: options, |
| 110 | + callbackQueue: callbackQueue, |
| 111 | + completion: completion) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// MARK: Link |
| 116 | +public extension ParseInstagram { |
| 117 | + |
| 118 | + /** |
| 119 | + Link the *current* `ParseUser` *asynchronously* using Instagram authentication. |
| 120 | + - parameter id: The **Instagram profile id** from **Instagram**. |
| 121 | + - parameter accessToken: Required **access_token** from **Instagram**. |
| 122 | + - parameter apiURL: The `Instagram's most recent graph api url` from **Instagram**. |
| 123 | + - parameter options: A set of header options sent to the server. Defaults to an empty set. |
| 124 | + - parameter callbackQueue: The queue to return to after completion. Default value of .main. |
| 125 | + - parameter completion: The block to execute. |
| 126 | + */ |
| 127 | + func link(id: String, |
| 128 | + accessToken: String, |
| 129 | + apiURL: String = Self.graphAPIBaseURL, |
| 130 | + options: API.Options = [], |
| 131 | + callbackQueue: DispatchQueue = .main, |
| 132 | + completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) { |
| 133 | + let instagramAuthData = AuthenticationKeys.id |
| 134 | + .makeDictionary(id: id, |
| 135 | + accessToken: accessToken, |
| 136 | + apiURL: apiURL) |
| 137 | + link(authData: instagramAuthData, |
| 138 | + options: options, |
| 139 | + callbackQueue: callbackQueue, |
| 140 | + completion: completion) |
| 141 | + } |
| 142 | + |
| 143 | + func link(authData: [String: String], |
| 144 | + options: API.Options = [], |
| 145 | + callbackQueue: DispatchQueue = .main, |
| 146 | + completion: @escaping (Result<AuthenticatedUser, ParseError>) -> Void) { |
| 147 | + guard AuthenticationKeys.id.verifyMandatoryKeys(authData: authData) else { |
| 148 | + callbackQueue.async { |
| 149 | + completion(.failure(.init(code: .unknownError, |
| 150 | + message: "Should have authData in consisting of keys \"id\", \"accessToken\", and \"isMobileSDK\"."))) |
| 151 | + } |
| 152 | + return |
| 153 | + } |
| 154 | + AuthenticatedUser.link(Self.__type, |
| 155 | + authData: authData, |
| 156 | + options: options, |
| 157 | + callbackQueue: callbackQueue, |
| 158 | + completion: completion) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +// MARK: 3rd Party Authentication - ParseInstagram |
| 163 | +public extension ParseUser { |
| 164 | + |
| 165 | + /// A Instagram `ParseUser`. |
| 166 | + static var instagram: ParseInstagram<Self> { |
| 167 | + ParseInstagram<Self>() |
| 168 | + } |
| 169 | + |
| 170 | + /// An Instagram `ParseUser`. |
| 171 | + var instagram: ParseInstagram<Self> { |
| 172 | + Self.instagram |
| 173 | + } |
| 174 | +} |
0 commit comments