feat(git): enhance token authentication and credential handling

This commit is contained in:
Bohdan Triapitsyn
2026-01-14 02:54:48 +02:00
parent fa83c1e645
commit 1319033df9
7 changed files with 11 additions and 56 deletions
+1 -14
View File
@@ -4,11 +4,6 @@ import os from 'os';
const GIT_CREDENTIALS_PATH = path.join(os.homedir(), '.git-credentials');
/**
* Parse ~/.git-credentials file and return discovered credentials.
* Format: https://username:token@host or https://username:token@host/path
* @returns {Array<{host: string, username: string}>}
*/
export function discoverGitCredentials() {
const credentials = [];
@@ -25,19 +20,16 @@ export function discoverGitCredentials() {
const url = new URL(line.trim());
const hostname = url.hostname;
const pathname = url.pathname && url.pathname !== '/' ? url.pathname : '';
// Include path for repo-specific tokens (e.g., github.com/user/repo)
const host = hostname + pathname;
const username = url.username || '';
if (host && username) {
// Avoid duplicates
const exists = credentials.some(c => c.host === host && c.username === username);
if (!exists) {
credentials.push({ host, username });
}
}
} catch {
// Skip malformed lines
continue;
}
}
@@ -48,11 +40,6 @@ export function discoverGitCredentials() {
return credentials;
}
/**
* Get credential for a specific host from ~/.git-credentials
* @param {string} host - The host to look up (e.g., "github.com" or "github.com/user/repo")
* @returns {{username: string, token: string} | null}
*/
export function getCredentialForHost(host) {
if (!fs.existsSync(GIT_CREDENTIALS_PATH)) {
return null;
@@ -68,7 +55,7 @@ export function getCredentialForHost(host) {
const hostname = url.hostname;
const pathname = url.pathname && url.pathname !== '/' ? url.pathname : '';
const credHost = hostname + pathname;
if (credHost === host) {
return {
username: url.username || '',
-4
View File
@@ -177,18 +177,14 @@ export async function setLocalIdentity(directory, profile) {
false,
'local'
);
// Clear credential helper if previously set for token auth
await git.raw(['config', '--local', '--unset', 'credential.helper']).catch(() => {});
} else if (authType === 'token' && profile.host) {
// For token auth, configure git to use the store credential helper
// which reads from ~/.git-credentials
await git.addConfig(
'credential.helper',
'store',
false,
'local'
);
// Clear SSH command if previously set
await git.raw(['config', '--local', '--unset', 'core.sshCommand']).catch(() => {});
}