Skip to content

Commit 78bde87

Browse files
authored
Isolate all WebAuth files from non iOS targets (#348)
1 parent b77fe13 commit 78bde87

7 files changed

+107
-46
lines changed

Auth0.podspec

+29-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,20 @@ web_auth_files = [
1616
'Auth0/SilentSafariViewController.swift',
1717
'Auth0/NativeAuth.swift',
1818
'Auth0/AuthProvider.swift',
19-
'Auth0/BioAuthentication.swift'
19+
'Auth0/BioAuthentication.swift',
20+
'Auth0/A0RSA.h',
21+
'Auth0/A0RSA.m',
22+
'Auth0/A0SHA.h',
23+
'Auth0/A0SHA.m',
24+
'Auth0/A0SimpleKeychain+RSAPublicKey.swift',
25+
'Auth0/Array+Encode.swift',
26+
'Auth0/ClaimValidators.swift',
27+
'Auth0/IDTokenSignatureValidator.swift',
28+
'Auth0/IDTokenValidator.swift',
29+
'Auth0/IDTokenValidatorContext.swift',
30+
'Auth0/JWT+Header.swift',
31+
'Auth0/JWK+RSA.swift',
32+
'Auth0/JWTAlgorithm.swift'
2033
]
2134

2235
watchos_exclude_files = [
@@ -37,7 +50,20 @@ watchos_exclude_files = [
3750
'Auth0/AuthProvider.swift',
3851
'Auth0/BioAuthentication.swift',
3952
'Auth0/CredentialsManagerError.swift',
40-
'Auth0/CredentialsManager.swift'
53+
'Auth0/CredentialsManager.swift',
54+
'Auth0/A0RSA.h',
55+
'Auth0/A0RSA.m',
56+
'Auth0/A0SHA.h',
57+
'Auth0/A0SHA.m',
58+
'Auth0/A0SimpleKeychain+RSAPublicKey.swift',
59+
'Auth0/Array+Encode.swift',
60+
'Auth0/ClaimValidators.swift',
61+
'Auth0/IDTokenSignatureValidator.swift',
62+
'Auth0/IDTokenValidator.swift',
63+
'Auth0/IDTokenValidatorContext.swift',
64+
'Auth0/JWT+Header.swift',
65+
'Auth0/JWK+RSA.swift',
66+
'Auth0/JWTAlgorithm.swift'
4167
]
4268

4369
Pod::Spec.new do |s|
@@ -70,6 +96,7 @@ Pod::Spec.new do |s|
7096
s.osx.dependency 'JWTDecode'
7197
s.watchos.source_files = 'Auth0/*.swift'
7298
s.watchos.exclude_files = watchos_exclude_files
99+
s.watchos.dependency 'JWTDecode'
73100
s.tvos.source_files = 'Auth0/*.swift'
74101
s.tvos.exclude_files = web_auth_files
75102
s.tvos.dependency 'SimpleKeychain'

Auth0.xcodeproj/project.pbxproj

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
5BEDE18A1EC21B040007300D /* CredentialsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BEDE1891EC21B040007300D /* CredentialsManager.swift */; };
5353
5BEDE1951EC333380007300D /* CredentialsManagerSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BEDE1931EC3331A0007300D /* CredentialsManagerSpec.swift */; };
5454
5BFB98A51F7D1232001FE50D /* SafariAuthenticationCallback.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BFB98A41F7D1232001FE50D /* SafariAuthenticationCallback.swift */; };
55+
5C49EB3523EB5A80008D562F /* JWK+RSA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C49EB3423EB5A80008D562F /* JWK+RSA.swift */; };
56+
5C49EB3623EB5A80008D562F /* JWK+RSA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C49EB3423EB5A80008D562F /* JWK+RSA.swift */; };
57+
5C49EB3723EB5A80008D562F /* JWK+RSA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C49EB3423EB5A80008D562F /* JWK+RSA.swift */; };
58+
5C49EB3823EB5A80008D562F /* JWK+RSA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C49EB3423EB5A80008D562F /* JWK+RSA.swift */; };
5559
5C4F550723C8FADF00C89615 /* A0SHA.m in Sources */ = {isa = PBXBuildFile; fileRef = 5C4F550223C8FADE00C89615 /* A0SHA.m */; };
5660
5C4F550823C8FADF00C89615 /* A0SHA.h in Headers */ = {isa = PBXBuildFile; fileRef = 5C4F550323C8FADE00C89615 /* A0SHA.h */; settings = {ATTRIBUTES = (Public, ); }; };
5761
5C4F550923C8FADF00C89615 /* JWTAlgorithm.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C4F550423C8FADE00C89615 /* JWTAlgorithm.swift */; };
@@ -514,6 +518,7 @@
514518
5BEDE1891EC21B040007300D /* CredentialsManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CredentialsManager.swift; sourceTree = "<group>"; };
515519
5BEDE1931EC3331A0007300D /* CredentialsManagerSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; name = CredentialsManagerSpec.swift; path = Auth0Tests/CredentialsManagerSpec.swift; sourceTree = SOURCE_ROOT; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
516520
5BFB98A41F7D1232001FE50D /* SafariAuthenticationCallback.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SafariAuthenticationCallback.swift; sourceTree = "<group>"; };
521+
5C49EB3423EB5A80008D562F /* JWK+RSA.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "JWK+RSA.swift"; sourceTree = "<group>"; };
517522
5C4F550223C8FADE00C89615 /* A0SHA.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = A0SHA.m; sourceTree = "<group>"; };
518523
5C4F550323C8FADE00C89615 /* A0SHA.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = A0SHA.h; sourceTree = "<group>"; };
519524
5C4F550423C8FADE00C89615 /* JWTAlgorithm.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JWTAlgorithm.swift; sourceTree = "<group>"; };
@@ -1090,6 +1095,7 @@
10901095
5F7504F41D8C3F2900E3BA1C /* NSError+Helper.swift */,
10911096
5C4F552723C9116B00C89615 /* A0SimpleKeychain+RSAPublicKey.swift */,
10921097
5CB41D6B23D0BBA500074024 /* JWT+Header.swift */,
1098+
5C49EB3423EB5A80008D562F /* JWK+RSA.swift */,
10931099
);
10941100
name = Extensions;
10951101
sourceTree = "<group>";
@@ -1827,6 +1833,7 @@
18271833
5FC34AF81D0101BF000F28F5 /* A0ChallengeGenerator.m in Sources */,
18281834
5F6FAC631D09E98000D5B4EA /* Logger.swift in Sources */,
18291835
5F28B4611D8216180000EB23 /* Loggable.swift in Sources */,
1836+
5C49EB3523EB5A80008D562F /* JWK+RSA.swift in Sources */,
18301837
5FDE87551D8A424700EA27DC /* Auth0Authentication.swift in Sources */,
18311838
5F53F5CE1CFD157300476A46 /* AuthTransaction.swift in Sources */,
18321839
5CB41D6C23D0BBA600074024 /* JWT+Header.swift in Sources */,
@@ -1914,6 +1921,7 @@
19141921
5FE2F8AA1CCE54F1003628F4 /* Result.swift in Sources */,
19151922
5FCAB1741D09009600331C84 /* NSData+URLSafe.swift in Sources */,
19161923
5C4F551523C8FAEE00C89615 /* A0RSA.m in Sources */,
1924+
5C49EB3623EB5A80008D562F /* JWK+RSA.swift in Sources */,
19171925
5F7504F61D8C3F2900E3BA1C /* NSError+Helper.swift in Sources */,
19181926
5FCAB17A1D09124D00331C84 /* NSURL+Auth0.swift in Sources */,
19191927
5C4F554D23C9195100C89615 /* JWTAlgorithm.swift in Sources */,
@@ -2012,6 +2020,7 @@
20122020
5C4F554E23C9195100C89615 /* JWTAlgorithm.swift in Sources */,
20132021
5F23E6E81D4ACD8500C3F2D9 /* Result.swift in Sources */,
20142022
5F23E6DD1D4ACD6100C3F2D9 /* NSURL+Auth0.swift in Sources */,
2023+
5C49EB3723EB5A80008D562F /* JWK+RSA.swift in Sources */,
20152024
5F23E6E71D4ACD8500C3F2D9 /* Response.swift in Sources */,
20162025
5B2860D11EEAC30A00C75D54 /* UserInfo.swift in Sources */,
20172026
5CB41D4E23D0BA2C00074024 /* Optional+DebugDescription.swift in Sources */,
@@ -2059,6 +2068,7 @@
20592068
5F23E7131D4B890500C3F2D9 /* A0ChallengeGenerator.m in Sources */,
20602069
5F23E71A1D4B891E00C3F2D9 /* Auth0.swift in Sources */,
20612070
5F23E7101D4B88FC00C3F2D9 /* Response.swift in Sources */,
2071+
5C49EB3823EB5A80008D562F /* JWK+RSA.swift in Sources */,
20622072
5B2860D01EEAC30A00C75D54 /* UserInfo.swift in Sources */,
20632073
5F28B4641D8216180000EB23 /* Loggable.swift in Sources */,
20642074
5B0893E720F8A52400FBF962 /* CredentialsManagerError.swift in Sources */,

Auth0/AuthTransaction.swift

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222

2323
import UIKit
2424

25+
#if swift(>=4.2)
26+
public typealias A0URLOptionsKey = UIApplication.OpenURLOptionsKey
27+
#else
28+
public typealias A0URLOptionsKey = UIApplicationOpenURLOptionsKey
29+
#endif
30+
2531
/**
2632
Represents an ongoing Auth transaction with an Identity Provider (Auth0 or a third party).
2733

@@ -58,4 +64,5 @@ public protocol AuthTransaction {
5864
Terminates the transaction and reports back that it was cancelled.
5965
*/
6066
func cancel()
67+
6168
}

Auth0/JWK+RSA.swift

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// JWK+RSA.swift
2+
//
3+
// Copyright (c) 2020 Auth0 (http://auth0.com)
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in
13+
// all copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
// THE SOFTWARE.
22+
23+
import Foundation
24+
import SimpleKeychain
25+
26+
extension JWK {
27+
var rsaPublicKey: SecKey? {
28+
if let usage = usage, usage != "sig" { return nil }
29+
guard keyType == "RSA",
30+
algorithm == JWTAlgorithm.rs256.rawValue,
31+
let modulus = rsaModulus?.a0_decodeBase64URLSafe(),
32+
let exponent = rsaExponent?.a0_decodeBase64URLSafe() else { return nil }
33+
let encodedKey = encodeRSAPublicKey(modulus: [UInt8](modulus), exponent: [UInt8](exponent))
34+
if #available(iOS 10, OSX 10.12, tvOS 10, watchOS 3, *) {
35+
return generateRSAPublicKey(from: encodedKey)
36+
}
37+
let tag = "com.auth0.tmp.RSAPublicKey"
38+
let keychain = A0SimpleKeychain()
39+
guard keychain.setRSAPublicKey(data: encodedKey, forKey: tag) else { return nil }
40+
return keychain.keyRefOfRSAKey(withTag: tag).takeRetainedValue()
41+
}
42+
43+
private func encodeRSAPublicKey(modulus: [UInt8], exponent: [UInt8]) -> Data {
44+
let encodedModulus = modulus.a0_derEncode(as: 2) // Integer
45+
let encodedExponent = exponent.a0_derEncode(as: 2) // Integer
46+
let encodedSequence = (encodedModulus + encodedExponent).a0_derEncode(as: 48) // Sequence
47+
return Data(encodedSequence)
48+
}
49+
50+
@available(iOS 10, OSX 10.12, tvOS 10, watchOS 3, *)
51+
private func generateRSAPublicKey(from derEncodedData: Data) -> SecKey? {
52+
let sizeInBits = derEncodedData.count * MemoryLayout<UInt8>.size
53+
let attributes: [CFString: Any] = [kSecClass: kSecClassKey,
54+
kSecAttrKeyType: kSecAttrKeyTypeRSA,
55+
kSecAttrKeyClass: kSecAttrKeyClassPublic,
56+
kSecAttrAccessible: kSecAttrAccessibleAlways,
57+
kSecAttrKeySizeInBits: NSNumber(value: sizeInBits)]
58+
return SecKeyCreateWithData(derEncodedData as CFData, attributes as CFDictionary, nil)
59+
}
60+
}

Auth0/JWKS.swift

-37
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
// THE SOFTWARE.
2222

2323
import Foundation
24-
import SimpleKeychain
2524

2625
public struct JWKS: Codable {
2726
let keys: [JWK]
@@ -56,39 +55,3 @@ public struct JWK: Codable {
5655
case rsaExponent = "e"
5756
}
5857
}
59-
60-
extension JWK {
61-
var rsaPublicKey: SecKey? {
62-
if let usage = usage, usage != "sig" { return nil }
63-
guard keyType == "RSA",
64-
algorithm == JWTAlgorithm.rs256.rawValue,
65-
let modulus = rsaModulus?.a0_decodeBase64URLSafe(),
66-
let exponent = rsaExponent?.a0_decodeBase64URLSafe() else { return nil }
67-
let encodedKey = encodeRSAPublicKey(modulus: [UInt8](modulus), exponent: [UInt8](exponent))
68-
if #available(iOS 10, OSX 10.12, tvOS 10, watchOS 3, *) {
69-
return generateRSAPublicKey(from: encodedKey)
70-
}
71-
let tag = "com.auth0.tmp.RSAPublicKey"
72-
let keychain = A0SimpleKeychain()
73-
guard keychain.setRSAPublicKey(data: encodedKey, forKey: tag) else { return nil }
74-
return keychain.keyRefOfRSAKey(withTag: tag).takeRetainedValue()
75-
}
76-
77-
private func encodeRSAPublicKey(modulus: [UInt8], exponent: [UInt8]) -> Data {
78-
let encodedModulus = modulus.a0_derEncode(as: 2) // Integer
79-
let encodedExponent = exponent.a0_derEncode(as: 2) // Integer
80-
let encodedSequence = (encodedModulus + encodedExponent).a0_derEncode(as: 48) // Sequence
81-
return Data(encodedSequence)
82-
}
83-
84-
@available(iOS 10, OSX 10.12, tvOS 10, watchOS 3, *)
85-
private func generateRSAPublicKey(from derEncodedData: Data) -> SecKey? {
86-
let sizeInBits = derEncodedData.count * MemoryLayout<UInt8>.size
87-
let attributes: [CFString: Any] = [kSecClass: kSecClassKey,
88-
kSecAttrKeyType: kSecAttrKeyTypeRSA,
89-
kSecAttrKeyClass: kSecAttrKeyClassPublic,
90-
kSecAttrAccessible: kSecAttrAccessibleAlways,
91-
kSecAttrKeySizeInBits: NSNumber(value: sizeInBits)]
92-
return SecKeyCreateWithData(derEncodedData as CFData, attributes as CFDictionary, nil)
93-
}
94-
}

Auth0/WebAuth.swift

-6
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ import UIKit
2525
import AuthenticationServices
2626
#endif
2727

28-
#if swift(>=4.2)
29-
public typealias A0URLOptionsKey = UIApplication.OpenURLOptionsKey
30-
#else
31-
public typealias A0URLOptionsKey = UIApplicationOpenURLOptionsKey
32-
#endif
33-
3428
/**
3529
Auth0 iOS component for authenticating with web-based flow
3630

Auth0/_ObjectiveAuthenticationAPI.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public class _ObjectiveAuthenticationAPI: NSObject {
7878
}
7979
}
8080

81-
#if canImport(UIKit)
81+
#if os(iOS)
8282
@objc(resumeAuthWithURL:options:)
8383
public static func resume(_ url: URL, options: [A0URLOptionsKey: Any]) -> Bool {
8484
return resumeAuth(url, options: options)

0 commit comments

Comments
 (0)