37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
githubconfig_actions.go 3.82 KB
一键复制 编辑 原始数据 按行查看 历史
Craig Jellick 提交于 2018-02-15 16:23 . Use apierrors in auth
package github
import (
"encoding/json"
"fmt"
"net/http"
"github.com/pkg/errors"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/rancher/pkg/auth/providers/common"
"github.com/rancher/rancher/pkg/auth/tokens"
"github.com/rancher/types/apis/management.cattle.io/v3public"
"github.com/rancher/types/client/management/v3"
)
func (g *ghProvider) formatter(apiContext *types.APIContext, resource *types.RawResource) {
common.AddCommonActions(apiContext, resource)
resource.AddAction(apiContext, "configureTest")
resource.AddAction(apiContext, "testAndApply")
}
func (g *ghProvider) actionHandler(actionName string, action *types.Action, request *types.APIContext) error {
handled, err := common.HandleCommonAction(actionName, action, request, Name, g.authConfigs)
if err != nil {
return err
}
if handled {
return nil
}
if actionName == "configureTest" {
return g.configureTest(actionName, action, request)
} else if actionName == "testAndApply" {
return g.testAndApply(actionName, action, request)
}
return httperror.NewAPIError(httperror.ActionNotAvailable, "")
}
func (g *ghProvider) configureTest(actionName string, action *types.Action, request *types.APIContext) error {
githubConfig := &v3.GithubConfig{}
if err := json.NewDecoder(request.Request.Body).Decode(githubConfig); err != nil {
return httperror.NewAPIError(httperror.InvalidBodyContent,
fmt.Sprintf("Failed to parse body: %v", err))
}
redirectURL := formGithubRedirectURL(githubConfig)
data := map[string]interface{}{
"redirectUrl": redirectURL,
"type": "githubConfigTestOutput",
}
request.WriteResponse(http.StatusOK, data)
return nil
}
func formGithubRedirectURL(githubConfig *v3.GithubConfig) string {
return githubRedirectURL(githubConfig.Hostname, githubConfig.ClientID, githubConfig.TLS)
}
func formGithubRedirectURLFromMap(config map[string]interface{}) string {
hostname, _ := config[client.GithubConfigFieldHostname].(string)
clientID, _ := config[client.GithubConfigFieldClientID].(string)
tls, _ := config[client.GithubConfigFieldTLS].(bool)
return githubRedirectURL(hostname, clientID, tls)
}
func githubRedirectURL(hostname, clientID string, tls bool) string {
redirect := ""
if hostname != "" {
scheme := "http://"
if tls {
scheme = "https://"
}
redirect = scheme + hostname
} else {
redirect = githubDefaultHostName
}
redirect = redirect + "/login/oauth/authorize?client_id=" + clientID
return redirect
}
func (g *ghProvider) testAndApply(actionName string, action *types.Action, request *types.APIContext) error {
var githubConfig v3.GithubConfig
githubConfigApplyInput := &v3.GithubConfigApplyInput{}
if err := json.NewDecoder(request.Request.Body).Decode(githubConfigApplyInput); err != nil {
return httperror.NewAPIError(httperror.InvalidBodyContent,
fmt.Sprintf("Failed to parse body: %v", err))
}
githubConfig = githubConfigApplyInput.GithubConfig
githubLogin := &v3public.GithubLogin{
Code: githubConfigApplyInput.Code,
}
//Call provider to testLogin
userPrincipal, groupPrincipals, providerInfo, err := g.LoginUser(githubLogin, &githubConfig)
if err != nil {
if httperror.IsAPIError(err) {
return err
}
return errors.Wrap(err, "server error while authenticating")
}
//if this works, save githubConfig CR adding enabled flag
githubConfig.Enabled = githubConfigApplyInput.Enabled
err = g.saveGithubConfig(&githubConfig)
if err != nil {
return httperror.NewAPIError(httperror.ServerError, fmt.Sprintf("Failed to save github config: %v", err))
}
user, err := g.userMGR.SetPrincipalOnCurrentUser(request, userPrincipal)
if err != nil {
return err
}
return tokens.CreateTokenAndSetCookie(user.Name, userPrincipal, groupPrincipals, providerInfo, 0, "Token via Githu Configuration", request)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.0-alpha17

搜索帮助