import { KJUR } from 'jsrsasign' ; import { Credentials } from './options' ; export class AccessToken { value : string ; private readonly header : string ; private readonly payload : string ; constructor ( private readonly credentials ) { this . header = JSON . stringify ( { alg : 'RS256' , typ : 'JWT' , kid : credentials . privateKeyId , } ) ; this . payload = JSON . stringify ( { iss : credentials . clientEmail , sub : credentials . clientEmail , iat : KJUR . jws . IntDate . get ( 'now' ) , exp : KJUR . jws . IntDate . get ( 'now + 1hour' ) , aud : 'https://translation.googleapis.com/' , } ) ; this . value = this . generate ( ) ; } refresh ( ) { this . value = this . generate ( ) ; } private generate ( ) { return KJUR . jws . JWS . sign ( 'RS256' , this . header , this . payload , this . credentials . privateKey ) ; } }