2022-06-18 13:39:08 -04:00
|
|
|
import * as core from './core.js';
|
|
|
|
import * as form from './form.js';
|
2016-03-12 13:50:43 -05:00
|
|
|
|
2022-10-04 21:20:47 -04:00
|
|
|
const kRefreshInterval = 1 * 7 * 24 * 60 * 60 * 1000;
|
2016-03-12 13:50:43 -05:00
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
/**
|
|
|
|
* Makes a Base64 value URL safe
|
2024-02-22 15:23:39 -05:00
|
|
|
* @param {string} value
|
2024-02-19 13:12:42 -05:00
|
|
|
* @returns TODOC
|
|
|
|
*/
|
2022-09-28 19:52:44 -04:00
|
|
|
function b64url(value) {
|
|
|
|
value = value.replaceAll('+', '-').replaceAll('/', '_');
|
|
|
|
let equals = value.indexOf('=');
|
2024-02-19 13:12:42 -05:00
|
|
|
|
2022-09-28 19:52:44 -04:00
|
|
|
if (equals !== -1) {
|
|
|
|
return value.substring(0, equals);
|
|
|
|
} else {
|
|
|
|
return value;
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
/**
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
* @param {string} value
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
*/
|
2022-09-28 19:52:44 -04:00
|
|
|
function unb64url(value) {
|
|
|
|
value = value.replaceAll('-', '+').replaceAll('_', '/');
|
|
|
|
let remainder = value.length % 4;
|
2024-02-19 13:12:42 -05:00
|
|
|
|
2022-09-28 19:52:44 -04:00
|
|
|
if (remainder == 3) {
|
|
|
|
return value + '=';
|
|
|
|
} else if (remainder == 2) {
|
|
|
|
return value + '==';
|
|
|
|
} else {
|
|
|
|
return value;
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
/**
|
|
|
|
* Creates a JSON Web Token
|
|
|
|
* @param {object} payload Object: {"name": "username"}
|
|
|
|
* @returns the JWT
|
|
|
|
*/
|
2022-09-28 19:52:44 -04:00
|
|
|
function makeJwt(payload) {
|
2024-02-19 13:12:42 -05:00
|
|
|
const ids = ssb.getIdentities(':auth');
|
2022-09-28 19:52:44 -04:00
|
|
|
let id;
|
2024-02-22 15:23:39 -05:00
|
|
|
|
2022-09-28 19:52:44 -04:00
|
|
|
if (ids?.length) {
|
|
|
|
id = ids[0];
|
|
|
|
} else {
|
|
|
|
id = ssb.createIdentity(':auth');
|
|
|
|
}
|
|
|
|
|
2024-02-24 11:09:34 -05:00
|
|
|
const final_payload = b64url(
|
|
|
|
base64Encode(
|
|
|
|
JSON.stringify(
|
|
|
|
Object.assign({}, payload, {
|
|
|
|
exp: new Date().valueOf() + kRefreshInterval,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const jwt = [
|
|
|
|
b64url(base64Encode(JSON.stringify({alg: 'HS256', typ: 'JWT'}))),
|
|
|
|
final_payload,
|
|
|
|
b64url(ssb.hmacsha256sign(final_payload, ':auth', id)),
|
|
|
|
].join('.');
|
2022-09-28 19:52:44 -04:00
|
|
|
return jwt;
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
/**
|
|
|
|
* Validates a JWT ?
|
|
|
|
* @param {*} session TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
*/
|
2022-09-28 19:52:44 -04:00
|
|
|
function readSession(session) {
|
|
|
|
let jwt_parts = session?.split('.');
|
2024-02-19 13:12:42 -05:00
|
|
|
|
2022-09-28 19:52:44 -04:00
|
|
|
if (jwt_parts?.length === 3) {
|
|
|
|
let [header, payload, signature] = jwt_parts;
|
2024-01-03 12:25:34 -05:00
|
|
|
header = JSON.parse(utf8Decode(base64Decode(unb64url(header))));
|
2024-02-19 13:12:42 -05:00
|
|
|
|
2022-09-28 19:52:44 -04:00
|
|
|
if (header.typ === 'JWT' && header.alg === 'HS256') {
|
|
|
|
signature = unb64url(signature);
|
|
|
|
let id = ssb.getIdentities(':auth');
|
2024-02-19 13:12:42 -05:00
|
|
|
|
2022-09-28 19:52:44 -04:00
|
|
|
if (id?.length && ssb.hmacsha256verify(id[0], payload, signature)) {
|
2024-02-19 13:12:42 -05:00
|
|
|
const result = JSON.parse(utf8Decode(base64Decode(unb64url(payload))));
|
2024-02-24 11:09:34 -05:00
|
|
|
const now = new Date().valueOf();
|
2024-02-19 13:12:42 -05:00
|
|
|
|
2022-10-04 21:20:47 -04:00
|
|
|
if (now < result.exp) {
|
|
|
|
print(`JWT valid for another ${(result.exp - now) / 1000} seconds.`);
|
2022-09-28 19:52:44 -04:00
|
|
|
return result;
|
|
|
|
} else {
|
2022-10-04 21:20:47 -04:00
|
|
|
print(`JWT expired by ${(now - result.exp) / 1000} seconds.`);
|
2022-09-28 19:52:44 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print('JWT verification failed.');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print('Invalid JWT header.');
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
/**
|
|
|
|
* TODOC
|
|
|
|
* @param {*} headers most likely an object
|
2024-02-22 15:23:39 -05:00
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
*/
|
2022-03-17 21:24:29 -04:00
|
|
|
function getCookies(headers) {
|
2023-01-28 17:44:45 -05:00
|
|
|
let cookies = {};
|
2022-03-17 21:24:29 -04:00
|
|
|
|
|
|
|
if (headers.cookie) {
|
2023-01-28 17:44:45 -05:00
|
|
|
let parts = headers.cookie.split(/,|;/);
|
|
|
|
for (let i in parts) {
|
2024-02-24 11:09:34 -05:00
|
|
|
let equals = parts[i].indexOf('=');
|
2023-01-28 17:44:45 -05:00
|
|
|
let name = parts[i].substring(0, equals).trim();
|
|
|
|
let value = parts[i].substring(equals + 1).trim();
|
2022-03-17 21:24:29 -04:00
|
|
|
cookies[name] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cookies;
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
/**
|
|
|
|
* Gets a user's permissions based on it's session ?
|
|
|
|
* @param {*} session TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
*/
|
2016-03-12 13:50:43 -05:00
|
|
|
function getPermissions(session) {
|
2023-01-28 17:44:45 -05:00
|
|
|
let permissions;
|
|
|
|
let entry = readSession(session);
|
2016-03-12 13:50:43 -05:00
|
|
|
if (entry) {
|
|
|
|
permissions = getPermissionsForUser(entry.name);
|
2024-02-24 11:09:34 -05:00
|
|
|
permissions.authenticated = entry.name !== 'guest';
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
return permissions || {};
|
|
|
|
}
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
/**
|
|
|
|
* Get a user's permissions ?
|
|
|
|
* @param {string} userName TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
*/
|
2016-03-12 13:50:43 -05:00
|
|
|
function getPermissionsForUser(userName) {
|
2023-01-28 17:44:45 -05:00
|
|
|
let permissions = {};
|
2024-02-24 11:09:34 -05:00
|
|
|
if (
|
|
|
|
core.globalSettings &&
|
|
|
|
core.globalSettings.permissions &&
|
|
|
|
core.globalSettings.permissions[userName]
|
|
|
|
) {
|
2023-01-28 17:44:45 -05:00
|
|
|
for (let i in core.globalSettings.permissions[userName]) {
|
2022-03-17 21:24:29 -04:00
|
|
|
permissions[core.globalSettings.permissions[userName][i]] = true;
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return permissions;
|
|
|
|
}
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
/**
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
* @param {*} headers
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
*/
|
2016-03-12 13:50:43 -05:00
|
|
|
function query(headers) {
|
2023-01-28 17:44:45 -05:00
|
|
|
let session = getCookies(headers).session;
|
|
|
|
let entry;
|
|
|
|
let autologin = tildefriends.args.autologin;
|
2024-02-24 11:09:34 -05:00
|
|
|
if ((entry = autologin ? {name: autologin} : readSession(session))) {
|
2022-10-04 21:20:47 -04:00
|
|
|
return {
|
|
|
|
session: entry,
|
2024-02-24 11:09:34 -05:00
|
|
|
permissions: autologin
|
|
|
|
? getPermissionsForUser(autologin)
|
|
|
|
: getPermissions(session),
|
2022-10-04 21:20:47 -04:00
|
|
|
};
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
/**
|
|
|
|
* Refreshes a JWT ?
|
|
|
|
* @param {*} credentials TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
*/
|
|
|
|
function makeRefresh(credentials) {
|
2023-07-16 18:03:47 -04:00
|
|
|
if (credentials?.session?.name) {
|
|
|
|
return {
|
|
|
|
token: makeJwt({name: credentials.session.name}),
|
|
|
|
interval: kRefreshInterval,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 16:15:50 -04:00
|
|
|
export {query, makeRefresh};
|