Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to log into staging and get a new token #FF #8

Merged
merged 4 commits into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/package-lock.json
/tmp
node_modules
.env
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"@oclif/config": "^1",
"@oclif/plugin-help": "^2",
"@types/node-fetch": "^2.5.0",
"cli-ux": "^5.3.1",
"dotenv": "^8.0.0",
"node-fetch": "^2.6.0",
"tslib": "^1"
},
Expand Down
28 changes: 28 additions & 0 deletions src/commands/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Command, flags } from "@oclif/command"
import cli from "cli-ux"
import Gravity from "../lib/gravity"

export default class Login extends Command {
static description =
"Log into the Artsy API. This is a prerequisite for many other commands."

static flags = {
help: flags.help({ char: "h" }),
}

async run() {
require("dotenv").config()

const email = await cli.prompt("Email", { type: "normal" })
const password = await cli.prompt("Password", { type: "hide" })

this.log(`Authenticating against stagingapi.artsy.net for ${email}...`)

const result = await new Gravity().getAccessToken({
email,
password,
})

this.log(result.access_token)
}
}
40 changes: 38 additions & 2 deletions src/lib/gravity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,30 @@ class Gravity {
production: "api.artsy.net",
}

async getAccessToken(credentials: Credentials) {
const gravityUrl = this.url("oauth2/access_token")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joeyAghion this seems like a reasonable way to authenticate, but let me know if there is a method that makes more sense.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have to dig... is this where Force posts credentials to obtain an access token today?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is. And I imagine this cli is not tight to Force, as long as it's a valid access token generated by Gravity?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct - any valid id/secret will do.

const body: AccessTokenRequest = {
client_id: process.env.CLIENT_ID!,
client_secret: process.env.CLIENT_SECRET!,
grant_type: "credentials",
...credentials,
}

const response = await fetch(gravityUrl, {
method: "post",
body: JSON.stringify(body),
headers: { "Content-Type": "application/json" },
})

const json = await response.json()

return json as AccessTokenResponse
}

async get(endpoint: string) {
const token: string = process.env.TOKEN! // temp until our auth/token plumbing is hooked up

const gravityUrl: string = this.url(endpoint)
const gravityUrl: string = this.url(`api/v1/${endpoint}`)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to do a little bit of shuffling around of URLs, because the auth endpoint isn't under api/v1....

const headers = { "X-Access-Token": token }
const response = await fetch(gravityUrl, { headers })

Expand All @@ -18,8 +38,24 @@ class Gravity {

url(endpoint: string): string {
const host = Gravity.HOSTS.staging
return `https://${host}/api/v1/${endpoint}`
return `https://${host}/${endpoint}`
}
}

export default Gravity

export interface Credentials {
email: string
password: string
}

interface AccessTokenRequest extends Credentials {
grant_type: string
client_id: string
client_secret: string
}

interface AccessTokenResponse {
access_token: string
expires_in: string
}
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ clean-stack@^2.0.0:
resolved "https://registry.yarnpkg.com/clean-stack/-/clean-stack-2.2.0.tgz#ee8472dbb129e727b31e8a10a427dee9dfe4008b"
integrity sha512-4diC9HaTE+KRAMWhDhrGOECgWZxoevMc5TlkObMqNSsVU62PYzXZ/SMTjzyGAFF1YusgxGcSWTEXBhp0CPwQ1A==

cli-ux@^5.2.1:
cli-ux@^5.2.1, cli-ux@^5.3.1:
version "5.3.1"
resolved "https://registry.yarnpkg.com/cli-ux/-/cli-ux-5.3.1.tgz#3bed8b37c44b03a5d1b9d71d39a69a919a101835"
integrity sha512-l2MXbitx0FjtHKSbHytuxfxWv6MdWBRh23ItRJjU17cjj0dqZxfAL863tzbR1FIs7jccPllPUvn3QWK6BQg3Pg==
Expand Down Expand Up @@ -650,6 +650,11 @@ doctrine@0.7.2:
esutils "^1.1.6"
isarray "0.0.1"

dotenv@^8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.0.0.tgz#ed310c165b4e8a97bb745b0a9d99c31bda566440"
integrity sha512-30xVGqjLjiUOArT4+M5q9sYdvuR4riM6yK9wMcas9Vbp6zZa+ocC9dp6QoftuhTPhFAiLK/0C5Ni2nou/Bk8lg==

"emoji-regex@>=6.0.0 <=6.1.1":
version "6.1.1"
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-6.1.1.tgz#c6cd0ec1b0642e2a3c67a1137efc5e796da4f88e"
Expand Down