-
Notifications
You must be signed in to change notification settings - Fork 191
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
Verify v2 #1392
Verify v2 #1392
Changes from 15 commits
6bd05a3
d3b80b3
94dd258
601a01b
e98962b
dffbf64
66104b1
f23fc74
9814f95
6e0167c
85ffdde
7d8c2f4
572591d
d563f75
1a5bba4
df44756
c54013a
41afb9e
bd983cb
a009088
a495480
8489f46
83c7c63
8d228bc
4d8f292
ee030b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
import Foundation | ||
import CryptoKit | ||
|
||
struct JWTValidator { | ||
public struct JWTValidator { | ||
|
||
let jwtString: String | ||
|
||
func isValid(publicKey: SigningPublicKey) throws -> Bool { | ||
public init(jwtString: String) { | ||
self.jwtString = jwtString | ||
} | ||
|
||
public func isValid(publicKey: SigningPublicKey) throws -> Bool { | ||
var components = jwtString.components(separatedBy: ".") | ||
|
||
guard components.count == 3 else { throw JWTError.undefinedFormat } | ||
|
@@ -20,3 +25,32 @@ struct JWTValidator { | |
return publicKey.isValid(signature: signatureData, for: unsignedData) | ||
} | ||
} | ||
|
||
|
||
public struct P256JWTValidator { | ||
|
||
let jwtString: String | ||
|
||
public init(jwtString: String) { | ||
self.jwtString = jwtString | ||
} | ||
|
||
public func isValid(publicKey: P256.Signing.PublicKey) throws -> Bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this unit tested? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not as a unit here but we have it covered in the AttestationJWTVerifierTests as it depends on P256JWTValidator |
||
var components = jwtString.components(separatedBy: ".") | ||
|
||
guard components.count == 3 else { throw JWTError.undefinedFormat } | ||
|
||
let signature = components.removeLast() | ||
|
||
guard let unsignedData = components | ||
.joined(separator: ".") | ||
.data(using: .utf8) | ||
else { throw JWTError.invalidJWTString } | ||
|
||
let signatureData = try JWTEncoder.base64urlDecodedData(string: signature) | ||
|
||
let P256Signature = try P256.Signing.ECDSASignature(rawRepresentation: signatureData) | ||
|
||
return publicKey.isValidSignature(P256Signature, for: unsignedData) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -14,6 +14,8 @@ class ApproveSessionAuthenticateUtil { | |||||||||||||||||||
private let logger: ConsoleLogging | ||||||||||||||||||||
private let sessionStore: WCSessionStorage | ||||||||||||||||||||
private let sessionNamespaceBuilder: SessionNamespaceBuilder | ||||||||||||||||||||
private let verifyContextStore: CodableStore<VerifyContext> | ||||||||||||||||||||
private let verifyClient: VerifyClientProtocol | ||||||||||||||||||||
|
||||||||||||||||||||
init( | ||||||||||||||||||||
logger: ConsoleLogging, | ||||||||||||||||||||
|
@@ -23,7 +25,9 @@ class ApproveSessionAuthenticateUtil { | |||||||||||||||||||
messageFormatter: SIWEFromCacaoFormatting, | ||||||||||||||||||||
sessionStore: WCSessionStorage, | ||||||||||||||||||||
sessionNamespaceBuilder: SessionNamespaceBuilder, | ||||||||||||||||||||
networkingInteractor: NetworkInteracting | ||||||||||||||||||||
networkingInteractor: NetworkInteracting, | ||||||||||||||||||||
verifyContextStore: CodableStore<VerifyContext>, | ||||||||||||||||||||
verifyClient: VerifyClientProtocol | ||||||||||||||||||||
) { | ||||||||||||||||||||
self.logger = logger | ||||||||||||||||||||
self.kms = kms | ||||||||||||||||||||
|
@@ -33,6 +37,8 @@ class ApproveSessionAuthenticateUtil { | |||||||||||||||||||
self.signatureVerifier = signatureVerifier | ||||||||||||||||||||
self.messageFormatter = messageFormatter | ||||||||||||||||||||
self.networkingInteractor = networkingInteractor | ||||||||||||||||||||
self.verifyContextStore = verifyContextStore | ||||||||||||||||||||
self.verifyClient = verifyClient | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
func getsessionAuthenticateRequestParams(requestId: RPCID) throws -> (request: SessionAuthenticateRequestParams, topic: String) { | ||||||||||||||||||||
|
@@ -60,13 +66,13 @@ class ApproveSessionAuthenticateUtil { | |||||||||||||||||||
return (topic, keys) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
func createSession( | ||||||||||||||||||||
response: SessionAuthenticateResponseParams, | ||||||||||||||||||||
pairingTopic: String, | ||||||||||||||||||||
request: SessionAuthenticateRequestParams, | ||||||||||||||||||||
sessionTopic: String, | ||||||||||||||||||||
transportType: WCSession.TransportType | ||||||||||||||||||||
transportType: WCSession.TransportType, | ||||||||||||||||||||
verifyContext: VerifyContext | ||||||||||||||||||||
) throws -> Session? { | ||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
|
@@ -101,7 +107,8 @@ class ApproveSessionAuthenticateUtil { | |||||||||||||||||||
settleParams: settleParams, | ||||||||||||||||||||
requiredNamespaces: [:], | ||||||||||||||||||||
acknowledged: true, | ||||||||||||||||||||
transportType: transportType | ||||||||||||||||||||
transportType: transportType, | ||||||||||||||||||||
verifyContext: verifyContext | ||||||||||||||||||||
) | ||||||||||||||||||||
logger.debug("created a session with topic: \(sessionTopic)") | ||||||||||||||||||||
|
||||||||||||||||||||
|
@@ -114,6 +121,12 @@ class ApproveSessionAuthenticateUtil { | |||||||||||||||||||
return session.publicRepresentation() | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
func getVerifyContext(requestId: RPCID, domain: String) -> VerifyContext { | ||||||||||||||||||||
(try? verifyContextStore.get(key: requestId.string)) ?? verifyClient.createVerifyContext(origin: nil, domain: domain, isScam: false) | ||||||||||||||||||||
} | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this a bit easier to read.
Suggested change
|
||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
func recoverAndVerifySignature(cacaos: [Cacao]) async throws { | ||||||||||||||||||||
try await cacaos.asyncForEach { [unowned self] cacao in | ||||||||||||||||||||
guard | ||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be
private
?