5058

Token, Cached

oauth
secrettext
json
caching
interface

Token, Cached

Your extension talks to an external logistics API that is protected by OAuth 2.0 client credentials. Every call needs a bearer token from the identity provider's token endpoint — but tokens are not free: the provider throttles the token endpoint, and a busy session that requests a fresh token for every call gets rate-limited within minutes. The fix is a token cache that requests once, serves the token from memory while it is still fresh, and renews it just before it expires. And because a leaked token is a leaked identity, the secret material must live in SecretText from the moment it enters your code.

Requirements

Create an interface named "OAuth Token Client", declared exactly like this — it is the wire your cache sends token requests through:

interface "OAuth Token Client"
{
    procedure SendTokenRequest(TokenEndpointUrl: Text; ClientId: Text; ClientSecret: SecretText; Scope: Text; var ResponseStatusCode: Integer; var ResponseBody: Text)
}

The grading container has no outbound network, so the tests inject their own implementation of this interface — a mock identity provider that counts every request and answers through the two var parameters. In production, the registered client would URL-encode the pieces into the form body and make the real HttpClient POST behind this same interface.

Then create a codeunit named "OAuth Token Cache" with four public procedures:

procedure SetTokenClient(NewTokenClient: Interface "OAuth Token Client")
procedure Configure(TokenEndpointUrl: Text; ClientId: Text; ClientSecret: SecretText; Scope: Text)
procedure GetAccessToken(AsOf: DateTime): SecretText
procedure GetAuthorizationHeaderValue(AsOf: DateTime): SecretText

The tests create one instance of your codeunit, call SetTokenClient and Configure once each, and then call the two getters with explicit timestamps. All state — the client, the configuration and the cached token — lives in that instance.

The rest of the statement and 4 hints Unlock with Pro
ALBusiness Central 28.5
Pro
Pro problem

Solve it with Pro

You’re reading the opening of the statement. Pro unlocks the whole problem:

  • The full statement
  • Starter code in the browser editor
  • 13 grading tests, run on Business Central 28.5
  • 4 hints
  • The reference solution

Pro also opens all 92 Pro problems and lifts the daily limits on compile checks and submissions.

$10 / month
or $100 / year
Upgrade
This problem is part of Pro
Pro unlocks the full statement, tests, hints and solution. The code editor works on a computer.
Upgrade