Skip to content

Commit 3ff7fac

Browse files
committed
add auth checks to other commands
1 parent 7d1f26f commit 3ff7fac

File tree

13 files changed

+109
-42
lines changed

13 files changed

+109
-42
lines changed

src/commands/audit-log.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import {
99
handleUnsuccessfulApiResponse
1010
} from '../utils/api-helpers'
1111
import { printFlagList } from '../utils/formatting'
12-
import { FREE_API_KEY, getDefaultKey, setupSdk } from '../utils/sdk'
12+
import { getDefaultKey, setupSdk } from '../utils/sdk'
1313

1414
import type { CliSubcommand } from '../utils/meow-with-subcommands'
1515
import type { Ora } from 'ora'
16+
import { AuthError } from '../utils/errors'
1617

1718
export const auditLog: CliSubcommand = {
1819
description: 'Look up the audit log for an organization',
@@ -21,8 +22,12 @@ export const auditLog: CliSubcommand = {
2122

2223
const input = setupCommand(name, auditLog.description, argv, importMeta)
2324
if (input) {
25+
const apiKey = getDefaultKey()
26+
if(!apiKey){
27+
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
28+
}
2429
const spinner = ora(`Looking up audit log for ${input.orgSlug}\n`).start()
25-
await fetchOrgAuditLog(input.orgSlug, input, spinner)
30+
await fetchOrgAuditLog(input.orgSlug, input, spinner, apiKey)
2631
}
2732
}
2833
}
@@ -132,9 +137,10 @@ type AuditChoices = (Separator | AuditChoice)[]
132137
async function fetchOrgAuditLog(
133138
orgSlug: string,
134139
input: CommandContext,
135-
spinner: Ora
140+
spinner: Ora,
141+
apiKey: string
136142
): Promise<void> {
137-
const socketSdk = await setupSdk(getDefaultKey() || FREE_API_KEY)
143+
const socketSdk = await setupSdk(apiKey)
138144
const result = await handleApiCall(
139145
socketSdk.getAuditLogEvents(orgSlug, input),
140146
`Looking up audit log for ${orgSlug}\n`

src/commands/organization.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import { getDefaultKey, setupSdk } from '../utils/sdk'
1010

1111
import type { CliSubcommand } from '../utils/meow-with-subcommands'
12+
import { AuthError } from '../utils/errors'
1213

1314
export const organizations: CliSubcommand = {
1415
description: 'List organizations associated with the API key used',
@@ -42,6 +43,9 @@ function setupCommand(
4243

4344
async function fetchOrganizations(): Promise<void> {
4445
const apiKey = getDefaultKey()
46+
if(!apiKey){
47+
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
48+
}
4549
const socketSdk = await setupSdk(apiKey)
4650
const spinner = ora('Fetching organizations...').start()
4751

@@ -57,13 +61,10 @@ async function fetchOrganizations(): Promise<void> {
5761
spinner.stop()
5862

5963
const organizations = Object.values(result.data.organizations)
60-
if (apiKey) {
61-
console.log(
62-
`List of organizations associated with your API key: ${chalk.italic(apiKey)}`
63-
)
64-
} else {
65-
console.log('List of organizations associated with your API key.')
66-
}
64+
65+
console.log(
66+
`List of organizations associated with your API key: ${chalk.italic(apiKey)}`
67+
)
6768

6869
for (const o of organizations) {
6970
console.log(`

src/commands/report/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { meowWithSubcommands } from '../../utils/meow-with-subcommands'
44

55
import type { CliSubcommand } from '../../utils/meow-with-subcommands'
66

7-
const description = 'Project report related commands'
7+
const description = '[Deprecated] Project report related commands'
88

99
export const report: CliSubcommand = {
1010
description,

src/commands/repos/create.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,21 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk'
1212

1313
import type { CliSubcommand } from '../../utils/meow-with-subcommands'
1414
import type { Ora } from 'ora'
15+
import { AuthError } from '../../utils/errors'
1516

1617
export const create: CliSubcommand = {
1718
description: 'Create a repository in an organization',
1819
async run(argv, importMeta, { parentName }) {
1920
const name = `${parentName} create`
2021
const input = setupCommand(name, create.description, argv, importMeta)
2122
if (input) {
23+
const apiKey = getDefaultKey()
24+
if(!apiKey){
25+
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
26+
}
2227
const spinnerText = 'Creating repository... \n'
2328
const spinner = ora(spinnerText).start()
24-
await createRepo(input.orgSlug, input, spinner)
29+
await createRepo(input.orgSlug, input, spinner, apiKey)
2530
}
2631
}
2732
}
@@ -145,9 +150,10 @@ function setupCommand(
145150
async function createRepo(
146151
orgSlug: string,
147152
input: CommandContext,
148-
spinner: Ora
153+
spinner: Ora,
154+
apiKey: string
149155
): Promise<void> {
150-
const socketSdk = await setupSdk(getDefaultKey())
156+
const socketSdk = await setupSdk(apiKey)
151157
const result = await handleApiCall(
152158
socketSdk.createOrgRepo(orgSlug, input),
153159
'creating repository'

src/commands/repos/delete.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,21 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk'
1010

1111
import type { CliSubcommand } from '../../utils/meow-with-subcommands'
1212
import type { Ora } from 'ora'
13+
import { AuthError } from '../../utils/errors'
1314

1415
export const del: CliSubcommand = {
1516
description: 'Delete a repository in an organization',
1617
async run(argv, importMeta, { parentName }) {
1718
const name = `${parentName} del`
1819
const input = setupCommand(name, del.description, argv, importMeta)
1920
if (input) {
21+
const apiKey = getDefaultKey()
22+
if(!apiKey){
23+
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
24+
}
2025
const spinnerText = 'Deleting repository... \n'
2126
const spinner = ora(spinnerText).start()
22-
await deleteRepository(input.orgSlug, input.repoName, spinner)
27+
await deleteRepository(input.orgSlug, input.repoName, spinner, apiKey)
2328
}
2429
}
2530
}
@@ -71,9 +76,10 @@ function setupCommand(
7176
async function deleteRepository(
7277
orgSlug: string,
7378
repoName: string,
74-
spinner: Ora
79+
spinner: Ora,
80+
apiKey: string
7581
): Promise<void> {
76-
const socketSdk = await setupSdk(getDefaultKey())
82+
const socketSdk = await setupSdk(apiKey)
7783
const result = await handleApiCall(
7884
socketSdk.deleteOrgRepo(orgSlug, repoName),
7985
'deleting repository'

src/commands/repos/list.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk'
1414

1515
import type { CliSubcommand } from '../../utils/meow-with-subcommands'
1616
import type { Ora } from 'ora'
17+
import { AuthError } from '../../utils/errors'
1718

1819
export const list: CliSubcommand = {
1920
description: 'List repositories in an organization',
2021
async run(argv, importMeta, { parentName }) {
2122
const name = `${parentName} list`
2223
const input = setupCommand(name, list.description, argv, importMeta)
2324
if (input) {
25+
const apiKey = getDefaultKey()
26+
if(!apiKey){
27+
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
28+
}
2429
const spinnerText = 'Listing repositories... \n'
2530
const spinner = ora(spinnerText).start()
26-
await listOrgRepos(input.orgSlug, input, spinner)
31+
await listOrgRepos(input.orgSlug, input, spinner, apiKey)
2732
}
2833
}
2934
}
@@ -129,9 +134,10 @@ function setupCommand(
129134
async function listOrgRepos(
130135
orgSlug: string,
131136
input: CommandContext,
132-
spinner: Ora
137+
spinner: Ora,
138+
apiKey: string
133139
): Promise<void> {
134-
const socketSdk = await setupSdk(getDefaultKey())
140+
const socketSdk = await setupSdk(apiKey)
135141
const result = await handleApiCall(
136142
socketSdk.getOrgRepoList(orgSlug, input),
137143
'listing repositories'

src/commands/repos/update.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,21 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk'
1212

1313
import type { CliSubcommand } from '../../utils/meow-with-subcommands'
1414
import type { Ora } from 'ora'
15+
import { AuthError } from '../../utils/errors'
1516

1617
export const update: CliSubcommand = {
1718
description: 'Update a repository in an organization',
1819
async run(argv, importMeta, { parentName }) {
1920
const name = `${parentName} update`
2021
const input = setupCommand(name, update.description, argv, importMeta)
2122
if (input) {
23+
const apiKey = getDefaultKey()
24+
if(!apiKey){
25+
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
26+
}
2227
const spinnerText = 'Updating repository... \n'
2328
const spinner = ora(spinnerText).start()
24-
await updateRepository(input.orgSlug, input, spinner)
29+
await updateRepository(input.orgSlug, input, spinner, apiKey)
2530
}
2631
}
2732
}
@@ -145,9 +150,10 @@ function setupCommand(
145150
async function updateRepository(
146151
orgSlug: string,
147152
input: CommandContext,
148-
spinner: Ora
153+
spinner: Ora,
154+
apiKey: string
149155
): Promise<void> {
150-
const socketSdk = await setupSdk(getDefaultKey())
156+
const socketSdk = await setupSdk(apiKey)
151157
const result = await handleApiCall(
152158
socketSdk.updateOrgRepo(orgSlug, input.name, input),
153159
'updating repository'

src/commands/repos/view.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk'
1414

1515
import type { CliSubcommand } from '../../utils/meow-with-subcommands'
1616
import type { Ora } from 'ora'
17+
import { AuthError } from '../../utils/errors'
1718

1819
export const view: CliSubcommand = {
1920
description: 'View repositories in an organization',
2021
async run(argv, importMeta, { parentName }) {
2122
const name = `${parentName} view`
2223
const input = setupCommand(name, view.description, argv, importMeta)
2324
if (input) {
25+
const apiKey = getDefaultKey()
26+
if(!apiKey){
27+
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
28+
}
2429
const spinnerText = 'Fetching repository... \n'
2530
const spinner = ora(spinnerText).start()
26-
await viewRepository(input.orgSlug, input.repositoryName, spinner)
31+
await viewRepository(input.orgSlug, input.repositoryName, spinner, apiKey)
2732
}
2833
}
2934
}
@@ -89,9 +94,10 @@ function setupCommand(
8994
async function viewRepository(
9095
orgSlug: string,
9196
repoName: string,
92-
spinner: Ora
97+
spinner: Ora,
98+
apiKey: string
9399
): Promise<void> {
94-
const socketSdk = await setupSdk(getDefaultKey())
100+
const socketSdk = await setupSdk(apiKey)
95101
const result = await handleApiCall(
96102
socketSdk.getOrgRepo(orgSlug, repoName),
97103
'fetching repository'

src/commands/scan/create.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,21 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk'
1818

1919
import type { CliSubcommand } from '../../utils/meow-with-subcommands'
2020
import type { Ora } from 'ora'
21+
import { AuthError } from '../../utils/errors'
2122

2223
export const create: CliSubcommand = {
2324
description: 'Create a scan',
2425
async run(argv, importMeta, { parentName }) {
2526
const name = `${parentName} create`
2627
const input = await setupCommand(name, create.description, argv, importMeta)
2728
if (input) {
29+
const apiKey = getDefaultKey()
30+
if(!apiKey){
31+
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
32+
}
2833
const spinnerText = 'Creating a scan... \n'
2934
const spinner = ora(spinnerText).start()
30-
await createFullScan(input, spinner)
35+
await createFullScan(input, spinner, apiKey)
3136
}
3237
}
3338
}
@@ -203,9 +208,10 @@ async function setupCommand(
203208

204209
async function createFullScan(
205210
input: CommandContext,
206-
spinner: Ora
211+
spinner: Ora,
212+
apiKey: string
207213
): Promise<void> {
208-
const socketSdk = await setupSdk(getDefaultKey())
214+
const socketSdk = await setupSdk(apiKey)
209215
const {
210216
orgSlug,
211217
repoName,

src/commands/scan/delete.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,21 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk'
1212

1313
import type { CliSubcommand } from '../../utils/meow-with-subcommands'
1414
import type { Ora } from 'ora'
15+
import { AuthError } from '../../utils/errors'
1516

1617
export const del: CliSubcommand = {
1718
description: 'Delete a scan',
1819
async run(argv, importMeta, { parentName }) {
1920
const name = `${parentName} del`
2021
const input = setupCommand(name, del.description, argv, importMeta)
2122
if (input) {
23+
const apiKey = getDefaultKey()
24+
if(!apiKey){
25+
throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.")
26+
}
2227
const spinnerText = 'Deleting scan...'
2328
const spinner = ora(spinnerText).start()
24-
await deleteOrgFullScan(input.orgSlug, input.fullScanId, spinner)
29+
await deleteOrgFullScan(input.orgSlug, input.fullScanId, spinner, apiKey)
2530
}
2631
}
2732
}
@@ -87,9 +92,10 @@ function setupCommand(
8792
async function deleteOrgFullScan(
8893
orgSlug: string,
8994
fullScanId: string,
90-
spinner: Ora
95+
spinner: Ora,
96+
apiKey: string
9197
): Promise<void> {
92-
const socketSdk = await setupSdk(getDefaultKey())
98+
const socketSdk = await setupSdk(apiKey)
9399
const result = await handleApiCall(
94100
socketSdk.deleteOrgFullScan(orgSlug, fullScanId),
95101
'Deleting scan'

0 commit comments

Comments
 (0)