1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | Object.defineProperty(exports, "__esModule", { value: true });
|
9 | exports.AuthRemover = void 0;
|
10 | const kit_1 = require("@salesforce/kit");
|
11 | const configAggregator_1 = require("../config/configAggregator");
|
12 | const logger_1 = require("../logger");
|
13 | const messages_1 = require("../messages");
|
14 | const stateAggregator_1 = require("../stateAggregator");
|
15 | const orgConfigProperties_1 = require("./orgConfigProperties");
|
16 | messages_1.Messages.importMessagesDirectory(__dirname);
|
17 | const messages = messages_1.Messages.load('@salesforce/core', 'auth', ['targetOrgNotSet']);
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | class AuthRemover extends kit_1.AsyncOptionalCreatable {
|
42 | |
43 |
|
44 |
|
45 |
|
46 |
|
47 | async removeAuth(usernameOrAlias) {
|
48 | const username = await this.resolveUsername(usernameOrAlias);
|
49 | this.logger.debug(`Removing authorization for user ${username}`);
|
50 | await this.unsetConfigValues(username);
|
51 | await this.unsetAliases(username);
|
52 | await this.unsetTokens(username);
|
53 | await this.stateAggregator.orgs.remove(username);
|
54 | }
|
55 | |
56 |
|
57 |
|
58 | async removeAllAuths() {
|
59 | const auths = this.findAllAuths();
|
60 | const usernames = Object.keys(auths);
|
61 | for (const username of usernames) {
|
62 |
|
63 |
|
64 | await this.removeAuth(username);
|
65 | }
|
66 | }
|
67 | |
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | async findAuth(usernameOrAlias) {
|
76 | const username = await this.resolveUsername(usernameOrAlias ?? this.getTargetOrg());
|
77 | return this.stateAggregator.orgs.get(username, false, true);
|
78 | }
|
79 | |
80 |
|
81 |
|
82 |
|
83 |
|
84 | findAllAuths() {
|
85 | const orgs = this.stateAggregator.orgs.getAll();
|
86 | return orgs.reduce((x, y) =>
|
87 |
|
88 | ({ ...x, [y.username]: y }), {});
|
89 | }
|
90 | async init() {
|
91 | this.logger = await logger_1.Logger.child(this.constructor.name);
|
92 | this.config = await configAggregator_1.ConfigAggregator.create();
|
93 | this.stateAggregator = await stateAggregator_1.StateAggregator.getInstance();
|
94 | await this.stateAggregator.orgs.readAll();
|
95 | }
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | async resolveUsername(usernameOrAlias) {
|
104 | return this.stateAggregator.aliases.resolveUsername(usernameOrAlias);
|
105 | }
|
106 | |
107 |
|
108 |
|
109 | getTargetOrg() {
|
110 | const targetOrg = this.config.getInfo(orgConfigProperties_1.OrgConfigProperties.TARGET_ORG).value;
|
111 | if (!targetOrg) {
|
112 | throw messages.createError('targetOrgNotSet');
|
113 | }
|
114 | return targetOrg;
|
115 | }
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | getAliases(username) {
|
123 | return this.stateAggregator.aliases.getAll(username);
|
124 | }
|
125 | |
126 |
|
127 |
|
128 |
|
129 |
|
130 | async unsetConfigValues(username) {
|
131 | const aliases = this.getAliases(username);
|
132 | this.logger.debug(`Clearing config keys for username ${username} and aliases: ${aliases.join(',')}`);
|
133 | const configs = [this.config.getGlobalConfig(), this.config.getLocalConfig()];
|
134 | for (const config of configs) {
|
135 | if (config) {
|
136 | const keysWithUsername = config.getKeysByValue(username) || [];
|
137 | const keysWithAlias = aliases
|
138 | .map((alias) => config.getKeysByValue(alias))
|
139 | .filter((k) => !!k)
|
140 | .reduce((x, y) => x.concat(y), []);
|
141 | const allKeys = keysWithUsername.concat(keysWithAlias);
|
142 | this.logger.debug(`Found these config keys to remove: ${allKeys.join(',')}`);
|
143 | allKeys.forEach((key) => {
|
144 | try {
|
145 | config.unset(key);
|
146 | }
|
147 | catch {
|
148 | this.logger.debug(`Failed to remove ${key}`);
|
149 | }
|
150 | });
|
151 |
|
152 |
|
153 | await config.write();
|
154 | }
|
155 | }
|
156 | }
|
157 | |
158 |
|
159 |
|
160 |
|
161 |
|
162 | async unsetAliases(username) {
|
163 | this.logger.debug(`Clearing aliases for username: ${username}`);
|
164 | const existingAliases = this.stateAggregator.aliases.getAll(username);
|
165 | if (existingAliases.length === 0)
|
166 | return;
|
167 | this.logger.debug(`Found these aliases to remove: ${existingAliases.join(',')}`);
|
168 | existingAliases.forEach((alias) => this.stateAggregator.aliases.unset(alias));
|
169 | await this.stateAggregator.aliases.write();
|
170 | }
|
171 | async unsetTokens(username) {
|
172 | this.logger.debug(`Clearing tokens for username: ${username}`);
|
173 | const tokens = this.stateAggregator.tokens.getAll();
|
174 | for (const [key, token] of Object.entries(tokens)) {
|
175 | if (token.user === username) {
|
176 | this.stateAggregator.tokens.unset(key);
|
177 | }
|
178 | }
|
179 | await this.stateAggregator.tokens.write();
|
180 | }
|
181 | }
|
182 | exports.AuthRemover = AuthRemover;
|
183 |
|
\ | No newline at end of file |