Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from magiclabs/feat/open_source
Browse files Browse the repository at this point in the history
Migrated from fortmatic org
  • Loading branch information
Ethella authored Jan 21, 2022
2 parents 5ced141 + 83bc8ee commit 817ea95
Show file tree
Hide file tree
Showing 15 changed files with 558 additions and 9 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Package.resolved
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
Expand Down Expand Up @@ -88,3 +95,4 @@ fastlane/test_output
# https://github.com/johnno1962/injectionforxcode

iOSInjectionProject/

12 changes: 5 additions & 7 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
MIT License

Copyright (c) 2022 Magic
Copyright (c) 2020 Magic Labs Inc. <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -9,13 +7,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
27 changes: 27 additions & 0 deletions MagicExt-OAuth.podspec
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' => '[email protected]' }
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
30 changes: 30 additions & 0 deletions Package.swift
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"]),
]
)
44 changes: 42 additions & 2 deletions README.md
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!

---

141 changes: 141 additions & 0 deletions Sources/MagicExt-OAuth/Core/OAuthExtension.swift
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
}
}
}
60 changes: 60 additions & 0 deletions Sources/MagicExt-OAuth/Core/ShimASViewController.swift
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()
}
}
48 changes: 48 additions & 0 deletions Sources/MagicExt-OAuth/Core/ShimSFASViewController.swift
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()
}
}
Loading

0 comments on commit 817ea95

Please sign in to comment.