Skip to content

Commit

Permalink
sso_*: rename 'options' package to 'validators' (#312)
Browse files Browse the repository at this point in the history
* rename options package

* sso_proxy: "options" pkg is now "validators"

* sso_auth: "options" pkg is now "validators"
  • Loading branch information
Jusshersmith authored Jun 17, 2021
1 parent f02c1fc commit 9c912b1
Show file tree
Hide file tree
Showing 15 changed files with 41 additions and 41 deletions.
8 changes: 4 additions & 4 deletions internal/auth/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import (
"github.com/buzzfeed/sso/internal/auth/providers"
"github.com/buzzfeed/sso/internal/pkg/aead"
log "github.com/buzzfeed/sso/internal/pkg/logging"
"github.com/buzzfeed/sso/internal/pkg/options"
"github.com/buzzfeed/sso/internal/pkg/sessions"
"github.com/buzzfeed/sso/internal/pkg/templates"
"github.com/buzzfeed/sso/internal/pkg/validators"

"github.com/datadog/datadog-go/statsd"
"github.com/gorilla/mux"
)

// Authenticator stores all the information associated with proxying the request.
type Authenticator struct {
Validators []options.Validator
Validators []validators.Validator
EmailDomains []string
ProxyRootDomains []string
Host string
Expand Down Expand Up @@ -228,7 +228,7 @@ func (p *Authenticator) authenticate(rw http.ResponseWriter, req *http.Request)
}
}

errors := options.RunValidators(p.Validators, session)
errors := validators.RunValidators(p.Validators, session)
if len(errors) == len(p.Validators) {
logger.WithUser(session.Email).Info(
fmt.Sprintf("permission denied: unauthorized: %q", errors))
Expand Down Expand Up @@ -584,7 +584,7 @@ func (p *Authenticator) getOAuthCallback(rw http.ResponseWriter, req *http.Reque
// - for p.Validator see validator.go#newValidatorImpl for more info
// - for p.provider.ValidateGroup see providers/google.go#ValidateGroup for more info

errors := options.RunValidators(p.Validators, session)
errors := validators.RunValidators(p.Validators, session)
if len(errors) == len(p.Validators) {
tags := append(tags, "error:invalid_email")
p.StatsdClient.Incr("application_error", tags, 1.0)
Expand Down
14 changes: 7 additions & 7 deletions internal/auth/authenticator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (

"github.com/buzzfeed/sso/internal/auth/providers"
"github.com/buzzfeed/sso/internal/pkg/aead"
"github.com/buzzfeed/sso/internal/pkg/options"
"github.com/buzzfeed/sso/internal/pkg/sessions"
"github.com/buzzfeed/sso/internal/pkg/templates"
"github.com/buzzfeed/sso/internal/pkg/testutil"
"github.com/buzzfeed/sso/internal/pkg/validators"
)

func init() {
Expand Down Expand Up @@ -418,7 +418,7 @@ func TestSignIn(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
config := testConfiguration(t)
auth, err := NewAuthenticator(config,
SetValidators([]options.Validator{options.NewMockValidator(tc.validEmail)}),
SetValidators([]validators.Validator{validators.NewMockValidator(tc.validEmail)}),
setMockSessionStore(tc.mockSessionStore),
setMockTempl(),
setMockRedirectURL(),
Expand Down Expand Up @@ -565,7 +565,7 @@ func TestSignOutPage(t *testing.T) {
provider.RevokeError = tc.RevokeError

p, _ := NewAuthenticator(config,
SetValidators([]options.Validator{options.NewMockValidator(true)}),
SetValidators([]validators.Validator{validators.NewMockValidator(true)}),
setMockSessionStore(tc.mockSessionStore),
setMockTempl(),
setTestProvider(provider),
Expand Down Expand Up @@ -942,7 +942,7 @@ func TestGetProfile(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
config := testConfiguration(t)
p, _ := NewAuthenticator(config,
SetValidators([]options.Validator{options.NewMockValidator(true)}),
SetValidators([]validators.Validator{validators.NewMockValidator(true)}),
)
u, _ := url.Parse("http://example.com")
testProvider := providers.NewTestProvider(u)
Expand Down Expand Up @@ -1044,7 +1044,7 @@ func TestRedeemCode(t *testing.T) {
config := testConfiguration(t)

proxy, _ := NewAuthenticator(config,
SetValidators([]options.Validator{options.NewMockValidator(true)}),
SetValidators([]validators.Validator{validators.NewMockValidator(true)}),
)

testURL, err := url.Parse("example.com")
Expand Down Expand Up @@ -1433,7 +1433,7 @@ func TestOAuthCallback(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
config := testConfiguration(t)
proxy, _ := NewAuthenticator(config,
SetValidators([]options.Validator{options.NewMockValidator(tc.validEmail)}),
SetValidators([]validators.Validator{validators.NewMockValidator(tc.validEmail)}),
setMockCSRFStore(tc.csrfResp),
setMockSessionStore(tc.sessionStore),
)
Expand Down Expand Up @@ -1554,7 +1554,7 @@ func TestOAuthStart(t *testing.T) {
provider := providers.NewTestProvider(nil)
proxy, _ := NewAuthenticator(config,
setTestProvider(provider),
SetValidators([]options.Validator{options.NewMockValidator(true)}),
SetValidators([]validators.Validator{validators.NewMockValidator(true)}),
setMockRedirectURL(),
setMockCSRFStore(&sessions.MockCSRFStore{}),
)
Expand Down
10 changes: 5 additions & 5 deletions internal/auth/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/buzzfeed/sso/internal/pkg/hostmux"
log "github.com/buzzfeed/sso/internal/pkg/logging"
"github.com/buzzfeed/sso/internal/pkg/options"
"github.com/buzzfeed/sso/internal/pkg/validators"

"github.com/datadog/datadog-go/statsd"
"github.com/gorilla/mux"
Expand All @@ -19,11 +19,11 @@ type AuthenticatorMux struct {

func NewAuthenticatorMux(config Configuration, statsdClient *statsd.Client) (*AuthenticatorMux, error) {
logger := log.NewLogEntry()
validators := []options.Validator{}
v := []validators.Validator{}
if len(config.AuthorizeConfig.EmailConfig.Addresses) != 0 {
validators = append(validators, options.NewEmailAddressValidator(config.AuthorizeConfig.EmailConfig.Addresses))
v = append(v, validators.NewEmailAddressValidator(config.AuthorizeConfig.EmailConfig.Addresses))
} else {
validators = append(validators, options.NewEmailDomainValidator(config.AuthorizeConfig.EmailConfig.Domains))
v = append(v, validators.NewEmailDomainValidator(config.AuthorizeConfig.EmailConfig.Domains))
}

authenticators := []*Authenticator{}
Expand All @@ -39,7 +39,7 @@ func NewAuthenticatorMux(config Configuration, statsdClient *statsd.Client) (*Au

idpSlug := idp.Data().ProviderSlug
authenticator, err := NewAuthenticator(config,
SetValidators(validators),
SetValidators(v),
SetProvider(idp),
SetCookieStore(config.SessionConfig, idpSlug),
SetStatsdClient(statsdClient),
Expand Down
4 changes: 2 additions & 2 deletions internal/auth/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/buzzfeed/sso/internal/auth/providers"
"github.com/buzzfeed/sso/internal/pkg/aead"
"github.com/buzzfeed/sso/internal/pkg/groups"
"github.com/buzzfeed/sso/internal/pkg/options"
"github.com/buzzfeed/sso/internal/pkg/sessions"
"github.com/buzzfeed/sso/internal/pkg/validators"

"github.com/datadog/datadog-go/statsd"
)
Expand Down Expand Up @@ -115,7 +115,7 @@ func SetRedirectURL(serverConfig ServerConfig, slug string) func(*Authenticator)
}

// SetValidator sets the email validator
func SetValidators(validators []options.Validator) func(*Authenticator) error {
func SetValidators(validators []validators.Validator) func(*Authenticator) error {
return func(a *Authenticator) error {
a.Validators = validators
return nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package options
package validators

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package options
package validators

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package options
package validators

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package options
package validators

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package options
package validators

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package options
package validators

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package options
package validators

import (
"errors"
Expand Down
10 changes: 5 additions & 5 deletions internal/proxy/oauthproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

"github.com/buzzfeed/sso/internal/pkg/aead"
log "github.com/buzzfeed/sso/internal/pkg/logging"
"github.com/buzzfeed/sso/internal/pkg/options"
"github.com/buzzfeed/sso/internal/pkg/sessions"
"github.com/buzzfeed/sso/internal/pkg/validators"
"github.com/buzzfeed/sso/internal/proxy/providers"

"github.com/datadog/datadog-go/statsd"
Expand Down Expand Up @@ -59,7 +59,7 @@ const statusInvalidHost = 421
// OAuthProxy stores all the information associated with proxying the request.
type OAuthProxy struct {
cookieSecure bool
Validators []options.Validator
Validators []validators.Validator
redirectURL *url.URL // the url to receive requests at
templates *template.Template

Expand Down Expand Up @@ -87,7 +87,7 @@ type StateParameter struct {
func NewOAuthProxy(sc SessionConfig, optFuncs ...func(*OAuthProxy) error) (*OAuthProxy, error) {
p := &OAuthProxy{
cookieSecure: sc.CookieConfig.Secure,
Validators: []options.Validator{},
Validators: []validators.Validator{},

redirectURL: &url.URL{Path: "/oauth2/callback"},
templates: getTemplates(),
Expand Down Expand Up @@ -478,7 +478,7 @@ func (p *OAuthProxy) OAuthCallback(rw http.ResponseWriter, req *http.Request) {
//
// set cookie, or deny

errors := options.RunValidators(p.Validators, session)
errors := validators.RunValidators(p.Validators, session)
if len(errors) == len(p.Validators) {
tags = append(tags, "error:validation_failed")
p.StatsdClient.Incr("application_error", tags, 1.0)
Expand Down Expand Up @@ -718,7 +718,7 @@ func (p *OAuthProxy) Authenticate(rw http.ResponseWriter, req *http.Request) (er
// To reduce strain on upstream identity providers we only revalidate email domains and
// addresses on each request here.
for _, v := range p.Validators {
_, EmailGroupValidator := v.(options.EmailGroupValidator)
_, EmailGroupValidator := v.(validators.EmailGroupValidator)

if !EmailGroupValidator {
err := v.Validate(session)
Expand Down
6 changes: 3 additions & 3 deletions internal/proxy/oauthproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"github.com/mccutchen/go-httpbin/httpbin"

"github.com/buzzfeed/sso/internal/pkg/aead"
"github.com/buzzfeed/sso/internal/pkg/options"
"github.com/buzzfeed/sso/internal/pkg/sessions"
"github.com/buzzfeed/sso/internal/pkg/testutil"
"github.com/buzzfeed/sso/internal/pkg/validators"
"github.com/buzzfeed/sso/internal/proxy/providers"
)

Expand Down Expand Up @@ -121,7 +121,7 @@ func testNewOAuthProxy(t *testing.T, optFuncs ...func(*OAuthProxy) error) (*OAut
statsdClient, _ := statsd.New("127.0.0.1:8125")

standardOptFuncs := []func(*OAuthProxy) error{
SetValidators([]options.Validator{options.NewMockValidator(true)}),
SetValidators([]validators.Validator{validators.NewMockValidator(true)}),
SetProvider(provider),
setSessionStore(&sessions.MockSessionStore{Session: testSession()}),
SetUpstreamConfig(upstreamConfig),
Expand Down Expand Up @@ -279,7 +279,7 @@ func TestAuthOnlyEndpoint(t *testing.T) {

proxy, close := testNewOAuthProxy(t,
setSessionStore(tc.sessionStore),
SetValidators([]options.Validator{options.NewMockValidator(tc.validEmail)}),
SetValidators([]validators.Validator{validators.NewMockValidator(tc.validEmail)}),
SetProvider(tp),
)
defer close()
Expand Down
4 changes: 2 additions & 2 deletions internal/proxy/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"strings"

log "github.com/buzzfeed/sso/internal/pkg/logging"
"github.com/buzzfeed/sso/internal/pkg/options"
"github.com/buzzfeed/sso/internal/pkg/sessions"
"github.com/buzzfeed/sso/internal/pkg/validators"
"github.com/buzzfeed/sso/internal/proxy/providers"

"github.com/datadog/datadog-go/statsd"
Expand Down Expand Up @@ -95,7 +95,7 @@ func SetProxyHandler(handler http.Handler) func(*OAuthProxy) error {
}

// SetValidator sets the email validator as a functional option
func SetValidators(validators []options.Validator) func(*OAuthProxy) error {
func SetValidators(validators []validators.Validator) func(*OAuthProxy) error {
return func(op *OAuthProxy) error {
op.Validators = validators
return nil
Expand Down
12 changes: 6 additions & 6 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"

"github.com/buzzfeed/sso/internal/pkg/hostmux"
"github.com/buzzfeed/sso/internal/pkg/options"
"github.com/buzzfeed/sso/internal/pkg/validators"
"github.com/datadog/datadog-go/statsd"
)

Expand Down Expand Up @@ -45,17 +45,17 @@ func New(config Configuration, statsdClient *statsd.Client) (*SSOProxy, error) {
return nil, err
}

validators := []options.Validator{}
v := []validators.Validator{}
if len(upstreamConfig.AllowedEmailAddresses) != 0 {
validators = append(validators, options.NewEmailAddressValidator(upstreamConfig.AllowedEmailAddresses))
v = append(v, validators.NewEmailAddressValidator(upstreamConfig.AllowedEmailAddresses))
}

if len(upstreamConfig.AllowedEmailDomains) != 0 {
validators = append(validators, options.NewEmailDomainValidator(upstreamConfig.AllowedEmailDomains))
v = append(v, validators.NewEmailDomainValidator(upstreamConfig.AllowedEmailDomains))
}

if len(upstreamConfig.AllowedGroups) != 0 {
validators = append(validators, options.NewEmailGroupValidator(provider, upstreamConfig.AllowedGroups))
v = append(v, validators.NewEmailGroupValidator(provider, upstreamConfig.AllowedGroups))
}

optFuncs = append(optFuncs,
Expand All @@ -64,7 +64,7 @@ func New(config Configuration, statsdClient *statsd.Client) (*SSOProxy, error) {
SetUpstreamConfig(upstreamConfig),
SetProxyHandler(handler),
SetStatsdClient(statsdClient),
SetValidators(validators),
SetValidators(v),
)

oauthproxy, err := NewOAuthProxy(config.SessionConfig, optFuncs...)
Expand Down

0 comments on commit 9c912b1

Please sign in to comment.