CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-azure--core-auth

Provides low-level interfaces and helper methods for authentication in Azure SDK

Overall
score

97%

Overview
Eval results
Files

task.mdevals/scenario-4/

Token Cache Manager

Build a token cache manager that intelligently handles access token storage and retrieval based on expiration tracking.

Requirements

Implement a TokenCacheManager class that manages cached access tokens from credentials. The manager should:

  1. Store tokens retrieved from a credential along with their expiration information
  2. Return cached tokens when they are still valid
  3. Automatically refresh tokens when they have expired or should be proactively refreshed
  4. Support multiple scopes, caching tokens separately for each scope

TokenCacheManager API

The manager should accept a credential that has a getToken(scopes, options) method which returns access tokens with expiration timestamps.

When getToken() is called on the manager:

  • If no token is cached for the requested scope, fetch a new token from the credential
  • If a cached token exists and the current time is before the expiresOnTimestamp, return the cached token
  • If a cached token has a refreshAfterTimestamp and the current time is after it, fetch a fresh token
  • If a cached token has expired (current time is after expiresOnTimestamp), fetch a fresh token

Time Handling

All timestamps are in Unix epoch milliseconds (number of milliseconds since January 1, 1970 UTC). Use Date.now() to get the current time.

Test Cases

  • When requesting a token for a scope that has never been cached, it fetches from the credential and caches the result @test
  • When requesting a token for a scope with a valid cached token (current time before expiresOnTimestamp), it returns the cached token without calling the credential @test
  • When requesting a token for a scope with an expired cached token (current time after expiresOnTimestamp), it fetches a fresh token from the credential @test
  • When requesting a token with a refreshAfterTimestamp that has passed, it fetches a fresh token even though the token has not expired @test

@generates

export interface AccessToken {
  token: string;
  expiresOnTimestamp: number;
  refreshAfterTimestamp?: number;
}

export interface TokenCredential {
  getToken(scopes: string | string[], options?: any): Promise<AccessToken | null>;
}

export class TokenCacheManager {
  constructor(credential: TokenCredential);
  getToken(scopes: string | string[]): Promise<AccessToken | null>;
}

Dependencies { .dependencies }

@azure/core-auth { .dependency }

Provides authentication types and interfaces.

Install with Tessl CLI

npx tessl i tessl/npm-azure--core-auth

tile.json