333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/// <reference types="node" />
 
import * as http from 'http';
import { Readable, Stream } from 'stream';
 
import * as shared from './shared';
 
type ms = number;
type s = number;
 
declare namespace XOAuth2 {
    interface Options {
        /** User e-mail address */
        user?: string;
        /** Client ID value */
        clientId?: string;
        /** Client secret value */
        clientSecret?: string;
        /** Refresh token for an user */
        refreshToken?: string;
        /** Endpoint for token generation, defaults to 'https://accounts.google.com/o/oauth2/token' */
        accessUrl?: string;
        /** An existing valid accessToken */
        accessToken?: string;
        /** Private key for JSW */
        privateKey?: string | { key: string; passphrase: string; };
        /** Optional Access Token expire time in ms */
        expires?: ms;
        /** Optional TTL for Access Token in seconds */
        timeout?: s;
        /** Function to run when a new access token is required */
        provisionCallback?(user: string, renew: boolean, callback: (err: Error | null, accessToken: string, expires: number) => void): void;
        serviceClient?: string;
    }
 
    interface Token {
        user: string;
        accessToken: string;
        expires: number;
    }
 
    interface RequestParams {
        customHeaders?: http.OutgoingHttpHeaders;
    }
}
 
declare class XOAuth2 extends Stream {
    options: XOAuth2.Options;
    logger: shared.Logger;
    accessToken: string | false;
    expires: number;
 
    constructor(options?: XOAuth2.Options, logger?: shared.Logger);
 
    /** Returns or generates (if previous has expired) a XOAuth2 token */
    getToken(renew: boolean, callback: (err: Error | null, accessToken: string) => void): void;
 
    /** Updates token values */
    updateToken(accessToken: string, timeout: s): XOAuth2.Token;
 
    /** Generates a new XOAuth2 token with the credentials provided at initialization */
    generateToken(callback: (err: Error | null, accessToken: string) => void): void;
 
    /** Converts an access_token and user id into a base64 encoded XOAuth2 token */
    buildXOAuth2Token(accessToken: string): string;
 
    /**
     * Custom POST request handler.
     * This is only needed to keep paths short in Windows – usually this module
     * is a dependency of a dependency and if it tries to require something
     * like the request module the paths get way too long to handle for Windows.
     * As we do only a simple POST request we do not actually require complicated
     * logic support (no redirects, no nothing) anyway.
     */
    postRequest(
        url: string,
        payload: string | Buffer | Readable | { [key: string]: string },
        params: XOAuth2.RequestParams,
        callback: (err: Error | null, buf: Buffer) => void
    ): void;
 
    /** Encodes a buffer or a string into Base64url format */
    toBase64URL(data: Buffer | string): string;
 
    /** Creates a JSON Web Token signed with RS256 (SHA256 + RSA) */
    jwtSignRS256(payload: object): string;
 
    addListener(event: 'error', listener: (err: Error) => void): this;
    addListener(event: 'token', listener: (token: XOAuth2.Token) => void): this;
 
    emit(event: 'error', error: Error): boolean;
    emit(event: 'token', token: XOAuth2.Token): boolean;
 
    on(event: 'error', listener: (err: Error) => void): this;
    on(event: 'token', listener: (token: XOAuth2.Token) => void): this;
 
    once(event: 'error', listener: (err: Error) => void): this;
    once(event: 'token', listener: (token: XOAuth2.Token) => void): this;
 
    prependListener(event: 'error', listener: (err: Error) => void): this;
    prependListener(event: 'end', listener: (token: XOAuth2.Token) => void): this;
 
    prependOnceListener(event: 'error', listener: (err: Error) => void): this;
    prependOnceListener(event: 'end', listener: (token: XOAuth2.Token) => void): this;
 
    listeners(event: 'error'): Array<(err: Error) => void>;
    listeners(event: 'end'): Array<(token: XOAuth2.Token) => void>;
}
 
export = XOAuth2;