forked from openshift/osin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urivalidate.go
39 lines (32 loc) · 887 Bytes
/
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
package osin
import (
"errors"
"fmt"
"net/url"
"strings"
)
// ValidateUri validates that redirectUri is contained in baseUri
func ValidateUri(baseUri string, redirectUri string) error {
if baseUri == "" || redirectUri == "" {
return errors.New("urls cannot be blank.")
}
// parse base url
base, err := url.Parse(baseUri)
if err != nil {
return err
}
// parse passed url
redirect, err := url.Parse(redirectUri)
if err != nil {
return err
}
// must not have fragment
if base.Fragment != "" || redirect.Fragment != "" {
return errors.New("url must not include fragment.")
}
// check if urls match
if base.Scheme == redirect.Scheme && base.Host == redirect.Host && len(redirect.Path) >= len(base.Path) && strings.HasPrefix(redirect.Path, base.Path) {
return nil
}
return errors.New(fmt.Sprintf("urls don't validate: %s / %s\n", baseUri, redirectUri))
}