Sources: accept login:password Basic credential for Gitea (API + git store)
This commit is contained in:
@@ -11,7 +11,10 @@ export type GiteaFetch = (url: string, init: { method: string; headers: Record<s
|
||||
|
||||
export interface GiteaTarget { url: string; token: string }
|
||||
|
||||
/** Reads the token from `env[tokenEnv]`; the token is never stored in Settings, only its variable name is. */
|
||||
/** The credential scheme: `login:password` (any value with a colon) authenticates as HTTP Basic, anything else as a token. */
|
||||
export const giteaAuthorization = (secret: string): string =>
|
||||
secret.includes(':') ? `Basic ${Buffer.from(secret, 'utf8').toString('base64')}` : `token ${secret}`
|
||||
/** Reads the credential from `env[tokenEnv]`; only its variable name is stored in Settings, never the value. */
|
||||
export function giteaClient(url: string, tokenEnv: string, env: Record<string, string | undefined> = process.env,
|
||||
fetchImpl: GiteaFetch = fetch): GiteaApi {
|
||||
if (!url) throw new Error('The Gitea Source names no URL')
|
||||
@@ -27,7 +30,7 @@ export class GiteaApi {
|
||||
async raw(path: string, init: { method?: string; body?: unknown; signal?: AbortSignal } = {}): Promise<GiteaResponse> {
|
||||
return await this.fetchImpl(`${this.target.url}/api/v1/${path.replace(/^\//, '')}`, {
|
||||
method: init.method ?? 'GET',
|
||||
headers: { authorization: `token ${this.target.token}`,
|
||||
headers: { authorization: giteaAuthorization(this.target.token),
|
||||
...(init.body === undefined ? {} : { 'content-type': 'application/json' }) },
|
||||
...(init.body === undefined ? {} : { body: JSON.stringify(init.body) }),
|
||||
...(init.signal === undefined ? {} : { signal: init.signal }) })
|
||||
|
||||
Reference in New Issue
Block a user