Skip to content

Commit

Permalink
Call gravity to get an auth token
Browse files Browse the repository at this point in the history
  • Loading branch information
pepopowitz committed Aug 12, 2019
1 parent 8f46147 commit 03a57e8
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 26 deletions.
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
18 changes: 14 additions & 4 deletions src/commands/login.ts
Original file line number Diff line number Diff line change
@@ -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 =
Expand All @@ -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 ^^^ --------------")
}
}
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")
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 })

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
}
19 changes: 0 additions & 19 deletions test/commands/login.test.ts

This file was deleted.

5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 03a57e8

Please sign in to comment.