This repository has been archived by the owner on Sep 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from magiclabs/feat/open_source
Migrated from fortmatic org
Showing
15 changed files
with
558 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,27 @@ | ||
# | ||
# Local Podspec for building local target | ||
# | ||
|
||
Pod::Spec.new do |s| | ||
s.name = 'MagicExt-OAuth' | ||
s.version = '1.0.0' | ||
s.summary = 'Magic IOS Extension - OAuth' | ||
|
||
s.description = <<-DESC | ||
TODO: Add long description of the pod here. | ||
DESC | ||
|
||
s.homepage = 'https://github.com/magicLabs/magic-ios-ext' | ||
s.license = { :type => 'MIT', :file => 'LICENSE' } | ||
s.author = { 'Jerry Liu' => 'jerry@magic.link' } | ||
s.source = { :git => 'https://github.com/magicLabs/magic-ios-ext.git', :tag => s.version.to_s } | ||
s.swift_version = '5.0' | ||
s.ios.deployment_target = '10.0' | ||
# s.osx.deployment_target = '10.12' | ||
|
||
s.source_files = 'Sources/MagicExt-OAuth/**/*' | ||
|
||
s.dependency 'MagicSDK', '~> 3.0' | ||
|
||
s.pod_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' } | ||
end |
This file contains 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,30 @@ | ||
// swift-tools-version:5.5 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "MagicExt-OAuth", | ||
|
||
products: [ | ||
// Products define the executables and libraries a package produces, and make them visible to other packages. | ||
.library( | ||
name: "MagicExt-OAuth", | ||
targets: ["MagicExt-OAuth"]), | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/magiclabs/magic-ios.git", from:"3.0.0"), | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package. A target can define a module or a test suite. | ||
// Targets can depend on other targets in this package, and on products in packages this package depends on. | ||
.target( | ||
name: "MagicExt-OAuth", | ||
dependencies: [ | ||
.product(name: "MagicSDK", package: "magic-ios"), | ||
]), | ||
.testTarget( | ||
name: "MagicExt-OAuthTests", | ||
dependencies: ["MagicExt-OAuth"]), | ||
] | ||
) |
This file contains 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 |
---|---|---|
@@ -1,2 +1,42 @@ | ||
# magic-ios-ext | ||
Magic IOS Extension libraries | ||
# MagicExt-OAuth | ||
[![CI Status](https://img.shields.io/travis/Ethella/MagicExt-OAuth.svg?style=flat)](https://travis-ci.org/Ethella/MagicExt-OAuth) | ||
[![Version](https://img.shields.io/cocoapods/v/MagicExt-OAuth.svg?style=flat)](https://cocoapods.org/pods/MagicExt-OAuth) | ||
[![License](https://img.shields.io/cocoapods/l/MagicExt-OAuth.svg?style=flat)](https://cocoapods.org/pods/MagicExt-OAuth) | ||
[![Platform](https://img.shields.io/cocoapods/p/MagicExt-OAuth.svg?style=flat)](https://cocoapods.org/pods/MagicExt-OAuth) | ||
|
||
Cocoapods | ||
--- | ||
## Set up the local development env | ||
1. To start the demo app with local development SDK, download following projects | ||
```bash | ||
# demo app | ||
$ git clone https://github.com/magiclabs/magic-ios-demo | ||
# ios SDK | ||
$ git clone https://github.com/magiclabs/magic-ios | ||
$ git clone https://github.com/magiclabs/magic-ios-ext | ||
``` | ||
|
||
2. To enable the demo use the local development SDK. Navigate to `magic-ios-demo/Podfile` and edit the following lines. | ||
This will make pod file install local dependencies instead of the ones distributed. | ||
|
||
```ruby | ||
# Distributed Library on Cocoapods | ||
# pod 'MagicSDK', '~> 3.0' | ||
# pod 'MagicExt-OAuth', '~> 1.0' | ||
|
||
# Local development library | ||
pod 'MagicSDK', :path => '../magic-ios/MagicSDK.podspec' | ||
pod 'MagicExt-OAuth', :path => '../magic-ios-ext/MagicExt-OAuth.podspec' | ||
``` | ||
|
||
```bash | ||
$ cd /YOUR/PATH/TO/magic-ios-demo | ||
|
||
# Install dependencies | ||
$ pod install | ||
``` | ||
|
||
3. Open `/YOUR/PATH/TO/magic-ios-demo/magic-ios-demo.xcworkspace` with XCode and try it out! | ||
|
||
--- | ||
|
This file contains 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,141 @@ | ||
// | ||
// OauthModule.swift | ||
// MagicSDK | ||
// | ||
// Created by Wentao Liu on 9/16/20. | ||
// | ||
|
||
import Foundation | ||
import AuthenticationServices | ||
import SafariServices | ||
import MagicSDK_Web3 | ||
import MagicSDK | ||
import PromiseKit | ||
|
||
public class OAuthExtension: BaseModule { | ||
|
||
public enum OAuthExtensionError: Swift.Error { | ||
case parseSuccessURLError(url: String) | ||
case unsupportedVersions | ||
case userDeniedAccess(Swift.Error) | ||
case unableToStartPopup | ||
} | ||
|
||
public func loginWithPopup (_ configuration: OAuthConfiguration) -> Promise<OAuthResponse> { | ||
return Promise { resolver in | ||
loginWithPopup(configuration, response: promiseResolver(resolver)) | ||
} | ||
} | ||
|
||
public func loginWithPopup (_ configuration: OAuthConfiguration, response: @escaping Web3ResponseCompletion<OAuthResponse>) { | ||
let oauthChallenge = OAuthChallenge() | ||
|
||
// Construct OAuth URL | ||
var components = URLComponents() | ||
components.scheme = "https" | ||
components.host = "auth.magic.link" | ||
// components.scheme = "http" | ||
// components.host = "192.168.0.106" | ||
// components.port = 3014 | ||
components.path = "/v1/oauth2/\(configuration.provider.rawValue.lowercased())/start" | ||
|
||
components.queryItems = [ | ||
URLQueryItem(name: "magic_api_key", value: self.provider.urlBuilder.apiKey), | ||
URLQueryItem(name: "magic_challenge", value: oauthChallenge.challenge), | ||
URLQueryItem(name: "state", value: oauthChallenge.state), | ||
URLQueryItem(name: "redirect_uri", value: configuration.redirectURI), | ||
URLQueryItem(name: "platform", value: "rn") | ||
] | ||
|
||
if let scope = configuration.scope { | ||
if scope.count > 0 { | ||
components.queryItems?.append(URLQueryItem(name: "scope", value: scope.joined(separator: " "))) | ||
} | ||
} | ||
|
||
if let loginHint = configuration.loginHint { | ||
components.queryItems?.append(URLQueryItem(name: "login_hint", value: loginHint)) | ||
} | ||
|
||
let authURL = components.url | ||
|
||
|
||
|
||
firstly { | ||
// Pop Authentication Session | ||
createAuthenticationSession(authURL: authURL, configuration: configuration) | ||
}.done {successURL -> Void in | ||
|
||
// Remove Percentage Encode to prevent double encoding | ||
guard let query = URL(string:successURL)?.query?.removingPercentEncoding else { | ||
throw OAuthExtensionError.parseSuccessURLError(url: successURL) | ||
} | ||
|
||
// send credential to auth relayer to authenticate | ||
let request = RPCRequest<[String]>(method: OAuthMethod.magic_oauth_parse_redirect_result.rawValue, params: [ "?\(query)", oauthChallenge.verifier, oauthChallenge.state]) | ||
self.provider.send(request: request, response: response) | ||
}.catch { error in | ||
let errResponse = Web3Response<OAuthResponse>(error: OAuthExtensionError.userDeniedAccess(error)) | ||
response(errResponse) | ||
// handleRollbarError(error, log: false) | ||
} | ||
} | ||
|
||
private func createAuthenticationSession(authURL: URL?, configuration: OAuthConfiguration) -> Promise<String> { | ||
|
||
// Remove "://" from app schemes to prevent error | ||
let callbackURLScheme = configuration.redirectURI.replacingOccurrences(of: "://", with: "", options: NSString.CompareOptions.literal, range: nil) | ||
|
||
return Promise { resolver in | ||
|
||
// find topmost view controller from the hierarchy and attach modal Controller to it | ||
guard let keyWindow = UIApplication.shared.windows.filter({$0.isKeyWindow}).first else { | ||
return resolver.reject(OAuthExtensionError.unableToStartPopup) | ||
} | ||
|
||
if var topController = keyWindow.rootViewController { | ||
while let presentedVC = topController.presentedViewController { | ||
topController = presentedVC | ||
} | ||
if #available(iOS 12, *) { | ||
let shimVC = ShimASViewController() | ||
shimVC.source = authURL | ||
shimVC.callbackURL = callbackURLScheme | ||
shimVC.resolver = resolver | ||
topController.present(shimVC, animated: true) | ||
} else if #available(iOS 11.0, *) { | ||
let shimVC = ShimSFASViewController() | ||
shimVC.source = authURL | ||
shimVC.callbackURL = callbackURLScheme | ||
shimVC.resolver = resolver | ||
topController.present(shimVC, animated: true) | ||
} else { | ||
resolver.reject(OAuthExtensionError.unsupportedVersions) | ||
} | ||
} else { | ||
return resolver.reject(OAuthExtensionError.unableToStartPopup) | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
extension Magic { | ||
public var oauth: OAuthExtension { | ||
return OAuthExtension(rpcProvider: self.rpcProvider) | ||
} | ||
} | ||
|
||
// Handles Specific OAuthError | ||
extension Web3Response { | ||
public var magicExtOAuthError: OAuthExtension.OAuthExtensionError? { | ||
switch self.status { | ||
case .failure(let error): | ||
return error as? OAuthExtension.OAuthExtensionError | ||
case .success: | ||
return nil | ||
@unknown default: | ||
return nil | ||
} | ||
} | ||
} |
This file contains 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,60 @@ | ||
// | ||
// FortmaticShimViewController.swift | ||
// Fortmatic | ||
// | ||
// Created by Wentao Liu on 2/3/20. | ||
// | ||
|
||
import Foundation | ||
import AuthenticationServices | ||
import SafariServices | ||
import MagicSDK | ||
import PromiseKit | ||
|
||
@available(iOS 12.0, *) | ||
class ShimASViewController: UIViewController, ASWebAuthenticationPresentationContextProviding | ||
{ | ||
var authSession: ASWebAuthenticationSession? | ||
|
||
/// X source url | ||
var source:URL? | ||
|
||
/// callback URL scheme | ||
var callbackURL:String! | ||
|
||
/// resolver | ||
var resolver:Resolver<String>? | ||
|
||
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor { | ||
// Perhaps I don't need the window object at all, and can just use: | ||
// return ASPresentationAnchor() | ||
return UIApplication.shared.keyWindow ?? ASPresentationAnchor() | ||
} | ||
|
||
override func viewDidLoad() { | ||
|
||
|
||
//tries ASWebAuthenticationSession | ||
authSession = ASWebAuthenticationSession.init(url: source!, callbackURLScheme: callbackURL, completionHandler: { (callBack:URL?, error:Error?) in | ||
|
||
//auto close VC after popup is closed | ||
DispatchQueue.main.async { | ||
self.dismiss(animated: true) | ||
} | ||
|
||
// handle response | ||
guard error == nil, let successURL = callBack else { | ||
self.resolver?.reject(error!) | ||
return | ||
} | ||
|
||
self.resolver?.fulfill(successURL.absoluteString) | ||
}) | ||
|
||
if #available(iOS 13, *){ | ||
authSession?.presentationContextProvider = self | ||
} | ||
|
||
authSession?.start() | ||
} | ||
} |
This file contains 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,48 @@ | ||
// | ||
// ShimSFViewController.swift | ||
// MagicExt-OAuth | ||
// | ||
// Created by Wentao Liu on 9/28/20. | ||
// | ||
|
||
import Foundation | ||
import SafariServices | ||
import MagicSDK | ||
import PromiseKit | ||
|
||
@available(iOS 11.0, *) | ||
class ShimSFASViewController: UIViewController | ||
{ | ||
var authSession: SFAuthenticationSession? | ||
|
||
/// X source url | ||
var source:URL? | ||
|
||
/// callback URL scheme | ||
var callbackURL:String! | ||
|
||
/// resolver | ||
var resolver: Resolver<String>? | ||
|
||
override func viewDidLoad() { | ||
|
||
|
||
//tries ASWebAuthenticationSession | ||
authSession = SFAuthenticationSession.init(url: source!, callbackURLScheme: callbackURL, completionHandler: { (callBack:URL?, error:Error?) in | ||
|
||
//auto close VC after popup is closed | ||
self.dismiss(animated: true) | ||
|
||
// handle response | ||
guard error == nil, let successURL = callBack else { | ||
self.resolver?.reject(error!) | ||
return | ||
} | ||
|
||
// Resolve data back to send | ||
self.resolver?.fulfill(successURL.absoluteString) | ||
}) | ||
|
||
authSession?.start() | ||
} | ||
} |
This file contains 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,37 @@ | ||
// | ||
// OAuthConfiguration.swift | ||
// MagicExt-OAuth | ||
// | ||
// Created by Wentao Liu on 9/23/20. | ||
// | ||
|
||
import Foundation | ||
import MagicSDK | ||
|
||
public struct OAuthConfiguration: BaseConfiguration { | ||
public var provider: OAuthProvider | ||
public var redirectURI: String | ||
public var scope: [String]? | ||
public var loginHint: String? | ||
|
||
public init(provider: OAuthProvider, redirectURI: String, scope: [String]? = nil, loginHint: String? = nil) { | ||
self.provider = provider | ||
self.redirectURI = redirectURI | ||
self.scope = scope | ||
self.loginHint = loginHint | ||
} | ||
} | ||
|
||
public enum OAuthProvider: String, CaseIterable, Codable { | ||
case GOOGLE | ||
case FACEBOOK | ||
case GITHUB | ||
case APPLE | ||
case LINKEDIN | ||
case BITBUCKET | ||
case GITLAB | ||
case TWITTER | ||
case DISCORD | ||
case TWITCH | ||
case MICROSOFT | ||
} |
This file contains 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,12 @@ | ||
// | ||
// OAuthMethod.swift | ||
// MagicExt-OAuth | ||
// | ||
// Created by Wentao Liu on 9/29/20. | ||
// | ||
|
||
import Foundation | ||
|
||
internal enum OAuthMethod: String, CaseIterable { | ||
case magic_oauth_parse_redirect_result | ||
} |
This file contains 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,16 @@ | ||
// | ||
// OAuthRedirectError.swift | ||
// MagicExt-OAuth | ||
// | ||
// Created by Wentao Liu on 6/25/21. | ||
// | ||
|
||
import Foundation | ||
import MagicSDK | ||
|
||
public struct OAuthRedirectError: MagicResponse { | ||
public let provider: OAuthProvider.RawValue | ||
public let error: String | ||
public let error_description: String | ||
public let error_uri: String? | ||
} |
This file contains 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,27 @@ | ||
// | ||
// OAuthResult.swift | ||
// MagicExt-OAuth | ||
// | ||
// Created by Wentao Liu on 9/29/20. | ||
// | ||
|
||
import Foundation | ||
import MagicSDK | ||
|
||
public struct OAuthResponse: MagicResponse { | ||
public let oauth: OauthPartialResult | ||
public let magic: MagicPartialResult | ||
} | ||
|
||
public struct OauthPartialResult: Codable { | ||
public let provider: String; | ||
public let scope: [String]; | ||
public let accessToken: String; | ||
public let userHandle: String; | ||
public let userInfo: OpenIDConnectProfile; | ||
|
||
} | ||
public struct MagicPartialResult: Codable { | ||
public let idToken: String; | ||
public let userMetadata: UserMetadata; | ||
} |
This file contains 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,47 @@ | ||
// | ||
// File.swift | ||
// MagicExt-OAuth | ||
// | ||
// Created by Wentao Liu on 9/29/20. | ||
// | ||
|
||
import Foundation | ||
import MagicSDK | ||
|
||
public struct OpenIDConnectProfile: Codable { | ||
public let name: String? | ||
public let familyName: String? | ||
public let givenName: String? | ||
public let middleName: String? | ||
public let nickname: String? | ||
public let preferredUsername: String? | ||
public let profile: String? | ||
public let picture: String? | ||
public let website: String? | ||
public let gender: String? | ||
public let birthdate: String? | ||
public let zoneinfo: String? | ||
public let locale: String? | ||
public let updatedAt: Int? | ||
|
||
// OpenIDConnectEmail | ||
public let email: String? | ||
public let emailVerified: Bool? | ||
|
||
// OpenIDConnectPhone | ||
public let phoneNumber: String? | ||
public let phoneNumberVerified: Bool? | ||
|
||
// OpenIDConnectAddress | ||
public let address: OIDAddress? | ||
|
||
// OIDAddress | ||
public struct OIDAddress: Codable { | ||
let formatted: String; | ||
let streetAddress: String; | ||
let locality: String; | ||
let region: String; | ||
let postalCode: String; | ||
let country: String; | ||
} | ||
} |
This file contains 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,51 @@ | ||
// | ||
// OAuthChallenge.swift | ||
// MagicExt-OAuth | ||
// | ||
// Created by Wentao Liu on 9/24/20. | ||
// | ||
|
||
import Foundation | ||
import MagicSDK | ||
import CryptoSwift | ||
|
||
internal class OAuthChallenge { | ||
var state: String | ||
let verifier: String | ||
let challenge: String | ||
|
||
init() { | ||
self.state = createRandomString(size: 128) | ||
self.verifier = createRandomString(size: 128) | ||
self.challenge = hexToBase64URLSafe(self.verifier.sha256()) | ||
} | ||
} | ||
|
||
func createRandomString(size: Int) -> String { | ||
let letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~" | ||
return String((0..<size).map{ _ in letters.randomElement()! }) | ||
} | ||
|
||
func hexToBase64URLSafe(_ hexString: String) -> String { | ||
|
||
/// Create `Data` from hexadecimal string representation | ||
/// | ||
/// This creates a `Data` object from hex string. Note, if the string has any spaces or non-hex characters (e.g. starts with '<' and with a '>'), those are ignored and only hex characters are processed. | ||
/// | ||
/// - returns: Data represented by this hexadecimal string. | ||
|
||
var data = Data(capacity: hexString.count / 2) | ||
|
||
let regex = try! NSRegularExpression(pattern: "[0-9a-f]{1,2}", options: .caseInsensitive) | ||
regex.enumerateMatches(in: hexString, range: NSRange(hexString.startIndex..., in: hexString)) { match, _, _ in | ||
let byteString = (hexString as NSString).substring(with: match!.range) | ||
let num = UInt8(byteString, radix: 16)! | ||
data.append(num) | ||
} | ||
|
||
guard data.count > 0 else { return "" } | ||
|
||
/// Remove or replace +, /, = | ||
let base64String = data.base64EncodedString() | ||
return base64String.replacingOccurrences(of: "+", with: "-").replacingOccurrences(of: "/", with: "_").replacingOccurrences(of: "=", with: "") | ||
} |
This file contains 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,7 @@ | ||
import XCTest | ||
@testable import MagicExt_OAuth | ||
|
||
final class MagicExt_OAuthTests: XCTestCase { | ||
func testExample() throws { | ||
} | ||
} |