Repository navigation
feat(oauth): implement custom OAuth provider #2857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
df6c669
refactor: unify OAuth providers with i18n support
Calcium-Ion 632baad
feat(oauth): migrate GitHub user identification from login to numeric ID
Calcium-Ion af54ea8
feat(oauth): implement custom OAuth provider management #1106
Calcium-Ion 2567cff
fix(oauth): enhance error handling and transaction management for OAu…
Calcium-Ion e8d26e5
refactor(oauth): update UpdateCustomOAuthProviderRequest to use point…
Calcium-Ion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,386 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "strconv" | ||
|
|
||
| "github.com/QuantumNous/new-api/common" | ||
| "github.com/QuantumNous/new-api/model" | ||
| "github.com/QuantumNous/new-api/oauth" | ||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| // CustomOAuthProviderResponse is the response structure for custom OAuth providers | ||
| // It excludes sensitive fields like client_secret | ||
| type CustomOAuthProviderResponse struct { | ||
| Id int `json:"id"` | ||
| Name string `json:"name"` | ||
| Slug string `json:"slug"` | ||
| Enabled bool `json:"enabled"` | ||
| ClientId string `json:"client_id"` | ||
| AuthorizationEndpoint string `json:"authorization_endpoint"` | ||
| TokenEndpoint string `json:"token_endpoint"` | ||
| UserInfoEndpoint string `json:"user_info_endpoint"` | ||
| Scopes string `json:"scopes"` | ||
| UserIdField string `json:"user_id_field"` | ||
| UsernameField string `json:"username_field"` | ||
| DisplayNameField string `json:"display_name_field"` | ||
| EmailField string `json:"email_field"` | ||
| WellKnown string `json:"well_known"` | ||
| AuthStyle int `json:"auth_style"` | ||
| } | ||
|
|
||
| func toCustomOAuthProviderResponse(p *model.CustomOAuthProvider) *CustomOAuthProviderResponse { | ||
| return &CustomOAuthProviderResponse{ | ||
| Id: p.Id, | ||
| Name: p.Name, | ||
| Slug: p.Slug, | ||
| Enabled: p.Enabled, | ||
| ClientId: p.ClientId, | ||
| AuthorizationEndpoint: p.AuthorizationEndpoint, | ||
| TokenEndpoint: p.TokenEndpoint, | ||
| UserInfoEndpoint: p.UserInfoEndpoint, | ||
| Scopes: p.Scopes, | ||
| UserIdField: p.UserIdField, | ||
| UsernameField: p.UsernameField, | ||
| DisplayNameField: p.DisplayNameField, | ||
| EmailField: p.EmailField, | ||
| WellKnown: p.WellKnown, | ||
| AuthStyle: p.AuthStyle, | ||
| } | ||
| } | ||
|
|
||
| // GetCustomOAuthProviders returns all custom OAuth providers | ||
| func GetCustomOAuthProviders(c *gin.Context) { | ||
| providers, err := model.GetAllCustomOAuthProviders() | ||
| if err != nil { | ||
| common.ApiError(c, err) | ||
| return | ||
| } | ||
|
|
||
| response := make([]*CustomOAuthProviderResponse, len(providers)) | ||
| for i, p := range providers { | ||
| response[i] = toCustomOAuthProviderResponse(p) | ||
| } | ||
|
|
||
| c.JSON(http.StatusOK, gin.H{ | ||
| "success": true, | ||
| "message": "", | ||
| "data": response, | ||
| }) | ||
| } | ||
|
|
||
| // GetCustomOAuthProvider returns a single custom OAuth provider by ID | ||
| func GetCustomOAuthProvider(c *gin.Context) { | ||
| idStr := c.Param("id") | ||
| id, err := strconv.Atoi(idStr) | ||
| if err != nil { | ||
| common.ApiErrorMsg(c, "无效的 ID") | ||
| return | ||
| } | ||
|
|
||
| provider, err := model.GetCustomOAuthProviderById(id) | ||
| if err != nil { | ||
| common.ApiErrorMsg(c, "未找到该 OAuth 提供商") | ||
| return | ||
| } | ||
|
|
||
| c.JSON(http.StatusOK, gin.H{ | ||
| "success": true, | ||
| "message": "", | ||
| "data": toCustomOAuthProviderResponse(provider), | ||
| }) | ||
| } | ||
|
|
||
| // CreateCustomOAuthProviderRequest is the request structure for creating a custom OAuth provider | ||
| type CreateCustomOAuthProviderRequest struct { | ||
| Name string `json:"name" binding:"required"` | ||
| Slug string `json:"slug" binding:"required"` | ||
| Enabled bool `json:"enabled"` | ||
| ClientId string `json:"client_id" binding:"required"` | ||
| ClientSecret string `json:"client_secret" binding:"required"` | ||
| AuthorizationEndpoint string `json:"authorization_endpoint" binding:"required"` | ||
| TokenEndpoint string `json:"token_endpoint" binding:"required"` | ||
| UserInfoEndpoint string `json:"user_info_endpoint" binding:"required"` | ||
| Scopes string `json:"scopes"` | ||
| UserIdField string `json:"user_id_field"` | ||
| UsernameField string `json:"username_field"` | ||
| DisplayNameField string `json:"display_name_field"` | ||
| EmailField string `json:"email_field"` | ||
| WellKnown string `json:"well_known"` | ||
| AuthStyle int `json:"auth_style"` | ||
| } | ||
|
|
||
| // CreateCustomOAuthProvider creates a new custom OAuth provider | ||
| func CreateCustomOAuthProvider(c *gin.Context) { | ||
| var req CreateCustomOAuthProviderRequest | ||
| if err := c.ShouldBindJSON(&req); err != nil { | ||
| common.ApiErrorMsg(c, "无效的请求参数: "+err.Error()) | ||
| return | ||
| } | ||
|
|
||
| // Check if slug is already taken | ||
| if model.IsSlugTaken(req.Slug, 0) { | ||
| common.ApiErrorMsg(c, "该 Slug 已被使用") | ||
| return | ||
| } | ||
|
|
||
| // Check if slug conflicts with built-in providers | ||
| if oauth.IsProviderRegistered(req.Slug) && !oauth.IsCustomProvider(req.Slug) { | ||
| common.ApiErrorMsg(c, "该 Slug 与内置 OAuth 提供商冲突") | ||
| return | ||
| } | ||
|
|
||
| provider := &model.CustomOAuthProvider{ | ||
| Name: req.Name, | ||
| Slug: req.Slug, | ||
| Enabled: req.Enabled, | ||
| ClientId: req.ClientId, | ||
| ClientSecret: req.ClientSecret, | ||
| AuthorizationEndpoint: req.AuthorizationEndpoint, | ||
| TokenEndpoint: req.TokenEndpoint, | ||
| UserInfoEndpoint: req.UserInfoEndpoint, | ||
| Scopes: req.Scopes, | ||
| UserIdField: req.UserIdField, | ||
| UsernameField: req.UsernameField, | ||
| DisplayNameField: req.DisplayNameField, | ||
| EmailField: req.EmailField, | ||
| WellKnown: req.WellKnown, | ||
| AuthStyle: req.AuthStyle, | ||
| } | ||
|
|
||
| if err := model.CreateCustomOAuthProvider(provider); err != nil { | ||
| common.ApiError(c, err) | ||
| return | ||
| } | ||
|
|
||
| // Register the provider in the OAuth registry | ||
| oauth.RegisterOrUpdateCustomProvider(provider) | ||
|
|
||
| c.JSON(http.StatusOK, gin.H{ | ||
| "success": true, | ||
| "message": "创建成功", | ||
| "data": toCustomOAuthProviderResponse(provider), | ||
| }) | ||
| } | ||
|
|
||
| // UpdateCustomOAuthProviderRequest is the request structure for updating a custom OAuth provider | ||
| type UpdateCustomOAuthProviderRequest struct { | ||
| Name string `json:"name"` | ||
| Slug string `json:"slug"` | ||
| Enabled bool `json:"enabled"` | ||
| ClientId string `json:"client_id"` | ||
| ClientSecret string `json:"client_secret"` // Optional: if empty, keep existing | ||
| AuthorizationEndpoint string `json:"authorization_endpoint"` | ||
| TokenEndpoint string `json:"token_endpoint"` | ||
| UserInfoEndpoint string `json:"user_info_endpoint"` | ||
| Scopes string `json:"scopes"` | ||
| UserIdField string `json:"user_id_field"` | ||
| UsernameField string `json:"username_field"` | ||
| DisplayNameField string `json:"display_name_field"` | ||
| EmailField string `json:"email_field"` | ||
| WellKnown string `json:"well_known"` | ||
| AuthStyle int `json:"auth_style"` | ||
| } | ||
|
|
||
| // UpdateCustomOAuthProvider updates an existing custom OAuth provider | ||
| func UpdateCustomOAuthProvider(c *gin.Context) { | ||
| idStr := c.Param("id") | ||
| id, err := strconv.Atoi(idStr) | ||
| if err != nil { | ||
| common.ApiErrorMsg(c, "无效的 ID") | ||
| return | ||
| } | ||
|
|
||
| var req UpdateCustomOAuthProviderRequest | ||
| if err := c.ShouldBindJSON(&req); err != nil { | ||
| common.ApiErrorMsg(c, "无效的请求参数: "+err.Error()) | ||
| return | ||
| } | ||
|
|
||
| // Get existing provider | ||
| provider, err := model.GetCustomOAuthProviderById(id) | ||
| if err != nil { | ||
| common.ApiErrorMsg(c, "未找到该 OAuth 提供商") | ||
| return | ||
| } | ||
|
|
||
| oldSlug := provider.Slug | ||
|
|
||
| // Check if new slug is taken by another provider | ||
| if req.Slug != "" && req.Slug != provider.Slug { | ||
| if model.IsSlugTaken(req.Slug, id) { | ||
| common.ApiErrorMsg(c, "该 Slug 已被使用") | ||
| return | ||
| } | ||
| // Check if slug conflicts with built-in providers | ||
| if oauth.IsProviderRegistered(req.Slug) && !oauth.IsCustomProvider(req.Slug) { | ||
| common.ApiErrorMsg(c, "该 Slug 与内置 OAuth 提供商冲突") | ||
| return | ||
| } | ||
| } | ||
|
|
||
| // Update fields | ||
| if req.Name != "" { | ||
| provider.Name = req.Name | ||
| } | ||
| if req.Slug != "" { | ||
| provider.Slug = req.Slug | ||
| } | ||
| provider.Enabled = req.Enabled | ||
| if req.ClientId != "" { | ||
| provider.ClientId = req.ClientId | ||
| } | ||
| if req.ClientSecret != "" { | ||
| provider.ClientSecret = req.ClientSecret | ||
| } | ||
| if req.AuthorizationEndpoint != "" { | ||
| provider.AuthorizationEndpoint = req.AuthorizationEndpoint | ||
| } | ||
| if req.TokenEndpoint != "" { | ||
| provider.TokenEndpoint = req.TokenEndpoint | ||
| } | ||
| if req.UserInfoEndpoint != "" { | ||
| provider.UserInfoEndpoint = req.UserInfoEndpoint | ||
| } | ||
| if req.Scopes != "" { | ||
| provider.Scopes = req.Scopes | ||
| } | ||
| if req.UserIdField != "" { | ||
| provider.UserIdField = req.UserIdField | ||
| } | ||
| if req.UsernameField != "" { | ||
| provider.UsernameField = req.UsernameField | ||
| } | ||
| if req.DisplayNameField != "" { | ||
| provider.DisplayNameField = req.DisplayNameField | ||
| } | ||
| if req.EmailField != "" { | ||
| provider.EmailField = req.EmailField | ||
| } | ||
| provider.WellKnown = req.WellKnown | ||
| provider.AuthStyle = req.AuthStyle | ||
|
|
||
| if err := model.UpdateCustomOAuthProvider(provider); err != nil { | ||
| common.ApiError(c, err) | ||
| return | ||
| } | ||
|
|
||
| // Update the provider in the OAuth registry | ||
| if oldSlug != provider.Slug { | ||
| oauth.UnregisterCustomProvider(oldSlug) | ||
| } | ||
| oauth.RegisterOrUpdateCustomProvider(provider) | ||
|
|
||
| c.JSON(http.StatusOK, gin.H{ | ||
| "success": true, | ||
| "message": "更新成功", | ||
| "data": toCustomOAuthProviderResponse(provider), | ||
| }) | ||
| } | ||
|
|
||
| // DeleteCustomOAuthProvider deletes a custom OAuth provider | ||
| func DeleteCustomOAuthProvider(c *gin.Context) { | ||
| idStr := c.Param("id") | ||
| id, err := strconv.Atoi(idStr) | ||
| if err != nil { | ||
| common.ApiErrorMsg(c, "无效的 ID") | ||
| return | ||
| } | ||
|
|
||
| // Get existing provider to get slug | ||
| provider, err := model.GetCustomOAuthProviderById(id) | ||
| if err != nil { | ||
| common.ApiErrorMsg(c, "未找到该 OAuth 提供商") | ||
| return | ||
| } | ||
|
|
||
| // Check if there are any user bindings | ||
| count, _ := model.GetBindingCountByProviderId(id) | ||
| if count > 0 { | ||
| common.ApiErrorMsg(c, "该 OAuth 提供商还有用户绑定,无法删除。请先解除所有用户绑定。") | ||
| return | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| if err := model.DeleteCustomOAuthProvider(id); err != nil { | ||
| common.ApiError(c, err) | ||
| return | ||
| } | ||
|
|
||
| // Unregister the provider from the OAuth registry | ||
| oauth.UnregisterCustomProvider(provider.Slug) | ||
|
|
||
| c.JSON(http.StatusOK, gin.H{ | ||
| "success": true, | ||
| "message": "删除成功", | ||
| }) | ||
| } | ||
|
|
||
| // GetUserOAuthBindings returns all OAuth bindings for the current user | ||
| func GetUserOAuthBindings(c *gin.Context) { | ||
| userId := c.GetInt("id") | ||
| if userId == 0 { | ||
| common.ApiErrorMsg(c, "未登录") | ||
| return | ||
| } | ||
|
|
||
| bindings, err := model.GetUserOAuthBindingsByUserId(userId) | ||
| if err != nil { | ||
| common.ApiError(c, err) | ||
| return | ||
| } | ||
|
|
||
| // Build response with provider info | ||
| type BindingResponse struct { | ||
| ProviderId int `json:"provider_id"` | ||
| ProviderName string `json:"provider_name"` | ||
| ProviderSlug string `json:"provider_slug"` | ||
| ProviderUserId string `json:"provider_user_id"` | ||
| } | ||
|
|
||
| response := make([]BindingResponse, 0) | ||
| for _, binding := range bindings { | ||
| provider, err := model.GetCustomOAuthProviderById(binding.ProviderId) | ||
| if err != nil { | ||
| continue // Skip if provider not found | ||
| } | ||
| response = append(response, BindingResponse{ | ||
| ProviderId: binding.ProviderId, | ||
| ProviderName: provider.Name, | ||
| ProviderSlug: provider.Slug, | ||
| ProviderUserId: binding.ProviderUserId, | ||
| }) | ||
| } | ||
|
|
||
| c.JSON(http.StatusOK, gin.H{ | ||
| "success": true, | ||
| "message": "", | ||
| "data": response, | ||
| }) | ||
| } | ||
|
|
||
| // UnbindCustomOAuth unbinds a custom OAuth provider from the current user | ||
| func UnbindCustomOAuth(c *gin.Context) { | ||
| userId := c.GetInt("id") | ||
| if userId == 0 { | ||
| common.ApiErrorMsg(c, "未登录") | ||
| return | ||
| } | ||
|
|
||
| providerIdStr := c.Param("provider_id") | ||
| providerId, err := strconv.Atoi(providerIdStr) | ||
| if err != nil { | ||
| common.ApiErrorMsg(c, "无效的提供商 ID") | ||
| return | ||
| } | ||
|
|
||
| if err := model.DeleteUserOAuthBinding(userId, providerId); err != nil { | ||
| common.ApiError(c, err) | ||
| return | ||
| } | ||
|
|
||
| c.JSON(http.StatusOK, gin.H{ | ||
| "success": true, | ||
| "message": "解绑成功", | ||
| }) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.