Class TestJwtBuilder
- Namespace
- XBullet.EasyTesting.Authentication
- Assembly
- XBullet.EasyTesting.dll
Fluently defines a JWT produced by a TestJwtAuthority.
public sealed class TestJwtBuilder
- Inheritance
-
TestJwtBuilder
- Inherited Members
Remarks
This mutable builder is not thread-safe. Unless overridden, the issuer, audience, and lifetime
come from the authority; the subject is a new identifier, the name is
integration-test-user, and the not-before time is the build time. Each token also receives
a new JWT identifier when it is built.
Methods
AddAudience(string)
Adds a token audience.
public TestJwtBuilder AddAudience(string audience)
Parameters
audiencestringThe non-empty audience claim to append. Duplicate audience values are retained.
Returns
- TestJwtBuilder
This builder so additional token values can be configured.
Exceptions
- ArgumentException
audienceis empty or whitespace.
ExpiresAfter(TimeSpan)
Sets the token expiration relative to the current time.
public TestJwtBuilder ExpiresAfter(TimeSpan lifetime)
Parameters
lifetimeTimeSpanThe positive duration added to UTC now when this method is called. This sets an absolute expiration and does not defer the calculation until token serialization.
Returns
- TestJwtBuilder
This builder so additional token values can be configured.
Exceptions
- ArgumentOutOfRangeException
lifetimeis zero or negative.
ExpiresAt(DateTimeOffset)
Sets the token's absolute expiry.
public TestJwtBuilder ExpiresAt(DateTimeOffset expires)
Parameters
expiresDateTimeOffsetThe absolute expiration instant. Its offset is converted to UTC when the token is serialized. No ordering check against the not-before time is performed by this method.
Returns
- TestJwtBuilder
This builder so additional token values can be configured.
NotBefore(DateTimeOffset)
Sets the token's not-before time.
public TestJwtBuilder NotBefore(DateTimeOffset notBefore)
Parameters
notBeforeDateTimeOffsetThe absolute instant at which the token becomes valid. Its offset is converted to UTC when the token is serialized. The default is the build time.
Returns
- TestJwtBuilder
This builder so additional token values can be configured.
WithAudience(string)
Replaces the token audiences with one value.
public TestJwtBuilder WithAudience(string audience)
Parameters
audiencestringThe non-empty audience claim. This removes every audience previously added to the builder.
Returns
- TestJwtBuilder
This builder so additional token values can be configured.
Exceptions
- ArgumentException
audienceis empty or whitespace.
WithClaim(string, string)
Adds an arbitrary JWT claim.
public TestJwtBuilder WithClaim(string type, string value)
Parameters
typestringThe non-empty claim-type identifier.
valuestringThe non-null claim value; an empty value is accepted.
Returns
- TestJwtBuilder
This builder so additional token values can be configured.
Exceptions
- ArgumentException
typeis empty or whitespace.- ArgumentNullException
valueis null.
WithIssuer(string)
Sets the token issuer.
public TestJwtBuilder WithIssuer(string issuer)
Parameters
issuerstringThe non-empty issuer claim. The value is used as supplied and is not normalized or validated as a URI.
Returns
- TestJwtBuilder
This builder so additional token values can be configured.
Exceptions
- ArgumentException
issueris empty or whitespace.
WithName(string)
Sets the name claim.
public TestJwtBuilder WithName(string name)
Parameters
namestringThe non-empty value for the
nameclaim. The default isintegration-test-user.
Returns
- TestJwtBuilder
This builder so additional token values can be configured.
Exceptions
- ArgumentException
nameis empty or whitespace.
WithRole(string)
Adds an application role.
public TestJwtBuilder WithRole(string role)
Parameters
rolestringThe non-empty role value to append as a
rolesclaim. Duplicate roles are retained.
Returns
- TestJwtBuilder
This builder so additional token values can be configured.
Exceptions
- ArgumentException
roleis empty or whitespace.
WithRoles(params string[])
Adds several application roles.
public TestJwtBuilder WithRoles(params string[] roles)
Parameters
rolesstring[]The non-null role array to append in order. Every element must be non-empty; duplicate roles are retained. The builder copies the values and does not own the array.
Returns
- TestJwtBuilder
This builder so additional token values can be configured.
Exceptions
- ArgumentNullException
rolesis null.- ArgumentException
An element is empty or whitespace.
WithScope(string)
Adds a delegated OAuth scope.
public TestJwtBuilder WithScope(string scope)
Parameters
scopestringThe non-empty scope value to append. Duplicate values are retained; all scopes are emitted in one space-delimited
scpclaim.
Returns
- TestJwtBuilder
This builder so additional token values can be configured.
Exceptions
- ArgumentException
scopeis empty or whitespace.
WithScopes(params string[])
Adds several delegated OAuth scopes.
public TestJwtBuilder WithScopes(params string[] scopes)
Parameters
scopesstring[]The non-null scope array to append in order. Every element must be non-empty; duplicate values are retained. The builder copies the values and does not own the array.
Returns
- TestJwtBuilder
This builder so additional token values can be configured.
Exceptions
- ArgumentNullException
scopesis null.- ArgumentException
An element is empty or whitespace.
WithSubject(string)
Sets the subject claim.
public TestJwtBuilder WithSubject(string subject)
Parameters
subjectstringThe non-empty subject identifier. By default, each builder uses a new 32-character GUID without separators.
Returns
- TestJwtBuilder
This builder so additional token values can be configured.
Exceptions
- ArgumentException
subjectis empty or whitespace.