Skip to content

Commit

Permalink
Merge pull request #13 from VirgilSecurity/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
SanjoDeundiak authored Jul 10, 2019
2 parents 8521a00 + c58f88e commit bd0cb0e
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 194 deletions.
1 change: 0 additions & 1 deletion .swift-version

This file was deleted.

2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ before_install:
- set -o pipefail

script:
- openssl aes-256-cbc -K $encrypted_ceba4cc4fb2f_key -iv $encrypted_ceba4cc4fb2f_iv -in config.tar.enc -out config.tar -d
- openssl aes-256-cbc -K $encrypted_0b555ac22efa_key -iv $encrypted_0b555ac22efa_iv -in config.tar.enc -out config.tar -d
- tar xvf config.tar

- |
Expand Down
3 changes: 1 addition & 2 deletions Cartfile
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
github "VirgilSecurity/virgil-sdk-x" ~> 5.7
github "VirgilSecurity/virgil-crypto-x" "5.0.0-alpha5"
github "VirgilSecurity/virgil-sdk-x" ~> 6.0
9 changes: 4 additions & 5 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
github "VirgilSecurity/virgil-crypto-c" "v0.8.0"
github "VirgilSecurity/virgil-crypto-x" "5.0.0-alpha5"
github "VirgilSecurity/virgil-cryptoapi-x" "1.0.6"
github "VirgilSecurity/virgil-cryptowrapper-x" "0.8.0"
github "VirgilSecurity/virgil-sdk-x" "5.8.0"
github "VirgilSecurity/virgil-crypto-c" "v0.8.1"
github "VirgilSecurity/virgil-crypto-x" "5.0.0"
github "VirgilSecurity/virgil-cryptowrapper-x" "0.8.1"
github "VirgilSecurity/virgil-sdk-x" "6.0.0"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ To integrate Virgil Pythia into your Xcode project using CocoaPods, specify it i
target '<Your Target Name>' do
use_frameworks!

pod 'VirgilSDKPythia', '~> 0.5.1'
pod 'VirgilSDKPythia', '~> 0.6.0'
end
```

Expand All @@ -67,7 +67,7 @@ $ brew install carthage
To integrate Virgil Pythia into your Xcode project using Carthage, create an empty file with name *Cartfile* in your project's root folder and add following lines to your *Cartfile*

```
github "VirgilSecurity/virgil-pythia-x" ~> 0.5.1
github "VirgilSecurity/virgil-pythia-x" ~> 0.6.0
```

#### Linking against prebuilt binaries
Expand Down
73 changes: 0 additions & 73 deletions Source/BrainKey/BrainKey+Operations.swift

This file was deleted.

39 changes: 11 additions & 28 deletions Source/BrainKey/BrainKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,12 @@ import VirgilCrypto
@objc public let client: PythiaClientProtocol
/// PythiaCryptoProtocol implementation
@objc public let pythiaCrypto: PythiaCryptoProtocol
/// AccessTokenProvider implementation
@objc public let accessTokenProvider: AccessTokenProvider

/// Initializer
///
/// - Parameter context: BrainKey context
@objc public init(context: BrainKeyContext) {
self.client = context.client
self.accessTokenProvider = context.accessTokenProvider
self.pythiaCrypto = context.pythiaCrypto
}

Expand All @@ -63,37 +60,23 @@ import VirgilCrypto
/// - Returns: GenericOperation with VirgilKeyPair
open func generateKeyPair(password: String, brainKeyId: String? = nil) -> GenericOperation<VirgilKeyPair> {
return CallbackOperation { _, completion in
let tokenContext = TokenContext(service: "pythia", operation: "seed", forceReload: false)
let getTokenOperation = OperationUtils.makeGetTokenOperation(
tokenContext: tokenContext, accessTokenProvider: self.accessTokenProvider)

let blindedResult: BlindResult
do {
blindedResult = try self.pythiaCrypto.blind(password: password)
let blindedResult = try self.pythiaCrypto.blind(password: password)

let seed = try self.client.generateSeed(blindedPassword: blindedResult.blindedPassword,
brainKeyId: brainKeyId)

let deblindedPassword = try self.pythiaCrypto.deblind(transformedPassword: seed,
blindingSecret: blindedResult.blindingSecret)

let keyPair = try self.pythiaCrypto.generateKeyPair(usingSeed: deblindedPassword)

completion(keyPair, nil)
}
catch {
completion(nil, error)
return
}

let seedOperation = self.makeSeedOperation(blindedPassword: blindedResult.blindedPassword,
brainKeyId: brainKeyId)

let generateOperation = self.makeGenerateOperation(blindingSecret: blindedResult.blindingSecret)

let completionOperation = OperationUtils.makeCompletionOperation(completion: completion)

seedOperation.addDependency(getTokenOperation)

generateOperation.addDependency(seedOperation)

completionOperation.addDependency(getTokenOperation)
completionOperation.addDependency(seedOperation)
completionOperation.addDependency(generateOperation)

let queue = OperationQueue()
let operations = [getTokenOperation, seedOperation, generateOperation, completionOperation]
queue.addOperations(operations, waitUntilFinished: false)
}
}
}
14 changes: 4 additions & 10 deletions Source/BrainKey/BrainKeyContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,14 @@ import VirgilCrypto
@objc public let client: PythiaClientProtocol
/// PythiaCryptoProtocol implementation
@objc public let pythiaCrypto: PythiaCryptoProtocol
/// AccessTokenProvider implementation
@objc public let accessTokenProvider: AccessTokenProvider

/// Initializer
///
/// - Parameters:
/// - client: PythiaClientProtocol implementation
/// - pythiaCrypto: PythiaCryptoProtocol implementation
/// - accessTokenProvider: AccessTokenProvider implementation
@objc public init(client: PythiaClientProtocol = PythiaClient(),
/// - keyPairType: Keypair type
@objc public init(client: PythiaClientProtocol,
pythiaCrypto: PythiaCryptoProtocol? = nil,
accessTokenProvider: AccessTokenProvider,
keyPairType: KeyPairType = .ed25519) throws {
self.client = client
if let pythiaCrypto = pythiaCrypto {
Expand All @@ -64,7 +60,6 @@ import VirgilCrypto
let crypto = try VirgilCrypto(defaultKeyType: keyPairType, useSHA256Fingerprints: false)
self.pythiaCrypto = try PythiaCrypto(crypto: crypto)
}
self.accessTokenProvider = accessTokenProvider

super.init()
}
Expand All @@ -74,8 +69,7 @@ import VirgilCrypto
/// - Parameter accessTokenProvider: AccessTokenProvider implementation
/// - Returns: Initialized BrainKeyContext instance
@objc public static func makeContext(accessTokenProvider: AccessTokenProvider) throws -> BrainKeyContext {
return try BrainKeyContext(client: PythiaClient(),
pythiaCrypto: PythiaCrypto(crypto: try VirgilCrypto()),
accessTokenProvider: accessTokenProvider)
return try BrainKeyContext(client: PythiaClient(accessTokenProvider: accessTokenProvider),
pythiaCrypto: PythiaCrypto(crypto: try VirgilCrypto()))
}
}
14 changes: 11 additions & 3 deletions Source/Client/PythiaClient+Queries.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ import VirgilSDK

// MARK: - PythiaClientProtocol implementation
extension PythiaClient: PythiaClientProtocol {
private func createRetry() -> RetryProtocol {
return ExpBackoffRetry(config: self.retryConfig)
}

/// Generates seed using given blinded password and brainkey id
///
/// - Parameters:
Expand All @@ -49,7 +53,7 @@ extension PythiaClient: PythiaClientProtocol {
/// - `PythiaClientError.constructingUrl` if url is not valid
/// - Rethrows from HttpConnectionProtocol.send, PythiaClient.proccessResponse.
/// See PythiaClient.handleError
@objc public func generateSeed(blindedPassword: Data, brainKeyId: String?, token: String) throws -> Data {
@objc public func generateSeed(blindedPassword: Data, brainKeyId: String?) throws -> Data {
guard let url = URL(string: "pythia/v1/brainkey", relativeTo: self.serviceUrl) else {
throw PythiaClientError.constructingUrl
}
Expand All @@ -62,9 +66,13 @@ extension PythiaClient: PythiaClientProtocol {
params["brainkey_id"] = brainKeyId
}

let request = try ServiceRequest(url: url, method: .post, accessToken: token, params: params)
let tokenContext = TokenContext(service: "pythia", operation: "seed", forceReload: false)

let request = try ServiceRequest(url: url, method: .post, params: params)

let response = try self.connection.send(request)
let response = try self.sendWithRetry(request, retry: self.createRetry(), tokenContext: tokenContext)
.startSync()
.get()

class SeedResponse: Codable {
let seed: Data
Expand Down
55 changes: 31 additions & 24 deletions Source/Client/PythiaClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,38 +83,45 @@ import VirgilSDK
/// Error domain for Error instances thrown from service
@objc override open class var serviceErrorDomain: String { return "VirgilSDK.PythiaServiceErrorDomain" }

/// Initializes a new `PythiaClient` instance
///
/// - Parameters:
/// - serviceUrl: URL of service client will use
/// - connection: custom HTTPConnection
override public init(serviceUrl: URL = PythiaClient.defaultURL, connection: HttpConnectionProtocol) {
super.init(serviceUrl: serviceUrl, connection: connection)
}
internal let retryConfig: ExpBackoffRetry.Config

/// Initializes a new `PythiaClient` instance
@objc public convenience init() {
self.init(serviceUrl: PythiaClient.defaultURL)
/// Initializes new `PythiaClient` instance
///
/// - Parameter accessTokenProvider: Access Token Provider
@objc public convenience init(accessTokenProvider: AccessTokenProvider) {
self.init(accessTokenProvider: accessTokenProvider, serviceUrl: CardClient.defaultURL)
}

/// Initializes a new `PythiaClient` instance
/// Initializes new `PythiaClient` instance
///
/// - Parameter serviceUrl: URL of service client will use
@objc public convenience init(serviceUrl: URL) {
self.init(serviceUrl: serviceUrl, connection: HttpConnection())
/// - Parameters:
/// - accessTokenProvider: Access Token Provider
/// - serviceUrl: service URL
@objc public convenience init(accessTokenProvider: AccessTokenProvider, serviceUrl: URL) {
self.init(accessTokenProvider: accessTokenProvider,
serviceUrl: serviceUrl,
retryConfig: ExpBackoffRetry.Config())
}

/// Handles error
/// Initializes new `PythiaClient` instance
///
/// - Parameters:
/// - statusCode: http status code
/// - body: response
/// - Returns: Corresponding error
override open func handleError(statusCode: Int, body: Data?) -> Error {
if let body = body, let rawServiceError = try? JSONDecoder().decode(RawServiceError.self, from: body) {
return PythiaServiceError(httpStatusCode: statusCode, rawServiceError: rawServiceError)
}
/// - accessTokenProvider: Access Token Provider
/// - serviceUrl: service URL
/// - requestRetryConfig: Retry config
public init(accessTokenProvider: AccessTokenProvider,
serviceUrl: URL,
connection: HttpConnectionProtocol? = nil,
retryConfig: ExpBackoffRetry.Config) {
let version = VersionUtils.getVersion(bundleIdentitifer: "com.virgilsecurity.VirgilSDKPythia")

let connection = connection ??
HttpConnection(adapters: [VirgilAgentAdapter(product: "brainkey", version: version)])

self.retryConfig = retryConfig

return super.handleError(statusCode: statusCode, body: body)
super.init(accessTokenProvider: accessTokenProvider,
serviceUrl: serviceUrl,
connection: connection)
}
}
2 changes: 1 addition & 1 deletion Source/Client/PythiaClientProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ import Foundation
/// - token: authorization token
/// - Returns: Generated seed
/// - Throws: Depends on implementation
@objc func generateSeed(blindedPassword: Data, brainKeyId: String?, token: String) throws -> Data
@objc func generateSeed(blindedPassword: Data, brainKeyId: String?) throws -> Data
}
2 changes: 1 addition & 1 deletion Tests/TestConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import Foundation

class TestConfig: Decodable {
let ApiPublicKeyId: String
let ApiPrivateKey: String
let ApiKeyId: String
let AppId: String
let ServiceURL: String

Expand Down
19 changes: 12 additions & 7 deletions Tests/VSY001_BrainKeyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,28 @@ class VSY001_BrainKeyTests: XCTestCase {

func test001_RealClient() {
let crypto = try! VirgilCrypto()
let client = PythiaClient(serviceUrl: URL(string: self.config.ServiceURL)!)
let apiKey = try! crypto.importPrivateKey(from: Data(base64Encoded: self.config.ApiPrivateKey)!).privateKey

let generator = JwtGenerator(apiKey: apiKey, apiPublicKeyIdentifier: self.config.ApiPublicKeyId, accessTokenSigner: VirgilAccessTokenSigner(virgilCrypto: crypto), appId: self.config.AppId, ttl: 3600)
let generator = try! JwtGenerator(apiKey: apiKey,
apiPublicKeyIdentifier: self.config.ApiKeyId,
crypto: crypto,
appId: self.config.AppId,
ttl: 3600)
let identity = UUID().uuidString
let provider = GeneratorJwtProvider(jwtGenerator: generator, defaultIdentity: identity)

let brainKeyContext = try! BrainKeyContext.init(client: client, pythiaCrypto: PythiaCrypto(crypto: crypto), accessTokenProvider: provider)
let client = PythiaClient(accessTokenProvider: provider, serviceUrl: URL(string: self.config.ServiceURL)!)

let brainKeyContext = try! BrainKeyContext.init(client: client, pythiaCrypto: PythiaCrypto(crypto: crypto))
let brainKey = BrainKey(context: brainKeyContext)

let keyPair1 = try! brainKey.generateKeyPair(password: "some password").startSync().getResult()
let keyPair1 = try! brainKey.generateKeyPair(password: "some password").startSync().get()
sleep(5)
let keyPair2 = try! brainKey.generateKeyPair(password: "some password").startSync().getResult()
let keyPair2 = try! brainKey.generateKeyPair(password: "some password").startSync().get()
sleep(5)
let keyPair3 = try! brainKey.generateKeyPair(password: "another password").startSync().getResult()
let keyPair3 = try! brainKey.generateKeyPair(password: "another password").startSync().get()
sleep(5)
let keyPair4 = try! brainKey.generateKeyPair(password: "some password", brainKeyId: "my password 1").startSync().getResult()
let keyPair4 = try! brainKey.generateKeyPair(password: "some password", brainKeyId: "my password 1").startSync().get()

XCTAssert(keyPair1.publicKey.identifier == keyPair2.publicKey.identifier)
XCTAssert(keyPair1.publicKey.identifier != keyPair3.publicKey.identifier)
Expand Down
Loading

0 comments on commit bd0cb0e

Please sign in to comment.