From 7ccd364c7e99a4027b8520433ce616b0929a50bb Mon Sep 17 00:00:00 2001 From: Bobby DeSimone <1544881+desimone@users.noreply.github.com> Date: Tue, 26 May 2020 10:31:55 -0700 Subject: [PATCH] state: infer user from subject (#772) Signed-off-by: Bobby DeSimone --- go.sum | 1 + internal/sessions/state.go | 5 +++-- internal/sessions/state_test.go | 39 +++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/go.sum b/go.sum index a1782f03f..f33c1f24c 100644 --- a/go.sum +++ b/go.sum @@ -241,6 +241,7 @@ github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyC github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 h1:Iju5GlWwrvL6UBg4zJJt3btmonfrMlCDdsejg4CZE7c= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= diff --git a/internal/sessions/state.go b/internal/sessions/state.go index 457fcf925..cf561ec83 100644 --- a/internal/sessions/state.go +++ b/internal/sessions/state.go @@ -186,7 +186,8 @@ func (s *State) UnmarshalJSON(b []byte) error { if t.AccessToken == nil { t.AccessToken = t.OldToken } - *s = *(*State)(t.Alias) - + if t.User == "" { + t.User = t.Subject + } return nil } diff --git a/internal/sessions/state_test.go b/internal/sessions/state_test.go index d9db5e4df..0c18e4f4b 100644 --- a/internal/sessions/state_test.go +++ b/internal/sessions/state_test.go @@ -1,6 +1,7 @@ package sessions import ( + "encoding/json" "strings" "testing" "time" @@ -144,3 +145,41 @@ func TestState_accessTokenHash(t *testing.T) { }) } } + +func TestState_UnmarshalJSON(t *testing.T) { + fixedTime := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC) + timeNow = func() time.Time { + return fixedTime + } + defer func() { timeNow = time.Now }() + tests := []struct { + name string + in *State + want State + wantErr bool + }{ + {"good", &State{}, State{}, false}, + {"with user", &State{User: "user"}, State{User: "user"}, false}, + {"without", &State{Subject: "user"}, State{User: "user", Subject: "user"}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + data, err := json.Marshal(tt.in) + if err != nil { + t.Fatal(err) + } + + s := &State{} + if err := s.UnmarshalJSON(data); (err != nil) != tt.wantErr { + t.Errorf("State.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) + } + got := *s + cmpOpts := []cmp.Option{ + cmpopts.IgnoreUnexported(State{}), + } + if diff := cmp.Diff(got, tt.want, cmpOpts...); diff != "" { + t.Errorf("State.UnmarshalJSON() error = %v", diff) + } + }) + } +}