mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-05-05 12:21:45 +00:00
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:
59
oauth/types.go
Normal file
59
oauth/types.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package oauth
|
||||
|
||||
// OAuthToken represents the token received from OAuth provider
|
||||
type OAuthToken struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
TokenType string `json:"token_type"`
|
||||
RefreshToken string `json:"refresh_token,omitempty"`
|
||||
ExpiresIn int `json:"expires_in,omitempty"`
|
||||
Scope string `json:"scope,omitempty"`
|
||||
IDToken string `json:"id_token,omitempty"`
|
||||
}
|
||||
|
||||
// OAuthUser represents the user info from OAuth provider
|
||||
type OAuthUser struct {
|
||||
// ProviderUserID is the unique identifier from the OAuth provider
|
||||
ProviderUserID string
|
||||
// Username is the username from the OAuth provider (e.g., GitHub login)
|
||||
Username string
|
||||
// DisplayName is the display name from the OAuth provider
|
||||
DisplayName string
|
||||
// Email is the email from the OAuth provider
|
||||
Email string
|
||||
// Extra contains any additional provider-specific data
|
||||
Extra map[string]any
|
||||
}
|
||||
|
||||
// OAuthError represents a translatable OAuth error
|
||||
type OAuthError struct {
|
||||
// MsgKey is the i18n message key
|
||||
MsgKey string
|
||||
// Params contains optional parameters for the message template
|
||||
Params map[string]any
|
||||
// RawError is the underlying error for logging purposes
|
||||
RawError string
|
||||
}
|
||||
|
||||
func (e *OAuthError) Error() string {
|
||||
if e.RawError != "" {
|
||||
return e.RawError
|
||||
}
|
||||
return e.MsgKey
|
||||
}
|
||||
|
||||
// NewOAuthError creates a new OAuth error with the given message key
|
||||
func NewOAuthError(msgKey string, params map[string]any) *OAuthError {
|
||||
return &OAuthError{
|
||||
MsgKey: msgKey,
|
||||
Params: params,
|
||||
}
|
||||
}
|
||||
|
||||
// NewOAuthErrorWithRaw creates a new OAuth error with raw error message for logging
|
||||
func NewOAuthErrorWithRaw(msgKey string, params map[string]any, rawError string) *OAuthError {
|
||||
return &OAuthError{
|
||||
MsgKey: msgKey,
|
||||
Params: params,
|
||||
RawError: rawError,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user