Logo

dev-resources.site

for different kinds of informations.

Oidc node mongodb adapter in normal functions

Published at
3/1/2024
Categories
node
oidc
mongodb
javascript
Author
vimal_adithan
Categories
4 categories in total
node
open
oidc
open
mongodb
open
javascript
open
Author
13 person written this
vimal_adithan
open
Oidc node mongodb adapter in normal functions

const { MongoClient } = require('mongodb');
const snakeCase = require('lodash/snakeCase');

let DB;

const grantable = new Set([
'access_token',
'authorization_code',
'refresh_token',
'device_code',
'backchannel_authentication_request',
]);

const collections = new Set();

function addCollection(name) {
const nu = collections.has(name);
collections.add(name);
if (!nu) {
createIndexes(name);
}
}

function createIndexes(name) {
DB.collection(name).createIndexes([
...(grantable.has(name)
? [{
key: { 'payload.grantId': 1 },
}] : []),
...(name === 'device_code'
? [{
key: { 'payload.userCode': 1 },
unique: true,
}] : []),
...(name === 'session'
? [{
key: { 'payload.uid': 1 },
unique: true,
}] : []),
{
key: { expiresAt: 1 },
expireAfterSeconds: 0,
},
]).catch(console.error);
}

async function upsert(name, _id, payload, expiresIn) {
let expiresAt;

if (expiresIn) {
expiresAt = new Date(Date.now() + (expiresIn * 1000));
}

await updateOne(name, { _id }, { $set: { payload, ...(expiresAt ? { expiresAt } : undefined) } }, { upsert: true });
}

async function find(name, _id) {
const result = await findOne(name, { _id }, { payload: 1 });

if (!result) return undefined;
return result.payload;
}

async function findByUserCode(name, userCode) {
const result = await findOne(name, { 'payload.userCode': userCode }, { payload: 1 });

if (!result) return undefined;
return result.payload;
}

async function findByUid(name, uid) {
const result = await findOne(name, { 'payload.uid': uid }, { payload: 1 });

if (!result) return undefined;
return result.payload;
}

async function destroy(name, _id) {
await deleteOne(name, { _id });
}

async function revokeByGrantId(name, grantId) {
await deleteMany(name, { 'payload.grantId': grantId });
}

async function consume(name, _id) {
await findOneAndUpdate(name, { _id }, { $set: { 'payload.consumed': Math.floor(Date.now() / 1000) } });
}

function findOneAndUpdate(name, filter, update) {
return DB.collection(name).findOneAndUpdate(filter, update);
}

function findOne(name, filter, projection) {
return DB.collection(name).findOne(filter, projection);
}

function updateOne(name, filter, update, options) {
return DB.collection(name).updateOne(filter, update, options);
}

function deleteOne(name, filter) {
return DB.collection(name).deleteOne(filter);
}

function deleteMany(name, filter) {
return DB.collection(name).deleteMany(filter);
}

function connect() {
return MongoClient.connect(process.env.MONGODB_URI)
.then(connection => {
DB = connection.db(connection.s.options.dbName);
});
}

module.exports = {
addCollection,
upsert,
find,
findByUserCode,
findByUid,
destroy,
revokeByGrantId,
consume,
connect
};

oidc Article's
30 articles in total
Favicon
Defending OAuth2: Advanced Tactics to Block Replay Attacks
Favicon
Understanding the Differences Between OAuth2 and OpenID Connect (OIDC)
Favicon
Demystifying Social Logins: How OAuth2 Powers Seamless Authentication
Favicon
OAuth2 vs. OpenID Connect: Understanding the Differences
Favicon
GitHub Action security hardening with OpenID (OIDC) Connect - "Password-Less"
Favicon
OIDC vs SAML: A Comprehensive Technical Comparison
Favicon
OIDC Prompt 101: A simple guide for developers
Favicon
How to create a WeCom App to enable WeCom Login for the Web app
Favicon
No More Passwords! OIDC Terraform Module Makes GCP-GitHub Authentication a Breeze
Favicon
Learn OIDC - Part 2 - JWT
Favicon
Oidc node mongodb adapter in normal functions
Favicon
Learn OIDC - Part 1 - JWS
Favicon
OpenVPN + SSO via OAUTH2
Favicon
Kubernetes Cluster as an OpenID Connect Identity Provider
Favicon
How To Configure Audience In Keycloak
Favicon
Single Sign-On (SSO) with Zoho in Vue3
Favicon
Demystifying OpenID Connect (OIDC) - The Key to Secure and Seamless Authentication
Favicon
Adding single sign-on to a Next.js app using OIDC
Favicon
Implementing OpenID Connect (OIDC) Authentication with Nuxt 3
Favicon
Connect GitHub Actions to Azure using OpenID Connect
Favicon
OpenID Connect authentication with Apache Kafka 3.1
Favicon
Writing Java library to build OAuth 2.0 Authorization Server / OpenID Connect Identity Provider
Favicon
SSO Building blocks - SAML, OAuth 2.0 and OpenID Connect
Favicon
ASP.NET: Autenticaciรณn OIDC Multi Tenant - Parte 2
Favicon
Keycloak 19.0.1 and Setting the id_token_hint
Favicon
OIDC Forever, IAM Credentials Never!
Favicon
Fortifying federated access to AWS via OIDC
Favicon
Understanding OAuth and OIDC: Introduction
Favicon
OAuth 2.0 and OpenID Connect Explained: Building Secure Authentication Systems
Favicon
Securely authenticate to Google Cloud from GitHub

Featured ones: