github.com/greenpau/go-authcrunch@v1.1.4/pkg/redirects/redirect.go (about) 1 // Copyright 2024 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 redirects 16 17 import ( 18 "fmt" 19 "regexp" 20 "strings" 21 ) 22 23 type matchStrategy int 24 25 const ( 26 matchUnknown matchStrategy = 0 27 matchExact matchStrategy = 1 28 matchPartial matchStrategy = 2 29 matchPrefix matchStrategy = 3 30 matchSuffix matchStrategy = 4 31 matchRegex matchStrategy = 5 32 ) 33 34 // RedirectURIMatchConfig holds the configuration for a redirect URI. 35 type RedirectURIMatchConfig struct { 36 PathMatchType string `json:"path_match_type,omitempty" xml:"path_match_type,omitempty" yaml:"path_match_type,omitempty"` 37 Path string `json:"path,omitempty" xml:"path,omitempty" yaml:"path,omitempty"` 38 DomainMatchType string `json:"domain_match_type,omitempty" xml:"domain_match_type,omitempty" yaml:"domain_match_type,omitempty"` 39 Domain string `json:"domain,omitempty" xml:"domain,omitempty" yaml:"domain,omitempty"` 40 41 pathMatch matchStrategy 42 pathRegex *regexp.Regexp 43 domainMatch matchStrategy 44 domainRegex *regexp.Regexp 45 } 46 47 // NewRedirectURIMatchConfig return an instance of *RedirectURIMatchConfig. 48 func NewRedirectURIMatchConfig(domainMatchType, domain, pathMatchType, path string) (*RedirectURIMatchConfig, error) { 49 c := &RedirectURIMatchConfig{ 50 PathMatchType: strings.TrimSpace(pathMatchType), 51 Path: strings.TrimSpace(path), 52 DomainMatchType: strings.TrimSpace(domainMatchType), 53 Domain: strings.TrimSpace(domain), 54 } 55 if err := c.Validate(); err != nil { 56 return nil, err 57 } 58 return c, nil 59 } 60 61 // Validate validates RedirectURIMatchConfig. 62 func (c *RedirectURIMatchConfig) Validate() error { 63 switch c.PathMatchType { 64 case "exact": 65 c.pathMatch = matchExact 66 case "partial": 67 c.pathMatch = matchPartial 68 case "prefix": 69 c.pathMatch = matchPrefix 70 case "suffix": 71 c.pathMatch = matchSuffix 72 case "regex": 73 c.pathMatch = matchRegex 74 case "": 75 return fmt.Errorf("undefined redirect uri path match type") 76 default: 77 return fmt.Errorf("invalid %q redirect uri path match type", c.PathMatchType) 78 } 79 80 switch c.DomainMatchType { 81 case "exact": 82 c.domainMatch = matchExact 83 case "partial": 84 c.domainMatch = matchPartial 85 case "prefix": 86 c.domainMatch = matchPrefix 87 case "suffix": 88 c.domainMatch = matchSuffix 89 case "regex": 90 c.domainMatch = matchRegex 91 case "": 92 return fmt.Errorf("undefined redirect uri domain name match type") 93 default: 94 return fmt.Errorf("invalid %q redirect uri domain name match type", c.DomainMatchType) 95 } 96 97 c.Path = strings.TrimSpace(c.Path) 98 c.Domain = strings.TrimSpace(c.Domain) 99 100 if c.Path == "" { 101 return fmt.Errorf("undefined redirect uri path") 102 } 103 104 if c.Domain == "" { 105 return fmt.Errorf("undefined redirect uri domain") 106 } 107 108 if c.pathRegex == nil { 109 r, err := regexp.Compile(c.Path) 110 if err != nil { 111 return err 112 } 113 c.pathRegex = r 114 } 115 116 if c.domainRegex == nil { 117 r, err := regexp.Compile(c.Domain) 118 if err != nil { 119 return err 120 } 121 c.domainRegex = r 122 } 123 124 return nil 125 }