mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
* add support for custom JWTs as static token * check issuer during jwt.verify in authentication * add issuer in JWTs for pwd resets & user invites
35 lines
760 B
TypeScript
35 lines
760 B
TypeScript
import atob from 'atob';
|
|
import logger from '../logger';
|
|
|
|
/**
|
|
* Check if a given string conforms to the structure of a JWT
|
|
* and whether it is issued by Directus.
|
|
*/
|
|
export default function isDirectusJWT(string: string): boolean {
|
|
const parts = string.split('.');
|
|
|
|
// JWTs have the structure header.payload.signature
|
|
if (parts.length !== 3) return false;
|
|
|
|
// Check if all segments are base64 encoded
|
|
try {
|
|
atob(parts[0]);
|
|
atob(parts[1]);
|
|
atob(parts[2]);
|
|
} catch (err: any) {
|
|
logger.error(err);
|
|
return false;
|
|
}
|
|
|
|
// Check if the header and payload are valid JSON
|
|
try {
|
|
JSON.parse(atob(parts[0]));
|
|
const payload = JSON.parse(atob(parts[1]));
|
|
if (payload.iss !== 'directus') return false;
|
|
} catch {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|