From fd896de270466092fabe0ca39462505a4d91667b Mon Sep 17 00:00:00 2001 From: reesporte Date: Wed, 5 Jan 2022 12:10:33 -0600 Subject: [PATCH] rename CookieValue to AuthContext because we're not using cookies anymore --- authn/authenticate.go | 24 ++++++++++++------------ authn/authenticate_internal_test.go | 10 +++++----- http/handler_internal_test.go | 6 +++--- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/authn/authenticate.go b/authn/authenticate.go index af95e953d..a5567842b 100644 --- a/authn/authenticate.go +++ b/authn/authenticate.go @@ -67,8 +67,8 @@ func NewAuth(logger logger.Logger, url string, scopes []string, authURL, tokenUR return auth, nil } -// CookieValue holds the value of an authenticated user's cookie -type CookieValue struct { +// AuthContext holds the value of an authenticated user's cookie +type AuthContext struct { UserID string UserName string GroupMembership []Group @@ -145,7 +145,7 @@ func (a *Auth) Redirect(w http.ResponseWriter, r *http.Request) { return } - cv, err := a.newCookieValue(token) + cv, err := a.newAuthContext(token) if err != nil || cv == nil { a.logger.Warnf("creating cookie: %+v", err) http.Error(w, "Bad Request: 400", http.StatusBadRequest) @@ -180,8 +180,8 @@ func (a *Auth) getToken(r *http.Request, code string) (*oauth2.Token, error) { return token, nil } -// newCookieValue parses a jwt `token` and returns relevant information in a cookie value struct -func (a *Auth) newCookieValue(token *oauth2.Token) (*CookieValue, error) { +// newAuthContext parses a jwt `token` and returns relevant information in a cookie value struct +func (a *Auth) newAuthContext(token *oauth2.Token) (*AuthContext, error) { if token == nil { return nil, errors.New("baking cookie due to nil token") } @@ -205,7 +205,7 @@ func (a *Auth) newCookieValue(token *oauth2.Token) (*CookieValue, error) { } // not needed at this point in the logic and makes the encoded cookie too large token.AccessToken = "" - return &CookieValue{ + return &AuthContext{ UserID: claims["oid"].(string), UserName: claims["name"].(string), GroupMembership: groups.Groups, @@ -242,13 +242,13 @@ func (a *Auth) getGroupMembership(token *oauth2.Token) (Groups, error) { } // readCookie decodes an encrypted and signed cookie and returns the contained info -func (a *Auth) readCookie(w http.ResponseWriter, r *http.Request) (*CookieValue, error) { +func (a *Auth) readCookie(w http.ResponseWriter, r *http.Request) (*AuthContext, error) { cookie, err := r.Cookie(a.cookieName) if err != nil { return nil, errors.Wrap(err, "cookie not found") } - var value CookieValue + var value AuthContext err = a.secure.Decode(a.cookieName, cookie.Value, &value) if err != nil { http.SetCookie(w, a.getEmptyCookie()) @@ -258,10 +258,10 @@ func (a *Auth) readCookie(w http.ResponseWriter, r *http.Request) (*CookieValue, return &value, nil } -func (a *Auth) setCookie(w http.ResponseWriter, cookie *CookieValue) error { +func (a *Auth) setCookie(w http.ResponseWriter, cookie *AuthContext) error { encoded, err := a.secure.Encode(a.cookieName, cookie) if err != nil { - return errors.Wrap(err, "encoding CookieValue") + return errors.Wrap(err, "encoding AuthContext") } http.SetCookie(w, &http.Cookie{ @@ -276,7 +276,7 @@ func (a *Auth) setCookie(w http.ResponseWriter, cookie *CookieValue) error { return nil } -func (a *Auth) refreshToken(w http.ResponseWriter, cookie *CookieValue) error { +func (a *Auth) refreshToken(w http.ResponseWriter, cookie *AuthContext) error { if cookie.Token.RefreshToken == "" { return errors.New("no refresh token found, check auth scopes to see if refresh tokens are being provided by your IdP") } @@ -287,7 +287,7 @@ func (a *Auth) refreshToken(w http.ResponseWriter, cookie *CookieValue) error { } if newToken.Expiry != cookie.Token.Expiry { - cv, err := a.newCookieValue(newToken) + cv, err := a.newAuthContext(newToken) if err != nil { return errors.Wrap(err, "creating cookie value from token") } diff --git a/authn/authenticate_internal_test.go b/authn/authenticate_internal_test.go index b4f17b17e..7af62092e 100644 --- a/authn/authenticate_internal_test.go +++ b/authn/authenticate_internal_test.go @@ -56,7 +56,7 @@ func TestAuth(t *testing.T) { GroupID: "abcd123-A", GroupName: "Romantic Painters", } - validCV := CookieValue{ + validCV := AuthContext{ UserID: "snowstorm", UserName: "J.M.W. Turner", GroupMembership: []Group{grp}, @@ -103,15 +103,15 @@ func TestAuth(t *testing.T) { t.Fatalf("expected error decoding block key got: %v", err) } }) - t.Run("NewCookieValue-BadAccessToken", func(t *testing.T) { - _, err := a.newCookieValue(&tokenAT) + t.Run("NewAuthContext-BadAccessToken", func(t *testing.T) { + _, err := a.newAuthContext(&tokenAT) if err == nil || !strings.Contains(err.Error(), "jwt claims") { t.Fatalf("expected failure regarding jwt claims, got: %v", err) } }) - t.Run("CookieValue-NoAccessToken", func(t *testing.T) { - _, err := a.newCookieValue(&tokenNoAT) + t.Run("AuthContext-NoAccessToken", func(t *testing.T) { + _, err := a.newAuthContext(&tokenNoAT) if err == nil || !strings.Contains(err.Error(), "access token") { t.Fatalf("expected failure regarding access token, got: %v", err) } diff --git a/http/handler_internal_test.go b/http/handler_internal_test.go index f4856e235..a073e7a38 100644 --- a/http/handler_internal_test.go +++ b/http/handler_internal_test.go @@ -246,20 +246,20 @@ func TestAuthentication(t *testing.T) { GroupName: "Romantic Painters", } - validCV := authn.CookieValue{ + validCV := authn.AuthContext{ UserID: "snowstorm", UserName: "J.M.W. Turner", GroupMembership: []authn.Group{grp}, Token: &token, } - emptyCV := authn.CookieValue{ + emptyCV := authn.AuthContext{ UserID: "narcissus", UserName: "Caravaggio", GroupMembership: []authn.Group{}, Token: &token, } - expiredCV := authn.CookieValue{ + expiredCV := authn.AuthContext{ UserID: "narcissus", UserName: "Caravaggio", GroupMembership: []authn.Group{grp},