Overview
The Authentication Services provide OAuth 2.0 authentication for GitHub and GitLab, handling token management, user information retrieval, and repository access. Namespace:Chapi.Infrastructure.Services.Auth
Provider Factory
IGitAuthProviderFactory
Factory interface for obtaining the correct authentication provider.GetProvider
Returns the appropriate authentication provider.The Git provider (GitHub, GitLab, Bitbucket, AzureDevOps, or Unknown)
Instance of the requested authentication provider
DetectProviderFromUrl
Detects the Git provider from a remote URL.Git remote URL (e.g., “https://github.com/user/repo.git”)
Detected provider based on URL patterns
- Contains
"github.com"→ GitHub - Contains
"gitlab.com"or"gitlab"→ GitLab - Contains
"bitbucket.org"→ Bitbucket - Contains
"dev.azure.com"or"visualstudio.com"→ AzureDevOps
GitHub OAuth Provider
GitHubOAuthProvider
Implements OAuth 2.0 authentication for GitHub. Configuration: RequiresGitHubConfig with ClientId, ClientSecret, RedirectUri, and Scope
AuthenticateAsync
Initiates GitHub OAuth flow.Returns GitCredential with Provider, Username, Email, AvatarUrl, and AccessToken
- Checks for existing valid credentials
- Opens browser to GitHub authorization URL
- Listens for OAuth callback on local HTTP server
- Exchanges authorization code for access token
- Retrieves user information
- Stores credentials securely
ValidateTokenAsync
Validates a GitHub access token.GitHub access token to validate
True if token is valid and has access to user information
GetRepositoriesAsync
Retrieves user’s GitHub repositories.Valid GitHub access token
List of repositories sorted by last update, up to 100 items
Name- Repository nameFullName- Full name (owner/repo)CloneUrl- HTTPS clone URLIsPrivate- Privacy statusDescription- Repository descriptionUpdatedAt- Last update timestamp
GetUserInfoAsync
Retrieves GitHub user information.RefreshTokenAsync
GitHub does not support refresh tokens in this OAuth flow.Always fails with message “GitHub no soporta refresh token en este flujo.”
GitLab OAuth Provider
GitLabOAuthProvider
Implements OAuth 2.0 authentication for GitLab with refresh token support. Configuration: RequiresGitLabConfig with BaseUrl, ClientId, ClientSecret, RedirectUri, and Scope
AuthenticateAsync
Initiates GitLab OAuth flow.Returns GitCredential with Provider, Username, Email, AvatarUrl, and AccessToken
- Checks for existing valid credentials
- Opens browser to GitLab authorization URL
- Listens for OAuth callback
- Exchanges code for access token and refresh token
- Retrieves user information
- Stores access token and refresh token
RefreshTokenAsync
Renews expired access token using refresh token.Updated credentials with new access token
- Retrieves stored refresh token
- Requests new access token from GitLab
- Updates stored credentials
- Handles token rotation (new refresh token)
GetRepositoriesAsync
Retrieves user’s GitLab projects.Valid GitLab access token
List of projects (repositories) sorted by last activity, up to 100 items
Common Interface
IGitAuthProvider
Base interface implemented by all providers.OAuth Callback UI
Both providers display a premium callback page in the browser: Success Response:- Green gradient theme
- Success icon (✅)
- Confirmation message
- Instructions to close the browser tab
- Red gradient theme
- Error icon (❌)
- Error description (state mismatch or cancelled)
- Retry instructions
Usage Example
Token Storage
Both providers useICredentialStorageService to securely store tokens:
- GitHub: Stores access token only (no refresh)
- GitLab: Stores both access token and refresh token separately
Error Handling
Common error scenarios:- Authentication Cancelled: User closes browser without completing OAuth
- State Mismatch: Security validation failed (possible CSRF attack)
- Invalid Token: Token expired or revoked
- Network Error: Unable to connect to Git provider API
- Refresh Failed: Refresh token expired (requires re-authentication)
Security Features
- State Parameter: CSRF protection using random GUID
- HTTPS Only: Secure token transmission
- Local Callback: HTTP listener on localhost only
- Token Rotation: GitLab refresh tokens are rotated on each renewal
- Secure Storage: Credentials stored using Windows Credential Manager
Notes
- GitHub OAuth does not provide refresh tokens (requires re-authentication when expired)
- GitLab supports token refresh for long-running sessions
- Both providers automatically retry authentication with refreshed token on 401/403 errors
- Repository lists are limited to 100 items per request
- User-Agent header is set to “ChapiAssistant” for API requests