2022-09-23 09:03:25 +00:00
|
|
|
// @ts-ignore
|
2021-03-17 09:30:42 +00:00
|
|
|
import jwtDecode from 'jwt-decode';
|
|
|
|
|
2022-09-23 09:03:25 +00:00
|
|
|
import { IState } from '../../app/types';
|
2022-08-16 12:53:53 +00:00
|
|
|
import { getLocalParticipant } from '../participants/functions';
|
2022-09-23 09:03:25 +00:00
|
|
|
import { parseURLParams } from '../util/parseURLParams';
|
2017-05-26 22:11:33 +00:00
|
|
|
|
2021-03-17 09:30:42 +00:00
|
|
|
import { MEET_FEATURES } from './constants';
|
|
|
|
|
2017-05-26 22:11:33 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the JSON Web Token (JWT), if any, defined by a specific
|
|
|
|
* {@link URL}.
|
|
|
|
*
|
|
|
|
* @param {URL} url - The {@code URL} to parse and retrieve the JSON Web Token
|
|
|
|
* (JWT), if any, from.
|
|
|
|
* @returns {string} The JSON Web Token (JWT), if any, defined by the specified
|
|
|
|
* {@code url}; otherwise, {@code undefined}.
|
|
|
|
*/
|
2022-09-23 09:03:25 +00:00
|
|
|
export function parseJWTFromURLParams(url: URL | Location = window.location) {
|
|
|
|
// @ts-ignore
|
2017-05-26 22:11:33 +00:00
|
|
|
return parseURLParams(url, true, 'search').jwt;
|
|
|
|
}
|
2020-10-26 11:53:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the user name after decoding the jwt.
|
|
|
|
*
|
2022-09-23 09:03:25 +00:00
|
|
|
* @param {IState} state - The app state.
|
2020-10-26 11:53:44 +00:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
2022-09-23 09:03:25 +00:00
|
|
|
export function getJwtName(state: IState) {
|
2020-11-05 17:35:03 +00:00
|
|
|
const { user } = state['features/base/jwt'];
|
2020-10-26 11:53:44 +00:00
|
|
|
|
2021-03-10 19:36:32 +00:00
|
|
|
return user?.name;
|
2020-10-26 11:53:44 +00:00
|
|
|
}
|
2021-03-17 09:30:42 +00:00
|
|
|
|
2022-08-16 12:53:53 +00:00
|
|
|
/**
|
|
|
|
* Check if the given JWT feature is enabled.
|
|
|
|
*
|
2022-09-23 09:03:25 +00:00
|
|
|
* @param {IState} state - The app state.
|
2022-08-16 12:53:53 +00:00
|
|
|
* @param {string} feature - The feature we want to check.
|
|
|
|
* @param {boolean} ifNoToken - Default value if there is no token.
|
2022-10-17 15:32:18 +00:00
|
|
|
* @param {boolean} ifNotInFeatures - Default value if features prop exists but does not have the {@code feature}.
|
|
|
|
* @returns {bolean}
|
2022-08-16 12:53:53 +00:00
|
|
|
*/
|
2022-10-17 15:32:18 +00:00
|
|
|
export function isJwtFeatureEnabled(state: IState, feature: string, ifNoToken = false, ifNotInFeatures = false) {
|
2022-08-16 12:53:53 +00:00
|
|
|
const { jwt } = state['features/base/jwt'];
|
|
|
|
|
|
|
|
if (!jwt) {
|
|
|
|
return ifNoToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { features } = getLocalParticipant(state) || {};
|
|
|
|
|
|
|
|
// If `features` is undefined, act as if everything is enabled.
|
|
|
|
if (typeof features === 'undefined') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-17 15:32:18 +00:00
|
|
|
if (typeof features[feature as keyof typeof features] === 'undefined') {
|
|
|
|
return ifNotInFeatures;
|
|
|
|
}
|
|
|
|
|
2022-09-23 09:03:25 +00:00
|
|
|
return String(features[feature as keyof typeof features]) === 'true';
|
2022-08-16 12:53:53 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 09:30:42 +00:00
|
|
|
/**
|
|
|
|
* Checks whether a given timestamp is a valid UNIX timestamp in seconds.
|
2022-08-30 14:21:58 +00:00
|
|
|
* We convert to milliseconds during the check since `Date` works with milliseconds for UNIX timestamp values.
|
2021-03-17 09:30:42 +00:00
|
|
|
*
|
|
|
|
* @param {any} timestamp - A UNIX timestamp in seconds as stored in the jwt.
|
|
|
|
* @returns {boolean} - Whether the timestamp is indeed a valid UNIX timestamp or not.
|
|
|
|
*/
|
2022-09-23 09:03:25 +00:00
|
|
|
function isValidUnixTimestamp(timestamp: number | string) {
|
2021-03-17 09:30:42 +00:00
|
|
|
return typeof timestamp === 'number' && timestamp * 1000 === new Date(timestamp * 1000).getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list with all validation errors for the given jwt.
|
|
|
|
*
|
|
|
|
* @param {string} jwt - The jwt.
|
|
|
|
* @returns {Array<string>} - An array containing all jwt validation errors.
|
|
|
|
*/
|
|
|
|
export function validateJwt(jwt: string) {
|
2022-09-23 09:03:25 +00:00
|
|
|
const errors: string[] = [];
|
2021-03-17 09:30:42 +00:00
|
|
|
|
|
|
|
if (!jwt) {
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentTimestamp = new Date().getTime();
|
|
|
|
|
|
|
|
try {
|
|
|
|
const header = jwtDecode(jwt, { header: true });
|
|
|
|
const payload = jwtDecode(jwt);
|
|
|
|
|
|
|
|
if (!header || !payload) {
|
|
|
|
errors.push('- Missing header or payload');
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
2021-05-05 09:21:22 +00:00
|
|
|
const {
|
|
|
|
aud,
|
|
|
|
context,
|
|
|
|
exp,
|
|
|
|
iss,
|
|
|
|
nbf,
|
|
|
|
sub
|
|
|
|
} = payload;
|
2021-03-17 09:30:42 +00:00
|
|
|
|
2021-05-05 09:21:22 +00:00
|
|
|
// JaaS only
|
2022-09-23 09:03:25 +00:00
|
|
|
if (sub?.startsWith('vpaas-magic-cookie')) {
|
2021-05-05 09:21:22 +00:00
|
|
|
const { kid } = header;
|
2021-03-17 09:30:42 +00:00
|
|
|
|
2021-05-05 09:21:22 +00:00
|
|
|
// if Key ID is missing, we return the error immediately without further validations.
|
|
|
|
if (!kid) {
|
|
|
|
errors.push('- Key ID(kid) missing');
|
2021-03-17 09:30:42 +00:00
|
|
|
|
2021-05-05 09:21:22 +00:00
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kid.substring(0, kid.indexOf('/')) !== sub) {
|
2021-03-17 09:30:42 +00:00
|
|
|
errors.push('- Key ID(kid) does not match sub');
|
|
|
|
}
|
2021-05-05 09:21:22 +00:00
|
|
|
|
|
|
|
if (aud !== 'jitsi') {
|
2021-03-17 09:30:42 +00:00
|
|
|
errors.push('- invalid `aud` value. It should be `jitsi`');
|
|
|
|
}
|
|
|
|
|
2021-05-05 09:21:22 +00:00
|
|
|
if (iss !== 'chat') {
|
2021-03-17 09:30:42 +00:00
|
|
|
errors.push('- invalid `iss` value. It should be `chat`');
|
|
|
|
}
|
|
|
|
|
2021-05-05 09:21:22 +00:00
|
|
|
if (!context?.features) {
|
2021-03-17 09:30:42 +00:00
|
|
|
errors.push('- `features` object is missing from the payload');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 09:21:22 +00:00
|
|
|
if (!isValidUnixTimestamp(nbf)) {
|
2021-03-17 09:30:42 +00:00
|
|
|
errors.push('- invalid `nbf` value');
|
2021-05-05 09:21:22 +00:00
|
|
|
} else if (currentTimestamp < nbf * 1000) {
|
2021-03-17 09:30:42 +00:00
|
|
|
errors.push('- `nbf` value is in the future');
|
|
|
|
}
|
|
|
|
|
2021-05-05 09:21:22 +00:00
|
|
|
if (!isValidUnixTimestamp(exp)) {
|
2021-03-17 09:30:42 +00:00
|
|
|
errors.push('- invalid `exp` value');
|
2021-05-05 09:21:22 +00:00
|
|
|
} else if (currentTimestamp > exp * 1000) {
|
2021-03-17 09:30:42 +00:00
|
|
|
errors.push('- token is expired');
|
|
|
|
}
|
|
|
|
|
2021-05-05 09:21:22 +00:00
|
|
|
if (!context) {
|
2021-03-17 09:30:42 +00:00
|
|
|
errors.push('- `context` object is missing from the payload');
|
2021-05-05 09:21:22 +00:00
|
|
|
} else if (context.features) {
|
|
|
|
const { features } = context;
|
2022-10-04 13:02:49 +00:00
|
|
|
const meetFeatures = Object.values(MEET_FEATURES);
|
2021-03-17 09:30:42 +00:00
|
|
|
|
|
|
|
Object.keys(features).forEach(feature => {
|
2022-10-04 13:02:49 +00:00
|
|
|
if (meetFeatures.includes(feature)) {
|
2021-03-17 09:30:42 +00:00
|
|
|
const featureValue = features[feature];
|
|
|
|
|
|
|
|
// cannot use truthy or falsy because we need the exact value and type check.
|
|
|
|
if (
|
|
|
|
featureValue !== true
|
|
|
|
&& featureValue !== false
|
|
|
|
&& featureValue !== 'true'
|
|
|
|
&& featureValue !== 'false'
|
|
|
|
) {
|
|
|
|
errors.push(`- Invalid value for feature: ${feature}`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errors.push(`- Invalid feature: ${feature}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-09-23 09:03:25 +00:00
|
|
|
} catch (e: any) {
|
2021-03-17 09:30:42 +00:00
|
|
|
errors.push(e ? e.message : '- unspecified jwt error');
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
}
|