Skip to content

Commit

Permalink
Fail on macOS 10.11 or lower
Browse files Browse the repository at this point in the history
  • Loading branch information
irar2 committed Jan 30, 2017
1 parent 980a019 commit b3d793e
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 16 deletions.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ let package = Package(
dependencies: [
.Package(url: "https://github.com/IBM-Swift/BlueCryptor.git", majorVersion: 0, minor: 8),
.Package(url: "https://github.com/IBM-Swift/BlueRSA.git", majorVersion: 0, minor: 1),
.Package(url: "https://github.com/IBM-Swift/HeliumLogger.git", majorVersion: 1, minor: 6),
],
exclude: []
)
2 changes: 0 additions & 2 deletions Sources/KituraJWT/Algorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ public enum Algorithm {
}
}

@available(macOS 10.12, iOS 10.0, *)
func sign(_ input: String) -> Data? {
return encryptionAlgortihm.sign(input)
}

@available(macOS 10.12, iOS 10.0, *)
func verify(signature: Data, for input: String) -> Bool {
return encryptionAlgortihm.verify(signature: signature, for: input)
}
Expand Down
13 changes: 9 additions & 4 deletions Sources/KituraJWT/BlueRSA.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
**/

import CryptorRSA
import LoggerAPI

import Foundation

Expand All @@ -30,8 +31,11 @@ class BlueRSA: EncryptionAlgorithm {
self.algorithm = algorithm
}

@available(macOS 10.12, iOS 10.0, *)
func sign(_ data: Data) -> Data? {
guard #available(macOS 10.12, iOS 10.0, *) else {
Log.error("macOS 10.12.0 (Sierra) or higher or iOS 10.0 or higher is required by CryptorRSA")
return nil
}
do {
let privateKey = try CryptorRSA.createPrivateKey(with: key)
let myPlaintext = CryptorRSA.createPlaintext(with: data)
Expand All @@ -45,16 +49,18 @@ class BlueRSA: EncryptionAlgorithm {
}
}

@available(macOS 10.12, iOS 10.0, *)
func sign(_ string: String, encoding: String.Encoding) -> Data? {
guard let data: Data = string.data(using: encoding) else {
Log.error("macOS 10.12.0 (Sierra) or higher or iOS 10.0 or higher is required by CryptorRSA")
return nil
}
return sign(data)
}

@available(macOS 10.12, iOS 10.0, *)
func verify(signature: Data, for data: Data) -> Bool {
guard #available(macOS 10.12, iOS 10.0, *) else {
return false
}
do {
var publicKey: CryptorRSA.PublicKey
switch keyType {
Expand All @@ -73,7 +79,6 @@ class BlueRSA: EncryptionAlgorithm {
}
}

@available(macOS 10.12, iOS 10.0, *)
func verify(signature: Data, for string: String, encoding: String.Encoding) -> Bool {
guard let data: Data = string.data(using: encoding) else {
return false
Expand Down
6 changes: 0 additions & 6 deletions Sources/KituraJWT/EncryptionAlhorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,20 @@
import Foundation

protocol EncryptionAlgorithm {
@available(macOS 10.12, iOS 10.0, *)
func sign(_ data: Data) -> Data?

@available(macOS 10.12, iOS 10.0, *)
func sign(_ string: String, encoding: String.Encoding) -> Data?

@available(macOS 10.12, iOS 10.0, *)
func verify(signature: Data, for data: Data) -> Bool

@available(macOS 10.12, iOS 10.0, *)
func verify(signature: Data, for string: String, encoding: String.Encoding) -> Bool
}

extension EncryptionAlgorithm {
@available(macOS 10.12, iOS 10.0, *)
func sign(_ string: String, encoding: String.Encoding = .utf8) -> Data? {
return sign(string, encoding: encoding)
}

@available(macOS 10.12, iOS 10.0, *)
func verify(signature: Data, for string: String, encoding: String.Encoding = .utf8) -> Bool {
return verify(signature: signature, for: string, encoding: encoding)
}
Expand Down
2 changes: 0 additions & 2 deletions Sources/KituraJWT/JWT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public struct JWT {
/// - Parameter using algorithm: The algorithm to sign with.
/// - Returns: A String with the encoded and signed JWT.
/// - Throws: An error thrown during the encoding or signing.
@available(macOS 10.12, iOS 10.0, *)
public mutating func sign(using algorithm: Algorithm) throws -> String? {
header[.alg] = algorithm.name
guard let encodedHeader = try header.encode(),
Expand All @@ -71,7 +70,6 @@ public struct JWT {
/// - Parameter using algorithm: The algorithm to verify with.
/// - Returns: A Bool indicating whether the verification was successful.
/// - Throws: An error thrown during the verification.
@available(macOS 10.12, iOS 10.0, *)
public static func verify(_ jwt: String, using algorithm: Algorithm) throws -> Bool {
let components = jwt.components(separatedBy: ".")
guard components.count == 3,
Expand Down
3 changes: 1 addition & 2 deletions Tests/KituraJWTTests/TestJWT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ import Foundation
let rsaPrivateKey = read(fileName: "rsa_private_key")
let rsaPublicKey = read(fileName: "rsa_public_key")

@available(macOS 10.12, iOS 10.0, *)
class TestJWT: XCTestCase {

@available(macOS 10.12, iOS 10.0, *)
static var allTests: [(String, (TestJWT) -> () throws -> Void)] {
return [
("testSign", testSign),
]
}

@available(macOS 10.12, iOS 10.0, *)
func testSign() {

var jwt = JWT(header: Header([.alg:"rs256"]), claims: Claims([.name:"Kitura"]))
Expand Down

0 comments on commit b3d793e

Please sign in to comment.