Skip to content

Commit 94f6944

Browse files
authored
Remove auth headers for unauthenticated endpoints in REST code samples (#56278)
1 parent c29b180 commit 94f6944

File tree

2 files changed

+575
-2
lines changed

2 files changed

+575
-2
lines changed

src/rest/components/get-rest-code-samples.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ import { stringify } from 'javascript-stringify'
44
import type { CodeSample, Operation } from '@/rest/components/types'
55
import { type VersionItem } from '@/frame/components/context/MainContext'
66

7+
// Helper function to determine if authentication should be omitted
8+
function shouldOmitAuthentication(operation: Operation, currentVersion: string): boolean {
9+
// Only omit auth for operations that explicitly allow permissionless access
10+
if (!operation?.progAccess?.allowPermissionlessAccess) {
11+
return false
12+
}
13+
14+
// Only omit auth on dotcom versions (free-pro-team, enterprise-cloud)
15+
// GHES and other versions still require authentication
16+
const isDotcomVersion =
17+
currentVersion.startsWith('free-pro-team') || currentVersion.startsWith('enterprise-cloud')
18+
19+
return isDotcomVersion
20+
}
21+
722
// Helper function to escape shell values containing single quotes (contractions)
823
// This prevents malformed shell commands when contractions like "there's" are used
924
function escapeShellValue(value: string): string {
@@ -46,6 +61,9 @@ export function getShellExample(
4661
contentTypeHeader = '-H "Content-Type: multipart/form-data"'
4762
}
4863

64+
// Check if we should omit authentication for this operation
65+
const omitAuth = shouldOmitAuthentication(operation, currentVersion)
66+
4967
// GHES Manage API requests differ from the dotcom API requests and make use of multipart/form-data and json content types
5068
if (operation.subcategory === 'manage-ghes') {
5169
// GET requests don't have a requestBody set, therefore let's default them to application/json
@@ -94,7 +112,7 @@ export function getShellExample(
94112
}
95113
}
96114

97-
let authHeader = '-H "Authorization: Bearer <YOUR-TOKEN>"'
115+
let authHeader = omitAuth ? '' : '-H "Authorization: Bearer <YOUR-TOKEN>"'
98116
let apiVersionHeader =
99117
allVersions[currentVersion].apiVersions.length > 0 &&
100118
allVersions[currentVersion].latestApiVersion
@@ -116,6 +134,15 @@ export function getShellExample(
116134
acceptHeader = acceptHeader === `-H "Accept: application/vnd.github+json"` ? '' : acceptHeader
117135
}
118136

137+
// For unauthenticated endpoints, remove the auth header completely
138+
if (
139+
omitAuth &&
140+
operation.subcategory !== 'management-console' &&
141+
operation.subcategory !== 'manage-ghes'
142+
) {
143+
authHeader = ''
144+
}
145+
119146
if (operation?.progAccess?.basicAuth) {
120147
authHeader = '-u "<YOUR_CLIENT_ID>:<YOUR_CLIENT_SECRET>"'
121148
}
@@ -306,6 +333,8 @@ export function getJSExample(
306333
currentVersion: string,
307334
allVersions: Record<string, VersionItem>,
308335
) {
336+
// Check if we should omit authentication for this operation
337+
const omitAuth = shouldOmitAuthentication(operation, currentVersion)
309338
const parameters: { [key: string]: string | object } = {}
310339

311340
if (codeSample.request) {
@@ -359,9 +388,15 @@ export function getJSExample(
359388

360389
const comment = `// Octokit.js\n// https://github.com/octokit/core.js#readme\n`
361390
const authOctokit = `const octokit = new Octokit(${stringify({ auth: 'YOUR-TOKEN' }, null, 2)})\n\n`
391+
const unauthenticatedOctokit = `const octokit = new Octokit()\n\n`
362392
const oauthOctokit = `import { createOAuthAppAuth } from "@octokit/auth-oauth-app"\n\nconst octokit = new Octokit({\n authStrategy: createOAuthAppAuth,\n auth:{\n clientType: 'oauth-app',\n clientId: '<YOUR_CLIENT ID>',\n clientSecret: '<YOUR_CLIENT SECRET>'\n }\n})\n\n`
363393
const isBasicAuth = operation?.progAccess?.basicAuth
364-
const authString = isBasicAuth ? oauthOctokit : authOctokit
394+
let authString = isBasicAuth ? oauthOctokit : authOctokit
395+
396+
// Use unauthenticated Octokit for endpoints that allow permissionless access
397+
if (omitAuth) {
398+
authString = unauthenticatedOctokit
399+
}
365400

366401
return `${comment}${authString}await octokit.request('${operation.verb.toUpperCase()} ${
367402
operation.requestPath

0 commit comments

Comments
 (0)