Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

iOS shell : access secured files

William Balcaenc564a42704f3
diff --git a/console/iOS/CustomURLProtocol.swift b/console/iOS/CustomURLProtocol.swift
new file mode 100644
index 000000000..e4f01a366
--- /dev/null
+++ b/console/iOS/CustomURLProtocol.swift
@@ -0,0 +1,88 @@
+//
+// CustomURLProtocol.swift
+// console
+//
+// Created by William Balcaen on 16/02/17.
+// Copyright © 2017 TInSys. All rights reserved.
+//
+
+import Foundation
+class CustomURLProtocol: URLProtocol, URLSessionDataDelegate, URLSessionTaskDelegate {
+ private var dataTask: URLSessionDataTask?
+ private var urlResponse: URLResponse?
+ private var receivedData: NSMutableData?
+ public static var accessToken : String?
+ class var CustomHeaderSet: String {
+ return "CustomHeaderSet"
+ }
+
+ // MARK: NSURLProtocol
+
+ override class func canInit(with request: URLRequest) -> Bool {
+ guard let host = request.url?.host, host == "192.168.99.100" else {
+ return false
+ }
+ if (URLProtocol.property(forKey: CustomURLProtocol.CustomHeaderSet, in: request as URLRequest) != nil) {
+ return false
+ }
+
+ return true
+ }
+
+ override class func canonicalRequest(for request: URLRequest) -> URLRequest {
+ return request
+ }
+
+ override func startLoading() {
+
+ let mutableRequest = NSMutableURLRequest.init(url: self.request.url!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 240.0)
+
+ //Add Custom headers
+
+ mutableRequest.setValue(String(format: "Bearer %@",CustomURLProtocol.accessToken!), forHTTPHeaderField: "Authorization")
+ mutableRequest.setValue("master", forHTTPHeaderField: "Auth-Realm")
+ URLProtocol.setProperty("true", forKey: CustomURLProtocol.CustomHeaderSet, in: mutableRequest)
+ let defaultConfigObj = URLSessionConfiguration.default
+ let defaultSession = URLSession(configuration: defaultConfigObj, delegate: self, delegateQueue: nil)
+ self.dataTask = defaultSession.dataTask(with: mutableRequest as URLRequest)
+ self.dataTask!.resume()
+
+ }
+
+ override func stopLoading() {
+ self.dataTask?.cancel()
+ self.dataTask = nil
+ self.receivedData = nil
+ self.urlResponse = nil
+ }
+
+ // MARK: NSURLSessionDataDelegate
+
+ func urlSession(_ session: URLSession, dataTask: URLSessionDataTask,
+ didReceive response: URLResponse,
+ completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
+
+ self.client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
+
+ self.urlResponse = response
+ self.receivedData = NSMutableData()
+
+ completionHandler(.allow)
+ }
+
+ func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
+ self.client?.urlProtocol(self, didLoad: data as Data)
+
+ self.receivedData?.append(data as Data)
+ }
+
+ // MARK: NSURLSessionTaskDelegate
+
+ func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
+ if error != nil {
+ self.client?.urlProtocol(self, didFailWithError: error!)
+ } else {
+ self.client?.urlProtocolDidFinishLoading(self)
+ }
+ }
+}
diff --git a/console/iOS/console.xcodeproj/project.pbxproj b/console/iOS/console.xcodeproj/project.pbxproj
index 7f606c386..8487b9ab6 100644
--- a/console/iOS/console.xcodeproj/project.pbxproj
+++ b/console/iOS/console.xcodeproj/project.pbxproj
@@ -14,6 +14,7 @@
FA1D73E91E53374E00F6240A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FA1D73E81E53374E00F6240A /* Assets.xcassets */; };
FA1D73EC1E53374E00F6240A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FA1D73EA1E53374E00F6240A /* LaunchScreen.storyboard */; };
FA1D73F61E53392F00F6240A /* ORViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA1D73F51E53392F00F6240A /* ORViewController.swift */; };
+ FA2D482F1E55F8F300C95665 /* CustomURLProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA2D482E1E55F8F300C95665 /* CustomURLProtocol.swift */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
@@ -28,6 +29,7 @@
FA1D73EB1E53374E00F6240A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
FA1D73ED1E53374E00F6240A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
FA1D73F51E53392F00F6240A /* ORViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ORViewController.swift; sourceTree = "<group>"; };
+ FA2D482E1E55F8F300C95665 /* CustomURLProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = CustomURLProtocol.swift; path = ../CustomURLProtocol.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -83,6 +85,7 @@
FA1D73E11E53374E00F6240A /* AppDelegate.swift */,
FA1D73E31E53374E00F6240A /* ViewController.swift */,
FA1D73F51E53392F00F6240A /* ORViewController.swift */,
+ FA2D48291E55D9F600C95665 /* utils */,
FA1D73E51E53374E00F6240A /* Main.storyboard */,
FA1D73E81E53374E00F6240A /* Assets.xcassets */,
FA1D73EA1E53374E00F6240A /* LaunchScreen.storyboard */,
@@ -91,6 +94,14 @@
path = console;
sourceTree = "<group>";
};
+ FA2D48291E55D9F600C95665 /* utils */ = {
+ isa = PBXGroup;
+ children = (
+ FA2D482E1E55F8F300C95665 /* CustomURLProtocol.swift */,
+ );
+ name = utils;
+ sourceTree = "<group>";
+ };
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
@@ -216,6 +227,7 @@
buildActionMask = 2147483647;
files = (
FA1D73E41E53374E00F6240A /* ViewController.swift in Sources */,
+ FA2D482F1E55F8F300C95665 /* CustomURLProtocol.swift in Sources */,
FA1D73F61E53392F00F6240A /* ORViewController.swift in Sources */,
FA1D73E21E53374E00F6240A /* AppDelegate.swift in Sources */,
);
diff --git a/console/iOS/console/AppDelegate.swift b/console/iOS/console/AppDelegate.swift
index 2baabc473..4a1d3e0ff 100644
--- a/console/iOS/console/AppDelegate.swift
+++ b/console/iOS/console/AppDelegate.swift
@@ -16,7 +16,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
- // Override point for customization after application launch.
+ // allows to catch all request and add custom headers (works only with UIWebView not WKWebView).
+ URLProtocol.registerClass(CustomURLProtocol.self)
return true
}
diff --git a/console/iOS/console/Info.plist b/console/iOS/console/Info.plist
index c84ab7bee..e75960e3f 100644
--- a/console/iOS/console/Info.plist
+++ b/console/iOS/console/Info.plist
@@ -16,17 +16,17 @@
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
- <key>CFBundleURLTypes</key>
- <array>
- <dict>
- <key>CFBundleTypeRole</key>
- <string>Editor</string>
- <key>CFBundleURLSchemes</key>
- <array>
- <string>org.openremote.console</string>
- </array>
- </dict>
- </array>
+ <key>CFBundleURLTypes</key>
+ <array>
+ <dict>
+ <key>CFBundleTypeRole</key>
+ <string>Editor</string>
+ <key>CFBundleURLSchemes</key>
+ <array>
+ <string>org.openremote.console</string>
+ </array>
+ </dict>
+ </array>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
@@ -52,5 +52,7 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
+ <key>NSPhotoLibraryUsageDescription</key>
+ <string>$(PRODUCT_NAME) photo use</string>
</dict>
</plist>
diff --git a/console/iOS/console/ORViewController.swift b/console/iOS/console/ORViewController.swift
index afbb48c74..2e8201548 100644
--- a/console/iOS/console/ORViewController.swift
+++ b/console/iOS/console/ORViewController.swift
@@ -10,26 +10,28 @@ import Foundation
import UIKit
import WebKit
-class ORViewcontroller : UIViewController, WKUIDelegate, WKNavigationDelegate {
+class ORViewcontroller : UIViewController, URLSessionDelegate, UIWebViewDelegate {
+ var accessToken : String = ""
override func viewDidLoad() {
super.viewDidLoad()
- navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(done))
+
+ CustomURLProtocol.accessToken = accessToken
+
let webConfiguration = WKWebViewConfiguration()
webConfiguration.preferences.javaScriptEnabled = true;
webConfiguration.ignoresViewportScaleLimits = true;
webConfiguration.preferences.javaScriptCanOpenWindowsAutomatically = true;
- let myWebView = WKWebView(frame: .zero, configuration: webConfiguration)
- myWebView.frame = view.frame
+ let myWebView = UIWebView(frame: view.frame)
view.addSubview(myWebView)
+ myWebView.delegate = self
- myWebView.load(URLRequest(url: URL(string: "http://192.168.99.100:8080/console/master/index.html")!))
- }
-
- func done() {
- self.dismiss(animated: true, completion: nil)
+ let url = URL(string: "http://192.168.99.100:8080/console/index.html")
+ let request = URLRequest(url: url!)
+
+ myWebView.loadRequest(request)
}
-}
+ }
diff --git a/console/iOS/console/ViewController.swift b/console/iOS/console/ViewController.swift
index a2d0d5a8f..9588bc06f 100644
--- a/console/iOS/console/ViewController.swift
+++ b/console/iOS/console/ViewController.swift
@@ -33,17 +33,30 @@ class ViewController: UIViewController {
keycloakConfig.isWebView = true
let oauth2Module = AccountManager.addAccountWith(config: keycloakConfig, moduleClass: OAuth2Module.self)
http.authzModule = oauth2Module
+ http.authzModule = oauth2Module
+ requestAccess(oauth2Module: oauth2Module)
+
+ }
+
+ func requestAccess(oauth2Module : OAuth2Module) {
oauth2Module.requestAccess { (response, error) in
var token : String
if response != nil {
token = response! as! String
- print(token)
let orVC = ORViewcontroller()
+ orVC.accessToken = token
self.navigationController?.pushViewController(orVC, animated: true)
} else if error != nil {
- let alertVC = UIAlertController(title: "Error", message: error.debugDescription, preferredStyle: UIAlertControllerStyle.alert)
+ let alertVC = UIAlertController(title: "Error", message: error.debugDescription, preferredStyle: UIAlertControllerStyle.actionSheet)
+ let defaultAction = UIAlertAction(title: "Retry", style: .default, handler: { (alertAction) in
+ oauth2Module.requestAuthorizationCode(completionHandler: { (_, error) in
+ self.requestAccess(oauth2Module: oauth2Module)
+ })
+ })
+ alertVC.addAction(defaultAction)
+
self.present(alertVC, animated: true, completion: nil)
}
}