11import { addAzureParams , AZURE_BASE_URL } from '../../../client-side-encryption/providers/azure' ;
22import { MongoAzureError } from '../../../error' ;
33import { get } from '../../../utils' ;
4- import type { MongoCredentials } from '../mongo_credentials' ;
5- import { type AccessToken , MachineWorkflow } from './machine_workflow' ;
6- import { type TokenCache } from './token_cache' ;
4+ import type { OIDCCallbackFunction , OIDCCallbackParams , OIDCResponse } from '../mongodb_oidc' ;
75
86/** Azure request headers. */
97const AZURE_HEADERS = Object . freeze ( { Metadata : 'true' , Accept : 'application/json' } ) ;
@@ -17,39 +15,29 @@ const TOKEN_RESOURCE_MISSING_ERROR =
1715 'TOKEN_RESOURCE must be set in the auth mechanism properties when ENVIRONMENT is azure.' ;
1816
1917/**
20- * Device workflow implementation for Azure .
21- *
22- * @internal
18+ * The callback function to be used in the automated callback workflow .
19+ * @param params - The OIDC callback parameters.
20+ * @returns The OIDC response.
2321 */
24- export class AzureMachineWorkflow extends MachineWorkflow {
25- /**
26- * Instantiate the machine workflow.
27- */
28- constructor ( cache : TokenCache ) {
29- super ( cache ) ;
22+ export const callback : OIDCCallbackFunction = async (
23+ params : OIDCCallbackParams
24+ ) : Promise < OIDCResponse > => {
25+ const tokenAudience = params . tokenAudience ;
26+ const username = params . username ;
27+ if ( ! tokenAudience ) {
28+ throw new MongoAzureError ( TOKEN_RESOURCE_MISSING_ERROR ) ;
3029 }
31-
32- /**
33- * Get the token from the environment.
34- */
35- async getToken ( credentials ?: MongoCredentials ) : Promise < AccessToken > {
36- const tokenAudience = credentials ?. mechanismProperties . TOKEN_RESOURCE ;
37- const username = credentials ?. username ;
38- if ( ! tokenAudience ) {
39- throw new MongoAzureError ( TOKEN_RESOURCE_MISSING_ERROR ) ;
40- }
41- const response = await getAzureTokenData ( tokenAudience , username ) ;
42- if ( ! isEndpointResultValid ( response ) ) {
43- throw new MongoAzureError ( ENDPOINT_RESULT_ERROR ) ;
44- }
45- return response ;
30+ const response = await getAzureTokenData ( tokenAudience , username ) ;
31+ if ( ! isEndpointResultValid ( response ) ) {
32+ throw new MongoAzureError ( ENDPOINT_RESULT_ERROR ) ;
4633 }
47- }
34+ return response ;
35+ } ;
4836
4937/**
5038 * Hit the Azure endpoint to get the token data.
5139 */
52- async function getAzureTokenData ( tokenAudience : string , username ?: string ) : Promise < AccessToken > {
40+ async function getAzureTokenData ( tokenAudience : string , username ?: string ) : Promise < OIDCResponse > {
5341 const url = new URL ( AZURE_BASE_URL ) ;
5442 addAzureParams ( url , tokenAudience , username ) ;
5543 const response = await get ( url , {
@@ -62,8 +50,8 @@ async function getAzureTokenData(tokenAudience: string, username?: string): Prom
6250 }
6351 const result = JSON . parse ( response . body ) ;
6452 return {
65- access_token : result . access_token ,
66- expires_in : Number ( result . expires_in )
53+ accessToken : result . access_token ,
54+ expiresInSeconds : Number ( result . expires_in )
6755 } ;
6856}
6957
@@ -77,9 +65,9 @@ function isEndpointResultValid(
7765) : token is { access_token : unknown ; expires_in : unknown } {
7866 if ( token == null || typeof token !== 'object' ) return false ;
7967 return (
80- 'access_token ' in token &&
81- typeof token . access_token === 'string' &&
82- 'expires_in ' in token &&
83- typeof token . expires_in === 'number'
68+ 'accessToken ' in token &&
69+ typeof token . accessToken === 'string' &&
70+ 'expiresInSeconds ' in token &&
71+ typeof token . expiresInSeconds === 'number'
8472 ) ;
8573}
0 commit comments