forked from mesosphere/traefik-forward-auth
-
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.
[breaking change] wildcard matching improved and additional matching …
…types added Original wildcard prefix match now allows multiple * characters. For increased safety, a single '*' character now matches within one path segment only. To match any number of path segments, two consecutive charaters must be specified '**'. In addition to original "prefix match", full URL match is possible: 1. Wildcard Prefix Match `/admin/*` matches /admin/overview, /admin/users, *not* /admin/users/1 `/admin/**` matches what '/admin/*' matched + also '/admin/users/1' 2. Full URL Wildcard Match `*://a.com/admin` matches http://a.com/admin and https://a.com/admin `*://a.com/**` matches everything under http://a.com/ and https://a.com/ `https://b.com/admin` matches https://b.com/admin only 3. Full URL Regular Expression Match (prefixed by ~ character!) `~^https?://[cd].com/.*` matches everything under http://c.com/, http://d.com/ and their https versions Tests were extended for new functionality and updated for the fact that single '*' now matches within the one path component only. cherry-pick 36c3eee
- Loading branch information
Showing
15 changed files
with
558 additions
and
230 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
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 |
---|---|---|
|
@@ -5,67 +5,71 @@ import ( | |
"github.com/mesosphere/traefik-forward-auth/internal/configuration" | ||
"github.com/mesosphere/traefik-forward-auth/internal/util" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var ( | ||
testAuthKey1 = "4Zhbg4n22r4I8Kdg1gHMzRWQpT7TOArD" | ||
testEncKey1 = "8jAnK6NGuzEuH3y13V+5Bm2jgp5bv8ku" | ||
) | ||
|
||
func newTestConfig(authKey, encKey string) *configuration.Config { | ||
c, _ := configuration.NewConfig([]string{}) | ||
c.SecretString = authKey | ||
c.EncryptionKeyString = encKey | ||
|
||
return c | ||
} | ||
|
||
/** | ||
* Tests | ||
*/ | ||
|
||
func TestAuthValidateCookie(t *testing.T) { | ||
assert := assert.New(t) | ||
config, _ := configuration.NewConfig([]string{}) | ||
config := newTestConfig(testAuthKey1, testEncKey1) | ||
a := NewAuthenticator(config) | ||
r, _ := http.NewRequest("GET", "http://example.com", nil) | ||
c := &http.Cookie{} | ||
|
||
// Should require 3 parts | ||
// Should not accept an empty value | ||
c.Value = "" | ||
_, err := a.ValidateCookie(r, c) | ||
if assert.Error(err) { | ||
assert.Equal("invalid cookie format", err.Error()) | ||
} | ||
c.Value = "1|2" | ||
_, err = a.ValidateCookie(r, c) | ||
if assert.Error(err) { | ||
assert.Equal("invalid cookie format", err.Error()) | ||
} | ||
c.Value = "1|2|3|4" | ||
_, err = a.ValidateCookie(r, c) | ||
if assert.Error(err) { | ||
assert.Equal("invalid cookie format", err.Error()) | ||
assert.Equal("securecookie: the value is not valid", err.Error()) | ||
} | ||
|
||
// Should catch invalid mac | ||
c.Value = "MQ==|2|3" | ||
c.Value = "MQ==" | ||
_, err = a.ValidateCookie(r, c) | ||
if assert.Error(err) { | ||
assert.Equal("invalid cookie mac", err.Error()) | ||
assert.Equal("securecookie: the value is not valid", err.Error()) | ||
} | ||
|
||
// Should catch expired | ||
config.Lifetime = time.Second * time.Duration(-1) | ||
c = a.MakeIDCookie(r, "[email protected]") | ||
a = NewAuthenticator(config) | ||
c = a.MakeIDCookie(r, "[email protected]", "") | ||
_, err = a.ValidateCookie(r, c) | ||
if assert.Error(err) { | ||
assert.Equal("cookie has expired", err.Error()) | ||
assert.Equal("securecookie: expired timestamp", err.Error()) | ||
} | ||
|
||
// Should accept valid cookie | ||
config.Lifetime = time.Second * time.Duration(10) | ||
c = a.MakeIDCookie(r, "[email protected]") | ||
email, err := a.ValidateCookie(r, c) | ||
a = NewAuthenticator(config) | ||
c = a.MakeIDCookie(r, "[email protected]", "") | ||
id, err := a.ValidateCookie(r, c) | ||
assert.Nil(err, "valid request should not return an error") | ||
assert.Equal("[email protected]", email, "valid request should return user email") | ||
assert.Equal("[email protected]", id.Email, "valid request should return user email") | ||
} | ||
|
||
func TestAuthValidateEmail(t *testing.T) { | ||
assert := assert.New(t) | ||
config, _ := configuration.NewConfig([]string{}) | ||
config := newTestConfig(testAuthKey1, testEncKey1) | ||
|
||
a := NewAuthenticator(config) | ||
// Should allow any | ||
|
@@ -106,7 +110,7 @@ func TestAuthValidateEmail(t *testing.T) { | |
// } | ||
|
||
func getConfigWithLifetime() *configuration.Config { | ||
config, _ := configuration.NewConfig([]string{}) | ||
config := newTestConfig(testAuthKey1, testEncKey1) | ||
// Lifetime is set during validation, so we short circuit it here | ||
config.Lifetime = time.Second * time.Duration(config.LifetimeString) | ||
return config | ||
|
@@ -120,10 +124,9 @@ func TestAuthMakeCookie(t *testing.T) { | |
r, _ := http.NewRequest("GET", "http://app.example.com", nil) | ||
r.Header.Add("X-Forwarded-Host", "app.example.com") | ||
|
||
c := a.MakeIDCookie(r, "[email protected]") | ||
c := a.MakeIDCookie(r, "[email protected]", "") | ||
assert.Equal("_forward_auth", c.Name) | ||
parts := strings.Split(c.Value, "|") | ||
assert.Len(parts, 3, "cookie should be 3 parts") | ||
assert.Greater(len(c.Value), 18, "encoded securecookie should be longer") | ||
_, err := a.ValidateCookie(r, c) | ||
assert.Nil(err, "should generate valid cookie") | ||
assert.Equal("/", c.Path) | ||
|
@@ -135,7 +138,7 @@ func TestAuthMakeCookie(t *testing.T) { | |
|
||
config.CookieName = "testname" | ||
config.InsecureCookie = true | ||
c = a.MakeIDCookie(r, "[email protected]") | ||
c = a.MakeIDCookie(r, "[email protected]", "") | ||
assert.Equal("testname", c.Name) | ||
assert.False(c.Secure) | ||
} | ||
|
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,6 +1,8 @@ | ||
package authorization | ||
|
||
import "net/url" | ||
|
||
// Authorizer is the interface for implementing user authorization (check to see if the user can perform the action) | ||
type Authorizer interface { | ||
Authorize(user User, requestVerb, requestResource string) (bool, error) | ||
Authorize(user User, requestVerb string, resource *url.URL) (bool, error) | ||
} |
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
Oops, something went wrong.