Skip to content

Commit e298b4f

Browse files
committed
auth: use jwt
1 parent 4cd3deb commit e298b4f

File tree

22 files changed

+274
-178
lines changed

22 files changed

+274
-178
lines changed

auth/go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ go 1.25.5
44

55
require (
66
github.com/authzed/authzed-go v1.7.0
7-
github.com/ory/client-go v1.22.21
87
github.com/shortlink-org/go-sdk/grpc v0.0.0-20260107222411-453281b10921
98
github.com/shortlink-org/go-sdk/logger v0.0.0-20260107222411-453281b10921
109
github.com/shortlink-org/go-sdk/observability v0.0.0-20260121215533-0995bc8d4d88
1110
go.opentelemetry.io/otel/trace v1.39.0
1211
google.golang.org/grpc v1.78.0
1312
)
1413

15-
require github.com/klauspost/compress v1.18.2 // indirect
14+
require github.com/ory/client-go v1.22.21 // indirect
1615

1716
require (
1817
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.10-20250912141014-52f32327d4b0.1 // indirect

auth/go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
6565
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
6666
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
6767
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
68-
github.com/ory/client-go v1.22.16 h1:JNjTwJqIb/apOFLeYygrO48wJgZJrLeA01ltM5piMns=
69-
github.com/ory/client-go v1.22.16/go.mod h1:VJznBChrOG0Fg/nmplykTgTXWPYIfuC/rBvCcL60ukQ=
7068
github.com/ory/client-go v1.22.21 h1:qYyhozw1UcA94QWMqpetPY2Ir2wwDdv1Wh0Rn668eO0=
7169
github.com/ory/client-go v1.22.21/go.mod h1:VJznBChrOG0Fg/nmplykTgTXWPYIfuC/rBvCcL60ukQ=
7270
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
@@ -101,8 +99,6 @@ github.com/shortlink-org/go-sdk/http v0.0.0-20260107222411-453281b10921 h1:pJSw0
10199
github.com/shortlink-org/go-sdk/http v0.0.0-20260107222411-453281b10921/go.mod h1:Byc5mjbHKVrp4UFT0SOxYJfKM8B45K0utEl06fZ8K7Y=
102100
github.com/shortlink-org/go-sdk/logger v0.0.0-20260107222411-453281b10921 h1:mgUyseLj+kSW+R+lhB029ZS2rCJQZOkumRKKvHIFT7o=
103101
github.com/shortlink-org/go-sdk/logger v0.0.0-20260107222411-453281b10921/go.mod h1:26SEqudMpXAwKF7iNyfxhPoHUgJeHEzx8bw5kCultTQ=
104-
github.com/shortlink-org/go-sdk/observability v0.0.0-20260107222628-ad66d85c8a41 h1:xo1/NOyh9QHoN1A2je85Jfsy0bivohgf7yongeCWD6w=
105-
github.com/shortlink-org/go-sdk/observability v0.0.0-20260107222628-ad66d85c8a41/go.mod h1:dLjQilcHlTfVBzVeDK8QdwgiTq5x3sMijWoW7OeWzNo=
106102
github.com/shortlink-org/go-sdk/observability v0.0.0-20260121215533-0995bc8d4d88 h1:lTszkny6KLkXvLZ2IOC3Y0+aeH25YFLaODheDbakzSs=
107103
github.com/shortlink-org/go-sdk/observability v0.0.0-20260121215533-0995bc8d4d88/go.mod h1:71EjSBzc/rwe+xLYn1ZMlyQUkbFBFRLwqv/25s38IEQ=
108104
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw=

auth/session/session.go

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,71 @@ package session
22

33
import (
44
"context"
5-
6-
ory "github.com/ory/client-go"
75
)
86

97
type Session string
108

119
const (
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.
2041
func (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.
4165
func 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.
4570
func GetUserID(ctx context.Context) (string, error) {
4671
userID := ctx.Value(ContextUserIDKey)
4772
if userID == nil {

cache/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ require (
106106
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
107107
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
108108
go.mongodb.org/mongo-driver v1.17.2 // indirect
109-
go.mongodb.org/mongo-driver/v2 v2.4.1 // indirect
109+
go.mongodb.org/mongo-driver/v2 v2.4.2 // indirect
110110
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
111111
go.opentelemetry.io/otel v1.39.0 // indirect
112112
go.opentelemetry.io/otel/exporters/prometheus v0.61.0 // indirect

cache/go.sum

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,6 @@ github.com/prometheus/otlptranslator v1.0.0/go.mod h1:vRYWnXvI6aWGpsdY/mOT/cbeVR
429429
github.com/prometheus/procfs v0.19.2 h1:zUMhqEW66Ex7OXIiDkll3tl9a1ZdilUOd/F6ZXw4Vws=
430430
github.com/prometheus/procfs v0.19.2/go.mod h1:M0aotyiemPhBCM0z5w87kL22CxfcH05ZpYlu+b4J7mw=
431431
github.com/redis/go-redis/v9 v9.0.0-rc.4/go.mod h1:Vo3EsyWnicKnSKCA7HhgnvnyA74wOA69Cd2Meli5mmA=
432-
github.com/redis/go-redis/v9 v9.17.2 h1:P2EGsA4qVIM3Pp+aPocCJ7DguDHhqrXNhVcEp4ViluI=
433-
github.com/redis/go-redis/v9 v9.17.2/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370=
434432
github.com/redis/go-redis/v9 v9.17.3 h1:fN29NdNrE17KttK5Ndf20buqfDZwGNgoUr9qjl1DQx4=
435433
github.com/redis/go-redis/v9 v9.17.3/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370=
436434
github.com/redis/rueidis v1.0.70 h1:O01v0Mt27/qXV9mKU/zahgxHdC8piHzIepqW4Nyzn/I=
@@ -546,8 +544,7 @@ gitlab.com/nyarla/go-crypt v0.0.0-20160106005555-d9a5dc2b789b/go.mod h1:T3BPAOm2
546544
go.mongodb.org/mongo-driver v1.1.0/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
547545
go.mongodb.org/mongo-driver v1.17.2 h1:gvZyk8352qSfzyZ2UMWcpDpMSGEr1eqE4T793SqyhzM=
548546
go.mongodb.org/mongo-driver v1.17.2/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ=
549-
go.mongodb.org/mongo-driver/v2 v2.4.1 h1:hGDMngUao03OVQ6sgV5csk+RWOIkF+CuLsTPobNMGNI=
550-
go.mongodb.org/mongo-driver/v2 v2.4.1/go.mod h1:jHeEDJHJq7tm6ZF45Issun9dbogjfnPySb1vXA7EeAI=
547+
go.mongodb.org/mongo-driver/v2 v2.4.2 h1:HrJ+Auygxceby9MLp3YITobef5a8Bv4HcPFIkml1U7U=
551548
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
552549
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
553550
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=

db/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,6 @@ go.etcd.io/etcd/client/v3 v3.6.6/go.mod h1:36Qv6baQ07znPR3+n7t+Rk5VHEzVYPvFfGmfF
543543
go.mongodb.org/mongo-driver v1.1.0/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
544544
go.mongodb.org/mongo-driver v1.17.2 h1:gvZyk8352qSfzyZ2UMWcpDpMSGEr1eqE4T793SqyhzM=
545545
go.mongodb.org/mongo-driver v1.17.2/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ=
546-
go.mongodb.org/mongo-driver/v2 v2.4.1 h1:hGDMngUao03OVQ6sgV5csk+RWOIkF+CuLsTPobNMGNI=
547-
go.mongodb.org/mongo-driver/v2 v2.4.1/go.mod h1:jHeEDJHJq7tm6ZF45Issun9dbogjfnPySb1vXA7EeAI=
548546
go.mongodb.org/mongo-driver/v2 v2.4.2 h1:HrJ+Auygxceby9MLp3YITobef5a8Bv4HcPFIkml1U7U=
549547
go.mongodb.org/mongo-driver/v2 v2.4.2/go.mod h1:jHeEDJHJq7tm6ZF45Issun9dbogjfnPySb1vXA7EeAI=
550548
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=

eventsourcing/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ require (
9595
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
9696
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
9797
go.mongodb.org/mongo-driver v1.17.2 // indirect
98-
go.mongodb.org/mongo-driver/v2 v2.4.1 // indirect
98+
go.mongodb.org/mongo-driver/v2 v2.4.2 // indirect
9999
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
100100
go.opentelemetry.io/otel/metric v1.39.0 // indirect
101101
go.opentelemetry.io/otel/sdk v1.39.0 // indirect

eventsourcing/go.sum

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,7 @@ gitlab.com/nyarla/go-crypt v0.0.0-20160106005555-d9a5dc2b789b/go.mod h1:T3BPAOm2
472472
go.mongodb.org/mongo-driver v1.1.0/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
473473
go.mongodb.org/mongo-driver v1.17.2 h1:gvZyk8352qSfzyZ2UMWcpDpMSGEr1eqE4T793SqyhzM=
474474
go.mongodb.org/mongo-driver v1.17.2/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ=
475-
go.mongodb.org/mongo-driver/v2 v2.4.1 h1:hGDMngUao03OVQ6sgV5csk+RWOIkF+CuLsTPobNMGNI=
476-
go.mongodb.org/mongo-driver/v2 v2.4.1/go.mod h1:jHeEDJHJq7tm6ZF45Issun9dbogjfnPySb1vXA7EeAI=
475+
go.mongodb.org/mongo-driver/v2 v2.4.2 h1:HrJ+Auygxceby9MLp3YITobef5a8Bv4HcPFIkml1U7U=
477476
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
478477
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
479478
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=

0 commit comments

Comments
 (0)