|
| 1 | +/** |
| 2 | + * Copyright 2026 GitProxy Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import crypto from 'crypto'; |
| 18 | + |
| 19 | +export type ScmUserInfo = { |
| 20 | + login: string; |
| 21 | + email?: string; |
| 22 | +}; |
| 23 | + |
| 24 | +type CacheEntry = { username: string; provider: string; cachedAt: number }; |
| 25 | +const DEFAULT_TTL_MS = 5 * 60 * 1000; |
| 26 | + |
| 27 | +export class ScmTokenCache { |
| 28 | + private readonly cache = new Map<string, CacheEntry>(); |
| 29 | + private readonly ttlMs: number; |
| 30 | + |
| 31 | + constructor(ttlMs = DEFAULT_TTL_MS) { |
| 32 | + this.ttlMs = ttlMs; |
| 33 | + } |
| 34 | + |
| 35 | + private key(provider: string, token: string): string { |
| 36 | + return crypto.createHash('sha512').update(`${provider}:${token}`).digest('hex'); |
| 37 | + } |
| 38 | + |
| 39 | + lookup(provider: string, token: string): string | null { |
| 40 | + const k = this.key(provider, token); |
| 41 | + const entry = this.cache.get(k); |
| 42 | + if (!entry) return null; |
| 43 | + if (Date.now() - entry.cachedAt > this.ttlMs) { |
| 44 | + this.cache.delete(k); |
| 45 | + return null; |
| 46 | + } |
| 47 | + return entry.username; |
| 48 | + } |
| 49 | + |
| 50 | + store(provider: string, token: string, username: string): void { |
| 51 | + this.cache.set(this.key(provider, token), { username, provider, cachedAt: Date.now() }); |
| 52 | + } |
| 53 | + |
| 54 | + evictByUsername(provider: string, username: string): void { |
| 55 | + for (const [k, entry] of this.cache.entries()) { |
| 56 | + if (entry.username === username && entry.provider === provider) this.cache.delete(k); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +export const scmTokenCache = new ScmTokenCache(); |
| 62 | + |
| 63 | +export interface TokenIdentityProvider { |
| 64 | + readonly name: string; |
| 65 | + matches(hostname: string): boolean; |
| 66 | + fetchScmIdentity(token: string): Promise<ScmUserInfo | null>; |
| 67 | +} |
| 68 | + |
| 69 | +type GitHubUserResponse = { |
| 70 | + login: string; |
| 71 | + id: number; |
| 72 | + email: string | null; |
| 73 | +}; |
| 74 | + |
| 75 | +export class GitHubTokenIdentityProvider implements TokenIdentityProvider { |
| 76 | + readonly name = 'github'; |
| 77 | + |
| 78 | + matches(hostname: string): boolean { |
| 79 | + return hostname === 'github.com'; |
| 80 | + } |
| 81 | + |
| 82 | + async fetchScmIdentity(token: string): Promise<ScmUserInfo | null> { |
| 83 | + try { |
| 84 | + const response = await fetch('https://api.github.com/user', { |
| 85 | + headers: { |
| 86 | + Authorization: `token ${token}`, |
| 87 | + Accept: 'application/vnd.github+json', |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + if (!response.ok) { |
| 92 | + console.warn( |
| 93 | + `GitHub /user API returned ${response.status} — token may be invalid or lack read:user scope`, |
| 94 | + ); |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + const user: GitHubUserResponse = await response.json(); |
| 99 | + return { |
| 100 | + login: user.login, |
| 101 | + email: user.email ?? undefined, |
| 102 | + }; |
| 103 | + } catch (e) { |
| 104 | + console.warn(`Failed to fetch GitHub identity: ${e}`); |
| 105 | + return null; |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +const providers: TokenIdentityProvider[] = [new GitHubTokenIdentityProvider()]; |
| 111 | + |
| 112 | +export function getProviderForHost(hostname: string): TokenIdentityProvider | null { |
| 113 | + return providers.find((p) => p.matches(hostname)) ?? null; |
| 114 | +} |
0 commit comments