diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index c188d2c..5079c6d 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -2,27 +2,22 @@ name: Swift on: push: - branches: [ main ] + branches: [main] pull_request: - branches: [ main ] + branches: [main] jobs: - build-ubuntu: - runs-on: ubuntu-18.04 + runs-on: ubuntu-latest steps: - - name: Checkout - uses: actions/checkout@v3 - - name: Install Swift - run: | - SWIFT_URL=https://swift.org/builds/swift-5.1.1-release/ubuntu1404/swift-5.1.1-RELEASE/swift-5.1.1-RELEASE-ubuntu14.04.tar.gz - curl -fSsL $SWIFT_URL -o swift.tar.gz - sudo tar -xzf swift.tar.gz --strip-components=2 --directory=/usr/local - which swift - - name: Build - run: | - swift package -v resolve - make all + - name: Checkout + uses: actions/checkout@v3 + - name: Install swift version + uses: swift-actions/setup-swift@v1 + - name: Build + run: | + swift package -v resolve + make all build-macos: runs-on: macOS-latest diff --git a/Package.swift b/Package.swift index 035adfe..47fd134 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.0 +// swift-tools-version:5.1 // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,7 +19,7 @@ import PackageDescription let package = Package( name: "Auth", platforms: [ - .macOS(.v10_12), .iOS(.v9), .tvOS(.v9) + .macOS(.v10_15), .iOS(.v9), .tvOS(.v9) ], products: [ .library(name: "OAuth1", targets: ["OAuth1"]), @@ -29,17 +29,27 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/apple/swift-nio.git", from: "2.59.0"), - .package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", "1.1.3"..."1.3.2"), - .package(url: "https://github.com/attaswift/BigInt", from: "5.0.0"), + .package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", from: "1.8.1"), + .package(url: "https://github.com/attaswift/BigInt.git", from: "5.0.0"), + .package(url: "https://github.com/vapor/jwt-kit.git", from: "4.0.0"), ], targets: [ .target(name: "OAuth1", - dependencies: ["CryptoSwift", "TinyHTTPServer"]), + dependencies: [ + .product(name: "CryptoSwift", package: "CryptoSwift"), + "TinyHTTPServer" + ]), .target(name: "OAuth2", - dependencies: ["CryptoSwift", "TinyHTTPServer", "BigInt", "SwiftyBase64"], + dependencies: [.product(name: "CryptoSwift", package: "CryptoSwift"), + "TinyHTTPServer", + .product(name: "BigInt", package: "BigInt"), + "SwiftyBase64", + .product(name: "JWTKit", package: "jwt-kit") + ], exclude: ["FCMTokenProvider"]), .target(name: "TinyHTTPServer", - dependencies: ["NIO", "NIOHTTP1"]), + dependencies: [.product(name: "NIO" , package: "swift-nio"), + .product(name: "NIOHTTP1" , package: "swift-nio")]), .target(name: "SwiftyBase64"), .target(name: "TokenSource", dependencies: ["OAuth2"], path: "Sources/Examples/TokenSource"), .target(name: "Google", dependencies: ["OAuth2"], path: "Sources/Examples/Google"), diff --git a/Sources/OAuth1/Connection.swift b/Sources/OAuth1/Connection.swift index 88e4d74..4b16e0a 100644 --- a/Sources/OAuth1/Connection.swift +++ b/Sources/OAuth1/Connection.swift @@ -49,7 +49,7 @@ public class Connection { // generate the signature let hmac = try! CryptoSwift.HMAC(key: secret, variant: .sha1).authenticate(Array(signatureBaseString.utf8)) - parameters["oauth_signature"] = hmac.toBase64()! + parameters["oauth_signature"] = hmac.toBase64() } public class func performRequest( diff --git a/Sources/OAuth2/ServiceAccountTokenProvider/JWT.swift b/Sources/OAuth2/ServiceAccountTokenProvider/JWT.swift index 6d12a04..f7ffef7 100644 --- a/Sources/OAuth2/ServiceAccountTokenProvider/JWT.swift +++ b/Sources/OAuth2/ServiceAccountTokenProvider/JWT.swift @@ -15,6 +15,7 @@ import Foundation import CryptoSwift import SwiftyBase64 +import JWTKit struct JWTHeader : Codable { let Algorithm : String @@ -51,9 +52,8 @@ struct JWT { let claims = SwiftyBase64.EncodeString(Array(claimsData), alphabet:.URLAndFilenameSafe) let body = header + "." + claims let bodyData = body.data(using: String.Encoding.utf8)! - let sha2 = SHA2(variant: SHA2.Variant(rawValue:256)!) - let hash = sha2.calculate(for:Array(bodyData)) - let signature = rsaKey.sign(hash:hash) + let signer = JWTSigner.rs256(key: rsaKey) + let signature = try signer.algorithm.sign(bodyData) let signatureString = SwiftyBase64.EncodeString(signature, alphabet:.URLAndFilenameSafe) return body + "." + signatureString } diff --git a/Sources/OAuth2/ServiceAccountTokenProvider/RSA.swift b/Sources/OAuth2/ServiceAccountTokenProvider/RSA.swift deleted file mode 100644 index 4e629bf..0000000 --- a/Sources/OAuth2/ServiceAccountTokenProvider/RSA.swift +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright 2019 Google LLC. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -import Foundation -import BigInt - -struct RSAKey { - var N : BigUInt - var E : BigUInt - var D : BigUInt - /* - var Primes : [BigUInt] - var Dp: BigUInt - var Dq: BigUInt - var Qinv : BigUInt - */ - - init?(privateKey:String) { - var pem = "" - for line in privateKey.components(separatedBy:"\n") { - if line.range(of:"PRIVATE KEY") == nil { - pem += line - } - } - if let der = Data(base64Encoded: pem, options: []), - let asn1Array = ASN1Decoder.decode(der: der) { - N = BigUInt(asn1Array[2].children?[1].data ?? Data()) - E = BigUInt(asn1Array[2].children?[2].data ?? Data()) - D = BigUInt(asn1Array[2].children?[3].data ?? Data()) - } else { - return nil - } - } - - func sign(hash:[UInt8]) -> [UInt8] { - let prefix : [UInt8] = [0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20] - var dataToEncode = [UInt8](repeating: 0xFF, count: 256) - dataToEncode[0] = 0x00 - dataToEncode[1] = 0x01 - let offset1 = 256 - hash.count - prefix.count - dataToEncode[offset1 - 1] = 0x00 - for i in 0..