This repository was archived by the owner on Aug 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Update token lookup #11
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ const openBrowser = require('./utils/open-browser') | |
const findRoot = require('./utils/find-root') | ||
const { track, identify } = require('./utils/telemetry') | ||
const merge = require('lodash.merge') | ||
const { NETLIFY_AUTH_TOKEN } = process.env | ||
|
||
// Netlify CLI client id. Lives in [email protected] | ||
// Todo setup client for multiple environments | ||
|
@@ -22,7 +23,7 @@ class BaseCommand extends Command { | |
async init(err) { | ||
const projectRoot = findRoot(process.cwd()) | ||
// Grab netlify API token | ||
const token = getConfigToken() | ||
const [ token ] = this.getConfigToken() | ||
// Get site config from netlify.toml | ||
const configPath = getConfigPath(projectRoot) | ||
// TODO: https://github.com/request/caseless to handle key casing issues | ||
|
@@ -54,14 +55,6 @@ class BaseCommand extends Command { | |
} | ||
} | ||
|
||
get clientToken() { | ||
return this.netlify.api.accessToken | ||
} | ||
|
||
set clientToken(token) { | ||
this.netlify.api.accessToken = token | ||
} | ||
|
||
async isLoggedIn() { | ||
try { | ||
await this.netlify.api.getCurrentUser() | ||
|
@@ -71,22 +64,38 @@ class BaseCommand extends Command { | |
} | ||
} | ||
|
||
async authenticate(authTokenFromFlag) { | ||
const token = getConfigToken(authTokenFromFlag) | ||
/** | ||
* Get user netlify API token | ||
* @param {string} - [tokenFromFlag] - value passed in by CLI flag | ||
* @return {[string, string]} - tokenValue & location of resolved Netlify API token | ||
*/ | ||
getConfigToken(tokenFromFlag) { | ||
// 1. First honor command flag --auth | ||
if (tokenFromFlag) { | ||
return [ tokenFromFlag, 'flag' ] | ||
} | ||
// 2. then Check ENV var | ||
if (NETLIFY_AUTH_TOKEN && NETLIFY_AUTH_TOKEN !== 'null') { | ||
return [ NETLIFY_AUTH_TOKEN, 'env' ] | ||
} | ||
// 3. If no env var use global user setting | ||
const userId = globalConfig.get('userId') | ||
const tokenFromConfig = globalConfig.get(`users.${userId}.auth.token`) | ||
if (tokenFromConfig) { | ||
return [ tokenFromConfig, 'config' ] | ||
} | ||
return [ null, 'not found'] | ||
} | ||
|
||
async authenticate(tokenFromFlag) { | ||
const [ token ] = this.getConfigToken(tokenFromFlag) | ||
if (!token) { | ||
return this.expensivelyAuthenticate() | ||
} else { | ||
return token | ||
} | ||
} | ||
async expensivelyCheckToken(token) { | ||
// this used to be inside authenticate() but was slowing down everything | ||
// https://github.com/netlify/cli/issues/286 | ||
// we split it out so you have to be mindful of where you incur cost | ||
this.clientToken = token | ||
await this.netlify.api.getCurrentUser() | ||
return token | ||
} | ||
|
||
async expensivelyAuthenticate() { | ||
const webUI = process.env.NETLIFY_WEB_UI || 'https://app.netlify.com' | ||
this.log(`Logging into your Netlify account...`) | ||
|
@@ -98,12 +107,15 @@ class BaseCommand extends Command { | |
|
||
// Open browser for authentication | ||
const authLink = `${webUI}/authorize?response_type=ticket&ticket=${ticket.id}` | ||
|
||
this.log(`Opening ${authLink}`) | ||
await openBrowser(authLink) | ||
|
||
const accessToken = await this.netlify.api.getAccessToken(ticket) | ||
|
||
if (!accessToken) this.error('Could not retrieve access token') | ||
if (!accessToken) { | ||
this.error('Could not retrieve access token') | ||
} | ||
|
||
const user = await this.netlify.api.getCurrentUser() | ||
const userID = user.id | ||
|
@@ -134,6 +146,7 @@ class BaseCommand extends Command { | |
email: email | ||
}) | ||
}) | ||
|
||
// Log success | ||
this.log() | ||
this.log(`${chalk.greenBright('You are now logged into your Netlify account!')}`) | ||
|
@@ -146,23 +159,4 @@ class BaseCommand extends Command { | |
} | ||
} | ||
|
||
/** | ||
* Get user netlify API token | ||
* @param {string} authTokenFromFlag - value passed in by CLI flag | ||
* @return {string} - resolved Netlify API token | ||
*/ | ||
function getConfigToken(authTokenFromFlag) { | ||
// 1. First honor command flag --auth | ||
if (authTokenFromFlag) { | ||
return authTokenFromFlag | ||
} | ||
// 2. then Check ENV var | ||
if (process.env.NETLIFY_AUTH_TOKEN) { | ||
return process.env.NETLIFY_AUTH_TOKEN | ||
} | ||
// 3. If no env var use global user setting | ||
const userId = globalConfig.get('userId') | ||
return globalConfig.get(`users.${userId}.auth.token`) | ||
} | ||
|
||
module.exports = BaseCommand | ||
module.exports = BaseCommand |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this caused netlify/cli#356