UNPKG

5.18 kBTypeScriptView Raw
1import * as http from 'node:http';
2import { AsyncCreatable } from '@salesforce/kit';
3import { AuthInfo } from './org/authInfo';
4import { JwtOAuth2Config } from './org/authInfo';
5/**
6 * Handles the creation of a web server for web based login flows.
7 *
8 * Usage:
9 * ```
10 * const oauthConfig = {
11 * loginUrl: this.flags.instanceurl,
12 * clientId: this.flags.clientid,
13 * };
14 *
15 * const oauthServer = await WebOAuthServer.create({ oauthConfig });
16 * await oauthServer.start();
17 * await open(oauthServer.getAuthorizationUrl(), { wait: false });
18 * const authInfo = await oauthServer.authorizeAndSave();
19 * ```
20 */
21export declare class WebOAuthServer extends AsyncCreatable<WebOAuthServer.Options> {
22 static DEFAULT_PORT: number;
23 private authUrl;
24 private logger;
25 private webServer;
26 private oauth2;
27 private oauthConfig;
28 private oauthError;
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 type Options = {
83 oauthConfig: JwtOAuth2Config;
84 };
85 type Request = http.IncomingMessage & {
86 query: {
87 code: string;
88 state: string;
89 error?: string;
90 error_description?: string;
91 };
92 };
93}
94/**
95 * Handles the actions specific to the http server
96 */
97export declare class WebServer extends AsyncCreatable<WebServer.Options> {
98 static DEFAULT_CLIENT_SOCKET_TIMEOUT: number;
99 server: http.Server;
100 port: number;
101 host: string;
102 private logger;
103 private sockets;
104 private redirectStatus;
105 constructor(options: WebServer.Options);
106 /**
107 * Starts the http server after checking that the port is open
108 */
109 start(): Promise<void>;
110 /**
111 * Closes the http server and all open sockets
112 */
113 close(): void;
114 /**
115 * sends a response error.
116 *
117 * @param status the statusCode for the response.
118 * @param message the message for the http body.
119 * @param response the response to write the error to.
120 */
121 sendError(status: number, message: string, response: http.ServerResponse): void;
122 /**
123 * sends a response redirect.
124 *
125 * @param status the statusCode for the response.
126 * @param url the url to redirect to.
127 * @param response the response to write the redirect to.
128 */
129 doRedirect(status: number, url: string, response: http.ServerResponse): void;
130 /**
131 * sends a response to the browser reporting an error.
132 *
133 * @param error the oauth error
134 * @param response the HTTP response.
135 */
136 reportError(error: Error, response: http.ServerResponse): void;
137 /**
138 * sends a response to the browser reporting the success.
139 *
140 * @param response the HTTP response.
141 */
142 reportSuccess(response: http.ServerResponse): void;
143 /**
144 * Preflight request:
145 *
146 * https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
147 * https://www.w3.org/TR/2020/SPSD-cors-20200602/#resource-preflight-requests
148 */
149 handlePreflightRequest(response: http.ServerResponse): void;
150 handleSuccess(response: http.ServerResponse): Promise<void>;
151 handleError(response: http.ServerResponse): Promise<void>;
152 protected init(): Promise<void>;
153 private handleRedirect;
154 /**
155 * Make sure we can't open a socket on the localhost/host port. It's important because we don't want to send
156 * auth tokens to a random strange port listener. We want to make sure we can startup our server first.
157 *
158 * @private
159 */
160 private checkOsPort;
161 /**
162 * check and get the socket timeout form what was set in process.env.SFDX_HTTP_SOCKET_TIMEOUT
163 *
164 * @returns {number} - represents the socket timeout in ms
165 * @private
166 */
167 private getSocketTimeout;
168}
169declare namespace WebServer {
170 type Options = {
171 port?: number;
172 host?: string;
173 };
174}
175export {};