Files
directus/api/src/utils/is-directus-jwt.ts
Azri Kahar 0575cb4836 Add custom JWTs support for static token (#7830)
* add support for custom JWTs as static token

* check issuer during jwt.verify in authentication

* add issuer in JWTs for pwd resets & user invites
2021-09-09 14:47:28 -04:00

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;
}