Skip to content

Commit e36d4f0

Browse files
committed
feat(claims): Expose validation APIs
Closes #48 Closes #49
1 parent ce7285f commit e36d4f0

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
`decode` providing you convenience accessors. `encode` will now accept a
99
`ClaimSet`.
1010

11+
`ClaimSet` provides methods to manually validate individual claims.
12+
13+
```swift
14+
try claims.validateAudience("example.com")
15+
try claims.validateIssuer("fuller.li")
16+
try claims.validateExpiary()
17+
try claims.validateNotBefore()
18+
try claims.validateIssuedAt()
19+
```
20+
1121

1222
## 2.0.2
1323

Sources/ClaimSet.swift

+18-6
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ extension ClaimSet {
9191
// MARK: Validations
9292

9393
extension ClaimSet {
94-
func validate(audience: String? = nil, issuer: String? = nil) throws {
94+
public func validate(audience: String? = nil, issuer: String? = nil) throws {
9595
if let issuer = issuer {
9696
try validateIssuer(issuer)
9797
}
@@ -100,12 +100,12 @@ extension ClaimSet {
100100
try validateAudience(audience)
101101
}
102102

103-
try validateDate(claims, key: "exp", comparison: .orderedAscending, failure: .expiredSignature, decodeError: "Expiration time claim (exp) must be an integer")
104-
try validateDate(claims, key: "nbf", comparison: .orderedDescending, failure: .immatureSignature, decodeError: "Not before claim (nbf) must be an integer")
105-
try validateDate(claims, key: "iat", comparison: .orderedDescending, failure: .invalidIssuedAt, decodeError: "Issued at claim (iat) must be an integer")
103+
try validateExpiary()
104+
try validateNotBefore()
105+
try validateIssuedAt()
106106
}
107107

108-
func validateAudience(_ audience: String) throws {
108+
public func validateAudience(_ audience: String) throws {
109109
if let aud = self["aud"] as? [String] {
110110
if !aud.contains(audience) {
111111
throw InvalidToken.invalidAudience
@@ -119,7 +119,7 @@ extension ClaimSet {
119119
}
120120
}
121121

122-
func validateIssuer(_ issuer: String) throws {
122+
public func validateIssuer(_ issuer: String) throws {
123123
if let iss = self["iss"] as? String {
124124
if iss != issuer {
125125
throw InvalidToken.invalidIssuer
@@ -128,6 +128,18 @@ extension ClaimSet {
128128
throw InvalidToken.invalidIssuer
129129
}
130130
}
131+
132+
public func validateExpiary() throws {
133+
try validateDate(claims, key: "exp", comparison: .orderedAscending, failure: .expiredSignature, decodeError: "Expiration time claim (exp) must be an integer")
134+
}
135+
136+
public func validateNotBefore() throws {
137+
try validateDate(claims, key: "nbf", comparison: .orderedDescending, failure: .immatureSignature, decodeError: "Not before claim (nbf) must be an integer")
138+
}
139+
140+
public func validateIssuedAt() throws {
141+
try validateDate(claims, key: "iat", comparison: .orderedDescending, failure: .invalidIssuedAt, decodeError: "Issued at claim (iat) must be an integer")
142+
}
131143
}
132144

133145
// MARK: Builder

0 commit comments

Comments
 (0)