forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
saml_actions.go
71 lines (58 loc) · 2.09 KB
/
saml_actions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package saml
import (
"encoding/json"
"fmt"
"net/http"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/auth/providers/common"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
)
func (s *Provider) formatter(apiContext *types.APIContext, resource *types.RawResource) {
common.AddCommonActions(apiContext, resource)
resource.AddAction(apiContext, "testAndEnable")
}
func (s *Provider) actionHandler(actionName string, action *types.Action, request *types.APIContext) error {
handled, err := common.HandleCommonAction(actionName, action, request, s.name, s.authConfigs)
if err != nil {
return err
}
if handled {
return nil
}
if actionName == "testAndEnable" {
return s.testAndEnable(actionName, action, request)
}
return httperror.NewAPIError(httperror.ActionNotAvailable, "")
}
func (s *Provider) testAndEnable(actionName string, action *types.Action, request *types.APIContext) error {
// get Final redirect URL from request body
samlLogin := &v3.SamlConfigTestInput{}
if err := json.NewDecoder(request.Request.Body).Decode(samlLogin); err != nil {
return httperror.NewAPIError(httperror.InvalidBodyContent,
fmt.Sprintf("SAML: Failed to parse body: %v", err))
}
samlConfig, err := s.getSamlConfig()
if err != nil {
return err
}
err = InitializeSamlServiceProvider(samlConfig, s.name)
if err != nil {
return err
}
provider := SamlProviders[s.name]
finalRedirectURL := samlLogin.FinalRedirectURL
provider.clientState.SetState(request.Response, request.Request, "Rancher_UserID", provider.userMGR.GetUser(request))
provider.clientState.SetState(request.Response, request.Request, "Rancher_FinalRedirectURL", finalRedirectURL)
provider.clientState.SetState(request.Response, request.Request, "Rancher_Action", testAndEnableAction)
idpRedirectURL, err := provider.HandleSamlLogin(request.Response, request.Request)
if err != nil {
return err
}
data := map[string]interface{}{
"idpRedirectUrl": idpRedirectURL,
"type": "samlConfigTestOutput",
}
request.WriteResponse(http.StatusOK, data)
return nil
}