1
1
import { addAzureParams , AZURE_BASE_URL } from '../../../client-side-encryption/providers/azure' ;
2
2
import { MongoAzureError } from '../../../error' ;
3
3
import { 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' ;
7
5
8
6
/** Azure request headers. */
9
7
const AZURE_HEADERS = Object . freeze ( { Metadata : 'true' , Accept : 'application/json' } ) ;
@@ -17,39 +15,29 @@ const TOKEN_RESOURCE_MISSING_ERROR =
17
15
'TOKEN_RESOURCE must be set in the auth mechanism properties when ENVIRONMENT is azure.' ;
18
16
19
17
/**
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.
23
21
*/
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 ) ;
30
29
}
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 ) ;
46
33
}
47
- }
34
+ return response ;
35
+ } ;
48
36
49
37
/**
50
38
* Hit the Azure endpoint to get the token data.
51
39
*/
52
- async function getAzureTokenData ( tokenAudience : string , username ?: string ) : Promise < AccessToken > {
40
+ async function getAzureTokenData ( tokenAudience : string , username ?: string ) : Promise < OIDCResponse > {
53
41
const url = new URL ( AZURE_BASE_URL ) ;
54
42
addAzureParams ( url , tokenAudience , username ) ;
55
43
const response = await get ( url , {
@@ -62,8 +50,8 @@ async function getAzureTokenData(tokenAudience: string, username?: string): Prom
62
50
}
63
51
const result = JSON . parse ( response . body ) ;
64
52
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 )
67
55
} ;
68
56
}
69
57
@@ -77,9 +65,9 @@ function isEndpointResultValid(
77
65
) : token is { access_token : unknown ; expires_in : unknown } {
78
66
if ( token == null || typeof token !== 'object' ) return false ;
79
67
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'
84
72
) ;
85
73
}
0 commit comments