Skip to content

Commit

Permalink
Fix stale event payload in nonblocking events
Browse files Browse the repository at this point in the history
ref #1670
  • Loading branch information
louischan-oursky committed Nov 30, 2021
2 parents 990c2f2 + db78e1d commit cfe52cc
Show file tree
Hide file tree
Showing 139 changed files with 1,570 additions and 1,265 deletions.
4 changes: 2 additions & 2 deletions pkg/admin/facade/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package facade
import (
"sort"

"github.com/authgear/authgear-server/pkg/lib/authn"
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/authn/authenticator"
interactionintents "github.com/authgear/authgear-server/pkg/lib/interaction/intents"
)

type AuthenticatorService interface {
Get(userID string, typ authn.AuthenticatorType, id string) (*authenticator.Info, error)
Get(userID string, typ model.AuthenticatorType, id string) (*authenticator.Info, error)
Count(userID string) (uint64, error)
ListRefsByUsers(userIDs []string) ([]*authenticator.Ref, error)
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/admin/facade/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ import (
"sort"

"github.com/authgear/authgear-server/pkg/admin/model"
"github.com/authgear/authgear-server/pkg/lib/authn"
apimodel "github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/authn/identity"
"github.com/authgear/authgear-server/pkg/lib/interaction"
interactionintents "github.com/authgear/authgear-server/pkg/lib/interaction/intents"
"github.com/authgear/authgear-server/pkg/lib/interaction/nodes"
)

type IdentityService interface {
Get(userID string, typ authn.IdentityType, id string) (*identity.Info, error)
ListRefsByUsers(userIDs []string) ([]*identity.Ref, error)
Get(userID string, typ apimodel.IdentityType, id string) (*identity.Info, error)
ListRefsByUsers(userIDs []string) ([]*apimodel.IdentityRef, error)
}

type IdentityFacade struct {
Identities IdentityService
Interaction InteractionService
}

func (f *IdentityFacade) Get(ref *identity.Ref) (*identity.Info, error) {
func (f *IdentityFacade) Get(ref *apimodel.IdentityRef) (*identity.Info, error) {
return f.Identities.Get(ref.UserID, ref.Type, ref.ID)
}

func (f *IdentityFacade) List(userID string) ([]*identity.Ref, error) {
func (f *IdentityFacade) List(userID string) ([]*apimodel.IdentityRef, error) {
refs, err := f.Identities.ListRefsByUsers([]string{userID})
if err != nil {
return nil, err
Expand All @@ -53,7 +53,7 @@ func (f *IdentityFacade) Remove(identityInfo *identity.Info) error {
return nil
}

func (f *IdentityFacade) Create(userID string, identityDef model.IdentityDef, password string) (*identity.Ref, error) {
func (f *IdentityFacade) Create(userID string, identityDef model.IdentityDef, password string) (*apimodel.IdentityRef, error) {
var input interface{} = &addIdentityInput{identityDef: identityDef}
if password != "" {
input = &addPasswordInput{inner: input, password: password}
Expand Down
5 changes: 3 additions & 2 deletions pkg/admin/facade/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package facade

import (
"github.com/authgear/authgear-server/pkg/admin/model"
apimodel "github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/authn"
"github.com/authgear/authgear-server/pkg/lib/authn/authenticator"
"github.com/authgear/authgear-server/pkg/lib/authn/identity"
Expand Down Expand Up @@ -62,7 +63,7 @@ type removeIdentityInput struct {

var _ nodes.InputRemoveIdentity = &removeIdentityInput{}

func (i *removeIdentityInput) GetIdentityType() authn.IdentityType {
func (i *removeIdentityInput) GetIdentityType() apimodel.IdentityType {
return i.identityInfo.Type
}
func (i *removeIdentityInput) GetIdentityID() string {
Expand All @@ -85,7 +86,7 @@ type removeAuthenticatorInput struct {

var _ nodes.InputRemoveAuthenticator = &removeAuthenticatorInput{}

func (i *removeAuthenticatorInput) GetAuthenticatorType() authn.AuthenticatorType {
func (i *removeAuthenticatorInput) GetAuthenticatorType() apimodel.AuthenticatorType {
return i.authenticatorInfo.Type
}
func (i *removeAuthenticatorInput) GetAuthenticatorID() string {
Expand Down
6 changes: 3 additions & 3 deletions pkg/admin/graphql/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ type UserFacade interface {
}

type IdentityFacade interface {
Get(ref *identity.Ref) (*identity.Info, error)
List(userID string) ([]*identity.Ref, error)
Get(ref *apimodel.IdentityRef) (*identity.Info, error)
List(userID string) ([]*apimodel.IdentityRef, error)
Remove(identityInfo *identity.Info) error
Create(userID string, identityDef model.IdentityDef, password string) (*identity.Ref, error)
Create(userID string, identityDef model.IdentityDef, password string) (*apimodel.IdentityRef, error)
}

type AuthenticatorFacade interface {
Expand Down
7 changes: 4 additions & 3 deletions pkg/admin/graphql/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/graphql-go/graphql"

"github.com/authgear/authgear-server/pkg/admin/loader"
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/authn/identity"
"github.com/authgear/authgear-server/pkg/util/graphqlutil"
)
Expand Down Expand Up @@ -40,15 +41,15 @@ var nodeIdentity = node(
},
Fields: graphql.Fields{
"id": entityIDField(typeIdentity, func(obj interface{}) (string, error) {
ref := obj.(interface{ ToRef() *identity.Ref }).ToRef()
ref := obj.(interface{ ToRef() *model.IdentityRef }).ToRef()
return loader.EncodeIdentityID(ref), nil
}),
"createdAt": entityCreatedAtField(loadIdentity),
"updatedAt": entityUpdatedAtField(loadIdentity),
"type": &graphql.Field{
Type: graphql.NewNonNull(identityType),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
ref := p.Source.(interface{ ToRef() *identity.Ref }).ToRef()
ref := p.Source.(interface{ ToRef() *model.IdentityRef }).ToRef()
return string(ref.Type), nil
},
},
Expand Down Expand Up @@ -91,7 +92,7 @@ func loadIdentity(ctx context.Context, obj interface{}) *graphqlutil.Lazy {
switch obj := obj.(type) {
case *identity.Info:
return graphqlutil.NewLazyValue(obj)
case *identity.Ref:
case *model.IdentityRef:
return GQLContext(ctx).Identities.Load(loader.EncodeIdentityID(obj))
default:
panic(fmt.Sprintf("graphql: unknown identity type: %T", obj))
Expand Down
3 changes: 1 addition & 2 deletions pkg/admin/loader/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/authgear/authgear-server/pkg/api/apierrors"
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/authn"
"github.com/authgear/authgear-server/pkg/lib/authn/authenticator"
"github.com/authgear/authgear-server/pkg/util/graphqlutil"
)
Expand All @@ -25,7 +24,7 @@ func DecodeAuthenticatorID(id string) (*authenticator.Ref, error) {
}
return &authenticator.Ref{
UserID: parts[0],
Type: authn.AuthenticatorType(parts[1]),
Type: model.AuthenticatorType(parts[1]),
Meta: model.Meta{ID: parts[2]},
}, nil
}
Expand Down
13 changes: 6 additions & 7 deletions pkg/admin/loader/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,32 @@ import (

"github.com/authgear/authgear-server/pkg/api/apierrors"
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/authn"
"github.com/authgear/authgear-server/pkg/lib/authn/identity"
"github.com/authgear/authgear-server/pkg/util/graphqlutil"
)

func EncodeIdentityID(ref *identity.Ref) string {
func EncodeIdentityID(ref *model.IdentityRef) string {
return strings.Join([]string{
ref.UserID,
string(ref.Type),
ref.ID,
}, "|")
}

func DecodeIdentityID(id string) (*identity.Ref, error) {
func DecodeIdentityID(id string) (*model.IdentityRef, error) {
parts := strings.Split(id, "|")
if len(parts) != 3 {
return nil, apierrors.NewInvalid("invalid ID")
}
return &identity.Ref{
return &model.IdentityRef{
UserID: parts[0],
Type: authn.IdentityType(parts[1]),
Type: model.IdentityType(parts[1]),
Meta: model.Meta{ID: parts[2]},
}, nil
}

type IdentityLoaderIdentityService interface {
GetMany(refs []*identity.Ref) ([]*identity.Info, error)
GetMany(refs []*model.IdentityRef) ([]*identity.Info, error)
}

type IdentityLoader struct {
Expand All @@ -50,7 +49,7 @@ func NewIdentityLoader(identities IdentityLoaderIdentityService) *IdentityLoader

func (l *IdentityLoader) LoadFunc(keys []interface{}) ([]interface{}, error) {
// Prepare refs.
refs := make([]*identity.Ref, len(keys))
refs := make([]*model.IdentityRef, len(keys))
for i, key := range keys {
ref, err := DecodeIdentityID(key.(string))
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/admin/model/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package model

import (
"github.com/authgear/authgear-server/pkg/api/apierrors"
"github.com/authgear/authgear-server/pkg/lib/authn"
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/interaction/nodes"
)

Expand All @@ -14,12 +14,12 @@ type IdentityDefLoginID struct {
var _ IdentityDef = &IdentityDefLoginID{}
var _ nodes.InputUseIdentityLoginID = &IdentityDefLoginID{}

func (i *IdentityDefLoginID) Type() authn.IdentityType { return authn.IdentityTypeLoginID }
func (i *IdentityDefLoginID) Type() model.IdentityType { return model.IdentityTypeLoginID }
func (i *IdentityDefLoginID) GetLoginIDKey() string { return i.Key }
func (i *IdentityDefLoginID) GetLoginID() string { return i.Value }

type IdentityDef interface {
Type() authn.IdentityType
Type() model.IdentityType
}

func ParseIdentityDef(data map[string]interface{}) (IdentityDef, error) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/admin/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions pkg/api/event/blocking/user_pre_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const (
)

type UserPreCreateBlockingEventPayload struct {
User model.User `json:"user"`
UserRef model.UserRef `json:"-" resolve:"user"`
UserModel model.User `json:"user"`
Identities []model.Identity `json:"identities"`
OAuthState string `json:"-"`
AdminAPI bool `json:"-"`
Expand All @@ -21,7 +22,7 @@ func (e *UserPreCreateBlockingEventPayload) BlockingEventType() event.Type {
}

func (e *UserPreCreateBlockingEventPayload) UserID() string {
return e.User.ID
return e.UserRef.ID
}

func (e *UserPreCreateBlockingEventPayload) IsAdminAPI() bool {
Expand All @@ -39,7 +40,7 @@ func (e *UserPreCreateBlockingEventPayload) FillContext(ctx *event.Context) {
func (e *UserPreCreateBlockingEventPayload) ApplyMutations(mutations event.Mutations) (event.BlockingPayload, bool) {
if mutations.User.StandardAttributes != nil {
copied := *e
copied.User.StandardAttributes = mutations.User.StandardAttributes
copied.UserModel.StandardAttributes = mutations.User.StandardAttributes
return &copied, true
}

Expand All @@ -49,7 +50,7 @@ func (e *UserPreCreateBlockingEventPayload) ApplyMutations(mutations event.Mutat
func (e *UserPreCreateBlockingEventPayload) GenerateFullMutations() event.Mutations {
return event.Mutations{
User: event.UserMutations{
StandardAttributes: e.User.StandardAttributes,
StandardAttributes: e.UserModel.StandardAttributes,
},
}
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/api/event/blocking/user_profile_pre_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ const (
)

type UserProfilePreUpdateBlockingEventPayload struct {
User model.User `json:"user"`
AdminAPI bool `json:"-"`
UserRef model.UserRef `json:"-" resolve:"user"`
UserModel model.User `json:"user"`
AdminAPI bool `json:"-"`
}

func (e *UserProfilePreUpdateBlockingEventPayload) BlockingEventType() event.Type {
return UserProfilePreUpdate
}

func (e *UserProfilePreUpdateBlockingEventPayload) UserID() string {
return e.User.ID
return e.UserRef.ID
}

func (e *UserProfilePreUpdateBlockingEventPayload) IsAdminAPI() bool {
Expand All @@ -32,7 +33,7 @@ func (e *UserProfilePreUpdateBlockingEventPayload) FillContext(ctx *event.Contex
func (e *UserProfilePreUpdateBlockingEventPayload) ApplyMutations(mutations event.Mutations) (event.BlockingPayload, bool) {
if mutations.User.StandardAttributes != nil {
copied := *e
copied.User.StandardAttributes = mutations.User.StandardAttributes
copied.UserModel.StandardAttributes = mutations.User.StandardAttributes
return &copied, true
}

Expand All @@ -42,7 +43,7 @@ func (e *UserProfilePreUpdateBlockingEventPayload) ApplyMutations(mutations even
func (e *UserProfilePreUpdateBlockingEventPayload) GenerateFullMutations() event.Mutations {
return event.Mutations{
User: event.UserMutations{
StandardAttributes: e.User.StandardAttributes,
StandardAttributes: e.UserModel.StandardAttributes,
},
}
}
Expand Down
25 changes: 0 additions & 25 deletions pkg/api/event/event.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package event

import "fmt"

type Type string

type Payload interface {
Expand Down Expand Up @@ -33,29 +31,6 @@ type Event struct {
IsNonBlocking bool `json:"-"`
}

func newEvent(seq int64, payload Payload, context Context) *Event {
e := &Event{
Payload: payload,
Context: context,
}
e.Seq = seq
e.ID = fmt.Sprintf("%016x", seq)
return e
}

func NewBlockingEvent(seqNo int64, payload BlockingPayload, context Context) *Event {
event := newEvent(seqNo, payload, context)
event.Type = payload.BlockingEventType()
return event
}

func NewNonBlockingEvent(seqNo int64, payload NonBlockingPayload, context Context) *Event {
event := newEvent(seqNo, payload, context)
event.Type = payload.NonBlockingEventType()
event.IsNonBlocking = true
return event
}

func (e *Event) ApplyMutations(mutations Mutations) (*Event, bool) {
if blockingPayload, ok := e.Payload.(BlockingPayload); ok {
if payload, applied := blockingPayload.ApplyMutations(mutations); applied {
Expand Down
9 changes: 5 additions & 4 deletions pkg/api/event/nonblocking/authentication_failed.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ const (
)

type AuthenticationFailedEventPayload struct {
User model.User `json:"user"`
AuthenticationStage string `json:"authentication_stage"`
AuthenticationType string `json:"authenticator_type"`
UserRef model.UserRef `json:"-" resolve:"user"`
UserModel model.User `json:"user"`
AuthenticationStage string `json:"authentication_stage"`
AuthenticationType string `json:"authenticator_type"`
}

func (e *AuthenticationFailedEventPayload) NonBlockingEventType() event.Type {
return event.Type(fmt.Sprintf(AuthenticationFailedFormat, e.AuthenticationStage, e.AuthenticationType))
}

func (e *AuthenticationFailedEventPayload) UserID() string {
return e.User.ID
return e.UserRef.ID
}

func (e *AuthenticationFailedEventPayload) IsAdminAPI() bool {
Expand Down
Loading

0 comments on commit cfe52cc

Please sign in to comment.