-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathmongodb_oidc.ts
147 lines (131 loc) · 3.73 KB
/
mongodb_oidc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import type { Document } from 'bson';
import { MongoInvalidArgumentError, MongoMissingCredentialsError } from '../../error';
import type { HandshakeDocument } from '../connect';
import type { Connection } from '../connection';
import { AuthContext, AuthProvider } from './auth_provider';
import type { MongoCredentials } from './mongo_credentials';
import { AwsServiceWorkflow } from './mongodb_oidc/aws_service_workflow';
import { CallbackWorkflow } from './mongodb_oidc/callback_workflow';
/** Error when credentials are missing. */
const MISSING_CREDENTIALS_ERROR = 'AuthContext must provide credentials.';
/**
* @public
* @experimental
*/
export interface IdPServerInfo {
issuer: string;
clientId: string;
requestScopes?: string[];
}
/**
* @public
* @experimental
*/
export interface IdPServerResponse {
accessToken: string;
expiresInSeconds?: number;
refreshToken?: string;
}
/**
* @public
* @experimental
*/
export interface OIDCCallbackContext {
refreshToken?: string;
timeoutSeconds?: number;
timeoutContext?: AbortSignal;
version: number;
}
/**
* @public
* @experimental
*/
export type OIDCRequestFunction = (
info: IdPServerInfo,
context: OIDCCallbackContext
) => Promise<IdPServerResponse>;
/**
* @public
* @experimental
*/
export type OIDCRefreshFunction = (
info: IdPServerInfo,
context: OIDCCallbackContext
) => Promise<IdPServerResponse>;
type ProviderName = 'aws' | 'callback';
export interface Workflow {
/**
* All device workflows must implement this method in order to get the access
* token and then call authenticate with it.
*/
execute(
connection: Connection,
credentials: MongoCredentials,
reauthenticating: boolean,
response?: Document
): Promise<Document>;
/**
* Get the document to add for speculative authentication.
*/
speculativeAuth(credentials: MongoCredentials): Promise<Document>;
}
/** @internal */
export const OIDC_WORKFLOWS: Map<ProviderName, Workflow> = new Map();
OIDC_WORKFLOWS.set('callback', new CallbackWorkflow());
OIDC_WORKFLOWS.set('aws', new AwsServiceWorkflow());
/**
* OIDC auth provider.
* @experimental
*/
export class MongoDBOIDC extends AuthProvider {
/**
* Instantiate the auth provider.
*/
constructor() {
super();
}
/**
* Authenticate using OIDC
*/
override async auth(authContext: AuthContext): Promise<void> {
const { connection, reauthenticating, response } = authContext;
const credentials = getCredentials(authContext);
const workflow = getWorkflow(credentials);
await workflow.execute(connection, credentials, reauthenticating, response);
}
/**
* Add the speculative auth for the initial handshake.
*/
override async prepare(
handshakeDoc: HandshakeDocument,
authContext: AuthContext
): Promise<HandshakeDocument> {
const credentials = getCredentials(authContext);
const workflow = getWorkflow(credentials);
const result = await workflow.speculativeAuth(credentials);
return { ...handshakeDoc, ...result };
}
}
/**
* Get credentials from the auth context, throwing if they do not exist.
*/
function getCredentials(authContext: AuthContext): MongoCredentials {
const { credentials } = authContext;
if (!credentials) {
throw new MongoMissingCredentialsError(MISSING_CREDENTIALS_ERROR);
}
return credentials;
}
/**
* Gets either a device workflow or callback workflow.
*/
function getWorkflow(credentials: MongoCredentials): Workflow {
const providerName = credentials.mechanismProperties.PROVIDER_NAME;
const workflow = OIDC_WORKFLOWS.get(providerName || 'callback');
if (!workflow) {
throw new MongoInvalidArgumentError(
`Could not load workflow for provider ${credentials.mechanismProperties.PROVIDER_NAME}`
);
}
return workflow;
}