-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Philip Reichenberger
committed
Jun 12, 2018
1 parent
a88f3e0
commit 88dd4a1
Showing
7 changed files
with
133 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
package redirector | ||
|
||
type ValidationError struct { | ||
type Error struct { | ||
Message string | ||
} | ||
|
||
func (e ValidationError) Error() string { | ||
func (e Error) Error() string { | ||
return e.Message | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package redirector | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
type Redirector struct { | ||
WhitelistDomains []string | ||
} | ||
|
||
func validateDomainParts(a, b []string) bool { | ||
if len(a) != len(b) { | ||
return false | ||
} | ||
|
||
for i, _ := range a { | ||
if a[i] == "*" || b[i] == "*" { | ||
continue | ||
} | ||
|
||
if a[i] != b[i] { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
func NewRedirector(whitelist string) *Redirector { | ||
whitelistDomains := strings.Split(whitelist, ",") | ||
|
||
return &Redirector{ | ||
WhitelistDomains: whitelistDomains, | ||
} | ||
} | ||
|
||
func (r *Redirector) ValidateDomain(host string) bool { | ||
for _, whitelistDomain := range r.WhitelistDomains { | ||
whitelistDomainParts := strings.Split(whitelistDomain, ".") | ||
hostParts := strings.Split(host, ".") | ||
if whitelistDomain == "" { | ||
return true | ||
} | ||
|
||
if !validateDomainParts(whitelistDomainParts, hostParts) { | ||
continue | ||
} | ||
|
||
return true | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package redirector | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
) | ||
|
||
func TestValidateDomain(t *testing.T) { | ||
r := NewRedirector("") | ||
if !r.ValidateDomain("pizza") { | ||
t.Error(errors.New("Empty whitelist failed")) | ||
} | ||
} | ||
|
||
func TestValidateDomainWildcard(t *testing.T) { | ||
r := NewRedirector("google.com,apple.com,*.github.com") | ||
if !r.ValidateDomain("api.github.com") { | ||
t.Error(errors.New("Wildcard domain validation failed")) | ||
} | ||
} | ||
|
||
func TestValidateDomainNested(t *testing.T) { | ||
r := NewRedirector("google.com,*.api.apple.com,*.github.com") | ||
if !r.ValidateDomain("test.api.apple.com") { | ||
t.Error(errors.New("Nested domain validation failed")) | ||
} | ||
} | ||
|
||
func TestValidateDomainFail(t *testing.T) { | ||
r := NewRedirector("google.com,*.api.apple.com,*.github.com") | ||
if r.ValidateDomain("test.api.github.com") { | ||
t.Error(errors.New("Validate domain did not fail")) | ||
} | ||
if r.ValidateDomain("api.apple.com") { | ||
t.Error(errors.New("Validate domain did not fail")) | ||
} | ||
if r.ValidateDomain("test.google.com") { | ||
t.Error(errors.New("Validate domain did not fail")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters