Skip to content

Commit a8340b4

Browse files
committed
fix: use settings file for login insted of netrc
to avoid wget warnings
1 parent ee68293 commit a8340b4

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"@oclif/errors": "^1.1.2",
1010
"cli-ux": "^4.4.0",
1111
"debug": "^3.1.0",
12+
"fs-extra": "^6.0.1",
1213
"heroku-client": "^3.0.6",
1314
"http-call": "^5.1.4",
1415
"netrc-parser": "^3.1.5",
@@ -22,6 +23,7 @@
2223
"@oclif/tslint": "^1.1.2",
2324
"@types/ansi-styles": "^3.2.0",
2425
"@types/chai": "^4.1.3",
26+
"@types/fs-extra": "^5.0.2",
2527
"@types/mocha": "^5.2.0",
2628
"@types/nock": "^9.1.3",
2729
"@types/node": "^10.1.2",

src/login.ts

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import color from '@heroku-cli/color'
22
import * as Heroku from '@heroku-cli/schema'
33
import * as Config from '@oclif/config'
44
import ux from 'cli-ux'
5+
import * as fs from 'fs-extra'
56
import HTTP from 'http-call'
67
import Netrc from 'netrc-parser'
78
import opn = require('opn')
89
import {hostname} from 'os'
10+
import * as path from 'path'
911

1012
import {APIClient, HerokuAPIError} from './api_client'
1113
import {vars} from './vars'
@@ -23,19 +25,21 @@ export namespace Login {
2325
interface NetrcEntry {
2426
login: string
2527
password: string
26-
method?: 'interactive' | 'sso' | 'browser'
27-
org?: string
28-
refresh?: string
2928
}
3029

3130
const headers = (token: string) => ({headers: {accept: 'application/vnd.heroku+json; version=3', authorization: `Bearer ${token}`}})
3231

3332
export class Login {
3433
loginHost = process.env.HEROKU_LOGIN_HOST || 'https://cli-login.heroku.com'
34+
settings!: {
35+
method?: string
36+
org?: string
37+
}
3538

3639
constructor(private readonly config: Config.IConfig, private readonly heroku: APIClient) {}
3740

3841
async login(opts: Login.Options = {}): Promise<void> {
42+
await this.loadSettings()
3943
let loggedIn = false
4044
try {
4145
// timeout after 10 minutes
@@ -47,7 +51,7 @@ export class Login {
4751
await Netrc.load()
4852
const previousEntry = Netrc.machines['api.heroku.com']
4953
let input: string | undefined = opts.method
50-
const defaultMethod = (previousEntry && previousEntry.method) || 'interactive'
54+
const defaultMethod = this.settings.method || 'interactive'
5155
if (!input) {
5256
if (opts.expiresIn) {
5357
// can't use browser with --expires-in
@@ -64,6 +68,7 @@ export class Login {
6468
ux.warn(err)
6569
}
6670
let auth
71+
delete this.settings.method
6772
switch (input) {
6873
case 'b':
6974
case 'browser':
@@ -75,12 +80,13 @@ export class Login {
7580
break
7681
case 's':
7782
case 'sso':
78-
auth = await this.sso(previousEntry && previousEntry.org)
83+
auth = await this.sso()
7984
break
8085
default:
8186
return this.login(opts)
8287
}
8388
await this.saveToken(auth)
89+
await this.saveSettings()
8490
} catch (err) {
8591
throw new HerokuAPIError(err)
8692
} finally {
@@ -158,7 +164,6 @@ export class Login {
158164
return {
159165
login: account.email!,
160166
password: auth.access_token,
161-
method: 'browser',
162167
}
163168
}
164169

@@ -176,7 +181,6 @@ export class Login {
176181
auth = await this.createOAuthToken(login!, password, {expiresIn, secondFactor})
177182
}
178183
this.heroku.auth = auth.password
179-
auth.method = 'interactive'
180184
return auth
181185
}
182186

@@ -212,9 +216,6 @@ export class Login {
212216
Netrc.machines[host].login = entry.login
213217
Netrc.machines[host].password = entry.password
214218
})
215-
Netrc.machines[vars.apiHost].refresh = entry.refresh
216-
Netrc.machines[vars.apiHost].method = entry.method
217-
Netrc.machines[vars.apiHost].org = entry.org
218219
if (Netrc.machines._tokens) {
219220
(Netrc.machines._tokens as any).forEach((token: any) => {
220221
if (hosts.includes(token.host)) {
@@ -243,10 +244,10 @@ export class Login {
243244
return this.config.channel !== 'stable'
244245
}
245246

246-
private async sso(org?: string): Promise<NetrcEntry> {
247+
private async sso(): Promise<NetrcEntry> {
247248
let url = process.env.SSO_URL
249+
let org = process.env.HEROKU_ORGANIZATION || this.settings.org
248250
if (!url) {
249-
org = process.env.HEROKU_ORGANIZATION || org
250251
if (org) {
251252
org = await ux.prompt('Organization name', {default: org})
252253
} else {
@@ -265,6 +266,32 @@ export class Login {
265266
this.heroku.auth = password
266267
const {body: account} = await HTTP.get<Heroku.Account>(`${vars.apiUrl}/account`, headers(password))
267268

268-
return {password, login: account.email!, method: 'sso', org}
269+
this.settings.method = 'sso'
270+
this.settings.org = org
271+
return {password, login: account.email!}
272+
}
273+
274+
private async loadSettings() {
275+
try {
276+
this.settings = await fs.readJSON(this.settingsPath)
277+
} catch (err) {
278+
if (err.code !== 'ENOENT') ux.warn(err)
279+
else debug(err)
280+
this.settings = {}
281+
}
282+
}
283+
284+
private async saveSettings() {
285+
try {
286+
if (Object.keys(this.settings).length === 0) {
287+
await fs.remove(this.settingsPath)
288+
} else {
289+
await fs.outputJSON(this.settingsPath, this.settings)
290+
}
291+
} catch (err) {
292+
ux.warn(err)
293+
}
269294
}
295+
296+
private get settingsPath() { return path.join(this.config.dataDir, 'login.json') }
270297
}

yarn.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@
114114
version "1.1.0"
115115
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.0.tgz#926f76f7e66f49cc59ad880bb15b030abbf0b66d"
116116

117+
"@types/fs-extra@^5.0.2":
118+
version "5.0.2"
119+
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-5.0.2.tgz#235a7e2b56452cc0a6a4809b53e1d1eaffff9c96"
120+
dependencies:
121+
"@types/node" "*"
122+
117123
"@types/mocha@^5.2.0":
118124
version "5.2.0"
119125
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-5.2.0.tgz#b3c8e69f038835db1a7fdc0b3d879fc50506e29e"

0 commit comments

Comments
 (0)