Skip to content

Commit

Permalink
refactor: move authorizations, clients, and sessions commands from oa…
Browse files Browse the repository at this point in the history
…uth to cli (heroku#2419)

* test: add test to oauth authorizations

* refactor: add destroy alias to authorizations:revoke in oauth

* refactor: move authorizations commands, helper, and tests to cli

* refactor: move clients commands, helper, and tests to cli

* refactor: organize and alphabetize smoke tests

* test: add smoke test for heroku clients command

* refactor: move sessions commands and tests to cli

* test: add smoke test for heroku sessions command

* refactor: remove oauth-v5 from cli and update topics

* refactor: remove oauth package

* fix: change alias names for authorizations:revoke

* tests: fix smoke tests and unskip test that might be fixed
  • Loading branch information
k80bowman authored Aug 2, 2023
1 parent f6cbd5e commit 73abf47
Show file tree
Hide file tree
Showing 49 changed files with 287 additions and 837 deletions.
11 changes: 9 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"@heroku-cli/plugin-certs-v5": "^8.1.7",
"@heroku-cli/plugin-ci-v5": "^8.1.8",
"@heroku-cli/plugin-container-registry-v5": "^8.1.7",
"@heroku-cli/plugin-oauth-v5": "^8.1.9",
"@heroku-cli/plugin-orgs-v5": "^8.1.4",
"@heroku-cli/plugin-pg-v5": "^8.1.7",
"@heroku-cli/plugin-ps": "^8.1.7",
Expand Down Expand Up @@ -140,7 +139,6 @@
"@heroku-cli/plugin-certs-v5",
"@heroku-cli/plugin-ci-v5",
"@heroku-cli/plugin-container-registry-v5",
"@heroku-cli/plugin-oauth-v5",
"@heroku-cli/plugin-orgs-v5",
"@heroku-cli/plugin-pg-v5",
"@heroku-cli/plugin-ps-exec",
Expand Down Expand Up @@ -171,6 +169,9 @@
"apps": {
"description": "manage apps on Heroku"
},
"authorizations": {
"description": "OAuth authorizations"
},
"buildpacks": {
"description": "scripts used to compile apps"
},
Expand All @@ -180,6 +181,9 @@
"ci": {
"description": "test runner for Heroku Pipelines"
},
"clients": {
"description": "OAuth clients on the platform"
},
"commands": {
"hidden": true
},
Expand Down Expand Up @@ -229,6 +233,9 @@
"run": {
"description": "run a one-off process inside a Heroku dyno"
},
"sessions": {
"description": "OAuth sessions"
},
"stack": {
"description": "list available stacks",
"hidden": true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Command, flags} from '@heroku-cli/command'
import {ScopeCompletion} from '@heroku-cli/command/lib/completions'
import * as Heroku from '@heroku-cli/schema'
import {CliUx} from '@oclif/core'
import {ux} from '@oclif/core'

import {display} from '../../lib/authorizations'
import {display} from '../../lib/authorizations/authorizations'

export default class AuthorizationsCreate extends Command {
static description = 'create a new OAuth authorization'
Expand All @@ -23,7 +23,7 @@ export default class AuthorizationsCreate extends Command {
async run() {
const {flags} = await this.parse(AuthorizationsCreate)

CliUx.ux.action.start('Creating OAuth Authorization')
ux.action.start('Creating OAuth Authorization')

const {body: auth} = await this.heroku.post<Heroku.OAuthAuthorization>('/oauth/authorizations', {
body: {
Expand All @@ -33,12 +33,12 @@ export default class AuthorizationsCreate extends Command {
},
})

CliUx.ux.action.stop()
ux.action.stop()

if (flags.short) {
CliUx.ux.log(auth.access_token && auth.access_token.token)
ux.log(auth.access_token && auth.access_token.token)
} else if (flags.json) {
CliUx.ux.styledJSON(auth)
ux.styledJSON(auth)
} else {
display(auth)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import color from '@heroku-cli/color'
import {Command, flags} from '@heroku-cli/command'
import * as Heroku from '@heroku-cli/schema'
import {CliUx} from '@oclif/core'
const sortBy = require('lodash.sortby')
import {ux} from '@oclif/core'
const {sortBy} = require('lodash')

export default class AuthorizationsIndex extends Command {
static description = 'list OAuth authorizations'
Expand All @@ -23,12 +23,12 @@ export default class AuthorizationsIndex extends Command {
authorizations = sortBy(authorizations, 'description')

if (flags.json) {
CliUx.ux.styledJSON(authorizations)
ux.styledJSON(authorizations)
} else if (authorizations.length === 0) {
CliUx.ux.log('No OAuth authorizations.')
ux.log('No OAuth authorizations.')
} else {
const printLine: typeof this.log = (...args) => this.log(...args)
CliUx.ux.table(authorizations, {
ux.table(authorizations, {
description: {get: (v: any) => color.green(v.description)},
id: {},
scope: {get: (v: any) => v.scope.join(',')},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Command, flags} from '@heroku-cli/command'
import * as Heroku from '@heroku-cli/schema'
import {CliUx} from '@oclif/core'
import {Args, ux} from '@oclif/core'

import {display} from '../../lib/authorizations'
import {display} from '../../lib/authorizations/authorizations'

export default class AuthorizationsInfo extends Command {
static description = 'show an existing OAuth authorization'
Expand All @@ -11,7 +11,9 @@ export default class AuthorizationsInfo extends Command {
json: flags.boolean({char: 'j', description: 'output in json format'}),
}

static args = [{name: 'id', required: true}]
static args = {
id: Args.string({required: true}),
}

async run() {
const {args, flags} = await this.parse(AuthorizationsInfo)
Expand All @@ -21,7 +23,7 @@ export default class AuthorizationsInfo extends Command {
)

if (flags.json) {
CliUx.ux.styledJSON(authentication)
ux.styledJSON(authentication)
} else {
display(authentication)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import color from '@heroku-cli/color'
import {Command} from '@heroku-cli/command'
import * as Heroku from '@heroku-cli/schema'
import {CliUx} from '@oclif/core'
import {Args, ux} from '@oclif/core'

export default class AuthorizationsRevoke extends Command {
static description = 'revoke OAuth authorization'

static aliases = ['authorizations:revoke', 'authorizations:destroy']

static examples = [
'$ heroku authorizations:revoke 105a7bfa-34c3-476e-873a-b1ac3fdc12fb',
]

static args = [{name: 'id', required: true}]
static args = {
id: Args.string({required: true}),
}

async run() {
const {args} = await this.parse(AuthorizationsRevoke)

CliUx.ux.action.start('Revoking OAuth Authorization')
ux.action.start('Revoking OAuth Authorization')

const {body: auth} = await this.heroku.delete<Heroku.OAuthAuthorization>(
`/oauth/authorizations/${encodeURIComponent(args.id)}`,
)

CliUx.ux.action.stop(`done, revoked authorization from ${color.cyan(auth.description)}`)
ux.action.stop(`done, revoked authorization from ${color.cyan(auth.description)}`)
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import {Command} from '@heroku-cli/command'
import * as Heroku from '@heroku-cli/schema'
import {CliUx} from '@oclif/core'
import {Args, ux} from '@oclif/core'

import {display} from '../../lib/authorizations'
import {display} from '../../lib/authorizations/authorizations'

export default class AuthorizationsRotate extends Command {
static description = 'updates an OAuth authorization token'

static args = [{name: 'id', required: true}]
static args = {
id: Args.string({required: true}),
}

async run() {
const {args} = await this.parse(AuthorizationsRotate)

CliUx.ux.action.start('Rotating OAuth Authorization')
ux.action.start('Rotating OAuth Authorization')

const {body: authorization} = await this.heroku.post<Heroku.OAuthAuthorization>(
`/oauth/authorizations/${encodeURIComponent(args.id)}/actions/regenerate-tokens`,
)

CliUx.ux.action.stop()
ux.action.stop()

display(authorization)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Command, flags} from '@heroku-cli/command'
import * as Heroku from '@heroku-cli/schema'
import {CliUx} from '@oclif/core'
import {Args, ux} from '@oclif/core'

import {display} from '../../lib/authorizations'
import {display} from '../../lib/authorizations/authorizations'

export default class AuthorizationsUpdate extends Command {
static description = 'updates an OAuth authorization'
Expand All @@ -13,12 +13,14 @@ export default class AuthorizationsUpdate extends Command {
'client-secret': flags.string({description: 'secret of OAuth client to set', dependsOn: ['client-id']}),
}

static args = [{name: 'id', required: true}]
static args = {
id: Args.string({required: true}),
}

async run() {
const {args, flags} = await this.parse(AuthorizationsUpdate)

CliUx.ux.action.start('Updating OAuth Authorization')
ux.action.start('Updating OAuth Authorization')

let client
if (flags['client-id']) {
Expand All @@ -38,7 +40,7 @@ export default class AuthorizationsUpdate extends Command {
},
)

CliUx.ux.action.stop()
ux.action.stop()

display(authentication)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Command, flags} from '@heroku-cli/command'
import * as Heroku from '@heroku-cli/schema'
import {CliUx} from '@oclif/core'
import {Args, ux} from '@oclif/core'

import {validateURL} from '../../lib/clients'
import {validateURL} from '../../lib/clients/clients'

export default class ClientsCreate extends Command {
static description = 'create a new OAuth client'
Expand All @@ -16,30 +16,30 @@ export default class ClientsCreate extends Command {
shell: flags.boolean({char: 's', description: 'output in shell format'}),
}

static args = [
{name: 'name', required: true},
{name: 'redirect_uri', required: true},
]
static args = {
name: Args.string({required: true}),
redirect_uri: Args.string({required: true}),
}

async run() {
const {args, flags} = await this.parse(ClientsCreate)

const {redirect_uri, name} = args
validateURL(redirect_uri)

CliUx.ux.action.start(`Creating ${name}`)
ux.action.start(`Creating ${name}`)

const {body: client} = await this.heroku.post<Heroku.OAuthClient>('/oauth/clients', {
body: {name, redirect_uri},
})

CliUx.ux.action.stop()
ux.action.stop()

if (flags.json) {
CliUx.ux.styledJSON(client)
ux.styledJSON(client)
} else {
CliUx.ux.log(`HEROKU_OAUTH_ID=${client.id}`)
CliUx.ux.log(`HEROKU_OAUTH_SECRET=${client.secret}`)
ux.log(`HEROKU_OAUTH_ID=${client.id}`)
ux.log(`HEROKU_OAUTH_SECRET=${client.secret}`)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import color from '@heroku-cli/color'
import {Command} from '@heroku-cli/command'
import * as Heroku from '@heroku-cli/schema'
import {CliUx} from '@oclif/core'
import {Args, ux} from '@oclif/core'

export default class ClientsDestroy extends Command {
static description = 'delete client by ID'

static args = [{name: 'id', required: true}]
static args = {
id: Args.string({required: true}),
}

async run() {
const {args} = await this.parse(ClientsDestroy)

CliUx.ux.action.start(`Destroying ${color.cyan(args.id)}`)
ux.action.start(`Destroying ${color.cyan(args.id)}`)

await this.heroku.delete<Heroku.OAuthClient>(
`/oauth/clients/${args.id}`,
)

CliUx.ux.action.stop()
ux.action.stop()
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import color from '@heroku-cli/color'
import {Command, flags} from '@heroku-cli/command'
import * as Heroku from '@heroku-cli/schema'
import {CliUx} from '@oclif/core'
const sortBy = require('lodash.sortby')
import {ux} from '@oclif/core'
const {sortBy} = require('lodash')

export default class ClientsIndex extends Command {
static description = 'list your OAuth clients'
Expand All @@ -19,12 +19,12 @@ export default class ClientsIndex extends Command {
clients = sortBy(clients, 'name')

if (flags.json) {
CliUx.ux.styledJSON(clients)
ux.styledJSON(clients)
} else if (clients.length === 0) {
CliUx.ux.log('No OAuth clients.')
ux.log('No OAuth clients.')
} else {
const printLine: typeof this.log = (...args) => this.log(...args)
CliUx.ux.table(clients, {
ux.table(clients, {
name: {get: (w: any) => color.green(w.name)},
id: {},
redirect_uri: {},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Command, flags} from '@heroku-cli/command'
import * as Heroku from '@heroku-cli/schema'
import {CliUx} from '@oclif/core'
import {Args, ux} from '@oclif/core'

export default class ClientsInfo extends Command {
static description = 'show details of an oauth client'
Expand All @@ -14,21 +14,23 @@ export default class ClientsInfo extends Command {
shell: flags.boolean({char: 's', description: 'output in shell format'}),
}

static args = [{name: 'id', required: true}]
static args = {
id: Args.string({required: true}),
}

async run() {
const {args, flags} = await this.parse(ClientsInfo)

const {body: client} = await this.heroku.get<Heroku.OAuthClient>(`/oauth/clients/${args.id}`)

if (flags.json) {
CliUx.ux.styledJSON(client)
ux.styledJSON(client)
} else if (flags.shell) {
CliUx.ux.log(`HEROKU_OAUTH_ID=${client.id}`)
CliUx.ux.log(`HEROKU_OAUTH_SECRET=${client.secret}`)
ux.log(`HEROKU_OAUTH_ID=${client.id}`)
ux.log(`HEROKU_OAUTH_SECRET=${client.secret}`)
} else {
CliUx.ux.styledHeader(`${client.name}`)
CliUx.ux.styledObject(client)
ux.styledHeader(`${client.name}`)
ux.styledObject(client)
}
}
}
Loading

0 comments on commit 73abf47

Please sign in to comment.