Skip to content

Commit

Permalink
rpc: AccountList response with CurrentAccountID
Browse files Browse the repository at this point in the history
  • Loading branch information
patrislav committed Jul 11, 2024
1 parent bdeb37e commit 5d461ae
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
9 changes: 7 additions & 2 deletions rpc/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

func (s *RPC) listAccounts(
ctx context.Context, sess *data.Session, intent *intents.IntentTyped[intents.IntentDataListAccounts],
) ([]*intents.Account, error) {
) (*intents.IntentResponseAccountList, error) {
tntData := tenant.FromContext(ctx)

dbAccounts, err := s.Accounts.ListByUserID(ctx, sess.UserID)
Expand Down Expand Up @@ -45,7 +45,12 @@ func (s *RPC) listAccounts(
out[i].Email = &dbAcc.Email
}
}
return out, nil

res := &intents.IntentResponseAccountList{
Accounts: out,
CurrentAccountID: sess.Identity,
}
return res, nil
}

func (s *RPC) federateAccount(
Expand Down
15 changes: 8 additions & 7 deletions rpc/intents.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s *RPC) SendIntent(ctx context.Context, protoIntent *proto.Intent) (*proto
if err != nil {
return nil, err
}
return makeIntentResponse(proto.IntentResponseCode_authInitiated, res), nil
return makeTypedIntentResponse(res)
}

sess, found, err := s.Sessions.Get(ctx, tntData.ProjectID, sessionID)
Expand All @@ -99,7 +99,7 @@ func (s *RPC) SendIntent(ctx context.Context, protoIntent *proto.Intent) (*proto
if err != nil {
return nil, err
}
return makeIntentResponse(proto.IntentResponseCode_sessionClosed, intents.IntentResponseSessionClosed{}), nil
return makeTypedIntentResponse(&intents.IntentResponseSessionClosed{})

case intents.IntentName_listSessions:
intentTyped, err := intents.NewIntentTypedFromIntent[intents.IntentDataListSessions](intent)
Expand All @@ -110,6 +110,7 @@ func (s *RPC) SendIntent(ctx context.Context, protoIntent *proto.Intent) (*proto
if err != nil {
return nil, err
}
// TODO switch to correct response (compare the intent.Version semver to not break older clients)
return makeIntentResponse(proto.IntentResponseCode_sessionList, sessions), nil

case intents.IntentName_sessionAuthProof:
Expand Down Expand Up @@ -138,11 +139,11 @@ func (s *RPC) SendIntent(ctx context.Context, protoIntent *proto.Intent) (*proto
if err != nil {
return nil, err
}
accounts, err := s.listAccounts(ctx, sess, intentTyped)
res, err := s.listAccounts(ctx, sess, intentTyped)
if err != nil {
return nil, err
}
return makeIntentResponse(proto.IntentResponseCode_accountList, accounts), nil
return makeTypedIntentResponse(res)

case intents.IntentName_federateAccount:
intentTyped, err := intents.NewIntentTypedFromIntent[intents.IntentDataFederateAccount](intent)
Expand All @@ -153,7 +154,7 @@ func (s *RPC) SendIntent(ctx context.Context, protoIntent *proto.Intent) (*proto
if err != nil {
return nil, err
}
return makeIntentResponse(proto.IntentResponseCode_accountFederated, account), nil
return makeTypedIntentResponse(&intents.IntentResponseAccountFederated{Account: account})

case intents.IntentName_removeAccount:
intentTyped, err := intents.NewIntentTypedFromIntent[intents.IntentDataRemoveAccount](intent)
Expand All @@ -163,7 +164,7 @@ func (s *RPC) SendIntent(ctx context.Context, protoIntent *proto.Intent) (*proto
if err := s.removeAccount(ctx, sess, intentTyped); err != nil {
return nil, err
}
return makeIntentResponse(proto.IntentResponseCode_accountRemoved, true), nil
return makeTypedIntentResponse(&intents.IntentResponseAccountRemoved{})

case intents.IntentName_getIdToken:
intentTyped, err := intents.NewIntentTypedFromIntent[intents.IntentDataGetIdToken](intent)
Expand All @@ -174,7 +175,7 @@ func (s *RPC) SendIntent(ctx context.Context, protoIntent *proto.Intent) (*proto
if err != nil {
return nil, err
}
return makeIntentResponse(proto.IntentResponseCode_idToken, res), nil
return makeTypedIntentResponse(res)
}

// Generic forwarding of intent, no special handling
Expand Down
8 changes: 8 additions & 0 deletions rpc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ func makeIntentResponse(code proto.IntentResponseCode, data any) *proto.IntentRe
Data: data,
}
}

func makeTypedIntentResponse[T any](data *T) (*proto.IntentResponse, error) {
code := intents.IntentResponseTypeToCode[T](data)
if code == "" {
return nil, fmt.Errorf("invalid intent response type")
}
return &proto.IntentResponse{Code: proto.IntentResponseCode(code), Data: data}, nil
}

0 comments on commit 5d461ae

Please sign in to comment.