1
1
import { readFile } from 'node:fs/promises' ;
2
+ import path from 'node:path' ;
2
3
3
4
import { expect } from 'chai' ;
4
5
5
- import { MongoClient , MongoInvalidArgumentError , OIDC_WORKFLOWS } from '../mongodb' ;
6
+ import {
7
+ MongoClient ,
8
+ MongoInvalidArgumentError ,
9
+ OIDC_WORKFLOWS ,
10
+ OIDCClientInfo ,
11
+ OIDCMechanismServerStep1 ,
12
+ OIDCRefreshFunction ,
13
+ OIDCRequestFunction ,
14
+ OIDCRequestTokenResult ,
15
+ Collection
16
+ } from '../mongodb' ;
6
17
7
18
describe ( 'MONGODB-OIDC' , function ( ) {
8
19
context ( 'when running in the environment' , function ( ) {
@@ -12,13 +23,81 @@ describe('MONGODB-OIDC', function () {
12
23
} ) ;
13
24
14
25
describe ( 'OIDC Auth Spec Prose Tests' , function ( ) {
26
+ // Set up the cache variable.
27
+ const cache = OIDC_WORKFLOWS . get ( 'callback' ) . cache ;
28
+ // Creates a request function for use in the test.
29
+ const createRequestCallback = ( username = 'test_user1' , expiresInSeconds ?: number ) => {
30
+ return async ( clientInfo : OIDCClientInfo , serverInfo : OIDCMechanismServerStep1 ) => {
31
+ const token = await readFile ( path . join ( process . env . OIDC_TOKEN_DIR , username ) , {
32
+ encoding : 'utf8'
33
+ } ) ;
34
+ // Do some basic property assertions.
35
+ expect ( clientInfo ) . to . have . property ( 'timeoutSeconds' ) ;
36
+ expect ( serverInfo ) . to . have . property ( 'issuer' ) ;
37
+ expect ( serverInfo ) . to . have . property ( 'clientId' ) ;
38
+ const response : OIDCRequestTokenResult = { accessToken : token } ;
39
+ if ( expiresInSeconds ) {
40
+ response . expiresInSeconds = expiresInSeconds ;
41
+ }
42
+ return response ;
43
+ } ;
44
+ } ;
45
+
46
+ // Creates a refresh function for use in the test.
47
+ const createRefreshCallback = ( username = 'test_user1' , expiresInSeconds ?: number ) => {
48
+ return async (
49
+ clientInfo : OIDCClientInfo ,
50
+ serverInfo : OIDCMechanismServerStep1 ,
51
+ tokenResult : OIDCRequestTokenResult
52
+ ) => {
53
+ const token = await readFile ( path . join ( process . env . OIDC_TOKEN_DIR , username ) , {
54
+ encoding : 'utf8'
55
+ } ) ;
56
+ // Do some basic property assertions.
57
+ expect ( clientInfo ) . to . have . property ( 'timeoutSeconds' ) ;
58
+ expect ( serverInfo ) . to . have . property ( 'issuer' ) ;
59
+ expect ( serverInfo ) . to . have . property ( 'clientId' ) ;
60
+ expect ( tokenResult ) . to . have . property ( 'accessToken' ) ;
61
+ const response : OIDCRequestTokenResult = { accessToken : token } ;
62
+ if ( expiresInSeconds ) {
63
+ response . expiresInSeconds = expiresInSeconds ;
64
+ }
65
+ return response ;
66
+ } ;
67
+ } ;
68
+
15
69
describe ( '1. Callback-Driven Auth' , function ( ) {
70
+ let client : MongoClient ;
71
+ let collection : Collection ;
72
+
73
+ beforeEach ( function ( ) {
74
+ cache . clear ( ) ;
75
+ } ) ;
76
+
77
+ afterEach ( async function ( ) {
78
+ await client ?. close ( ) ;
79
+ } ) ;
80
+
16
81
describe ( '1.1 Single Principal Implicit Username' , function ( ) {
82
+ before ( function ( ) {
83
+ client = new MongoClient ( 'mongodb://localhost/?authMechanism=MONGODB-OIDC' , {
84
+ authMechanismProperties : {
85
+ REQUEST_TOKEN_CALLBACK : createRequestCallback ( )
86
+ }
87
+ } ) ;
88
+ collection = client . db ( 'test' ) . collection ( 'test' ) ;
89
+ } ) ;
90
+
17
91
// Clear the cache.
18
92
// Create a request callback returns a valid token.
19
93
// Create a client that uses the default OIDC url and the request callback.
20
94
// Perform a find operation. that succeeds.
21
95
// Close the client.
96
+ it ( 'successfully authenticates' , function ( ) {
97
+ expect ( async ( ) => {
98
+ await collection . findOne ( ) ;
99
+ } ) . to . not . throw ;
100
+ } ) ;
22
101
} ) ;
23
102
24
103
describe ( '1.2 Single Principal Explicit Username' , function ( ) {
0 commit comments