2015-03-15 16:23:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-11-16 03:08:30 +00:00
|
|
|
"crypto"
|
2016-07-19 19:51:25 +00:00
|
|
|
"fmt"
|
2015-03-17 19:15:15 +00:00
|
|
|
"net/url"
|
2015-03-15 16:23:13 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2015-05-09 21:16:19 +00:00
|
|
|
"time"
|
2015-03-15 16:23:13 +00:00
|
|
|
|
2017-10-23 16:23:46 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-03-15 16:23:13 +00:00
|
|
|
)
|
|
|
|
|
2015-03-17 19:15:15 +00:00
|
|
|
func testOptions() *Options {
|
2015-03-15 16:23:13 +00:00
|
|
|
o := NewOptions()
|
|
|
|
o.Upstreams = append(o.Upstreams, "http://127.0.0.1:8080/")
|
|
|
|
o.CookieSecret = "foobar"
|
|
|
|
o.ClientID = "bazquux"
|
|
|
|
o.ClientSecret = "xyzzyplugh"
|
2015-07-24 20:23:19 +00:00
|
|
|
o.EmailDomains = []string{"*"}
|
2015-03-15 16:23:13 +00:00
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
2015-03-17 19:15:15 +00:00
|
|
|
func errorMsg(msgs []string) string {
|
2015-03-15 16:23:13 +00:00
|
|
|
result := make([]string, 0)
|
|
|
|
result = append(result, "Invalid configuration:")
|
|
|
|
result = append(result, msgs...)
|
|
|
|
return strings.Join(result, "\n ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewOptions(t *testing.T) {
|
|
|
|
o := NewOptions()
|
2015-07-24 20:23:19 +00:00
|
|
|
o.EmailDomains = []string{"*"}
|
2015-03-15 16:23:13 +00:00
|
|
|
err := o.Validate()
|
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
|
|
|
|
expected := errorMsg([]string{
|
|
|
|
"missing setting: cookie-secret",
|
|
|
|
"missing setting: client-id",
|
|
|
|
"missing setting: client-secret"})
|
|
|
|
assert.Equal(t, expected, err.Error())
|
|
|
|
}
|
|
|
|
|
2015-08-20 10:07:02 +00:00
|
|
|
func TestGoogleGroupOptions(t *testing.T) {
|
|
|
|
o := testOptions()
|
|
|
|
o.GoogleGroups = []string{"googlegroup"}
|
|
|
|
err := o.Validate()
|
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
|
|
|
|
expected := errorMsg([]string{
|
|
|
|
"missing setting: google-admin-email",
|
|
|
|
"missing setting: google-service-account-json"})
|
|
|
|
assert.Equal(t, expected, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGoogleGroupInvalidFile(t *testing.T) {
|
|
|
|
o := testOptions()
|
|
|
|
o.GoogleGroups = []string{"test_group"}
|
|
|
|
o.GoogleAdminEmail = "admin@example.com"
|
|
|
|
o.GoogleServiceAccountJSON = "file_doesnt_exist.json"
|
|
|
|
err := o.Validate()
|
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
|
|
|
|
expected := errorMsg([]string{
|
|
|
|
"invalid Google credentials file: file_doesnt_exist.json",
|
|
|
|
})
|
|
|
|
assert.Equal(t, expected, err.Error())
|
|
|
|
}
|
|
|
|
|
2015-03-15 16:23:13 +00:00
|
|
|
func TestInitializedOptions(t *testing.T) {
|
|
|
|
o := testOptions()
|
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that it's not worth testing nonparseable URLs, since url.Parse()
|
|
|
|
// seems to parse damn near anything.
|
2015-11-08 23:47:44 +00:00
|
|
|
func TestRedirectURL(t *testing.T) {
|
2015-03-15 16:23:13 +00:00
|
|
|
o := testOptions()
|
2015-11-08 23:47:44 +00:00
|
|
|
o.RedirectURL = "https://myhost.com/oauth2/callback"
|
2015-03-15 16:23:13 +00:00
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
expected := &url.URL{
|
|
|
|
Scheme: "https", Host: "myhost.com", Path: "/oauth2/callback"}
|
2015-11-08 23:47:44 +00:00
|
|
|
assert.Equal(t, expected, o.redirectURL)
|
2015-03-15 16:23:13 +00:00
|
|
|
}
|
|
|
|
|
2015-11-08 23:47:44 +00:00
|
|
|
func TestProxyURLs(t *testing.T) {
|
2015-03-15 16:23:13 +00:00
|
|
|
o := testOptions()
|
|
|
|
o.Upstreams = append(o.Upstreams, "http://127.0.0.1:8081")
|
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
expected := []*url.URL{
|
|
|
|
&url.URL{Scheme: "http", Host: "127.0.0.1:8080", Path: "/"},
|
|
|
|
// note the '/' was added
|
|
|
|
&url.URL{Scheme: "http", Host: "127.0.0.1:8081", Path: "/"},
|
|
|
|
}
|
2015-11-08 23:47:44 +00:00
|
|
|
assert.Equal(t, expected, o.proxyURLs)
|
2015-03-15 16:23:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompiledRegex(t *testing.T) {
|
|
|
|
o := testOptions()
|
|
|
|
regexps := []string{"/foo/.*", "/ba[rz]/quux"}
|
|
|
|
o.SkipAuthRegex = regexps
|
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
actual := make([]string, 0)
|
|
|
|
for _, regex := range o.CompiledRegex {
|
|
|
|
actual = append(actual, regex.String())
|
|
|
|
}
|
|
|
|
assert.Equal(t, regexps, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCompiledRegexError(t *testing.T) {
|
|
|
|
o := testOptions()
|
|
|
|
o.SkipAuthRegex = []string{"(foobaz", "barquux)"}
|
|
|
|
err := o.Validate()
|
|
|
|
assert.NotEqual(t, nil, err)
|
|
|
|
|
|
|
|
expected := errorMsg([]string{
|
|
|
|
"error compiling regex=\"(foobaz\" error parsing regexp: " +
|
|
|
|
"missing closing ): `(foobaz`",
|
|
|
|
"error compiling regex=\"barquux)\" error parsing regexp: " +
|
|
|
|
"unexpected ): `barquux)`"})
|
|
|
|
assert.Equal(t, expected, err.Error())
|
|
|
|
}
|
2015-03-30 19:48:30 +00:00
|
|
|
|
|
|
|
func TestDefaultProviderApiSettings(t *testing.T) {
|
|
|
|
o := testOptions()
|
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
p := o.provider.Data()
|
2015-06-23 17:23:47 +00:00
|
|
|
assert.Equal(t, "https://accounts.google.com/o/oauth2/auth?access_type=offline",
|
2015-11-08 23:47:44 +00:00
|
|
|
p.LoginURL.String())
|
2015-06-23 17:23:47 +00:00
|
|
|
assert.Equal(t, "https://www.googleapis.com/oauth2/v3/token",
|
2015-11-08 23:47:44 +00:00
|
|
|
p.RedeemURL.String())
|
|
|
|
assert.Equal(t, "", p.ProfileURL.String())
|
2015-03-30 19:48:30 +00:00
|
|
|
assert.Equal(t, "profile email", p.Scope)
|
|
|
|
}
|
2015-04-05 13:43:40 +00:00
|
|
|
|
|
|
|
func TestPassAccessTokenRequiresSpecificCookieSecretLengths(t *testing.T) {
|
|
|
|
o := testOptions()
|
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
|
|
|
|
assert.Equal(t, false, o.PassAccessToken)
|
|
|
|
o.PassAccessToken = true
|
|
|
|
o.CookieSecret = "cookie of invalid length-"
|
|
|
|
assert.NotEqual(t, nil, o.Validate())
|
|
|
|
|
2015-05-09 21:31:13 +00:00
|
|
|
o.PassAccessToken = false
|
|
|
|
o.CookieRefresh = time.Duration(24) * time.Hour
|
|
|
|
assert.NotEqual(t, nil, o.Validate())
|
|
|
|
|
2015-04-05 13:43:40 +00:00
|
|
|
o.CookieSecret = "16 bytes AES-128"
|
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
|
|
|
|
o.CookieSecret = "24 byte secret AES-192--"
|
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
|
|
|
|
o.CookieSecret = "32 byte secret for AES-256------"
|
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
}
|
2015-05-09 21:16:19 +00:00
|
|
|
|
|
|
|
func TestCookieRefreshMustBeLessThanCookieExpire(t *testing.T) {
|
|
|
|
o := testOptions()
|
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
|
2016-06-20 11:17:39 +00:00
|
|
|
o.CookieSecret = "0123456789abcdefabcd"
|
2015-05-09 21:16:19 +00:00
|
|
|
o.CookieRefresh = o.CookieExpire
|
|
|
|
assert.NotEqual(t, nil, o.Validate())
|
|
|
|
|
|
|
|
o.CookieRefresh -= time.Duration(1)
|
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
}
|
2015-11-16 03:08:30 +00:00
|
|
|
|
2016-06-20 11:17:39 +00:00
|
|
|
func TestBase64CookieSecret(t *testing.T) {
|
|
|
|
o := testOptions()
|
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
|
|
|
|
// 32 byte, base64 (urlsafe) encoded key
|
|
|
|
o.CookieSecret = "yHBw2lh2Cvo6aI_jn_qMTr-pRAjtq0nzVgDJNb36jgQ="
|
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
|
|
|
|
// 32 byte, base64 (urlsafe) encoded key, w/o padding
|
|
|
|
o.CookieSecret = "yHBw2lh2Cvo6aI_jn_qMTr-pRAjtq0nzVgDJNb36jgQ"
|
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
|
|
|
|
// 24 byte, base64 (urlsafe) encoded key
|
|
|
|
o.CookieSecret = "Kp33Gj-GQmYtz4zZUyUDdqQKx5_Hgkv3"
|
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
|
|
|
|
// 16 byte, base64 (urlsafe) encoded key
|
|
|
|
o.CookieSecret = "LFEqZYvYUwKwzn0tEuTpLA=="
|
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
|
|
|
|
// 16 byte, base64 (urlsafe) encoded key, w/o padding
|
|
|
|
o.CookieSecret = "LFEqZYvYUwKwzn0tEuTpLA"
|
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
}
|
|
|
|
|
2015-11-16 03:08:30 +00:00
|
|
|
func TestValidateSignatureKey(t *testing.T) {
|
|
|
|
o := testOptions()
|
|
|
|
o.SignatureKey = "sha1:secret"
|
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
assert.Equal(t, o.signatureData.hash, crypto.SHA1)
|
|
|
|
assert.Equal(t, o.signatureData.key, "secret")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateSignatureKeyInvalidSpec(t *testing.T) {
|
|
|
|
o := testOptions()
|
|
|
|
o.SignatureKey = "invalid spec"
|
|
|
|
err := o.Validate()
|
|
|
|
assert.Equal(t, err.Error(), "Invalid configuration:\n"+
|
|
|
|
" invalid signature hash:key spec: "+o.SignatureKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateSignatureKeyUnsupportedAlgorithm(t *testing.T) {
|
|
|
|
o := testOptions()
|
|
|
|
o.SignatureKey = "unsupported:default secret"
|
|
|
|
err := o.Validate()
|
|
|
|
assert.Equal(t, err.Error(), "Invalid configuration:\n"+
|
|
|
|
" unsupported signature hash algorithm: "+o.SignatureKey)
|
|
|
|
}
|
2016-07-19 19:51:25 +00:00
|
|
|
|
|
|
|
func TestValidateCookie(t *testing.T) {
|
|
|
|
o := testOptions()
|
|
|
|
o.CookieName = "_valid_cookie_name"
|
|
|
|
assert.Equal(t, nil, o.Validate())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateCookieBadName(t *testing.T) {
|
|
|
|
o := testOptions()
|
|
|
|
o.CookieName = "_bad_cookie_name{}"
|
|
|
|
err := o.Validate()
|
|
|
|
assert.Equal(t, err.Error(), "Invalid configuration:\n"+
|
|
|
|
fmt.Sprintf(" invalid cookie name: %q", o.CookieName))
|
|
|
|
}
|