Skip to content

Commit

Permalink
implemet out own js
Browse files Browse the repository at this point in the history
  • Loading branch information
mastahyeti committed Aug 23, 2016
1 parent a56c218 commit 6077f56
Show file tree
Hide file tree
Showing 27 changed files with 360 additions and 3,637 deletions.
875 changes: 83 additions & 792 deletions Extension/Action.js

Large diffs are not rendered by default.

134 changes: 80 additions & 54 deletions Extension/ActionRequestHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,6 @@ import MobileCoreServices
class ActionRequestHandler: NSObject, NSExtensionRequestHandling {

var extensionContext: NSExtensionContext?

func generateOrGetKeyWithName(name:String) -> NSData? {
if KeyInterface.publicKeyExists(name) {
print("key pair exists")
} else {
if KeyInterface.generateTouchIDKeyPair(name) {
print("generated key pair")
} else {
print("error generating key pair")
return nil
}
}

return KeyInterface.publicKeyBits(name)
}

func beginRequestWithExtensionContext(context: NSExtensionContext) {
self.extensionContext = context
Expand Down Expand Up @@ -59,56 +44,112 @@ class ActionRequestHandler: NSObject, NSExtensionRequestHandling {
}

func itemLoadCompletedWithPreprocessingResults(javaScriptValues: NSDictionary) {
guard let reqType = javaScriptValues["type"] as? String else {
print("bad request type")
return
}

switch reqType {
case "enroll":
handleEnrollRequest(javaScriptValues)
case "sign":
handleSignRequest(javaScriptValues)
default:
print("bad request")
return
}
}

func handleEnrollRequest(javaScriptValues: NSDictionary) {
guard
let appId = javaScriptValues["appId"] as? String,
let jsonToSign = javaScriptValues["toSign"] as? String,
let toSign = decodeJsonByteArrayAsString(jsonToSign)
else {
print("bad data from JavaScript")
return
else {
print("bad data from JavaScript")
return
}

guard
let key = generateOrGetKeyWithName(appId),
let strKey = String(data: key, encoding: NSASCIIStringEncoding)
else {
print("error generating or finding key")
return
else {
print("error generating or finding key")
return
}

guard let ssc = SelfSignedCertificate()
else {
print("error generating certificate")
return
else {
print("error generating certificate")
return
}

let fullToSign = NSMutableData()
fullToSign.appendData(toSign)
fullToSign.appendData(key)

guard let sig = ssc.signData(fullToSign)
else {
print("error signing register request")
return
else {
print("error signing register request")
return
}

self.doneWithResults([
"signature": sig,
"publicKey": strKey,
"certificate": ssc.toDer()
])
}

func handleSignRequest(javaScriptValues: NSDictionary) {
guard
let appId = javaScriptValues["appId"] as? String,
let jsonToSign = javaScriptValues["toSign"] as? String,
let toSign = decodeJsonByteArrayAsString(jsonToSign)
else {
print("bad data from JavaScript")
return
}

KeyInterface.generateSignatureForData(toSign, withKeyName: appId) { (sig, err) in
if err == nil {
let strSig = String(data: sig, encoding: NSASCIIStringEncoding)!
self.doneWithResults(["signature": strSig])
} else {
print("failed to sign message")
return
}
}
}

func doneWithResults(resultsForJavaScriptFinalizeArg: [NSObject: AnyObject]?) {
if let resultsForJavaScriptFinalize = resultsForJavaScriptFinalizeArg {
let resultsDictionary = [NSExtensionJavaScriptFinalizeArgumentKey: resultsForJavaScriptFinalize]
let resultsProvider = NSItemProvider(item: resultsDictionary, typeIdentifier: String(kUTTypePropertyList))
let resultsItem = NSExtensionItem()
resultsItem.attachments = [resultsProvider]

self.extensionContext!.completeRequestReturningItems([resultsItem], completionHandler: nil)
} else {
self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
}

// KeyInterface.generateSignatureForData(toSign, withKeyName: appId) { (sig, err) in
// if err == nil {
// let strSig = String(data: sig, encoding: NSASCIIStringEncoding)!
// let strKey = String(data: key, encoding: NSASCIIStringEncoding)!
//
// self.doneWithResults(["signature": strSig, "publicKey": strKey])
// } else {
// self.doneWithResults(["error": "failed to sign message"])
// }
// }
self.extensionContext = nil
}

func generateOrGetKeyWithName(name:String) -> NSData? {
if KeyInterface.publicKeyExists(name) {
print("key pair exists")
} else {
if KeyInterface.generateTouchIDKeyPair(name) {
print("generated key pair")
} else {
print("error generating key pair")
return nil
}
}

return KeyInterface.publicKeyBits(name)
}

// There were encoding issues passing a binary string from JavaScript. My shitty solution is to
Expand All @@ -118,7 +159,7 @@ class ActionRequestHandler: NSObject, NSExtensionRequestHandling {
print("error converting raw to data")
return nil
}

do {
if let ints = try NSJSONSerialization.JSONObjectWithData(rawData, options: []) as? [Int] {
let uints = ints.map { UInt8($0) }
Expand All @@ -132,19 +173,4 @@ class ActionRequestHandler: NSObject, NSExtensionRequestHandling {
return nil
}
}

func doneWithResults(resultsForJavaScriptFinalizeArg: [NSObject: AnyObject]?) {
if let resultsForJavaScriptFinalize = resultsForJavaScriptFinalizeArg {
let resultsDictionary = [NSExtensionJavaScriptFinalizeArgumentKey: resultsForJavaScriptFinalize]
let resultsProvider = NSItemProvider(item: resultsDictionary, typeIdentifier: String(kUTTypePropertyList))
let resultsItem = NSExtensionItem()
resultsItem.attachments = [resultsProvider]

self.extensionContext!.completeRequestReturningItems([resultsItem], completionHandler: nil)
} else {
self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
}

self.extensionContext = nil
}
}
Loading

0 comments on commit 6077f56

Please sign in to comment.