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

audience string

The non-empty audience claim to append. Duplicate audience values are retained.

Returns

TestJwtBuilder

This builder so additional token values can be configured.

Exceptions

ArgumentException

audience is empty or whitespace.

ExpiresAfter(TimeSpan)

Sets the token expiration relative to the current time.

public TestJwtBuilder ExpiresAfter(TimeSpan lifetime)

Parameters

lifetime TimeSpan

The 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

lifetime is zero or negative.

ExpiresAt(DateTimeOffset)

Sets the token's absolute expiry.

public TestJwtBuilder ExpiresAt(DateTimeOffset expires)

Parameters

expires DateTimeOffset

The 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

notBefore DateTimeOffset

The 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

audience string

The 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

audience is empty or whitespace.

WithClaim(string, string)

Adds an arbitrary JWT claim.

public TestJwtBuilder WithClaim(string type, string value)

Parameters

type string

The non-empty claim-type identifier.

value string

The non-null claim value; an empty value is accepted.

Returns

TestJwtBuilder

This builder so additional token values can be configured.

Exceptions

ArgumentException

type is empty or whitespace.

ArgumentNullException

value is null.

WithIssuer(string)

Sets the token issuer.

public TestJwtBuilder WithIssuer(string issuer)

Parameters

issuer string

The 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

issuer is empty or whitespace.

WithName(string)

Sets the name claim.

public TestJwtBuilder WithName(string name)

Parameters

name string

The non-empty value for the name claim. The default is integration-test-user.

Returns

TestJwtBuilder

This builder so additional token values can be configured.

Exceptions

ArgumentException

name is empty or whitespace.

WithRole(string)

Adds an application role.

public TestJwtBuilder WithRole(string role)

Parameters

role string

The non-empty role value to append as a roles claim. Duplicate roles are retained.

Returns

TestJwtBuilder

This builder so additional token values can be configured.

Exceptions

ArgumentException

role is empty or whitespace.

WithRoles(params string[])

Adds several application roles.

public TestJwtBuilder WithRoles(params string[] roles)

Parameters

roles string[]

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

roles is null.

ArgumentException

An element is empty or whitespace.

WithScope(string)

Adds a delegated OAuth scope.

public TestJwtBuilder WithScope(string scope)

Parameters

scope string

The non-empty scope value to append. Duplicate values are retained; all scopes are emitted in one space-delimited scp claim.

Returns

TestJwtBuilder

This builder so additional token values can be configured.

Exceptions

ArgumentException

scope is empty or whitespace.

WithScopes(params string[])

Adds several delegated OAuth scopes.

public TestJwtBuilder WithScopes(params string[] scopes)

Parameters

scopes string[]

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

scopes is null.

ArgumentException

An element is empty or whitespace.

WithSubject(string)

Sets the subject claim.

public TestJwtBuilder WithSubject(string subject)

Parameters

subject string

The 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

subject is empty or whitespace.