github.com/greenpau/go-authcrunch@v1.1.4/pkg/sso/request.go (about) 1 // Copyright 2022 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package sso 16 17 import ( 18 "net/http" 19 "strings" 20 21 "github.com/greenpau/go-authcrunch/pkg/errors" 22 ) 23 24 // RequestKind represents the type of SSO provider request. 25 type RequestKind int 26 27 const ( 28 ssoPrefix = "/apps/sso/" 29 metadataKeyword = "/metadata.xml" 30 ssumeKeyword = "/assume" 31 32 // UnknownRequest represents unspecified request type. 33 UnknownRequest RequestKind = iota 34 // MetadataRequest represents metadata request type. 35 MetadataRequest 36 // AssumeRoleRequest represents role assumption request type. 37 AssumeRoleRequest 38 // MenuRequest represents role selection request type. 39 MenuRequest 40 ) 41 42 // Request represents sso provider request. 43 type Request struct { 44 ProviderName string `json:"provider_name,omitempty" xml:"provider_name,omitempty" yaml:"provider_name,omitempty"` 45 Kind RequestKind `json:"kind,omitempty" xml:"kind,omitempty" yaml:"kind,omitempty"` 46 Params string `json:"params,omitempty" xml:"params,omitempty" yaml:"params,omitempty"` 47 } 48 49 // ParseRequestURL extracts provider name and request type from URL. 50 func ParseRequestURL(r *http.Request) (*Request, error) { 51 req := &Request{} 52 53 s := r.URL.Path 54 i := strings.Index(s, ssoPrefix) 55 if i < 0 { 56 return nil, errors.ErrSingleSignOnProviderRequestMalformed 57 } 58 s = strings.TrimPrefix(s[i:], ssoPrefix) 59 60 parts := strings.SplitN(s, "/", 2) 61 req.ProviderName = parts[0] 62 63 if len(parts) == 1 { 64 if parts[0] == "" { 65 return nil, errors.ErrSingleSignOnProviderRequestMalformed 66 } 67 req.Kind = MenuRequest 68 return req, nil 69 } 70 71 if strings.HasPrefix(parts[1], "assume/") { 72 params := strings.TrimPrefix(parts[1], "assume/") 73 if params == "" { 74 return nil, errors.ErrSingleSignOnProviderRequestMalformed 75 } 76 req.Params = params 77 req.Kind = AssumeRoleRequest 78 return req, nil 79 } 80 81 if parts[1] == "metadata.xml" { 82 req.Kind = MetadataRequest 83 return req, nil 84 } 85 86 return nil, errors.ErrSingleSignOnProviderRequestMalformed 87 }