Utilisation d'un Git Credential Helper Oauth sur Debian et WSL avec Gitlab

Sommaire

L'objectif de cet article est d'utiliser le "git credential helper" Oauth sur Debian et Microsoft WSL pour se connecter à Gitlab :

  • Pour Microsoft WSL, nous utiliserons "Git Credential Manager" qui est inclu avec "Git for Windows".
  • Pour Debian, nous utiliserons "git-credential-oauth".

Cela permettra de ne plus stocker les mots de passe et les jetons d'accès personnels, notamment lorsque votre Gitlab est accessible uniquement en HTTPS.

Prérequis

git-credential-oauth sur Debian

Dans un premier temps on installe le paquetage Debian (git-credential-oauth) :

1apt-get install git-credential-oauth

Ensuite on configure le Git Credential Helper pour utiliser "oauth" (attention la première commande supprime tous les "credential helpers" configurés actuellement sur votre système) :

1git config --global --unset-all credential.helper
2git config --global --add credential.helper "cache --timeout 21600" # six hours
3git config --global --add credential.helper oauth

En cas de Gitlab "Self Hosted" suivre les instructions pour créer une application "Oauth sur Gitlab" : https://github.com/hickford/git-credential-oauth?tab=readme-ov-file#gitlab

Enfin, renseigner les informations (remplacer example.com par le nom de domaine de votre instance Gitlab) :

1git config --global credential.https://gitlab.example.com.oauthClientId <CLIENTID>
2git config --global credential.https://gitlab.example.com.oauthScopes "read_repository write_repository"
3git config --global credential.https://gitlab.example.com.oauthAuthURL /oauth/authorize
4git config --global credential.https://gitlab.example.com.oauthTokenURL /oauth/token
5git config --global credential.https://gitlab.example.com.oauthDeviceAuthURL /oauth/authorize_device

Git Credential Manager sur Microsoft WSL

Pour Microsoft WSL (distribution Debian dans notre exemple) il existe une alternative qui consiste à utiliser le "Git Credential Manager" (GSM), qui est un "Git Credential Helper" développé en .NET et intégré dans "Git for Windows" : https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-git#git-credential-manager-setup

Sur votre système WSL, on nettoie les configuration "Git Credential Helper" (attention cela supprime tous les "credential helpers" configurés actuellement sur votre système) :

1git config --global --unset-all credential.helper

Sur votre système WSL, on configure Git pour utiliser le "Credential Helper" livré avec "Git for Windows" (attention, le chemin peut être différent en fonction de votre version de "Git for Windows") :

1git config --global credential.helper "/mnt/c/Program\ Files/Git/bin/git-credential-manager.exe"

En cas de Gitlab "Self Hosted" suivre les instructions suivantes pour créer une application "Oauth sur Gitlab" : https://github.com/git-ecosystem/git-credential-manager/blob/main/docs/gitlab.md#using-on-another-instance

Enfin, sur votre système Windows (le "Git for Windows"), renseigner les informations (remplacer example.com par le nom de domaine de votre instance Gitlab) :

1git config --global credential.https://gitlab.example.com.gitLabDevClientId <APPLICATION_ID>
2git config --global credential.https://gitlab.example.com.gitLabDevClientSecret <APPLICATION_SECRET>
3git config --global credential.https://gitlab.example.com.gitLabAuthModes browser
4git config --global credential.https://gitlab.example.com.provider gitlab
5git config --global --get-urlmatch credential https://gitlab.example.com

Annexes