UNPKG

twitter-api-v2-patch

Version:

Strongly typed, full-featured, light, versatile yet powerful Twitter API v1.1 and v2 client for Node.js.

28 lines (27 loc) 984 B
import * as crypto from 'crypto'; export class OAuth2Helper { static getCodeVerifier() { return this.generateRandomString(128); } static getCodeChallengeFromVerifier(verifier) { return this.escapeBase64Url(crypto .createHash('sha256') .update(verifier) .digest('base64')); } static getAuthHeader(clientId, clientSecret) { const key = encodeURIComponent(clientId) + ':' + encodeURIComponent(clientSecret); return Buffer.from(key).toString('base64'); } static generateRandomString(length) { let text = ''; const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~'; for (let i = 0; i < length; i++) { text += possible[Math.floor(Math.random() * possible.length)]; } return text; } static escapeBase64Url(string) { return string.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_'); } }