UNPKG

6.77 kBTypeScriptView Raw
1import { AsyncCreatable } from '@salesforce/kit';
2import { Nullable, Optional } from '@salesforce/ts-types';
3import { OAuth2Options } from 'jsforce';
4import { Connection } from './connection';
5/**
6 * Fields for authorization, org, and local information.
7 */
8export interface AuthFields {
9 accessToken?: string;
10 alias?: string;
11 authCode?: string;
12 clientId?: string;
13 clientSecret?: string;
14 created?: string;
15 createdOrgInstance?: string;
16 devHubUsername?: string;
17 instanceUrl?: string;
18 isDevHub?: boolean;
19 loginUrl?: string;
20 orgId?: string;
21 password?: string;
22 privateKey?: string;
23 refreshToken?: string;
24 scratchAdminUsername?: string;
25 snapshot?: string;
26 userId?: string;
27 username?: string;
28 usernames?: string[];
29 userProfileName?: string;
30}
31/**
32 * Options for access token flow.
33 */
34export interface AccessTokenOptions {
35 accessToken?: string;
36 loginUrl?: string;
37 instanceUrl?: string;
38}
39/**
40 * A function to update a refresh token when the access token is expired.
41 */
42export declare type RefreshFn = (conn: Connection, callback: (err: Nullable<Error>, accessToken?: string, res?: object) => Promise<void>) => Promise<void>;
43/**
44 * Options for {@link Connection}.
45 */
46export declare type ConnectionOptions = AuthFields & {
47 /**
48 * OAuth options.
49 */
50 oauth2?: Partial<OAuth2Options>;
51 /**
52 * Refresh token callback.
53 */
54 refreshFn?: RefreshFn;
55};
56/**
57 * Salesforce URLs.
58 */
59export declare enum SfdcUrl {
60 SANDBOX = "https://test.salesforce.com",
61 PRODUCTION = "https://login.salesforce.com"
62}
63/**
64 * Handles persistence and fetching of user authentication information using
65 * JWT, OAuth, or refresh tokens. Sets up the refresh flows that jsForce will
66 * use to keep tokens active. An AuthInfo can also be created with an access
67 * token, but AuthInfos created with access tokens can't be persisted to disk.
68 *
69 * **See** [Authorization](https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_auth.htm)
70 *
71 * **See** [Salesforce DX Usernames and Orgs](https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_cli_usernames_orgs.htm)
72 *
73 * ```
74 * // Creating a new authentication file.
75 * const authInfo = await AuthInfo.create({
76 * username: myAdminUsername,
77 * oauth2Options: {
78 * loginUrl, authCode, clientId, clientSecret
79 * }
80 * );
81 * authInfo.save();
82 *
83 * // Creating an authorization info with an access token.
84 * const authInfo = await AuthInfo.create({
85 * username: accessToken
86 * });
87 *
88 * // Using an existing authentication file.
89 * const authInfo = await AuthInfo.create({
90 * username: myAdminUsername
91 * });
92 *
93 * // Using the AuthInfo
94 * const connection = await Connection.create({ authInfo });
95 * ```
96 */
97export declare class AuthInfo extends AsyncCreatable<AuthInfo.Options> {
98 /**
99 * Get a list of all auth files stored in the global directory.
100 * @returns {Promise<string[]>}
101 */
102 static listAllAuthFiles(): Promise<string[]>;
103 /**
104 * Returns true if one or more authentications are persisted.
105 */
106 static hasAuthentications(): Promise<boolean>;
107 /**
108 * Get the authorization URL.
109 * @param options The options to generate the URL.
110 */
111 static getAuthorizationUrl(options: OAuth2Options): string;
112 /**
113 * Forces the auth file to be re-read from disk for a given user. Returns `true` if a value was removed.
114 * @param username The username for the auth info to re-read.
115 */
116 static clearCache(username: string): boolean;
117 private static authFilenameFilterRegEx;
118 private static cache;
119 private fields;
120 private usingAccessToken;
121 private logger;
122 private authInfoCrypto;
123 private options;
124 /**
125 * Constructor
126 * **Do not directly construct instances of this class -- use {@link AuthInfo.create} instead.**
127 * @param options The options for the class instance
128 */
129 constructor(options: AuthInfo.Options);
130 /**
131 * Get the username.
132 */
133 getUsername(): Optional<string>;
134 /**
135 * Returns true if `this` is using the JWT flow.
136 */
137 isJwt(): boolean;
138 /**
139 * Returns true if `this` is using an access token flow.
140 */
141 isAccessTokenFlow(): boolean;
142 /**
143 * Returns true if `this` is using the oauth flow.
144 */
145 isOauth(): boolean;
146 /**
147 * Returns true if `this` is using the refresh token flow.
148 */
149 isRefreshTokenFlow(): boolean;
150 /**
151 * Updates the cache and persists the authentication fields (encrypted).
152 * @param authData New data to save.
153 */
154 save(authData?: AuthFields): Promise<AuthInfo>;
155 /**
156 * Update the authorization fields, encrypting sensitive fields, but do not persist.
157 * For convenience `this` object is returned.
158 *
159 * @param authData Authorization fields to update.
160 * @param encrypt Encrypt the fields.
161 */
162 update(authData?: AuthFields, encrypt?: boolean): AuthInfo;
163 /**
164 * Get the auth fields (decrypted) needed to make a connection.
165 */
166 getConnectionOptions(): ConnectionOptions;
167 /**
168 * Get the authorization fields.
169 */
170 getFields(): AuthFields;
171 /**
172 * Returns true if this org is using access token auth.
173 */
174 isUsingAccessToken(): boolean;
175 /**
176 * Get the SFDX Auth URL.
177 *
178 * **See** [SFDX Authorization](https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_force_auth.htm#cli_reference_force_auth)
179 */
180 getSfdxAuthUrl(): string;
181 /**
182 * Initializes an instance of the AuthInfo class.
183 */
184 init(): Promise<void>;
185 /**
186 * Initialize this AuthInfo instance with the specified options. If options are not provided, initialize it from cache
187 * or by reading from the persistence store. For convenience `this` object is returned.
188 * @param options Options to be used for creating an OAuth2 instance.
189 *
190 * **Throws** *{@link SfdxError}{ name: 'NamedOrgNotFound' }* Org information does not exist.
191 * @returns {Promise<AuthInfo>}
192 */
193 private initAuthOptions;
194 private isTokenOptions;
195 private refreshFn;
196 private buildJwtConfig;
197 private buildRefreshTokenConfig;
198 private buildWebAuthConfig;
199 private lookup;
200}
201export declare namespace AuthInfo {
202 /**
203 * Constructor options for AuthInfo.
204 */
205 interface Options {
206 /**
207 * Org signup username.
208 */
209 username?: string;
210 /**
211 * OAuth options.
212 */
213 oauth2Options?: OAuth2Options;
214 /**
215 * Options for the access token auth.
216 */
217 accessTokenOptions?: AccessTokenOptions;
218 }
219}
220
\No newline at end of file