UNPKG

2.26 kBTypeScriptView Raw
1import { AsyncCreatable } from '@salesforce/kit';
2import { OAuth2Config } from '@jsforce/jsforce-node';
3import { JsonMap, Nullable } from '@salesforce/ts-types';
4import { AuthInfo } from './org/authInfo';
5export type DeviceCodeResponse = {
6 device_code: string;
7 interval: number;
8 user_code: string;
9 verification_uri: string;
10} & JsonMap;
11export type DeviceCodePollingResponse = {
12 access_token: string;
13 refresh_token: string;
14 signature: string;
15 scope: string;
16 instance_url: string;
17 id: string;
18 token_type: string;
19 issued_at: string;
20} & JsonMap;
21/**
22 * Handles device based login flows
23 *
24 * Usage:
25 * ```
26 * const oauthConfig = {
27 * loginUrl: this.flags.instanceurl,
28 * clientId: this.flags.clientid,
29 * };
30 * const deviceOauthService = await DeviceOauthService.create(oauthConfig);
31 * const loginData = await deviceOauthService.requestDeviceLogin();
32 * console.log(loginData);
33 * const approval = await deviceOauthService.awaitDeviceApproval(loginData);
34 * const authInfo = await deviceOauthService.authorizeAndSave(approval);
35 * ```
36 */
37export declare class DeviceOauthService extends AsyncCreatable<OAuth2Config> {
38 static RESPONSE_TYPE: string;
39 static GRANT_TYPE: string;
40 static SCOPE: string;
41 private static POLLING_COUNT_MAX;
42 private logger;
43 private options;
44 private pollingCount;
45 constructor(options: OAuth2Config);
46 /**
47 * Begin the authorization flow by requesting the login
48 *
49 * @returns {Promise<DeviceCodeResponse>}
50 */
51 requestDeviceLogin(): Promise<DeviceCodeResponse>;
52 /**
53 * Polls the server until successful response OR max attempts have been made
54 *
55 * @returns {Promise<Nullable<DeviceCodePollingResponse>>}
56 */
57 awaitDeviceApproval(loginData: DeviceCodeResponse): Promise<Nullable<DeviceCodePollingResponse>>;
58 /**
59 * Creates and saves new AuthInfo
60 *
61 * @returns {Promise<AuthInfo>}
62 */
63 authorizeAndSave(approval: DeviceCodePollingResponse): Promise<AuthInfo>;
64 protected init(): Promise<void>;
65 private getLoginOptions;
66 private getPollingOptions;
67 private getDeviceFlowRequestUrl;
68 private poll;
69 private shouldContinuePolling;
70 private pollForDeviceApproval;
71}