-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
redirect_parser.go
87 lines (81 loc) · 2.55 KB
/
redirect_parser.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2024 Paul Greenberg greenpau@outlook.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package redirects
import (
"fmt"
"net/url"
"strings"
)
// ParseRedirectURI parses redirect_uri from URL string.
func ParseRedirectURI(s string) (*url.URL, error) {
parsedURL, err := url.Parse(s)
if err != nil {
return nil, fmt.Errorf("failed to parse base uri")
}
if parsedURL.Scheme == "" || parsedURL.Host == "" || parsedURL.Path == "" {
return nil, fmt.Errorf("non compliant base uri")
}
if !HasRedirectURI(parsedURL) {
return nil, fmt.Errorf("redirect uri is not url")
}
queryParams := parsedURL.Query()
rawRedirectURI := queryParams.Get("redirect_uri")
if strings.HasPrefix(rawRedirectURI, "/") {
return nil, fmt.Errorf("redirect uri has no scheme and host")
}
parsedRedirectURI, err := url.Parse(rawRedirectURI)
if err != nil {
return nil, fmt.Errorf("failed to parse redirect uri")
}
if parsedRedirectURI.Scheme == "" || parsedRedirectURI.Host == "" || parsedRedirectURI.Path == "" {
return nil, fmt.Errorf("non compliant redirect uri")
}
return parsedRedirectURI, nil
}
// GetRedirectURI returns redirect_uri value from query parameters.
func GetRedirectURI(u *url.URL) *url.URL {
queryParams := u.Query()
if queryParams == nil {
return nil
}
rawRedirectURI := queryParams.Get("redirect_uri")
parsedRedirectURI, err := url.Parse(rawRedirectURI)
if err != nil {
return nil
}
if parsedRedirectURI.Scheme == "" || parsedRedirectURI.Host == "" || parsedRedirectURI.Path == "" {
return nil
}
return parsedRedirectURI
}
// GetRawRedirectURI returns raw redirect_uri value from query parameters.
func GetRawRedirectURI(u *url.URL) string {
queryParams := u.Query()
if queryParams == nil {
return ""
}
return queryParams.Get("redirect_uri")
}
// HasRedirectURI check whether URL has redirect_uri in query parameters.
func HasRedirectURI(u *url.URL) bool {
queryParams := u.Query()
if queryParams == nil {
return false
}
rawRedirectURI := queryParams.Get("redirect_uri")
if rawRedirectURI == "" {
return false
}
return true
}