Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagetypescript
import * as jsonwebtoken from 'jsonwebtoken';
import moment from "moment";
import fs from "fs";
import {JWTPrivateKey} from "./JWTPrivateKey";
   
export interface JWTPrivateKey {
    type: string,
    keyId: string,    
    key: string,
    userId: string
}
   
public static createJWT(): string {
    const jwtFile = fs.readFileSync('./jwt.json', 'utf8');
    const jwtData = JSON.parse(jwtFile) as JWTPrivateKey;
    
    // Valid from now minus 10 seconds
    const validFrom: number = Math.floor(Date.now() / 1000) - 10;
    
    // Expiration 1 hour after now
    const expiration: number = Math.floor(Date.now() / 1000) + (60 * 60);
    
    return jsonwebtoken.sign({
        iss: jwtData.userId,
        sub: jwtData.userId,
        aud: 'https://micromate-q4ee42.zitadel.cloud',
        iat: validFrom,
        exp: expiration
    }, jwtData.key, {algorithm: 'RS256', keyid: jwtData.keyId});
}