Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow device auth flow without id_token MONGOSH-1669 #187

Merged
merged 14 commits into from
Feb 7, 2024
Prev Previous commit
Next Next commit
test: add integration test
  • Loading branch information
paula-stacho committed Feb 5, 2024
commit d940a442cb600a4b217b38b5b8feb62c57bb1595
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -57,7 +57,7 @@
"@mongodb-js/eslint-config-devtools": "^0.9.9",
"@mongodb-js/mocha-config-devtools": "^1.0.0",
"@mongodb-js/monorepo-tools": "^1.1.4",
"@mongodb-js/oidc-mock-provider": "^0.6.2",
"@mongodb-js/oidc-mock-provider": "^0.7.0",
"@mongodb-js/prettier-config-devtools": "^1.0.1",
"@mongodb-js/tsconfig-devtools": "^1.0.0",
"@types/chai": "^4.2.21",
77 changes: 77 additions & 0 deletions src/integration.spec.ts
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import { createMongoDBOIDCPlugin } from './';
import { OIDCMockProvider } from '@mongodb-js/oidc-mock-provider';
import { MongoCluster } from 'mongodb-runner';
import path from 'path';
import sinon from 'sinon';

// node-fetch@3 is ESM-only...
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
@@ -235,4 +236,80 @@ describe('integration test with mongod', function () {
}
});
});

context('can authenticate with a mock IdP - without id_token', function () {
let provider: OIDCMockProvider;
let connectionString: string;

before(async function () {
if (+process.version.slice(1).split('.')[0] < 16) {
// JWK support for Node.js KeyObject.export() is only Node.js 16+
// but the OIDCMockProvider implementation needs it.
return this.skip();
}
provider = await OIDCMockProvider.create({
getTokenPayload() {
return {
expires_in: 3600,
payload: {
// Define the user information stored inside the access tokens
groups: ['testgroup'],
sub: 'testuser',
aud: 'resource-server-audience-value',
},
// id_token will not be included
skipIdToken: true,
};
},
});

const serverOidcConfig = [
{
issuer: provider.issuer,
clientId: 'mockclientid',
requestScopes: ['mongodbGroups'],
authorizationClaim: 'groups',
audience: 'resource-server-audience-value',
authNamePrefix: 'dev',
},
];

cluster = await spawnMongod(serverOidcConfig);
connectionString = `mongodb://${cluster.hostport}/?authMechanism=MONGODB-OIDC`;
});

after(async function () {
await Promise.all([cluster?.close?.(), provider?.close?.()]);
});

it('can successfully authenticate with a fake Device Auth Flow - with a warning', async function () {
const { mongoClientOptions, logger } = createMongoDBOIDCPlugin({
notifyDeviceFlow: () => {},
allowedFlows: ['device-auth'],
});
const logEmitSpy = sinon.spy(logger, 'emit');
const client = await MongoClient.connect(connectionString, {
...mongoClientOptions,
});
// without the id token, a warning will be logged
sinon.assert.calledWith(
logEmitSpy,
'mongodb-oidc-plugin:missing-id-token'
);
try {
const status = await client
.db('admin')
.command({ connectionStatus: 1 });
expect(status).to.deep.equal({
ok: 1,
authInfo: {
authenticatedUsers: [{ user: 'dev/testuser', db: '$external' }],
authenticatedUserRoles: [{ role: 'dev/testgroup', db: 'admin' }],
},
});
} finally {
await client.close();
}
});
});
});
Loading