Skip to main content

DeviceAuthorizationStorage Class

Abblix.Oidc.Server

Abblix.Oidc.Server.Features.DeviceAuthorization

DeviceAuthorizationStorage Class

Implements storage for device authorization requests as defined in RFC 8628. Stores requests by device_code (for client polling) with a secondary index by user_code (for user verification). Uses atomic distributed cache operations to prevent race conditions in token issuance.

public class DeviceAuthorizationStorage : Abblix.Oidc.Server.Features.DeviceAuthorization.Interfaces.IDeviceAuthorizationStorage

Inheritance System.Object → DeviceAuthorizationStorage

Implements IDeviceAuthorizationStorage

Constructors

DeviceAuthorizationStorage(IDistributedCache, IBinarySerializer, IEntityStorageKeyFactory, TimeProvider) Constructor

Implements storage for device authorization requests as defined in RFC 8628. Stores requests by device_code (for client polling) with a secondary index by user_code (for user verification). Uses atomic distributed cache operations to prevent race conditions in token issuance.

public DeviceAuthorizationStorage(Microsoft.Extensions.Caching.Distributed.IDistributedCache cache, Abblix.Oidc.Server.Common.Interfaces.IBinarySerializer serializer, Abblix.Oidc.Server.Features.Storages.IEntityStorageKeyFactory keyFactory, System.TimeProvider timeProvider);

Parameters

cache Microsoft.Extensions.Caching.Distributed.IDistributedCache

The distributed cache backend used for atomic operations.

serializer IBinarySerializer

The serializer for converting objects to/from binary format.

keyFactory IEntityStorageKeyFactory

The factory for generating standardized storage keys.

timeProvider System.TimeProvider

Provides the current time for seeding the request's absolute expiry.

Methods

DeviceAuthorizationStorage.RemoveAsync(string) Method

Removes a device authorization request from storage using its device code.

public System.Threading.Tasks.Task RemoveAsync(string deviceCode);

Parameters

deviceCode System.String

The device code identifier.

Implements RemoveAsync(string)

Returns

System.Threading.Tasks.Task
A task that completes when the request is removed from storage.

DeviceAuthorizationStorage.StoreAsync(string, DeviceAuthorizationRequest, TimeSpan) Method

Stores a device authorization request with the specified device code.

public System.Threading.Tasks.Task StoreAsync(string deviceCode, Abblix.Oidc.Server.Features.DeviceAuthorization.DeviceAuthorizationRequest request, System.TimeSpan expiresIn);

Parameters

deviceCode System.String

The unique device code identifier.

request DeviceAuthorizationRequest

The device authorization request to store.

expiresIn System.TimeSpan

The duration after which the stored request will expire.

Implements StoreAsync(string, DeviceAuthorizationRequest, TimeSpan)

Returns

System.Threading.Tasks.Task
A task that completes when the request is stored.

DeviceAuthorizationStorage.TryGetByDeviceCodeAsync(string) Method

Tries to retrieve a device authorization request by its device code. This is used by the client when polling the token endpoint.

public System.Threading.Tasks.Task<Abblix.Oidc.Server.Features.DeviceAuthorization.DeviceAuthorizationRequest?> TryGetByDeviceCodeAsync(string deviceCode);

Parameters

deviceCode System.String

The device code identifier.

Implements TryGetByDeviceCodeAsync(string)

Returns

System.Threading.Tasks.Task<DeviceAuthorizationRequest>
A task that returns the device authorization request if found; otherwise, null.

DeviceAuthorizationStorage.TryGetByUserCodeAsync(string) Method

Tries to retrieve a device authorization request by its user code. This is used during user verification to look up the pending request.

public System.Threading.Tasks.Task<System.Nullable<(string DeviceCode,Abblix.Oidc.Server.Features.DeviceAuthorization.DeviceAuthorizationRequest Request)>> TryGetByUserCodeAsync(string userCode);

Parameters

userCode System.String

The user-friendly verification code.

Implements TryGetByUserCodeAsync(string)

Returns

System.Threading.Tasks.Task<System.Nullable<<System.String,DeviceAuthorizationRequest>>>
A task that returns the device code and request if found; otherwise, null.

DeviceAuthorizationStorage.TryRemoveAsync(string, string) Method

Atomically attempts to remove a device authorization request by device code. Uses lock-based atomic removal protocol to prevent race conditions where multiple threads attempt to remove the same device code concurrently.

public System.Threading.Tasks.Task<bool> TryRemoveAsync(string deviceCode, string userCode);

Parameters

deviceCode System.String

The device code identifying the authorization request to remove.

userCode System.String

The user code for cleaning up the secondary index mapping.

Implements TryRemoveAsync(string, string)

Returns

System.Threading.Tasks.Task<System.Boolean>
A task that completes when the operation finishes, containing true if the request was successfully removed by this thread; false if another thread won the race or the device code didn't exist.

Remarks

This method performs atomic removal of both the device code entry and its associated user code mapping. By accepting the userCode as a parameter, it avoids an additional cache read operation, since the caller already has this information from a previous fetch.

Use Case: This method is used in the Device Authorization Grant flow (RFC 8628) when exchanging an authorized device code for tokens. The atomic removal ensures that concurrent token requests cannot both claim the same device code.

Atomicity: Uses Abblix.Utils.DistributedCacheExtensions.TryRemoveAsync(Microsoft.Extensions.Caching.Distributed.IDistributedCache,System.String,System.Nullable{System.TimeSpan},System.Threading.CancellationToken) which implements a lock-based protocol ensuring exactly one thread successfully removes the value even in concurrent scenarios. After successful removal, cleans up the user code mapping.

DeviceAuthorizationStorage.UpdateAsync(string, DeviceAuthorizationRequest, TimeSpan) Method

Updates an existing device authorization request in storage, refreshing its cache entry with the caller-supplied remaining lifetime.

public System.Threading.Tasks.Task UpdateAsync(string deviceCode, Abblix.Oidc.Server.Features.DeviceAuthorization.DeviceAuthorizationRequest request, System.TimeSpan expiresIn);

Parameters

deviceCode System.String

The device code identifier.

request DeviceAuthorizationRequest

The updated device authorization request.

expiresIn System.TimeSpan

The remaining lifetime to apply as the cache TTL. The caller derives it from the request's fixed expiry (RFC 8628 §3.2) so that repeated polling cannot extend the code.

Implements UpdateAsync(string, DeviceAuthorizationRequest, TimeSpan)

Returns

System.Threading.Tasks.Task
A task that completes when the request is updated.