Skip to content

Commit bbca017

Browse files
committed
rpc: add support for openScopedSession
1 parent 40ed792 commit bbca017

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

rpc/intents.go

+7
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ func (s *RPC) SendIntent(ctx context.Context, protoIntent *proto.Intent) (iResp
109109
case intents.IntentName_openSession:
110110
return nil, fmt.Errorf("opening a session is unsupported outside of RegisterSession")
111111

112+
case intents.IntentName_openScopedSession:
113+
intentTyped, err := intents.NewIntentTypedFromIntent[intents.IntentDataOpenScopedSession](intent)
114+
if err != nil {
115+
return nil, err
116+
}
117+
return s.openScopedSession(ctx, sess, intentTyped)
118+
112119
case intents.IntentName_closeSession:
113120
intentTyped, err := intents.NewIntentTypedFromIntent[intents.IntentDataCloseSession](intent)
114121
if err != nil {

rpc/sessions.go

+46
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,52 @@ func (s *RPC) initiateAuth(
318318
return authProvider.InitiateAuth(ctx, verifCtx, intent.Data.Verifier, intent.Signers()[0], storeSessFn)
319319
}
320320

321+
func (s *RPC) openScopedSession(
322+
ctx context.Context, dbSess *data.Session, intent *intents.IntentTyped[intents.IntentDataOpenScopedSession],
323+
) (*proto.IntentResponse, error) {
324+
tnt := tenant.FromContext(ctx)
325+
att := attestation.FromContext(ctx)
326+
sessData, _, err := crypto.DecryptData[*proto.SessionData](ctx, dbSess.EncryptedKey, dbSess.Ciphertext, tnt.KMSKeys)
327+
if err != nil {
328+
return nil, fmt.Errorf("decrypting session data: %w", err)
329+
}
330+
331+
scopedSessData := &proto.SessionData{
332+
ID: intent.Data.ScopedSessionID,
333+
ProjectID: sessData.ProjectID,
334+
UserID: sessData.UserID,
335+
Identity: sessData.Identity,
336+
CreatedAt: time.Now(),
337+
ExpiresAt: sessData.ExpiresAt,
338+
}
339+
encryptedKey, algorithm, ciphertext, err := crypto.EncryptData(ctx, att, tnt.KMSKeys[0], scopedSessData)
340+
if err != nil {
341+
return nil, fmt.Errorf("encrypting scoped session data: %w", err)
342+
}
343+
344+
scopedSess := &data.Session{
345+
ID: intent.Data.ScopedSessionID,
346+
ProjectID: sessData.ProjectID,
347+
UserID: sessData.UserID,
348+
Identity: sessData.Identity,
349+
EncryptedKey: encryptedKey,
350+
Algorithm: algorithm,
351+
Ciphertext: ciphertext,
352+
CreatedAt: scopedSessData.CreatedAt,
353+
RefreshedAt: scopedSessData.CreatedAt,
354+
}
355+
if err := s.Sessions.Put(ctx, scopedSess); err != nil {
356+
return nil, fmt.Errorf("saving scoped session: %w", err)
357+
}
358+
359+
res, err := s.Wallets.SendIntent(waasapi.Context(ctx), waasapi.ConvertToAPIIntent(intent.ToIntent()))
360+
if err != nil {
361+
return nil, fmt.Errorf("sending intent: %w", err)
362+
}
363+
364+
return convertIntentResponse(res), nil
365+
}
366+
321367
func (s *RPC) dropSession(
322368
ctx context.Context, sess *data.Session, intent *intents.IntentTyped[intents.IntentDataCloseSession],
323369
) (bool, error) {

0 commit comments

Comments
 (0)