-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathauth.ts
More file actions
125 lines (106 loc) · 4.3 KB
/
auth.ts
File metadata and controls
125 lines (106 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { AuthInfo, OrgAuthorization, Connection, StateAggregator } from "@salesforce/core";
import { SanitizedOrgAuthorization } from "./types.js";
/**
* Sanitizes org authorization data by filtering out sensitive fields
*
* @param orgs - Array of OrgAuthorization objects
* @returns Array of sanitized org authorization objects with only allowed fields
*/
export function sanitizeOrgs(orgs: OrgAuthorization[]): SanitizedOrgAuthorization[] {
return orgs.map((org) => ({
aliases: org.aliases,
configs: org.configs,
username: org.username,
instanceUrl: org.instanceUrl,
isScratchOrg: org.isScratchOrg,
isDevHub: org.isDevHub,
isSandbox: org.isSandbox,
orgId: org.orgId,
oauthMethod: org.oauthMethod,
isExpired: org.isExpired,
}));
}
export async function getAllAllowedOrgs(): Promise<(SanitizedOrgAuthorization & { orgType?: string })[]> {
const allOrgs = await AuthInfo.listAllAuthorizations();
const sanitizedOrgs = sanitizeOrgs(allOrgs);
return sanitizedOrgs;
}
export async function getConnection(usernameOrAlias: string): Promise<Connection> {
const allOrgs = await getAllAllowedOrgs();
let foundOrg = findOrgByUsernameOrAlias(allOrgs, usernameOrAlias);
// If not found, try resolving alias via StateAggregator (CLI alias store).
// listAllAuthorizations() may not include aliases in org.aliases in all environments.
if (!foundOrg) {
try {
await StateAggregator.clearInstanceAsync();
const resolvedUsername = (await StateAggregator.getInstance()).aliases.resolveUsername(
usernameOrAlias
);
if (resolvedUsername && resolvedUsername.includes("@")) {
foundOrg = findOrgByUsernameOrAlias(allOrgs, resolvedUsername);
}
} catch {
// StateAggregator or resolveUsername failed; continue to reject with same error below
}
}
if (!foundOrg)
return Promise.reject(
new Error(
'No org found with the provided username/alias. Ask the user to specify valid username or alias or login with correct org first. use sf cli login command.'
)
);
const authInfo = await AuthInfo.create({ username: foundOrg.username });
const connection = await Connection.create({ authInfo });
return connection;
}
export function findOrgByUsernameOrAlias(
allOrgs: SanitizedOrgAuthorization[],
usernameOrAlias: string
): SanitizedOrgAuthorization | undefined {
return allOrgs.find((org) => {
const isMatchingUsername = org.username === usernameOrAlias;
const isMatchingAlias = org.aliases && Array.isArray(org.aliases) && org.aliases.includes(usernameOrAlias);
return isMatchingUsername || isMatchingAlias;
});
}
/**
* Get only the DevOps Center org for commit operations
*/
export async function getDoceHubOrg(): Promise<{
doceHub: (SanitizedOrgAuthorization & { orgType?: string }) | null;
error?: string;
}> {
const allOrgs = await getAllAllowedOrgs();
const doceHub = allOrgs.find(org => org.orgType === 'DevOps Center') || null;
let error = '';
if (!doceHub) {
error += 'DevOps Center org not found. ';
}
return {
doceHub,
error: error || undefined
};
}
/**
* Get both DevOps Center and Sandbox orgs with validation
*/
export async function getRequiredOrgs(devopsUsername: string, sandboxUsername: string ): Promise<{
doceHub: (SanitizedOrgAuthorization & { orgType?: string }) | null;
sandbox: (SanitizedOrgAuthorization & { orgType?: string }) | null;
error?: string;
}> {
const allOrgs = await getAllAllowedOrgs();
const doceHub = allOrgs.find(org => org.username === devopsUsername) || null;
const sandbox = allOrgs.find(org => org.username === sandboxUsername) || null;
let error = '';
if (!doceHub) {
error += `DevOps Center org '${devopsUsername}' not found. Login with sf auth:web:login or set Devops_org_username. `;
}
if (!sandbox) {
error += `Sandbox org '${sandboxUsername}' not found. Login with sf auth:web:login or set Sandbox_org_username. `;
}
if (doceHub && sandbox && doceHub.username === sandbox.username) {
error += 'DevOps Center and Sandbox cannot be the same org. ';
}
return { doceHub, sandbox, error: error || undefined };
}