UNPKG

4.51 kBTypeScriptView Raw
1/// <reference types="node" />
2/// <reference types="node" />
3/// <reference types="node" />
4import * as childProcess from 'child_process';
5import * as nodeFs from 'fs';
6import { Nullable } from '@salesforce/ts-types';
7export type FsIfc = Pick<typeof nodeFs, 'statSync'>;
8/**
9 * Basic keychain interface.
10 */
11export interface PasswordStore {
12 /**
13 * Gets a password
14 *
15 * @param opts cli level password options.
16 * @param fn function callback for password.
17 * @param retryCount number of reties to get the password.
18 */
19 getPassword(opts: ProgramOpts, fn: (error: Nullable<Error>, password?: string) => void, retryCount?: number): Promise<void>;
20 /**
21 * Sets a password.
22 *
23 * @param opts cli level password options.
24 * @param fn function callback for password.
25 */
26 setPassword(opts: ProgramOpts, fn: (error: Nullable<Error>, contents?: SecretContents) => void): Promise<void>;
27}
28/**
29 * @private
30 */
31export declare class KeychainAccess implements PasswordStore {
32 private osImpl;
33 private fsIfc;
34 /**
35 * Abstract prototype for general cross platform keychain interaction.
36 *
37 * @param osImpl The platform impl for (linux, darwin, windows).
38 * @param fsIfc The file system interface.
39 */
40 constructor(osImpl: OsImpl, fsIfc: FsIfc);
41 /**
42 * Validates the os level program is executable.
43 */
44 validateProgram(): Promise<void>;
45 /**
46 * Returns a password using the native program for credential management.
47 *
48 * @param opts Options for the credential lookup.
49 * @param fn Callback function (err, password).
50 * @param retryCount Used internally to track the number of retries for getting a password out of the keychain.
51 */
52 getPassword(opts: ProgramOpts, fn: (error: Nullable<Error>, password?: string) => void, retryCount?: number): Promise<void>;
53 /**
54 * Sets a password using the native program for credential management.
55 *
56 * @param opts Options for the credential lookup.
57 * @param fn Callback function (err, ConfigContents).
58 */
59 setPassword(opts: ProgramOpts, fn: (error: Nullable<Error>, contents?: SecretContents) => void): Promise<void>;
60}
61interface ProgramOpts {
62 account: string;
63 service: string;
64 password?: string;
65}
66interface OsImpl {
67 getProgram(): string;
68 getProgramOptions(opts: ProgramOpts): string[];
69 getCommandFunc(opts: ProgramOpts, fn: (program: string, opts: string[]) => childProcess.ChildProcess): childProcess.ChildProcess;
70 onGetCommandClose(code: number, stdout: string, stderr: string, opts: ProgramOpts, fn: (err: Nullable<Error>, result?: string) => void): Promise<void>;
71 setProgramOptions(opts: ProgramOpts): string[];
72 setCommandFunc(opts: ProgramOpts, fn: (program: string, opts: string[]) => childProcess.ChildProcess): childProcess.ChildProcess;
73 onSetCommandClose(code: number, stdout: string, stderr: string, opts: ProgramOpts, fn: (err: Nullable<Error>) => void): Promise<void>;
74}
75declare enum SecretField {
76 SERVICE = "service",
77 ACCOUNT = "account",
78 KEY = "key"
79}
80type SecretContents = {
81 [SecretField.ACCOUNT]: string;
82 [SecretField.KEY]?: string;
83 [SecretField.SERVICE]: string;
84};
85/**
86 * @@ignore
87 */
88export declare class GenericKeychainAccess implements PasswordStore {
89 getPassword(opts: ProgramOpts, fn: (error: Nullable<Error>, password?: string) => void): Promise<void>;
90 setPassword(opts: ProgramOpts, fn: (error: Nullable<Error>, contents?: SecretContents) => void): Promise<void>;
91 protected isValidFileAccess(cb: (error: Nullable<NodeJS.ErrnoException>) => Promise<void>): Promise<void>;
92}
93/**
94 * @ignore
95 */
96export declare class GenericUnixKeychainAccess extends GenericKeychainAccess {
97 protected isValidFileAccess(cb: (error: Nullable<Error>) => Promise<void>): Promise<void>;
98}
99/**
100 * @ignore
101 */
102export declare class GenericWindowsKeychainAccess extends GenericKeychainAccess {
103 protected isValidFileAccess(cb: (error: Nullable<Error>) => Promise<void>): Promise<void>;
104}
105/**
106 * @ignore
107 */
108export declare const keyChainImpl: {
109 generic_unix: GenericUnixKeychainAccess;
110 generic_windows: GenericWindowsKeychainAccess;
111 darwin: KeychainAccess;
112 linux: KeychainAccess;
113 validateProgram: (programPath: string, fsIfc: FsIfc, isExeIfc: (mode: number, gid: number, uid: number) => boolean) => Promise<void>;
114};
115export type KeyChain = GenericUnixKeychainAccess | GenericWindowsKeychainAccess | KeychainAccess;
116export {};