Skip to content

Commit

Permalink
Merge pull request #62 from ConstantinKV/redirect
Browse files Browse the repository at this point in the history
[3.3.10]: Fix pinning & [Example]: Add redirect oAuth providers to SFSafariViewController
  • Loading branch information
ConstantinKV authored Feb 4, 2021
2 parents 0fc430e + abf32ee commit e5f3066
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 23 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [3.3.10] - 2021-02-03

### Fixed.

- Fixed SSL pinning.


## [3.3.9] - 2020-11-02

### Added.
Expand Down
4 changes: 4 additions & 0 deletions Example/saltedge-ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
607FACDB1AFB9204008FA782 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 607FACD91AFB9204008FA782 /* Main.storyboard */; };
607FACDD1AFB9204008FA782 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDC1AFB9204008FA782 /* Images.xcassets */; };
607FACE01AFB9204008FA782 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDE1AFB9204008FA782 /* LaunchScreen.xib */; };
95316E0325CAAD0400FAB796 /* UIApplicationExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95316E0225CAAD0400FAB796 /* UIApplicationExtensions.swift */; };
9D0E7BA222F426C9002D4DE9 /* MainTabBarViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9D0E7BA122F426C9002D4DE9 /* MainTabBarViewController.swift */; };
9D98546D230FF7C300913C3D /* InteractiveAlertView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9D98546C230FF7C300913C3D /* InteractiveAlertView.swift */; };
AE05129D201F3EE1000E5F64 /* OptionsButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE05129C201F3EE1000E5F64 /* OptionsButton.swift */; };
Expand Down Expand Up @@ -71,6 +72,7 @@
85538029745105922B2922DF /* SaltEdge-iOS-Swift.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = "SaltEdge-iOS-Swift.podspec"; path = "../SaltEdge-iOS-Swift.podspec"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.ruby; };
9091C52E7B273302ABC0152C /* Pods_saltedge_ios_Example.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_saltedge_ios_Example.framework; sourceTree = BUILT_PRODUCTS_DIR; };
9272D600EC690297D58E6E14 /* Pods-saltedge-ios_Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-saltedge-ios_Example.debug.xcconfig"; path = "Pods/Target Support Files/Pods-saltedge-ios_Example/Pods-saltedge-ios_Example.debug.xcconfig"; sourceTree = "<group>"; };
95316E0225CAAD0400FAB796 /* UIApplicationExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIApplicationExtensions.swift; sourceTree = "<group>"; };
9D0E7BA122F426C9002D4DE9 /* MainTabBarViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainTabBarViewController.swift; sourceTree = "<group>"; };
9D536B3222526F4E00410CF2 /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; name = CHANGELOG.md; path = ../CHANGELOG.md; sourceTree = "<group>"; };
9D98546C230FF7C300913C3D /* InteractiveAlertView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InteractiveAlertView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -270,6 +272,7 @@
AE299B4220178F440033FCFB /* UserDefaultsHelper.swift */,
AEEF47A2201B82CE0076CD98 /* StringExtensions.swift */,
FAC9BA6D22B2402200310F6B /* LocaleExtensions.swift */,
95316E0225CAAD0400FAB796 /* UIApplicationExtensions.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -528,6 +531,7 @@
AE299B492017A8F80033FCFB /* TransactionsViewController.swift in Sources */,
AE299B4E2017AD4E0033FCFB /* TransactionTableViewCell.swift in Sources */,
AEEF47A3201B82CE0076CD98 /* StringExtensions.swift in Sources */,
95316E0325CAAD0400FAB796 /* UIApplicationExtensions.swift in Sources */,
AE9F52CA20164A7A0057FE09 /* CreateViewController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
31 changes: 23 additions & 8 deletions Example/saltedge-ios/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@
import UIKit
import PKHUD
import SaltEdge
import SafariServices

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
static let returnToApplicationUrl: String = "saltedge://sdk.example" // the URL has to have a host, otherwise won't be a valid URL on the backend
static let customerIdPrefix: String = "ios-sdk-customer-identifier"

var window: UIWindow?

var tabBar: UITabBarController? {
if let tabBar = window?.rootViewController as? UITabBarController {
return tabBar
}
return nil
return window?.rootViewController as? UITabBarController
}

var createViewController: CreateViewController? {
Expand All @@ -42,6 +41,23 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {

if let connectionSecret = url.connectionSecret {
UserDefaultsHelper.append(connectionSecret: connectionSecret)
}

if let safariVc = UIApplication.topViewController as? SFSafariViewController {
safariVc.dismiss(animated: true)
}

if let connectVC = connectViewController {

let alert = UIAlertController(title: "Redirect", message: "Redirect finished.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Close", style: .default, handler: nil))
connectVC.present(alert, animated: true)
connectVC.switchToConnectionsController()
}

if let createVC = createViewController {
HUD.show(.labeledProgress(title: "Fetching OAuth Connection", subtitle: nil))
SERequestManager.shared.handleOpen(url: url, connectionFetchingDelegate: createVC)
Expand All @@ -51,9 +67,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let appId: String = "your-app-id"
let appSecret: String = "your-app-secret"
let customerId: String = "customer-secret"
let appId: String = "application-id"
let appSecret: String = "application-secret"

// By default SSL Pinning is enabled, to disable it use:
// SERequestManager.shared.set(sslPinningEnabled: false)
Expand All @@ -67,7 +82,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
if let secret = UserDefaultsHelper.customerSecret {
SERequestManager.shared.set(customerSecret: secret)
} else {
let params = SECustomerParams(identifier: customerId)
let params = SECustomerParams(identifier: "\(AppDelegate.customerIdPrefix)-\(UUID().uuidString)")
SERequestManager.shared.createCustomer(with: params) { response in
switch response {
case .success(let value):
Expand Down
15 changes: 15 additions & 0 deletions Example/saltedge-ios/Utils/UIApplicationExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// UIWindowExtensions.swift
// saltedge-ios_Example
//
// Created by Constantin Chelban on 3/2/21.
// Copyright © 2021 CocoaPods. All rights reserved.
//

import UIKit

extension UIApplication {
static var topViewController: UIViewController? {
return shared.windows.first?.rootViewController
}
}
23 changes: 17 additions & 6 deletions Example/saltedge-ios/View Controllers/ConnectViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

import UIKit
import SaltEdge
import WebKit
import PKHUD
import WebKit
import SafariServices

final class ConnectViewController: UIViewController {
private var provider: SEProvider?
Expand Down Expand Up @@ -70,6 +71,10 @@ final class ConnectViewController: UIViewController {
createSession()
}
}

func switchToConnectionsController() {
tabBarController?.selectedIndex = 2
}

private func handleConnectSessionResponse(_ response: SEResult<SEResponse<SEConnectSessionResponse>>) {
switch response {
Expand All @@ -79,8 +84,8 @@ final class ConnectViewController: UIViewController {
guard let url = URL(string: value.data.connectUrl) else { return }

// Check if provider mode is "oauth". Then try to open it in external browser or app.
if let provider = self.provider, provider.isOAuth, UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
if let provider = self.provider, provider.isOAuth {
openLinkInSafari(url)
} else {
let request = URLRequest(url: url)
webView.load(request)
Expand All @@ -90,9 +95,15 @@ final class ConnectViewController: UIViewController {
HUD.flash(.labeledError(title: "Error", subtitle: error.localizedDescription), delay: 3.0)
}
}

private func switchToConnectionsController() {
tabBarController?.selectedIndex = 2

private func openLinkInSafari(_ redirectURL: URL) {
let vc = SFSafariViewController(url: redirectURL)
vc.modalPresentationStyle = .overCurrentContext
if #available(iOS 10.0, *) {
vc.preferredBarTintColor = .systemBlue
vc.preferredControlTintColor = .white
}
UIApplication.topViewController?.present(vc, animated: true)
}

private func createSession() {
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Last SDK version (3+) supports Salt Edge API v5.
for Salt Edge API v5 use

```ruby
pod 'SaltEdge-iOS-Swift', '~> 3.3.9'
pod 'SaltEdge-iOS-Swift', '~> 3.3.10'
```

for Salt Edge API v4 (***Deprecated***) use
Expand Down Expand Up @@ -242,7 +242,7 @@ Set up the `appId`, `appSecret` and `customerId` constants to your App ID and c

## Versioning

The current version of the SDK is [3.3.9](https://github.com/saltedge/saltedge-ios-swift/releases/tag/3.3.9), and supports the latest available version of Salt Edge API. Any backward-incompatible changes in the API will result in changes to the SDK.
The current version of the SDK is [3.3.10](https://github.com/saltedge/saltedge-ios-swift/releases/tag/3.3.10), and supports the latest available version of Salt Edge API. Any backward-incompatible changes in the API will result in changes to the SDK.

## Security

Expand Down
2 changes: 1 addition & 1 deletion SaltEdge-iOS-Swift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'SaltEdge-iOS-Swift'
s.version = '3.3.9'
s.version = '3.3.10'
s.summary = "A handful of classes to help you interact with the Salt Edge API from your iOS or macOS app."
s.description = <<-DESC
SaltEdge-iOS is a library targeted at easing the interaction with the [Salt Edge API](https://docs.saltedge.com/).
Expand Down
16 changes: 10 additions & 6 deletions saltedge-ios-swift/Classes/API/HTTPService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private class URLSessionPinningDelegate: NSObject, URLSessionDelegate {
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void) {
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust,
let serverTrust = challenge.protectionSpace.serverTrust {

guard isSSLPinningEnabled else {
completionHandler(.useCredential, URLCredential(trust: serverTrust))
return
Expand Down Expand Up @@ -156,12 +156,16 @@ struct HTTPService<T: Decodable> {
}
}

TrustKitHelper.setup { error in
if let error = error {
completion?(.failure(error))
} else {
task.resume()
if SERequestManager.shared.sslPinningEnabled {
TrustKitHelper.setup { error in
if let error = error {
completion?(.failure(error))
} else {
task.resume()
}
}
} else {
task.resume()
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions saltedge-ios-swift/Classes/API/SERequestManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ public class SERequestManager {
private init() {}

public static let shared = SERequestManager()
public var sslPinningEnabled: Bool = true

public func set(sslPinningEnabled: Bool) {
self.sslPinningEnabled = sslPinningEnabled
SessionManager.initializeManager(isSSLPinningEnabled: sslPinningEnabled)
}

Expand Down
4 changes: 4 additions & 0 deletions saltedge-ios-swift/Classes/Extensions/URLExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ extension URL {

return parameters
}

public var connectionSecret: String? {
return queryParameters?["connection_secret"]
}
}

// MARK: SEWebView URL extensions
Expand Down

0 comments on commit e5f3066

Please sign in to comment.