auth

Paddy 2014-07-18

0:7a6f64db7246 Go to Latest

auth/urivalidate.go

Start rewriting the repo. This code originally was a carbon copy of https://github.com/RangelReale/osin, but I am methodically stripping out the extensible nature of it for a simpler interface, while simultaneously bringing the style into line with the Ducky style.

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/urivalidate.go	Fri Jul 18 07:13:22 2014 -0400
     1.3 @@ -0,0 +1,39 @@
     1.4 +package oauth2
     1.5 +
     1.6 +import (
     1.7 +	"errors"
     1.8 +	"fmt"
     1.9 +	"net/url"
    1.10 +	"strings"
    1.11 +)
    1.12 +
    1.13 +// ValidateURI validates that redirectURI is contained in baseURI
    1.14 +func ValidateURI(baseURI string, redirectURI string) error {
    1.15 +	if baseURI == "" || redirectURI == "" {
    1.16 +		return errors.New("urls cannot be blank.")
    1.17 +	}
    1.18 +
    1.19 +	// parse base url
    1.20 +	base, err := url.Parse(baseURI)
    1.21 +	if err != nil {
    1.22 +		return err
    1.23 +	}
    1.24 +
    1.25 +	// parse passed url
    1.26 +	redirect, err := url.Parse(redirectURI)
    1.27 +	if err != nil {
    1.28 +		return err
    1.29 +	}
    1.30 +
    1.31 +	// must not have fragment
    1.32 +	if base.Fragment != "" || redirect.Fragment != "" {
    1.33 +		return errors.New("url must not include fragment.")
    1.34 +	}
    1.35 +
    1.36 +	// check if urls match
    1.37 +	if base.Scheme == redirect.Scheme && base.Host == redirect.Host && len(redirect.Path) >= len(base.Path) && strings.HasPrefix(redirect.Path, base.Path) {
    1.38 +		return nil
    1.39 +	}
    1.40 +
    1.41 +	return errors.New(fmt.Sprintf("urls don't validate: %s / %s\n", baseURI, redirectURI))
    1.42 +}