Skip to content
Closed
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
18 changes: 15 additions & 3 deletions src/commands/authorizations/revoke.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {Command} from '@heroku-cli/command'
import {Command, flags} from '@heroku-cli/command'
import * as Heroku from '@heroku-cli/schema'
import * as color from '@heroku/heroku-cli-util/color'
import {Args, ux} from '@oclif/core'

import {SDK_HEADER} from '../../lib/api.js'

export default class AuthorizationsRevoke extends Command {
static aliases = ['authorizations:destroy']
static args = {
Expand All @@ -12,12 +14,22 @@ export default class AuthorizationsRevoke extends Command {
static examples = [
color.command('heroku authorizations:revoke 105a7bfa-34c3-476e-873a-b1ac3fdc12fb'),
]
static flags = {
team: flags.team(),
}

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

const endpoint = flags.team
? `/teams/${encodeURIComponent(flags.team)}/oauth/authorizations/${encodeURIComponent(args.id)}`
: `/oauth/authorizations/${encodeURIComponent(args.id)}`

ux.action.start('Revoking OAuth Authorization')
const {body: auth} = await this.heroku.delete<Heroku.OAuthAuthorization>(`/oauth/authorizations/${encodeURIComponent(args.id)}`)
const {body: auth} = await this.heroku.delete<Heroku.OAuthAuthorization>(
endpoint,
flags.team ? {headers: {Accept: SDK_HEADER}} : {},
)
ux.action.stop(`done, revoked authorization from ${color.cyan(auth.description)}`)
}
}
17 changes: 14 additions & 3 deletions src/commands/authorizations/rotate.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import {Command} from '@heroku-cli/command'
import {Command, flags} from '@heroku-cli/command'
import * as Heroku from '@heroku-cli/schema'
import {Args, ux} from '@oclif/core'

import {SDK_HEADER} from '../../lib/api.js'
import {display} from '../../lib/authorizations/authorizations.js'

export default class AuthorizationsRotate extends Command {
static args = {
id: Args.string({description: 'ID of the authorization', required: true}),
}
static description = 'updates an OAuth authorization token'
static flags = {
team: flags.team(),
}

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

const endpoint = flags.team
? `/teams/${encodeURIComponent(flags.team)}/oauth/authorizations/${encodeURIComponent(args.id)}/actions/regenerate-tokens`
: `/oauth/authorizations/${encodeURIComponent(args.id)}/actions/regenerate-tokens`

ux.action.start('Rotating OAuth Authorization')
const {body: authorization} = await this.heroku.post<Heroku.OAuthAuthorization>(`/oauth/authorizations/${encodeURIComponent(args.id)}/actions/regenerate-tokens`)
const {body: authorization} = await this.heroku.post<Heroku.OAuthAuthorization>(
endpoint,
flags.team ? {headers: {Accept: SDK_HEADER}} : {},
)
ux.action.stop()

display(authorization)
Expand Down
13 changes: 13 additions & 0 deletions test/unit/commands/authorizations/revoke.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ describe('authorizations:revoke', function () {
expect(stderr).to.contain('done, revoked authorization from Example Auth')
})

context('with --team', function () {
it('revokes the team-owned authorization', async function () {
api
.matchHeader('accept', 'application/vnd.heroku+json; version=3.sdk')
.delete(`/teams/my-team/oauth/authorizations/${authorizationID}`)
.reply(200, {description: 'Example Auth'})

const {stderr} = await runCommand(AuthorizationsRevoke, [authorizationID, '--team', 'my-team'])

expect(stderr).to.contain('done, revoked authorization from Example Auth')
})
})

context('without an ID argument', function () {
it('shows required ID error', async function () {
const {error} = await runCommand(AuthorizationsRevoke, [])
Expand Down
16 changes: 16 additions & 0 deletions test/unit/commands/authorizations/rotate.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,20 @@ describe('authorizations:rotate', function () {
expect(stdout).to.contain('Token: secrettoken\n')
expect(stderr).to.contain('Rotating OAuth Authorization... done')
})

context('with --team', function () {
it('rotates the team-owned authorization and prints the authentication', async function () {
api
.matchHeader('accept', 'application/vnd.heroku+json; version=3.sdk')
.post(`/teams/my-team/oauth/authorizations/${authorizationID}/actions/regenerate-tokens`)
.reply(200, {access_token: {token: 'secrettoken'}, scope: ['global', 'app']})

const {stderr, stdout} = await runCommand(AuthorizationsRotate, [authorizationID, '--team', 'my-team'])

expect(stdout).to.contain('Client: <none>\n')
expect(stdout).to.contain('Scope: global,app\n')
expect(stdout).to.contain('Token: secrettoken\n')
expect(stderr).to.contain('Rotating OAuth Authorization... done')
})
})
})
Loading