Skip to content

Commit 4eaf8f3

Browse files
Andrew-Lees11djones6
authored andcommitted
fix: Add ClaimsStandardJWT initializer (#22)
1 parent c69a7bd commit 4eaf8f3

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Therefore, a JWT typically looks like the following: xxxxx.yyyyy.zzzzz
6666
#### Header
6767

6868
The Header struct contains the fields of the JSON Web Token header as defined by [RFC7515](https://tools.ietf.org/html/rfc7515#section-4).
69-
The "typ" header will default to "JWT". The "alg" header will be set the algorithm name when you sign the JWT.
69+
The "typ" header will default to "JWT". The "alg" header will be set to the algorithm name when you sign the JWT.
7070
The other Header fields can be set when initializing the Header or by changing them directly on the Header object.
7171

7272
```swift
@@ -150,7 +150,7 @@ Initialize a JWTVerifier using the static function corresponding to the desired
150150
```swift
151151
let jwtVerifier = JWTVerifier.rs256(publicKey: publicKey)
152152
```
153-
To verify a signed JWT string, call the `sign` function on your JWT instance, passing in a JWTSigner:
153+
To verify a signed JWT string, call the static `verify` function, passing in your JWT string and the JWTVerifier:
154154

155155
```swift
156156
let verified = JWT<MyClaims>.verify(signedJWT, using: jwtVerifier)
@@ -211,8 +211,8 @@ The JWTEncoder and JWTDecoder classes encode and decode JWT Strings using the sa
211211
Because JWTEncoder and JWTDecoder conform to [KituraContract's](https://github.com/IBM-Swift/KituraContracts/blob/master/Sources/KituraContracts/Contracts.swift) BodyEncoder and BodyDecoder protocols, they can be used as a [custom coder](https://developer.ibm.com/swift/2018/09/01/kitura-custom-encoders-and-decoders/) in Codable routes for sending and receiving JWTs:
212212

213213
```swift
214-
router.encoders[MediaType(type: .application, subtype: "jwt")] = jwtEncoder
215-
router.decoders[MediaType(type: .application, subtype: "jwt")] = jwtDecoder
214+
router.encoders[MediaType(type: .application, subType: "jwt")] = { return jwtEncoder }
215+
router.decoders[MediaType(type: .application, subType: "jwt")] = { return jwtDecoder }
216216
```
217217

218218
This allows for the use of JWT's in information exchange. By sending and receiving JWT's you can ensure the sending is who they say they are and verify the content hasn't been tampered with.

Sources/SwiftJWT/ClaimsExamples/ClaimsMicroProfile.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616

1717
import Foundation
1818

19-
// MARK MicroProfileClaims
19+
// MARK ClaimsMicroProfile
2020

2121
/// A class representing the MicroProfile claims as listed in [MicroProfile specs](http://microprofile.io/project/eclipse/microprofile-jwt-auth/spec/src/main/asciidoc/interoperability.asciidoc).
2222
public class ClaimsMicroProfile: Claims {
2323

24+
/// Initialize a `ClaimsMicroProfile`
2425
public init(
2526
iss: String,
2627
sub: String,
@@ -38,6 +39,7 @@ public class ClaimsMicroProfile: Claims {
3839
self.upn = upn
3940
self.groups = groups
4041
}
42+
4143
/**
4244
The MP-JWT issuer. [RFC7519, Section 4.1.1](https://tools.ietf.org/html/rfc7519#section-4.1.1)
4345
*/

Sources/SwiftJWT/ClaimsExamples/ClaimsOpenID.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
import Foundation
1818

19-
// MARK OpenIDClaims
19+
// MARK ClaimsOpenID
2020

2121
/// A class representing OpenID related claims as decsribed in [OpenID specs](http://openid.net/specs/openid-connect-core-1_0.html).
2222
public class ClaimsOpenID: Claims {
2323

24-
/// Initalise the OpenIDClaims
24+
/// Initalise the `ClaimsOpenID`
2525
public init(
2626
iss: String,
2727
sub: String,

Sources/SwiftJWT/ClaimsExamples/ClaimsStandardJWT.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,30 @@
1616

1717
import Foundation
1818

19-
// MARK StandardJWTClaims
19+
// MARK ClaimsStandardJWT
2020

2121
/// A class representing the Standard JWT claims as described in [RFC7519](https://tools.ietf.org/html/rfc7519#section-4.1).
2222
public class ClaimsStandardJWT: Claims {
2323

24+
/// Initialize a `ClaimsStandardJWT`
25+
public init(
26+
iss: String? = nil,
27+
sub: String? = nil,
28+
aud: [String]? = nil,
29+
exp: Date? = nil,
30+
nbf: Date? = nil,
31+
iat: Date? = nil,
32+
jti: String? = nil
33+
) {
34+
self.iss = iss
35+
self.sub = sub
36+
self.aud = aud
37+
self.exp = exp
38+
self.nbf = nbf
39+
self.iat = iat
40+
self.jti = jti
41+
}
42+
2443
/**
2544
The "iss" (issuer) claim identifies the principal that issued the
2645
JWT. The processing of this claim is generally application specific.

0 commit comments

Comments
 (0)