UNPKG

4.49 kBTypeScriptView Raw
1/// <reference types="node" />
2import * as http from 'http';
3import { AsyncCreatable } from '@salesforce/kit';
4import { OAuth2Options } from 'jsforce';
5import { AuthInfo } from './authInfo';
6/**
7 * Handles the creation of a web server for web based login flows.
8 *
9 * Usage:
10 * ```
11 * const oauthConfig = {
12 * loginUrl: this.flags.instanceurl,
13 * clientId: this.flags.clientid,
14 * };
15 *
16 * const oauthServer = await WebOAuthServer.create({ oauthConfig });
17 * await oauthServer.start();
18 * await open(oauthServer.getAuthorizationUrl(), { wait: false });
19 * const authInfo = await oauthServer.authorizeAndSave();
20 * ```
21 */
22export declare class WebOAuthServer extends AsyncCreatable<WebOAuthServer.Options> {
23 static DEFAULT_PORT: number;
24 private authUrl;
25 private logger;
26 private webServer;
27 private oauth2;
28 private oauthConfig;
29 constructor(options: WebOAuthServer.Options);
30 /**
31 * Returns the configured oauthLocalPort or the WebOAuthServer.DEFAULT_PORT
32 *
33 * @returns {Promise<number>}
34 */
35 static determineOauthPort(): Promise<number>;
36 /**
37 * Returns the authorization url that's used for the login flow
38 *
39 * @returns {string}
40 */
41 getAuthorizationUrl(): string;
42 /**
43 * Executes the oauth request and creates a new AuthInfo when successful
44 *
45 * @returns {Promise<AuthInfo>}
46 */
47 authorizeAndSave(): Promise<AuthInfo>;
48 /**
49 * Starts the web server
50 */
51 start(): Promise<void>;
52 protected init(): Promise<void>;
53 /**
54 * Executes the oauth request
55 *
56 * @returns {Promise<AuthInfo>}
57 */
58 private executeOauthRequest;
59 /**
60 * Parses the auth code from the request url
61 *
62 * @param response the http response
63 * @param request the http request
64 * @returns {Nullable<string>}
65 */
66 private parseAuthCodeFromRequest;
67 /**
68 * Closes the request
69 *
70 * @param request the http request
71 */
72 private closeRequest;
73 /**
74 * Validates that the state param in the auth url matches the state
75 * param in the http request
76 *
77 * @param request the http request
78 */
79 private validateState;
80}
81export declare namespace WebOAuthServer {
82 interface Options {
83 oauthConfig: OAuth2Options;
84 }
85 type Request = http.IncomingMessage & {
86 query: {
87 code: string;
88 state: string;
89 };
90 };
91}
92/**
93 * Handles the actions specific to the http server
94 */
95export declare class WebServer extends AsyncCreatable<WebServer.Options> {
96 static DEFAULT_CLIENT_SOCKET_TIMEOUT: number;
97 server: http.Server;
98 port: number;
99 host: string;
100 private logger;
101 private sockets;
102 constructor(options: WebServer.Options);
103 /**
104 * Starts the http server after checking that the port is open
105 */
106 start(): Promise<void>;
107 /**
108 * Closes the http server and all open sockets
109 */
110 close(): void;
111 /**
112 * sends a response error.
113 *
114 * @param statusCode he statusCode for the response.
115 * @param message the message for the http body.
116 * @param response the response to write the error to.
117 */
118 sendError(status: number, message: string, response: http.ServerResponse): void;
119 /**
120 * sends a response redirect.
121 *
122 * @param statusCode the statusCode for the response.
123 * @param url the url to redirect to.
124 * @param response the response to write the redirect to.
125 */
126 doRedirect(status: number, url: string, response: http.ServerResponse): void;
127 /**
128 * sends a response to the browser reporting an error.
129 *
130 * @param error the error
131 * @param response the response to write the redirect to.
132 */
133 reportError(error: Error, response: http.ServerResponse): void;
134 protected init(): Promise<void>;
135 /**
136 * Make sure we can't open a socket on the localhost/host port. It's important because we don't want to send
137 * auth tokens to a random strange port listener. We want to make sure we can startup our server first.
138 *
139 * @private
140 */
141 private checkOsPort;
142 /**
143 * check and get the socket timeout form what was set in process.env.SFDX_HTTP_SOCKET_TIMEOUT
144 *
145 * @returns {number} - represents the socket timeout in ms
146 * @private
147 */
148 private getSocketTimeout;
149}
150declare namespace WebServer {
151 interface Options {
152 port?: number;
153 host?: string;
154 }
155}
156export {};