state: infer user from subject (#772)

Signed-off-by: Bobby DeSimone <bobbydesimone@gmail.com>
This commit is contained in:
Bobby DeSimone 2020-05-26 10:31:55 -07:00 committed by Bobby DeSimone
parent ad79585ae8
commit 7ccd364c7e
No known key found for this signature in database
GPG key ID: AEE4CF12FE86D07E
3 changed files with 43 additions and 2 deletions

1
go.sum
View file

@ -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=

View file

@ -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
}

View file

@ -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)
}
})
}
}