Skip to content

Commit

Permalink
align with driver types
Browse files Browse the repository at this point in the history
  • Loading branch information
mabaasit committed Jun 4, 2024
1 parent 4cf9ffd commit bd406c1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 55 deletions.
6 changes: 2 additions & 4 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { MongoDBOIDCPluginImpl } from './plugin';
import type {
MongoDBOIDCLogEventsMap,
OIDCAbortSignal,
OIDCRefreshFunction,
OIDCRequestFunction,
OIDCCallbackFunction,
TypedEventEmitter,
} from './types';
import type { RequestOptions } from 'https';
Expand Down Expand Up @@ -197,8 +196,7 @@ export interface MongoDBOIDCPluginOptions {
/** @public */
export interface MongoDBOIDCPluginMongoClientOptions {
readonly authMechanismProperties: {
readonly REQUEST_TOKEN_CALLBACK: OIDCRequestFunction;
readonly REFRESH_TOKEN_CALLBACK: OIDCRefreshFunction;
readonly OIDC_HUMAN_CALLBACK: OIDCCallbackFunction;
};
}

Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ export type {

export type {
TypedEventEmitter,
OIDCCallbackContext,
OIDCRefreshFunction,
OIDCRequestFunction,
OIDCCallbackParams,
OIDCCallbackFunction,
IdPServerInfo,
IdPServerResponse,
OIDCAbortSignal,
Expand Down
19 changes: 8 additions & 11 deletions src/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import type {
MongoDBOIDCPlugin,
MongoDBOIDCPluginOptions,
OIDCAbortSignal,
OIDCCallbackContext,
IdPServerInfo,
OIDCRequestFunction,
OIDCCallbackFunction,
OpenBrowserOptions,
} from './';
import { createMongoDBOIDCPlugin, hookLoggerToMongoLogWriter } from './';
Expand Down Expand Up @@ -57,15 +56,13 @@ async function fetchBrowser({ url }: OpenBrowserOptions): Promise<void> {
function requestToken(
plugin: MongoDBOIDCPlugin,
oidcParams: IdPServerInfo,
abortSignal?: OIDCAbortSignal | number
): ReturnType<OIDCRequestFunction> {
const clientInfo: OIDCCallbackContext = { version: 0 };
if (typeof abortSignal === 'number') clientInfo.timeoutSeconds = abortSignal;
else if (abortSignal) clientInfo.timeoutContext = abortSignal;
return plugin.mongoClientOptions.authMechanismProperties.REQUEST_TOKEN_CALLBACK(
oidcParams,
clientInfo
);
abortSignal?: OIDCAbortSignal
): ReturnType<OIDCCallbackFunction> {
return plugin.mongoClientOptions.authMechanismProperties.OIDC_HUMAN_CALLBACK({
timeoutContext: abortSignal ?? new AbortController().signal,
version: 1,
idpInfo: oidcParams,
});
}

function getJWTContents(input: string): Record<string, unknown> {
Expand Down
35 changes: 15 additions & 20 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {
MongoDBOIDCLogEventsMap,
OIDCAbortSignal,
OIDCCallbackContext,
OIDCCallbackParams,
IdPServerInfo,
IdPServerResponse,
TypedEventEmitter,
Expand All @@ -13,7 +13,6 @@ import {
messageFromError,
normalizeObject,
throwIfAborted,
timeoutSignal,
validateSecureHTTPUrl,
withAbortCheck,
withLock,
Expand Down Expand Up @@ -179,8 +178,7 @@ export class MongoDBOIDCPluginImpl implements MongoDBOIDCPlugin {
this.logger = options.logger ?? new EventEmitter();
this.mongoClientOptions = {
authMechanismProperties: {
REQUEST_TOKEN_CALLBACK: this.requestToken.bind(this),
REFRESH_TOKEN_CALLBACK: this.requestToken.bind(this),
OIDC_HUMAN_CALLBACK: this.requestToken.bind(this),
},
};
this.timers = { setTimeout, clearTimeout };
Expand Down Expand Up @@ -871,12 +869,12 @@ export class MongoDBOIDCPluginImpl implements MongoDBOIDCPlugin {
}

public async requestToken(
serverMetadata: IdPServerInfo,
context: OIDCCallbackContext
params: OIDCCallbackParams
): Promise<IdPServerResponse> {
if (context.version !== 0) {
if (params.version !== 1) {
throw new MongoDBOIDCError(
`OIDC MongoDB driver protocol mismatch: unknown version ${context.version}`
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`OIDC MongoDB driver protocol mismatch: unknown version ${params.version}`
);
}

Expand All @@ -886,23 +884,20 @@ export class MongoDBOIDCPluginImpl implements MongoDBOIDCPlugin {
);
}

const state = this.getAuthState(serverMetadata);
if (!params.idpInfo) {
throw new MongoDBOIDCError('No IdP information provided');
}

const state = this.getAuthState(params.idpInfo);

if (state.currentAuthAttempt) {
return await state.currentAuthAttempt;
}

// The currently plan is for the 6.x driver (which may drop support
// for Node.js 14.x) to pass in an actual AbortSignal here. For
// compatibility with the 5.x driver/AbortSignal-less-Node.js, we accept
// a timeout in milliseconds as well.
const driverAbortSignal =
context.timeoutContext ??
(context.timeoutSeconds
? timeoutSignal(context.timeoutSeconds * 1000)
: undefined);

const newAuthAttempt = this.initiateAuthAttempt(state, driverAbortSignal);
const newAuthAttempt = this.initiateAuthAttempt(
state,
params.timeoutContext
);
try {
state.currentAuthAttempt = newAuthAttempt;
return await newAuthAttempt;
Expand Down
25 changes: 8 additions & 17 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,32 +94,23 @@ export interface IdPServerResponse {
}

/**
* A copy of the Node.js driver's `OIDCCallbackContext`
* A copy of the Node.js driver's `OIDCCallbackParams` using `OIDCAbortSignal` instead of `AbortSignal`
* @public
*/
export interface OIDCCallbackContext {
export interface OIDCCallbackParams {
refreshToken?: string;
timeoutSeconds?: number;
timeoutContext?: OIDCAbortSignal;
version: number;
timeoutContext: OIDCAbortSignal;
version: 1;
username?: string;
idpInfo?: IdPServerInfo;
}

/**
* A copy of the Node.js driver's `OIDCRequestFunction`
* @public
*/
export type OIDCRequestFunction = (
info: IdPServerInfo,
context: OIDCCallbackContext
) => Promise<IdPServerResponse>;

/**
* A copy of the Node.js driver's `OIDCRefreshFunction`
* @public
*/
export type OIDCRefreshFunction = (
info: IdPServerInfo,
context: OIDCCallbackContext
export type OIDCCallbackFunction = (
params: OIDCCallbackParams
) => Promise<IdPServerResponse>;

/** @public */
Expand Down

0 comments on commit bd406c1

Please sign in to comment.