()
| 27 | private _token: string; |
| 28 | |
| 29 | get token(): Promise<string> { |
| 30 | return (async () => { |
| 31 | if (this._token) return this._token; |
| 32 | |
| 33 | const oHeader = { alg: "RS256", typ: "JWT" }; |
| 34 | const tNow = KJUR.jws.IntDate.get("now"); |
| 35 | const tEnd = tNow + this.tokenExpiration; |
| 36 | const oPayload = { |
| 37 | iss: this.emailAddress, |
| 38 | scope: "https://www.googleapis.com/auth/chat.bot", |
| 39 | aud: "https://oauth2.googleapis.com/token", |
| 40 | iat: tNow, |
| 41 | exp: tEnd, |
| 42 | }; |
| 43 | |
| 44 | const sHeader = JSON.stringify(oHeader); |
| 45 | const sPayload = JSON.stringify(oPayload); |
| 46 | const sJWT = KJUR.jws.JWS.sign( |
| 47 | "RS256", |
| 48 | sHeader, |
| 49 | sPayload, |
| 50 | this.privateKey, |
| 51 | ); |
| 52 | |
| 53 | const response = await fetch("https://oauth2.googleapis.com/token", { |
| 54 | method: "POST", |
| 55 | headers: { |
| 56 | "Content-Type": "application/x-www-form-urlencoded", |
| 57 | }, |
| 58 | body: new URLSearchParams({ |
| 59 | grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer", |
| 60 | assertion: sJWT, |
| 61 | }).toString(), |
| 62 | }); |
| 63 | |
| 64 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 65 | // @ts-ignore |
| 66 | const { access_token } = await response.json(); |
| 67 | |
| 68 | this._token = access_token; |
| 69 | return access_token; |
| 70 | })(); |
| 71 | } |
| 72 | |
| 73 | private api = async (...args: Parameters<typeof fetch>) => { |
| 74 | const request = new Request(new Request(...args).clone()); |
nothing calls this directly
no outgoing calls
no test coverage detected