From 03a57e8e20722cb52077ecceeb0ef36589f3ff9a Mon Sep 17 00:00:00 2001 From: Steven Hicks Date: Fri, 9 Aug 2019 12:59:13 -0400 Subject: [PATCH] Call gravity to get an auth token --- .gitignore | 1 + package.json | 3 ++- src/commands/login.ts | 18 +++++++++++++---- src/lib/gravity/index.ts | 40 +++++++++++++++++++++++++++++++++++-- test/commands/login.test.ts | 19 ------------------ yarn.lock | 5 +++++ 6 files changed, 60 insertions(+), 26 deletions(-) delete mode 100644 test/commands/login.test.ts diff --git a/.gitignore b/.gitignore index 9d6ea2ca..ce0ead42 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ /package-lock.json /tmp node_modules +.env \ No newline at end of file diff --git a/package.json b/package.json index d3cab6c4..41bb6abd 100644 --- a/package.json +++ b/package.json @@ -12,8 +12,9 @@ "@oclif/config": "^1", "@oclif/plugin-help": "^2", "@types/node-fetch": "^2.5.0", - "node-fetch": "^2.6.0", "cli-ux": "^5.3.1", + "dotenv": "^8.0.0", + "node-fetch": "^2.6.0", "tslib": "^1" }, "devDependencies": { diff --git a/src/commands/login.ts b/src/commands/login.ts index c36b62d3..dd4d210b 100644 --- a/src/commands/login.ts +++ b/src/commands/login.ts @@ -1,5 +1,6 @@ import { Command, flags } from "@oclif/command" import cli from "cli-ux" +import Gravity from "../lib/gravity" export default class Login extends Command { static description = @@ -12,12 +13,21 @@ export default class Login extends Command { // static args = [{ name: 'file' }]; async run() { + require("dotenv").config() + // const {args, flags} = this.parse(Auth) - const username = await cli.prompt("Username", { type: "normal" }) + const email = await cli.prompt("Email", { type: "normal" }) const password = await cli.prompt("Password", { type: "hide" }) - this.log( - `Authenticating against stagingapi.artsy.net with ${username}|${password}` - ) + this.log(`Authenticating against stagingapi.artsy.net for ${email}...`) + + const result = await new Gravity().getAccessToken({ + email, + password, + }) + + this.log("-------------- vvv Your access token vvv --------------") + this.log(result.access_token) + this.log("-------------- ^^^ Your access token ^^^ --------------") } } diff --git a/src/lib/gravity/index.ts b/src/lib/gravity/index.ts index b9737bc0..ed3a5acc 100644 --- a/src/lib/gravity/index.ts +++ b/src/lib/gravity/index.ts @@ -6,10 +6,30 @@ class Gravity { production: "api.artsy.net", } + async getAccessToken(credentials: Credentials) { + const gravityUrl = this.url("oauth2/access_token") + 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}`) const headers = { "X-Access-Token": token } const response = await fetch(gravityUrl, { headers }) @@ -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 +} diff --git a/test/commands/login.test.ts b/test/commands/login.test.ts deleted file mode 100644 index 633e57b1..00000000 --- a/test/commands/login.test.ts +++ /dev/null @@ -1,19 +0,0 @@ -// import { expect, test } from "@oclif/test" - -// describe("login", () => { -// test -// .stdout() -// .command(["login"]) -// .it("prompts for user and password", ctx => { -// expect(ctx.stdout).to.contain("Username:") -// }) - -// test -// .stdout() -// .command(["login"]) -// .it("lets the user know where it is authenticating", ctx => { -// expect(ctx.stdout).to.contain( -// "Authenticating against stagingapi.artsy.net" -// ) -// }) -// }) diff --git a/yarn.lock b/yarn.lock index 0a433abb..579ffc40 100644 --- a/yarn.lock +++ b/yarn.lock @@ -645,6 +645,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"