forked from auth0/Auth0.swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJWKSpec.swift
110 lines (89 loc) · 5.95 KB
/
JWKSpec.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import Foundation
import Quick
import Nimble
@testable import Auth0
class JWKSpec: QuickSpec {
override func spec() {
describe("public key generation") {
let jwk = generateRSAJWK()
context("successful generation") {
it("should generate a RSA public key") {
let publicKey = JWK(keyType: "RSA",
keyId: "NUZFNkFDNUVDNzIxMjAyQTU5RUEzQ0UyMEQ2Mjc5OUZFREFCQ0E2MA",
usage: "sig",
algorithm: JWTAlgorithm.rs256.rawValue,
certUrl: nil,
certThumbprint: nil,
certChain: nil,
modulus: "42xFiJGFLj6e8PgJ-zDQE_KhXNscWFHmJylilVhpD0KUoNKict4IUBvmLYrKMiFLggBS-ttadXeJn7XMnsu6Dz8OzE6r9ELxjZK9sljwx-KWn3ojX8XB8c4LB4NLCEzcwAmE-1zEymJSRg7GJ1g5CHQ_uPeZgxPpEKg5XbrVjZO0KmKE2vCIEVFJIxXNIIu-yC4zR0dPLLEN0lPDZLwwYVRF5y9F_WzDX8fr2nGPQQHQdebBHe_ystvlNc1RdZvyM7BjN9z0l3CXTyR18bLNhJdRDU39NvS7IzGmnqL3WLAwZGtJ6rMhYCPsj-Dla4tUJCy6Yc4V7Gr8zBGQWmLKlQ",
exponent: "AQAB").rsaPublicKey
expect(publicKey).notTo(beNil())
let keyAttributes = SecKeyCopyAttributes(publicKey!) as! [String: Any]
expect(keyAttributes[String(kSecAttrKeyType)] as? String).to(equal(String(kSecAttrKeyTypeRSA)))
}
}
context("unsuccessful generation") {
it("should fail to generate a public key given an invalid key type") {
let jwkWithInvalidKeyType = JWK(keyType: "ECDSA",
keyId: jwk.keyId,
usage: jwk.usage,
algorithm: jwk.algorithm,
certUrl: nil,
certThumbprint: nil,
certChain: nil,
modulus: jwk.modulus,
exponent: jwk.exponent)
expect(jwkWithInvalidKeyType.rsaPublicKey).to(beNil())
}
it("should fail to generate a public key given an invalid algorithm") {
let jwkWithInvalidAlgorithm = JWK(keyType: jwk.keyType,
keyId: jwk.keyId,
usage: jwk.usage,
algorithm: "HS256",
certUrl: nil,
certThumbprint: nil,
certChain: nil,
modulus: jwk.modulus,
exponent: jwk.exponent)
expect(jwkWithInvalidAlgorithm.rsaPublicKey).to(beNil())
}
it("should fail to generate a public key given an unsupported usage") {
let jwkWithUnsupportedUsage = JWK(keyType: jwk.keyType,
keyId: jwk.keyId,
usage: "enc",
algorithm: jwk.algorithm,
certUrl: nil,
certThumbprint: nil,
certChain: nil,
modulus: jwk.modulus,
exponent: jwk.exponent)
expect(jwkWithUnsupportedUsage.rsaPublicKey).to(beNil())
}
it("should fail to generate a public key given an invalid modulus") {
let jwkWithInvalidModulus = JWK(keyType: jwk.keyType,
keyId: jwk.keyId,
usage: jwk.usage,
algorithm: jwk.algorithm,
certUrl: nil,
certThumbprint: nil,
certChain: nil,
modulus: "###",
exponent: jwk.exponent)
expect(jwkWithInvalidModulus.rsaPublicKey).to(beNil())
}
it("should fail to generate a public key given an invalid exponent") {
let jwkWithInvalidExponent = JWK(keyType: jwk.keyType,
keyId: jwk.keyId,
usage: jwk.usage,
algorithm: jwk.algorithm,
certUrl: nil,
certThumbprint: nil,
certChain: nil,
modulus: jwk.modulus,
exponent: "###")
expect(jwkWithInvalidExponent.rsaPublicKey).to(beNil())
}
}
}
}
}