this repo has no description
1import ExpoModulesCore
2
3enum ExpoAtprotoAuthError: Error {
4 case unsupportedAlgorithm(String)
5 case invalidJwk
6 case invalidHeader(String)
7 case invalidPayload(String)
8 case nullSigner
9}
10
11public class ExpoAtprotoAuthModule: Module {
12 public func definition() -> ModuleDefinition {
13 Name("ExpoAtprotoAuth")
14
15 Function("digest") { (data: Data, algo: String) throws -> Data in
16 if algo != "sha256" {
17 throw ExpoAtprotoAuthError.unsupportedAlgorithm(algo)
18 }
19 return CryptoUtil.digest(data: data)
20 }
21
22 Function("getRandomValues") { (byteLength: Int) -> Data in
23 return CryptoUtil.getRandomValues(byteLength: byteLength)
24 }
25
26 Function("generatePrivateJwk") { (algo: String) throws -> EncodedJWK in
27 if algo != "ES256" {
28 throw ExpoAtprotoAuthError.unsupportedAlgorithm(algo)
29 }
30 return CryptoUtil.generateJwk()
31 }
32
33 Function("createJwt") { (header: String, payload: String, jwk: EncodedJWK) throws -> String in
34 let jwk = try CryptoUtil.decodeJwk(x: jwk.x, y: jwk.y, d: jwk.d)
35 let jwt = try JoseUtil.createJwt(header: header, payload: payload, jwk: jwk)
36 return jwt
37 }
38
39 Function("verifyJwt") { (token: String, jwk: EncodedJWK, options: VerifyOptions) throws -> VerifyResult in
40 let jwk = try CryptoUtil.decodeJwk(x: jwk.x, y: jwk.y, d: jwk.d)
41 let res = try JoseUtil.verifyJwt(token: token, jwk: jwk, options: options)
42 return res
43 }
44 }
45}