@@ -41,35 +41,147 @@ public class JwtBuilder {
4141 internal val payloadBuilder: JwtPayload .Builder = JwtPayload .Builder ()
4242 private val headerBuilder: JwtHeader .Builder = JwtHeader .Builder ()
4343
44+ /* *
45+ * Sets the issuer (`iss`) claim.
46+ *
47+ * @param iss the issuer identifier
48+ * @return this builder for chaining
49+ */
4450 public fun issuer (iss : String ): JwtBuilder = apply { payloadBuilder.issuer = iss }
51+
52+ /* *
53+ * Sets the subject (`sub`) claim.
54+ *
55+ * @param sub the subject identifier
56+ * @return this builder for chaining
57+ */
4558 public fun subject (sub : String ): JwtBuilder = apply { payloadBuilder.subject = sub }
59+
60+ /* *
61+ * Sets the audience (`aud`) claim.
62+ *
63+ * @param aud one or more audience identifiers
64+ * @return this builder for chaining
65+ */
4666 public fun audience (vararg aud : String ): JwtBuilder = apply { payloadBuilder.audience = aud.toSet() }
67+
68+ /* *
69+ * Sets the expiration time (`exp`) claim.
70+ *
71+ * @param exp the absolute instant at which the token expires
72+ * @return this builder for chaining
73+ */
4774 public fun expiration (exp : Instant ): JwtBuilder = apply { payloadBuilder.expiration = exp }
75+
76+ /* *
77+ * Sets the expiration time (`exp`) claim relative to the current time.
78+ *
79+ * @param duration the duration from now until the token expires
80+ * @return this builder for chaining
81+ */
4882 public fun expiresIn (duration : Duration ): JwtBuilder = apply { payloadBuilder.expiresIn(duration) }
83+
84+ /* *
85+ * Sets the not-before (`nbf`) claim.
86+ *
87+ * @param nbf the absolute instant before which the token must not be accepted
88+ * @return this builder for chaining
89+ */
4990 public fun notBefore (nbf : Instant ): JwtBuilder = apply { payloadBuilder.notBefore = nbf }
91+
92+ /* *
93+ * Sets the not-before (`nbf`) claim to the current time.
94+ *
95+ * @return this builder for chaining
96+ */
5097 public fun notBeforeNow (): JwtBuilder = apply { payloadBuilder.notBeforeNow() }
98+
99+ /* *
100+ * Sets the issued-at (`iat`) claim.
101+ *
102+ * @param iat the instant at which the token was issued
103+ * @return this builder for chaining
104+ */
51105 public fun issuedAt (iat : Instant ): JwtBuilder = apply { payloadBuilder.issuedAt = iat }
106+
107+ /* *
108+ * Sets the issued-at (`iat`) claim to the current time.
109+ *
110+ * @return this builder for chaining
111+ */
52112 public fun issuedNow (): JwtBuilder = apply { payloadBuilder.issuedNow() }
113+
114+ /* *
115+ * Sets the JWT ID (`jti`) claim.
116+ *
117+ * @param jti the unique identifier for this token
118+ * @return this builder for chaining
119+ */
53120 public fun id (jti : String ): JwtBuilder = apply { payloadBuilder.id = jti }
54121
122+ /* *
123+ * Sets the JWT ID (`jti`) claim to a randomly generated UUID.
124+ *
125+ * @return this builder for chaining
126+ */
55127 @ExperimentalUuidApi
56128 public fun randomId (): JwtBuilder = apply { payloadBuilder.randomId() }
57129
130+ /* *
131+ * Sets a raw claim using a pre-built [JsonElement].
132+ *
133+ * @param name the claim name
134+ * @param value the claim value as a [JsonElement]
135+ * @return this builder for chaining
136+ */
58137 public fun claim (name : String , value : JsonElement ): JwtBuilder =
59138 apply { payloadBuilder.claim(name, value) }
60139
140+ /* *
141+ * Sets a typed claim using an explicit [SerializationStrategy].
142+ *
143+ * @param name the claim name
144+ * @param serializer the serialization strategy for [T]
145+ * @param value the claim value, or `null` to remove the claim
146+ * @return this builder for chaining
147+ */
61148 public fun <T > claim (name : String , serializer : SerializationStrategy <T >, value : T ? ): JwtBuilder =
62149 apply { payloadBuilder.claim(name, serializer, value) }
63150
151+ /* *
152+ * Sets a typed claim, inferring the serializer from the reified type [T].
153+ *
154+ * @param name the claim name
155+ * @param value the claim value
156+ * @return this builder for chaining
157+ */
64158 public inline fun <reified T > claim (name : String , value : T ): JwtBuilder =
65159 apply { payloadBuilder.claim(name, value) }
66160
161+ /* *
162+ * Configures multiple claims at once using a DSL block applied to [JwtPayload.Builder].
163+ *
164+ * @param block the configuration block
165+ * @return this builder for chaining
166+ */
67167 public fun claims (block : JwtPayload .Builder .() -> Unit ): JwtBuilder =
68168 apply { payloadBuilder.block() }
69169
170+ /* *
171+ * Configures JOSE header fields using a DSL block applied to [JwtHeader.Builder].
172+ *
173+ * @param block the configuration block
174+ * @return this builder for chaining
175+ */
70176 public fun header (block : JwtHeader .Builder .() -> Unit ): JwtBuilder =
71177 apply { headerBuilder.block() }
72178
179+ /* *
180+ * Sets the key ID (`kid`) header parameter.
181+ *
182+ * @param kid the key identifier
183+ * @return this builder for chaining
184+ */
73185 public fun keyId (kid : String ): JwtBuilder =
74186 apply { headerBuilder.keyId = kid }
75187
@@ -91,6 +203,13 @@ public class JwtBuilder {
91203 return JwtInstance .Jws (header, payload, signature.encodeBase64Url())
92204 }
93205
206+ /* *
207+ * Builds and returns an unsecured JWS token with `alg=none` and an empty signature.
208+ *
209+ * @param algorithm the [SigningAlgorithm.None] sentinel value
210+ * @return the resulting [JwtInstance.Jws] with an empty signature segment
211+ * @see co.touchlab.kjwt.parser.JwtParserBuilder.allowUnsecured
212+ */
94213 public suspend fun signWith (algorithm : SigningAlgorithm .None ): JwtInstance .Jws =
95214 signWith(algorithm, SimpleKey .Empty )
96215
0 commit comments