refactor: unify OAuth providers with i18n support
- Introduce Provider interface pattern for standard OAuth protocols - Create unified controller/oauth.go with common OAuth logic - Add OAuthError type for translatable error messages - Add i18n keys and translations (zh/en) for OAuth messages - Use common.ApiErrorI18n/ApiSuccessI18n for consistent responses - Preserve backward compatibility for existing routes and data
This commit is contained in:
36
oauth/provider.go
Normal file
36
oauth/provider.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package oauth
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/QuantumNous/new-api/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Provider defines the interface for OAuth providers
|
||||
type Provider interface {
|
||||
// GetName returns the display name of the provider (e.g., "GitHub", "Discord")
|
||||
GetName() string
|
||||
|
||||
// IsEnabled returns whether this OAuth provider is enabled
|
||||
IsEnabled() bool
|
||||
|
||||
// ExchangeToken exchanges the authorization code for an access token
|
||||
// The gin.Context is passed for providers that need request info (e.g., for redirect_uri)
|
||||
ExchangeToken(ctx context.Context, code string, c *gin.Context) (*OAuthToken, error)
|
||||
|
||||
// GetUserInfo retrieves user information using the access token
|
||||
GetUserInfo(ctx context.Context, token *OAuthToken) (*OAuthUser, error)
|
||||
|
||||
// IsUserIDTaken checks if the provider user ID is already associated with an account
|
||||
IsUserIDTaken(providerUserID string) bool
|
||||
|
||||
// FillUserByProviderID fills the user model by provider user ID
|
||||
FillUserByProviderID(user *model.User, providerUserID string) error
|
||||
|
||||
// SetProviderUserID sets the provider user ID on the user model
|
||||
SetProviderUserID(user *model.User, providerUserID string)
|
||||
|
||||
// GetProviderPrefix returns the prefix for auto-generated usernames (e.g., "github_")
|
||||
GetProviderPrefix() string
|
||||
}
|
||||
Reference in New Issue
Block a user