From 7ffbf5dd988272cf35a0d656d96e009842ed0934 Mon Sep 17 00:00:00 2001 From: Jared Scheib Date: Thu, 26 Oct 2017 15:46:06 -0700 Subject: [PATCH] Move Mock Authenticator into mocks package Signed-off-by: Michael de Sa --- mocks/auth.go | 51 +++++++++++++++++++++++++++++++++++++++++++++ server/auth_test.go | 36 +------------------------------- 2 files changed, 52 insertions(+), 35 deletions(-) create mode 100644 mocks/auth.go diff --git a/mocks/auth.go b/mocks/auth.go new file mode 100644 index 000000000..3cadb520f --- /dev/null +++ b/mocks/auth.go @@ -0,0 +1,51 @@ +package mocks + +import ( + "context" + "net/http" + + "github.com/influxdata/chronograf/oauth2" +) + +// Authenticator implements a OAuth2 authenticator +type Authenticator struct { + Principal oauth2.Principal + ValidateErr error + ExtendErr error + Serialized string +} + +// Validate returns Principal associated with authenticated and authorized +// entity if successful. +func (a *Authenticator) Validate(context.Context, *http.Request) (oauth2.Principal, error) { + return a.Principal, a.ValidateErr +} + +// Extend will extend the lifetime of a already validated Principal +func (a *Authenticator) Extend(ctx context.Context, w http.ResponseWriter, p oauth2.Principal) (oauth2.Principal, error) { + cookie := http.Cookie{} + + http.SetCookie(w, &cookie) + return a.Principal, a.ExtendErr +} + +// Authorize will grant privileges to a Principal +func (a *Authenticator) Authorize(ctx context.Context, w http.ResponseWriter, p oauth2.Principal) error { + cookie := http.Cookie{} + + http.SetCookie(w, &cookie) + return nil +} + +// Expire revokes privileges from a Principal +func (a *Authenticator) Expire(http.ResponseWriter) {} + +// ValidAuthorization returns the Principal +func (a *Authenticator) ValidAuthorization(ctx context.Context, serializedAuthorization string) (oauth2.Principal, error) { + return oauth2.Principal{}, nil +} + +// Serialize the serialized values stored on the Authenticator +func (a *Authenticator) Serialize(context.Context, oauth2.Principal) (string, error) { + return a.Serialized, nil +} diff --git a/server/auth_test.go b/server/auth_test.go index 5b5a2b756..832ee4739 100644 --- a/server/auth_test.go +++ b/server/auth_test.go @@ -15,40 +15,6 @@ import ( "github.com/influxdata/chronograf/server" ) -type MockAuthenticator struct { - Principal oauth2.Principal - ValidateErr error - ExtendErr error - Serialized string -} - -func (m *MockAuthenticator) Validate(context.Context, *http.Request) (oauth2.Principal, error) { - return m.Principal, m.ValidateErr -} - -func (m *MockAuthenticator) Extend(ctx context.Context, w http.ResponseWriter, p oauth2.Principal) (oauth2.Principal, error) { - cookie := http.Cookie{} - - http.SetCookie(w, &cookie) - return m.Principal, m.ExtendErr -} - -func (m *MockAuthenticator) Authorize(ctx context.Context, w http.ResponseWriter, p oauth2.Principal) error { - cookie := http.Cookie{} - - http.SetCookie(w, &cookie) - return nil -} - -func (m *MockAuthenticator) Expire(http.ResponseWriter) {} - -func (m *MockAuthenticator) ValidAuthorization(ctx context.Context, serializedAuthorization string) (oauth2.Principal, error) { - return oauth2.Principal{}, nil -} -func (m *MockAuthenticator) Serialize(context.Context, oauth2.Principal) (string, error) { - return m.Serialized, nil -} - func TestAuthorizedToken(t *testing.T) { var tests = []struct { Desc string @@ -82,7 +48,7 @@ func TestAuthorizedToken(t *testing.T) { req, _ := http.NewRequest("GET", "", nil) w := httptest.NewRecorder() - a := &MockAuthenticator{ + a := &mocks.Authenticator{ Principal: test.Principal, ValidateErr: test.ValidateErr, }