Skip to content

Commit

Permalink
Merge pull request #8 from adobe/users
Browse files Browse the repository at this point in the history
Allow for more than one user in Authorization
  • Loading branch information
auniverseaway authored Jan 21, 2024
2 parents bbc2540 + 3101bb0 commit a9a5754
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export default {
if (req.method === 'OPTIONS') return daResp({ status: 204 });

const daCtx = await getDaCtx(pathname, req, env);
const authed = await isAuthorized(env, daCtx.org, daCtx.user);
if (!authed) {

if (!daCtx.authorized) {
return daResp({ body: '', status: 401 });
}

Expand Down
57 changes: 38 additions & 19 deletions src/utils/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { decodeJwt } from 'jose';

async function setUser(user_id, expiration, headers, env) {
const resp = await fetch(`${env.IMS_ORIGIN}/ims/profile/v1`, { headers });
if (!resp.ok) {
// Something went wrong - either with the connection or the token isn't valid
// assume we are anon for now (but don't cache so we can try again next time)
return;
}
const json = await resp.json();

console.log(json);
Expand All @@ -11,31 +16,45 @@ async function setUser(user_id, expiration, headers, env) {
return value;
}

export async function getUser(req, env) {
export async function getUsers(req, env) {
const authHeader = req.headers.get('authorization');
const users = [];
if (authHeader) {
const token = req.headers.get('authorization').split(' ').pop();
if (!token) return;

console.log(decodeJwt(token));

const { user_id, created_at, expires_in } = decodeJwt(token);
console.log(user_id, created_at, expires_in);
// We accept mutliple tokens as this might be a collab session
for (let auth of authHeader.split(',')) {
const token = auth.split(' ').pop();
// If we have an empty token there was an anon user in the session
if (!token || token.trim().length === 0) {
users.push({ email: 'anonymous' });
continue;
}
console.log(decodeJwt(token));
const { user_id, created_at, expires_in } = decodeJwt(token);
console.log(user_id, created_at, expires_in);

const expires = Number(created_at) + Number(expires_in);
const now = Math.floor(new Date().getTime() / 1000);
const expires = Number(created_at) + Number(expires_in);
const now = Math.floor(new Date().getTime() / 1000);

if (expires >= now) {
// Find the user
let user = await env.DA_AUTH.get(user_id);
// If not found, create them
if (!user) user = await setUser(user_id, Math.floor(expires / 1000), req.headers, env);
// If something went wrong, die.
if (!user) return;
return JSON.parse(user);
if (expires >= now) {
// Find the user
let user = await env.DA_AUTH.get(user_id);
let headers = new Headers(req.headers);
headers.delete('authorization');
headers.set('authorization', `Bearer ${token}`);
// If not found, create them
if (!user) user = await setUser(user_id, Math.floor(expires / 1000), {'authorization': `Bearer ${token}`}, env);
// If something went wrong, be anon.
if (!user) {
users.push({ email: 'anonymous' });
} else {
users.push(JSON.parse(user));
}
}
}
} else {
users.push({ email: 'anonymous' });
}
return { email: 'anonymous' };
return users;
}

export async function isAuthorized(env, org, user) {
Expand Down
17 changes: 11 additions & 6 deletions src/utils/daCtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
*/

import getObject from '../storage/object/get';
import { getUser, isAuthorized } from './auth';
import { getUsers, isAuthorized } from './auth';

/**
* Gets Dark Alley Context
* @param {pathname} pathname
* @returns {DaCtx} The Dark Alley Context.
*/
export async function getDaCtx(pathname, req, env) {
const user = await getUser(req, env);
const users = await getUsers(req, env);

console.log(user);
console.log(users);

// Santitize the string
const lower = pathname.slice(1).toLowerCase();
Expand All @@ -29,11 +29,16 @@ export async function getDaCtx(pathname, req, env) {
const [api, org, ...parts] = sanitized.split('/');

// Set base details
const daCtx = { api, org, user };
const daCtx = { api, org, users };

// Get org properties
if (org) {
daCtx.authorized = await isAuthorized(env, org, user);
daCtx.authorized = true;
// check for all users in the session if they are authorized
for (let user in users) {
if (!await isAuthorized(env, org, user)) {
daCtx.authorized = false;
break;
}
}

// Sanitize the remaining path parts
Expand Down

0 comments on commit a9a5754

Please sign in to comment.