Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate existing Objective-C code to Swift [SDK-2905] #547

Merged
merged 21 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions Auth0.podspec
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
web_auth_files = [
'Auth0/ObjectiveC/A0ChallengeGenerator.h',
'Auth0/ObjectiveC/A0ChallengeGenerator.m',
'Auth0/ObjectiveC/A0RSA.h',
'Auth0/ObjectiveC/A0RSA.m',
'Auth0/ObjectiveC/A0SHA.h',
'Auth0/ObjectiveC/A0SHA.m',
'Auth0/Array+Encode.swift',
'Auth0/ASCallbackTransaction.swift',
'Auth0/ASTransaction.swift',
Expand All @@ -14,6 +8,7 @@ web_auth_files = [
'Auth0/BaseCallbackTransaction.swift',
'Auth0/BaseTransaction.swift',
'Auth0/BioAuthentication.swift',
'Auth0/ChallengeGenerator.swift',
'Auth0/ClaimValidators.swift',
'Auth0/IDTokenSignatureValidator.swift',
'Auth0/IDTokenValidator.swift',
Expand Down Expand Up @@ -61,24 +56,22 @@ Pod::Spec.new do |s|
s.tvos.deployment_target = '12.0'
s.requires_arc = true

s.ios.source_files = 'Auth0/*.{swift,h,m}', 'Auth0/ObjectiveC/*.{h,m}'
s.ios.source_files = 'Auth0/*.swift'
s.ios.exclude_files = macos_files
s.ios.frameworks = 'UIKit', 'LocalAuthentication', 'AuthenticationServices'
s.ios.dependency 'SimpleKeychain'
s.ios.dependency 'JWTDecode', '~> 2.0'
s.ios.pod_target_xcconfig = {
'SWIFT_ACTIVE_COMPILATION_CONDITIONS' => 'WEB_AUTH_PLATFORM',
'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) WEB_AUTH_PLATFORM=1'
'SWIFT_ACTIVE_COMPILATION_CONDITIONS' => 'WEB_AUTH_PLATFORM'
}

s.osx.source_files = 'Auth0/*.{swift,h,m}', 'Auth0/ObjectiveC/*.{h,m}'
s.osx.source_files = 'Auth0/*.swift'
s.osx.exclude_files = ios_files
s.osx.frameworks = 'AppKit', 'LocalAuthentication', 'AuthenticationServices'
s.osx.dependency 'SimpleKeychain'
s.osx.dependency 'JWTDecode', '~> 2.0'
s.osx.pod_target_xcconfig = {
'SWIFT_ACTIVE_COMPILATION_CONDITIONS' => 'WEB_AUTH_PLATFORM',
'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) WEB_AUTH_PLATFORM=1'
'SWIFT_ACTIVE_COMPILATION_CONDITIONS' => 'WEB_AUTH_PLATFORM'
}

s.watchos.source_files = 'Auth0/*.swift'
Expand Down
86 changes: 6 additions & 80 deletions Auth0.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

37 changes: 0 additions & 37 deletions Auth0/Auth0.h

This file was deleted.

31 changes: 31 additions & 0 deletions Auth0/ChallengeGenerator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Foundation
import CommonCrypto

private func getVerifier() -> String? {
let data = Data(count: 32)
var tempData = data
_ = tempData.withUnsafeMutableBytes {
SecRandomCopyBytes(kSecRandomDefault, data.count, $0.baseAddress!)
}
return tempData.a0_encodeBase64URLSafe()
}

private func getChallenge(for verifier: String) -> String? {
guard let data = verifier.data(using: .utf8) else { return nil }
var buffer = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
_ = data.withUnsafeBytes {
CC_SHA256($0.baseAddress, CC_LONG(data.count), &buffer)
}
return Data(buffer).a0_encodeBase64URLSafe()
}

struct ChallengeGenerator {
let verifier: String
let challenge: String
let method = "S256"

init(verifier: String? = nil) {
self.verifier = verifier ?? getVerifier()!
self.challenge = getChallenge(for: self.verifier)!
}
}
11 changes: 3 additions & 8 deletions Auth0/JWTAlgorithm.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#if WEB_AUTH_PLATFORM
adamjmcgrath marked this conversation as resolved.
Show resolved Hide resolved
import Foundation
import JWTDecode
#if SWIFT_PACKAGE
import Auth0ObjectiveC
#endif

enum JWTAlgorithm: String {
case rs256 = "RS256"
Expand All @@ -22,10 +18,9 @@ enum JWTAlgorithm: String {
!signature.isEmpty else { return false }
switch self {
case .rs256:
guard let publicKey = jwk.rsaPublicKey, let rsa = A0RSA(key: publicKey) else { return false }
let sha256 = A0SHA()
return rsa.verify(sha256.hash(data), signature: signature)
return SecKeyVerifySignature(
jwk.rsaPublicKey!, .rsaSignatureMessagePKCS1v15SHA256, data as CFData, signature as CFData, nil
)
}
}
}
#endif
7 changes: 1 addition & 6 deletions Auth0/OAuth2Grant.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#if WEB_AUTH_PLATFORM
import Foundation
import JWTDecode
#if SWIFT_PACKAGE
import Auth0ObjectiveC
#endif

protocol OAuth2Grant {
var defaults: [String: String] { get }
Expand All @@ -23,7 +19,7 @@ struct PKCE: OAuth2Grant {
let organization: String?

init(authentication: Authentication,
generator: A0SHA256ChallengeGenerator = A0SHA256ChallengeGenerator(),
generator: ChallengeGenerator = ChallengeGenerator(),
redirectURL: URL,
issuer: String,
leeway: Int,
Expand Down Expand Up @@ -109,4 +105,3 @@ struct PKCE: OAuth2Grant {
}

}
#endif
37 changes: 0 additions & 37 deletions Auth0/ObjectiveC/A0ChallengeGenerator.h

This file was deleted.

73 changes: 0 additions & 73 deletions Auth0/ObjectiveC/A0ChallengeGenerator.m

This file was deleted.

37 changes: 0 additions & 37 deletions Auth0/ObjectiveC/A0RSA.h

This file was deleted.

Loading