Skip to content

Commit

Permalink
Reject operation fix (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
kober32 committed Aug 30, 2022
1 parent 8333c32 commit c42ec78
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 79 deletions.
8 changes: 4 additions & 4 deletions WultraMobileTokenSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
DCD8B336246C1BAF00385F02 /* WMTRejectionReason.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCD8B335246C1BAF00385F02 /* WMTRejectionReason.swift */; };
DCE5EAB026BD81150061861A /* WMTOperationHistoryEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE5EAAF26BD81150061861A /* WMTOperationHistoryEntry.swift */; };
DCE660D124CEBECA00870E53 /* IntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE660D024CEBECA00870E53 /* IntegrationTests.swift */; };
DCE660D324CEF56400870E53 /* IntegrationUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE660D224CEF56400870E53 /* IntegrationUtils.swift */; };
DCE660D324CEF56400870E53 /* IntegrationProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCE660D224CEF56400870E53 /* IntegrationProxy.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -108,7 +108,7 @@
DCD8B335246C1BAF00385F02 /* WMTRejectionReason.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WMTRejectionReason.swift; sourceTree = "<group>"; };
DCE5EAAF26BD81150061861A /* WMTOperationHistoryEntry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WMTOperationHistoryEntry.swift; sourceTree = "<group>"; };
DCE660D024CEBECA00870E53 /* IntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntegrationTests.swift; sourceTree = "<group>"; };
DCE660D224CEF56400870E53 /* IntegrationUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntegrationUtils.swift; sourceTree = "<group>"; };
DCE660D224CEF56400870E53 /* IntegrationProxy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntegrationProxy.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -166,7 +166,7 @@
DC616237248508F8000DED17 /* Info.plist */,
DC61624124852B6D000DED17 /* NetworkingObjectsTests.swift */,
DCE660D024CEBECA00870E53 /* IntegrationTests.swift */,
DCE660D224CEF56400870E53 /* IntegrationUtils.swift */,
DCE660D224CEF56400870E53 /* IntegrationProxy.swift */,
DC395C0924E55B9B0007C36E /* PushParserTests.swift */,
DC6EDB7825A49ED900A229E4 /* OperationExpirationTests.swift */,
DC616235248508F8000DED17 /* QROperationParserTests.swift */,
Expand Down Expand Up @@ -461,7 +461,7 @@
DC6EDB7925A49ED900A229E4 /* OperationExpirationTests.swift in Sources */,
DC616236248508F8000DED17 /* QROperationParserTests.swift in Sources */,
DCE660D124CEBECA00870E53 /* IntegrationTests.swift in Sources */,
DCE660D324CEF56400870E53 /* IntegrationUtils.swift in Sources */,
DCE660D324CEF56400870E53 /* IntegrationProxy.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ class WMTOperationsImpl<T: WMTUserOperation>: WMTOperations {
) { _, error in
assert(Thread.isMainThread)
if let error = error {
self.operationsRegister.remove(operation: operation)
completion(.failure(self.adjustOperationError(error, auth: false)))
} else {
self.operationsRegister.remove(operation: operation)
completion(.success(()))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,41 @@ import PowerAuth2
import WultraMobileTokenSDK
import WultraPowerAuthNetworking

class IntegrationUtils {
class IntegrationProxy {

private static var config: IntegrationConfig!
private static let activationName = UUID().uuidString
private static var registrationId = "" // will be filled when activation is created
private(set) var powerAuth: PowerAuthSDK?
private(set) var operations: WMTOperations?

typealias Callback = (_ instances: (PowerAuthSDK, WMTOperations)?, _ error: String?) -> Void
private var config: IntegrationConfig!
private let activationName = UUID().uuidString
private var registrationId = "" // will be filled when activation is created

class func prepareActivation(pin: String, callback: @escaping Callback) {
typealias Callback = (_ error: String?) -> Void

func prepareActivation(pin: String, callback: @escaping Callback) {

guard let configPath = Bundle.init(for: IntegrationUtils.self).path(forResource: "config", ofType: "json", inDirectory: "Configs") else {
callback(nil, "Config file config.json is not present.")
guard let configPath = Bundle.init(for: IntegrationProxy.self).path(forResource: "config", ofType: "json", inDirectory: "Configs") else {
callback("Config file config.json is not present.")
return
}

do {
let configContent = try String(contentsOfFile: configPath)
config = try JSONDecoder().decode(IntegrationConfig.self, from: configContent.data(using: .utf8)!)
} catch _ {
callback(nil, "Config file config.json cannot be parsed.")
callback("Config file config.json cannot be parsed.")
return
}

let pa = preparePAInstance()
enrollPAInstance(pa: pa, pin: pin) { error in
if let error = error {
callback(nil, error)
callback(error)
} else {
let wpnConf = WPNConfig(baseUrl: URL(string: config.operationsServerUrl)!, sslValidation: .noValidation)
callback((pa,pa.createWMTOperations(networkingConfig: wpnConf, pollingOptions: [.pauseWhenOnBackground])), nil)
let wpnConf = WPNConfig(baseUrl: URL(string: self.config.operationsServerUrl)!, sslValidation: .noValidation)
self.powerAuth = pa
self.operations = pa.createWMTOperations(networkingConfig: wpnConf, pollingOptions: [.pauseWhenOnBackground])
callback(nil)
}
}
}
Expand All @@ -58,14 +63,14 @@ class IntegrationUtils {
case F_2FA
}

class func createOperation(_ factors: Factors = .F_2FA, completion: @escaping (OperationObject?) -> Void) {
func createOperation(_ factors: Factors = .F_2FA, completion: @escaping (OperationObject?) -> Void) {
DispatchQueue.global().async {
let opBody: String
switch factors {
case .F_2FA:
opBody = """
{
"userId": "\(activationName)",
"userId": "\(self.activationName)",
"template": "login",
"parameters": {
"party.id": "666",
Expand All @@ -77,30 +82,30 @@ class IntegrationUtils {
"""
}

completion(self.makeRequest(url: URL(string: "\(config.cloudServerUrl)/v2/operations")!, body: opBody))
completion(self.makeRequest(url: URL(string: "\(self.config.cloudServerUrl)/v2/operations")!, body: opBody))
}
}

class func getQROperation(operation: OperationObject, completion: @escaping (QROperationData?) -> Void) {
func getQROperation(operation: OperationObject, completion: @escaping (QROperationData?) -> Void) {
DispatchQueue.global().async {
completion(self.makeRequest(url: URL(string: "\(config.cloudServerUrl)/v2/operations/\(operation.operationId)/offline/qr?registrationId=\(registrationId)")!, body: "", httpMethod: "GET"))
completion(self.makeRequest(url: URL(string: "\(self.config.cloudServerUrl)/v2/operations/\(operation.operationId)/offline/qr?registrationId=\(self.registrationId)")!, body: "", httpMethod: "GET"))
}
}

class func verifyQROperation(operation: OperationObject, operationData: QROperationData, otp: String, completion: @escaping (QROperationVerify?) -> Void) {
func verifyQROperation(operation: OperationObject, operationData: QROperationData, otp: String, completion: @escaping (QROperationVerify?) -> Void) {
DispatchQueue.global().async {
let body = """
{
"otp": "\(otp)",
"nonce": "\(operationData.nonce)",
"registrationId": "\(registrationId)"
"registrationId": "\(self.registrationId)"
}
"""
completion(self.makeRequest(url: URL(string: "\(config.cloudServerUrl)/v2/operations/\(operation.operationId)/offline/otp")!, body: body))
completion(self.makeRequest(url: URL(string: "\(self.config.cloudServerUrl)/v2/operations/\(operation.operationId)/offline/otp")!, body: body))
}
}

private class func makeRequest<T: Codable>(url: URL, body: String, httpMethod: String = "POST") -> T? {
private func makeRequest<T: Codable>(url: URL, body: String, httpMethod: String = "POST") -> T? {
var r = URLRequest(url: url)
let creds = "\(config.cloudServerLogin):\(config.cloudServerPassword)".data(using: .utf8)?.base64EncodedString() ?? ""
r.httpMethod = httpMethod
Expand All @@ -119,7 +124,7 @@ class IntegrationUtils {
return result
}

private class func preparePAInstance() -> PowerAuthSDK {
private func preparePAInstance() -> PowerAuthSDK {

let cfg = PowerAuthConfiguration()
cfg.instanceId = "tests"
Expand All @@ -132,7 +137,7 @@ class IntegrationUtils {
return PowerAuthSDK(configuration: cfg)!
}

private class func enrollPAInstance(pa: PowerAuthSDK, pin: String, callback: @escaping (String?) -> Void) {
private func enrollPAInstance(pa: PowerAuthSDK, pin: String, callback: @escaping (String?) -> Void) {
guard let act = createActivation() else {
callback("Create activation on server failed.")
return
Expand All @@ -149,15 +154,15 @@ class IntegrationUtils {
callback("Commit activation locally failed.")
return
}
guard let _ = commitActivationOnServer(registrationId: act.registrationId) else {
guard let _ = self.commitActivationOnServer(registrationId: act.registrationId) else {
callback("Commit on server failed.")
return
}
callback(nil)
}
}

private class func createActivation() -> RegistrationObject? {
private func createActivation() -> RegistrationObject? {
let body = """
{
"userId": "\(activationName)",
Expand All @@ -169,7 +174,7 @@ class IntegrationUtils {
return resp
}

private class func commitActivationOnServer(registrationId: String) -> CommitObject? {
private func commitActivationOnServer(registrationId: String) -> CommitObject? {
let body = """
{
"externalUserId": "test"
Expand Down
Loading

0 comments on commit c42ec78

Please sign in to comment.