-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi-token.ts
More file actions
67 lines (57 loc) · 1.78 KB
/
api-token.ts
File metadata and controls
67 lines (57 loc) · 1.78 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
/**
* API Token authentication for Jira Cloud.
*
* Prompts the user for instance URL, email, and API token,
* then validates by calling GET /rest/api/3/myself with Basic auth.
*/
import axios from "axios";
import { handleJiraError } from "../errors.js";
import type { JiraApiTokenCredentials } from "../types.js";
import { normalizeUrl, prompt } from "./prompt.js";
/**
* Interactively prompt the user for Jira Cloud API token credentials
* and validate them by calling the /rest/api/3/myself endpoint.
*/
export async function promptForApiToken(): Promise<JiraApiTokenCredentials> {
process.stderr.write("\n Jira Cloud — API Token Authentication\n\n");
const rawUrl = await prompt(
" Jira instance URL (e.g. https://myteam.atlassian.net): "
);
if (!rawUrl) {
throw new Error("Instance URL is required.");
}
const baseUrl = normalizeUrl(rawUrl);
const email = await prompt(" Email: ");
if (!email) {
throw new Error("Email is required.");
}
const apiToken = await prompt(" API token: ", true);
if (!apiToken) {
throw new Error("API token is required.");
}
process.stderr.write(" Validating credentials...\n");
const encoded = Buffer.from(`${email}:${apiToken}`).toString("base64");
try {
const response = await axios.get(`${baseUrl}/rest/api/3/myself`, {
headers: {
Authorization: `Basic ${encoded}`,
Accept: "application/json",
},
timeout: 15_000,
});
const displayName =
(response.data as Record<string, unknown>)?.displayName ?? email;
process.stderr.write(` Authenticated as: ${displayName}\n\n`);
} catch (error) {
if (axios.isAxiosError(error)) {
handleJiraError(error);
}
throw error;
}
return {
method: "api-token",
email,
apiToken,
baseUrl,
};
}