@@ -2,46 +2,71 @@ package session
22
33import (
44 "context"
5-
6- ory "github.com/ory/client-go"
75)
86
97type Session string
108
119const (
12- // ContextSessionKey is the key used to store the session in the context.
13- contextSessionKey = Session ("session " )
10+ // contextClaimsKey is the key used to store JWT claims in the context.
11+ contextClaimsKey = Session ("jwt-claims " )
1412
1513 // ContextUserIDKey is the key used to store the user id in the context.
1614 ContextUserIDKey = Session ("user-id" )
1715)
1816
17+ // Claims represents JWT claims from Oathkeeper id_token mutator.
18+ // These claims are set by Oathkeeper after validating the session with Kratos.
19+ type Claims struct {
20+ // Subject is the user ID (from Kratos identity)
21+ Subject string `json:"sub"`
22+ // Email from identity traits
23+ Email string `json:"email"`
24+ // Name from identity traits
25+ Name string `json:"name"`
26+ // IdentityID is the Kratos identity ID
27+ IdentityID string `json:"identity_id"`
28+ // SessionID is the Kratos session ID
29+ SessionID string `json:"session_id"`
30+ // Metadata from identity metadata_public
31+ Metadata map [string ]any `json:"metadata"`
32+ // Issuer of the token
33+ Issuer string `json:"iss"`
34+ // IssuedAt timestamp
35+ IssuedAt int64 `json:"iat"`
36+ // ExpiresAt timestamp
37+ ExpiresAt int64 `json:"exp"`
38+ }
39+
1940// String returns the string representation of the session.
2041func (s Session ) String () string {
2142 return string (s )
2243}
2344
24- func WithSession (ctx context.Context , session * ory.Session ) context.Context {
25- return context .WithValue (ctx , contextSessionKey , session )
45+ // WithClaims stores JWT claims in the context.
46+ func WithClaims (ctx context.Context , claims * Claims ) context.Context {
47+ return context .WithValue (ctx , contextClaimsKey , claims )
2648}
2749
28- func GetSession (ctx context.Context ) (* ory.Session , error ) {
29- sess := ctx .Value (contextSessionKey )
30- if sess == nil {
50+ // GetClaims retrieves JWT claims from the context.
51+ func GetClaims (ctx context.Context ) (* Claims , error ) {
52+ claims := ctx .Value (contextClaimsKey )
53+ if claims == nil {
3154 return nil , ErrSessionNotFound
3255 }
3356
34- if session , ok := sess .(* ory. Session ); ok {
35- return session , nil
57+ if c , ok := claims .(* Claims ); ok {
58+ return c , nil
3659 }
3760
3861 return nil , ErrSessionNotFound
3962}
4063
64+ // WithUserID stores the user ID in the context.
4165func WithUserID (ctx context.Context , userID string ) context.Context {
4266 return context .WithValue (ctx , ContextUserIDKey , userID )
4367}
4468
69+ // GetUserID retrieves the user ID from the context.
4570func GetUserID (ctx context.Context ) (string , error ) {
4671 userID := ctx .Value (ContextUserIDKey )
4772 if userID == nil {
0 commit comments