-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathlogin.js
141 lines (119 loc) · 3.66 KB
/
login.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* @flow */
import type {Reporter} from '../../reporters/index.js';
import type Config from '../../config.js';
import {MessageError} from '../../errors.js';
async function getCredentials(
config: Config,
reporter: Reporter,
): Promise<?{
username: string,
email: string,
}> {
let {username, email} = config.registries.yarn.config;
if (username) {
reporter.info(`${reporter.lang('npmUsername')}: ${username}`);
} else {
username = await reporter.question(reporter.lang('npmUsername'));
if (!username) {
return null;
}
}
if (email) {
reporter.info(`${reporter.lang('npmEmail')}: ${email}`);
} else {
email = await reporter.question(reporter.lang('npmEmail'));
if (!email) {
return null;
}
}
await config.registries.yarn.saveHomeConfig({username, email});
return {username, email};
}
export function getOneTimePassword(reporter: Reporter): Promise<string> {
return reporter.question(reporter.lang('npmOneTimePassword'));
}
export async function getToken(
config: Config,
reporter: Reporter,
name: string = '',
flags: Object = {},
registry: string = '',
): Promise<() => Promise<void>> {
const auth = registry ? config.registries.npm.getAuthByRegistry(registry) : config.registries.npm.getAuth(name);
if (config.otp) {
config.registries.npm.setOtp(config.otp);
}
if (auth) {
config.registries.npm.setToken(auth);
return function revoke(): Promise<void> {
reporter.info(reporter.lang('notRevokingConfigToken'));
return Promise.resolve();
};
}
const env = process.env.YARN_AUTH_TOKEN || process.env.NPM_AUTH_TOKEN;
if (env) {
config.registries.npm.setToken(`Bearer ${env}`);
return function revoke(): Promise<void> {
reporter.info(reporter.lang('notRevokingEnvToken'));
return Promise.resolve();
};
}
// make sure we're not running in non-interactive mode before asking for login
if (flags.nonInteractive || config.nonInteractive) {
throw new MessageError(reporter.lang('nonInteractiveNoToken'));
}
//
const creds = await getCredentials(config, reporter);
if (!creds) {
reporter.warn(reporter.lang('loginAsPublic'));
return function revoke(): Promise<void> {
reporter.info(reporter.lang('noTokenToRevoke'));
return Promise.resolve();
};
}
const {username, email} = creds;
const password = await reporter.question(reporter.lang('npmPassword'), {
password: true,
required: true,
});
//
const userobj = {
_id: `org.couchdb.user:${username}`,
name: username,
password,
email,
type: 'user',
roles: [],
date: new Date().toISOString(),
};
//
const res = await config.registries.npm.request(`-/user/org.couchdb.user:${encodeURIComponent(username)}`, {
method: 'PUT',
registry,
body: userobj,
auth: {username, password, email},
});
if (res && res.ok) {
reporter.success(reporter.lang('loggedIn'));
const token = res.token;
config.registries.npm.setToken(`Bearer ${token}`);
return async function revoke(): Promise<void> {
reporter.success(reporter.lang('revokedToken'));
await config.registries.npm.request(`-/user/token/${token}`, {
method: 'DELETE',
registry,
});
};
} else {
throw new MessageError(reporter.lang('incorrectCredentials'));
}
}
export function hasWrapper(commander: Object, args: Array<string>): boolean {
return true;
}
export function setFlags(commander: Object) {
commander.description('Stores registry username and email.');
}
export async function run(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
await getCredentials(config, reporter);
}