forked from openshift/osin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urivalidate.go
50 lines (44 loc) · 1.1 KB
/
urivalidate.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
package osin
import (
"fmt"
"net/http"
"net/url"
"strings"
)
func ValidateUri(baseUri string, redirectUri string) *HttpError {
if baseUri == "" || redirectUri == "" {
return &HttpError{
Status: http.StatusBadRequest,
Message: "urls cannot be blank.",
}
}
base, err := url.Parse(baseUri)
if err != nil {
return &HttpError{
Status: http.StatusBadRequest,
Message: "Url parse error: " + err.Error(),
}
}
redirect, err := url.Parse(redirectUri)
if err != nil {
return &HttpError{
Status: http.StatusBadRequest,
Message: "Redirect url parse error: " + err.Error(),
}
}
if base.Fragment != "" || redirect.Fragment != "" {
return &HttpError{
Status: http.StatusBadRequest,
Message: "Url must not include fragment.",
}
}
validRedirect := base.Scheme == redirect.Scheme && base.Host == redirect.Host &&
len(redirect.Path) >= len(base.Path) && strings.HasPrefix(redirect.Path, base.Path)
if !validRedirect {
return &HttpError{
Status: http.StatusUnauthorized,
Message: fmt.Sprintf("Urls don't validate: %s / %s", baseUri, redirectUri),
}
}
return nil
}