Skip to content

Add support for OIDC type tokens. #36

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const {generateId} = require('bnid');
// load config defaults
require('./config');

const TOKEN_TYPES = ['password', 'nonce', 'challenge', 'totp'];
const TOKEN_TYPES = ['password', 'nonce', 'challenge', 'totp', 'oidc'];

// numeric-only digits for human readibility and easy mobile entry
const NUMERIC_DIGITS = '0123456789';
Expand Down
9 changes: 7 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const PERMISSIONS = bedrock.config.permission.permissions;
api.set = async ({
actor, account, email, type, clientId, serviceId, hash,
authenticationMethod = type, requiredAuthenticationMethods = [],
notify = true, typeOptions = {entryStyle: 'human'}
notify = true, typeOptions = {entryStyle: 'human'}, userId
} = {}) => {
assert.optionalString(account, 'account');
assert.optionalString(email, 'email');
Expand Down Expand Up @@ -118,6 +118,8 @@ api.set = async ({
validateBcryptHash(hash);
token.salt = getBcryptSalt(hash);
token.sha256 = prefixedHash(hash);
} else if(type === 'oidc') {
token.userId = userId;
} else if(type === 'nonce') {
// FIXME: may need to allow up to N tokens, one per `clientId` if
// `clientId` is set
Expand Down Expand Up @@ -504,7 +506,7 @@ api.verify = async ({
}

assert.optionalString(challenge, 'challenge');
if(!(hash || challenge)) {
if(type !== 'oidc' && !(hash || challenge)) {
throw new SyntaxError(
'One of "hash" or "challenge" must be provided.');
}
Expand Down Expand Up @@ -537,6 +539,7 @@ api.verify = async ({
}

let tokenValue = record.meta['bedrock-authn-token'].tokens[type];

const now = new Date();
if(type === 'nonce') {
for(const token of tokenValue) {
Expand Down Expand Up @@ -597,6 +600,8 @@ api.verify = async ({
verified = crypto.timingSafeEqual(
new Buffer(token.sha256, 'base64'),
new Buffer(prefixedHash(hash), 'base64'));
} else if(type === 'oidc') {
verified = token.userId === challenge;
} else if(type === 'totp') {
const cfg = config['authn-token'];
const {window} = cfg.totp;
Expand Down