Skip to content

Commit a833174

Browse files
committed
propagate user identity from session
1 parent 9704781 commit a833174

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

grpc/middleware/session/client_interceptors.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ var (
2525
// Algorithm:
2626
// 1. Try to get the full Ory session from context via session.GetSession.
2727
// - If an error occurs other than ErrSessionNotFound, it bubbles up.
28-
// 2. If the session exists, attach its ID to metadata under "user-id".
29-
// 3. If the session is missing, fall back to user-id stored separately in context.
28+
// 2. If the session exists and carries an identity, attach that identity ID under "user-id".
29+
// 3. If the session is missing (or does not include identity information), fall back to the user-id
30+
// stored separately in context.
3031
// - If user-id is also missing or empty, return an error.
3132
// 4. Return a new context containing metadata with the resolved user identifier.
3233
func attachUserMetadata(ctx context.Context) (context.Context, error) {
@@ -35,8 +36,12 @@ func attachUserMetadata(ctx context.Context) (context.Context, error) {
3536
return nil, fmt.Errorf("%w: %w", ErrFailedToGetSession, err)
3637
}
3738

38-
if sess != nil && sess.GetId() != "" {
39-
return metadata.AppendToOutgoingContext(ctx, userIDKey, sess.GetId()), nil
39+
if sess != nil {
40+
if identity, ok := sess.GetIdentityOk(); ok && identity != nil {
41+
if identityID := identity.GetId(); identityID != "" {
42+
return metadata.AppendToOutgoingContext(ctx, userIDKey, identityID), nil
43+
}
44+
}
4045
}
4146

4247
userID, err := session.GetUserID(ctx)

http/middleware/auth/auth.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ func (a auth) middleware(next http.Handler) http.Handler {
8181
ctx = context.WithValue(ctx, contextCookieKey, cookies)
8282
ctx = session.WithSession(ctx, sess)
8383

84+
if identity, ok := sess.GetIdentityOk(); ok && identity != nil {
85+
if id := identity.GetId(); id != "" {
86+
ctx = session.WithUserID(ctx, id)
87+
}
88+
}
89+
8490
// set the new context
8591
r = r.WithContext(ctx)
8692

0 commit comments

Comments
 (0)