Skip to content

Commit 7d15eec

Browse files
committed
feat: Support OAuth2
1 parent a722a86 commit 7d15eec

35 files changed

Lines changed: 1447 additions & 484 deletions

File tree

cmd/mcp-gateway/main.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"syscall"
1010
"time"
1111

12+
"github.com/mcp-ecosystem/mcp-gateway/internal/auth"
1213
"github.com/mcp-ecosystem/mcp-gateway/internal/common/cnst"
1314
"github.com/mcp-ecosystem/mcp-gateway/internal/common/config"
1415
"github.com/mcp-ecosystem/mcp-gateway/internal/core"
@@ -173,13 +174,19 @@ func run() {
173174
zap.Error(err))
174175
}
175176

176-
srv, err := core.NewServer(logger, cfg.Port, store, sessionStore)
177+
// Initialize auth service
178+
a, err := auth.NewAuth(logger, cfg.Auth)
177179
if err != nil {
178-
logger.Fatal("failed to create server",
179-
zap.Error(err))
180+
logger.Fatal("Failed to initialize auth service", zap.Error(err))
181+
}
182+
183+
// Create server instance
184+
server, err := core.NewServer(logger, cfg.Port, store, sessionStore, a)
185+
if err != nil {
186+
logger.Fatal("Failed to create server", zap.Error(err))
180187
}
181188

182-
err = srv.RegisterRoutes(ctx)
189+
err = server.RegisterRoutes(ctx)
183190
if err != nil {
184191
logger.Fatal("failed to register routes",
185192
zap.Error(err))
@@ -196,7 +203,7 @@ func run() {
196203
zap.Error(err))
197204
}
198205

199-
srv.Start()
206+
server.Start()
200207

201208
quit := make(chan os.Signal, 1)
202209
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
@@ -214,7 +221,7 @@ func run() {
214221
cancel()
215222

216223
// Shutdown the MCP server to close all SSE connections
217-
err = srv.Shutdown(ctx)
224+
err = server.Shutdown(ctx)
218225
if err != nil {
219226
logger.Error("failed to shutdown MCP server",
220227
zap.Error(err))
@@ -228,14 +235,14 @@ func run() {
228235

229236
if updateMCPConfig == nil {
230237
logger.Warn("Updated configuration is nil, falling back to full reload")
231-
srv.ReloadConfigs(ctx)
238+
server.ReloadConfigs(ctx)
232239
} else {
233-
srv.UpdateConfig(ctx, updateMCPConfig)
240+
server.UpdateConfig(ctx, updateMCPConfig)
234241
}
235242
case <-ticker.C:
236243
logger.Info("Received ticker signal", zap.Bool("reload_switch", cfg.ReloadSwitch))
237244
if cfg.ReloadSwitch {
238-
srv.ReloadConfigs(ctx)
245+
server.ReloadConfigs(ctx)
239246
}
240247
}
241248

configs/mcp-gateway.yaml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,26 @@ session:
7070
db: ${SESSION_REDIS_DB:0}
7171
topic: "${SESSION_REDIS_TOPIC:mcp-gateway:session}"
7272
prefix: "${SESSION_REDIS_PREFIX:session}"
73-
ttl: "${SESSION_REDIS_TTL:24h}" # Session TTL in Redis
73+
ttl: "${SESSION_REDIS_TTL:24h}" # Session TTL in Redis
74+
75+
# OAuth2 configuration
76+
auth:
77+
oauth2:
78+
issuer: https://github.com/mcp-ecosystem/mcp-gateway
79+
storage:
80+
type: memory
81+
cors:
82+
allowOrigins:
83+
- "*"
84+
allowMethods:
85+
- "GET"
86+
- "POST"
87+
- "PUT"
88+
- "OPTIONS"
89+
allowHeaders:
90+
- "Content-Type"
91+
- "Authorization"
92+
- "Mcp-Session-Id"
93+
exposeHeaders:
94+
- "Mcp-Session-Id"
95+
allowCredentials: true

go.mod

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ go 1.24.1
55
require (
66
github.com/BurntSushi/toml v1.5.0
77
github.com/getkin/kin-openapi v0.131.0
8-
github.com/gin-gonic/gin v1.10.0
8+
github.com/gin-gonic/gin v1.10.1
99
github.com/glebarez/sqlite v1.11.0
10-
github.com/go-openapi/loads v0.22.0
11-
github.com/go-openapi/spec v0.21.0
1210
github.com/go-redis/redis/v8 v8.11.5
1311
github.com/golang-jwt/jwt/v5 v5.2.2
1412
github.com/google/uuid v1.6.0
@@ -18,7 +16,6 @@ require (
1816
github.com/nicksnyder/go-i18n/v2 v2.6.0
1917
github.com/openai/openai-go v0.1.0-beta.10
2018
github.com/spf13/cobra v1.9.1
21-
github.com/spf13/viper v1.20.1
2219
github.com/stretchr/testify v1.10.0
2320
github.com/tidwall/gjson v1.18.0
2421
go.uber.org/zap v1.27.0
@@ -31,17 +28,6 @@ require (
3128
gorm.io/gorm v1.25.12
3229
)
3330

34-
require (
35-
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
36-
github.com/go-openapi/analysis v0.23.0 // indirect
37-
github.com/go-openapi/errors v0.22.0 // indirect
38-
github.com/go-openapi/jsonreference v0.21.0 // indirect
39-
github.com/go-openapi/strfmt v0.23.0 // indirect
40-
github.com/mitchellh/mapstructure v1.5.0 // indirect
41-
github.com/oklog/ulid v1.3.1 // indirect
42-
go.mongodb.org/mongo-driver v1.14.0 // indirect
43-
)
44-
4531
require (
4632
github.com/bytedance/sonic v1.13.2 // indirect
4733
github.com/bytedance/sonic/loader v0.2.4 // indirect
@@ -60,7 +46,6 @@ require (
6046
github.com/go-playground/universal-translator v0.18.1 // indirect
6147
github.com/go-playground/validator/v10 v10.26.0 // indirect
6248
github.com/go-sql-driver/mysql v1.7.0 // indirect
63-
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
6449
github.com/goccy/go-json v0.10.5 // indirect
6550
github.com/inconshreveable/mousetrap v1.1.0 // indirect
6651
github.com/jackc/pgpassfile v1.0.0 // indirect
@@ -86,13 +71,8 @@ require (
8671
github.com/pmezard/go-difflib v1.0.0 // indirect
8772
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
8873
github.com/rogpeppe/go-internal v1.14.1 // indirect
89-
github.com/sagikazarmark/locafero v0.7.0 // indirect
90-
github.com/sourcegraph/conc v0.3.0 // indirect
91-
github.com/spf13/afero v1.12.0 // indirect
9274
github.com/spf13/cast v1.7.1 // indirect
9375
github.com/spf13/pflag v1.0.6 // indirect
94-
github.com/stretchr/objx v0.5.2 // indirect
95-
github.com/subosito/gotenv v1.6.0 // indirect
9676
github.com/tidwall/match v1.1.1 // indirect
9777
github.com/tidwall/pretty v1.2.1 // indirect
9878
github.com/tidwall/sjson v1.2.5 // indirect
@@ -102,9 +82,9 @@ require (
10282
go.uber.org/multierr v1.10.0 // indirect
10383
golang.org/x/arch v0.16.0 // indirect
10484
golang.org/x/exp v0.0.0-20250506013437-ce4c2cf36ca6 // indirect
105-
golang.org/x/net v0.39.0 // indirect
85+
golang.org/x/net v0.39.0
10686
golang.org/x/sync v0.14.0 // indirect
107-
golang.org/x/sys v0.33.0 // indirect
87+
golang.org/x/sys v0.33.0
10888
google.golang.org/protobuf v1.36.6 // indirect
10989
modernc.org/libc v1.22.5 // indirect
11090
modernc.org/mathutil v1.5.0 // indirect

go.sum

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
22
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
3-
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
4-
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
53
github.com/bytedance/sonic v1.13.2 h1:8/H1FempDZqC4VqjptGo14QQlJx8VdZJegxs6wwfqpQ=
64
github.com/bytedance/sonic v1.13.2/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4=
75
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
@@ -30,26 +28,14 @@ github.com/getkin/kin-openapi v0.131.0 h1:NO2UeHnFKRYhZ8wg6Nyh5Cq7dHk4suQQr72a4p
3028
github.com/getkin/kin-openapi v0.131.0/go.mod h1:3OlG51PCYNsPByuiMB0t4fjnNlIDnaEDsjiKUV8nL58=
3129
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
3230
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
33-
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
34-
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
31+
github.com/gin-gonic/gin v1.10.1 h1:T0ujvqyCSqRopADpgPgiTT63DUQVSfojyME59Ei63pQ=
32+
github.com/gin-gonic/gin v1.10.1/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
3533
github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo=
3634
github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k=
3735
github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw=
3836
github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ=
39-
github.com/go-openapi/analysis v0.23.0 h1:aGday7OWupfMs+LbmLZG4k0MYXIANxcuBTYUC03zFCU=
40-
github.com/go-openapi/analysis v0.23.0/go.mod h1:9mz9ZWaSlV8TvjQHLl2mUW2PbZtemkE8yA5v22ohupo=
41-
github.com/go-openapi/errors v0.22.0 h1:c4xY/OLxUBSTiepAg3j/MHuAv5mJhnf53LLMWFB+u/w=
42-
github.com/go-openapi/errors v0.22.0/go.mod h1:J3DmZScxCDufmIMsdOuDHxJbdOGC0xtUynjIx092vXE=
4337
github.com/go-openapi/jsonpointer v0.21.1 h1:whnzv/pNXtK2FbX/W9yJfRmE2gsmkfahjMKB0fZvcic=
4438
github.com/go-openapi/jsonpointer v0.21.1/go.mod h1:50I1STOfbY1ycR8jGz8DaMeLCdXiI6aDteEdRNNzpdk=
45-
github.com/go-openapi/jsonreference v0.21.0 h1:Rs+Y7hSXT83Jacb7kFyjn4ijOuVGSvOdF2+tg1TRrwQ=
46-
github.com/go-openapi/jsonreference v0.21.0/go.mod h1:LmZmgsrTkVg9LG4EaHeY8cBDslNPMo06cago5JNLkm4=
47-
github.com/go-openapi/loads v0.22.0 h1:ECPGd4jX1U6NApCGG1We+uEozOAvXvJSF4nnwHZ8Aco=
48-
github.com/go-openapi/loads v0.22.0/go.mod h1:yLsaTCS92mnSAZX5WWoxszLj0u+Ojl+Zs5Stn1oF+rs=
49-
github.com/go-openapi/spec v0.21.0 h1:LTVzPc3p/RzRnkQqLRndbAzjY0d0BCL72A6j3CdL9ZY=
50-
github.com/go-openapi/spec v0.21.0/go.mod h1:78u6VdPw81XU44qEWGhtr982gJ5BWg2c0I5XwVMotYk=
51-
github.com/go-openapi/strfmt v0.23.0 h1:nlUS6BCqcnAk0pyhi9Y+kdDVZdZMHfEKQiS4HaMgO/c=
52-
github.com/go-openapi/strfmt v0.23.0/go.mod h1:NrtIpfKtWIygRkKVsxh7XQMDQW5HKQl6S5ik2elW+K4=
5339
github.com/go-openapi/swag v0.23.1 h1:lpsStH0n2ittzTnbaSloVZLuB5+fvSY/+hnagBjSNZU=
5440
github.com/go-openapi/swag v0.23.1/go.mod h1:STZs8TbRvEQQKUA+JZNAm3EWlgaOBGpyFDqQnDHMef0=
5541
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
@@ -66,8 +52,6 @@ github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ
6652
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
6753
github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
6854
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
69-
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
70-
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
7155
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
7256
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
7357
github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8=
@@ -119,8 +103,6 @@ github.com/mark3labs/mcp-go v0.27.0 h1:iok9kU4DUIU2/XVLgFS2Q9biIDqstC0jY4EQTK2Er
119103
github.com/mark3labs/mcp-go v0.27.0/go.mod h1:rXqOudj/djTORU/ThxYx8fqEVj/5pvTuuebQ2RC7uk4=
120104
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
121105
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
122-
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
123-
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
124106
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
125107
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
126108
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -136,8 +118,6 @@ github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 h1:G7ERwszslrBzRxj//J
136118
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037/go.mod h1:2bpvgLBZEtENV5scfDFEtB/5+1M4hkQhDQrccEJ/qGw=
137119
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 h1:bQx3WeLcUWy+RletIKwUIt4x3t8n2SxavmoclizMb8c=
138120
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90/go.mod h1:y5+oSEHCPT/DGrS++Wc/479ERge0zTFxaF8PbGKcg2o=
139-
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
140-
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
141121
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
142122
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
143123
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
@@ -156,34 +136,22 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qq
156136
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
157137
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
158138
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
159-
github.com/sagikazarmark/locafero v0.7.0 h1:5MqpDsTGNDhY8sGp0Aowyf0qKsPrhewaLSsFaodPcyo=
160-
github.com/sagikazarmark/locafero v0.7.0/go.mod h1:2za3Cg5rMaTMoG/2Ulr9AwtFaIppKXTRYnozin4aB5k=
161-
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
162-
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
163-
github.com/spf13/afero v1.12.0 h1:UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs=
164-
github.com/spf13/afero v1.12.0/go.mod h1:ZTlWwG4/ahT8W7T0WQ5uYmjI9duaLQGy3Q2OAl4sk/4=
165139
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
166140
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
167141
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
168142
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
169143
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
170144
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
171-
github.com/spf13/viper v1.20.1 h1:ZMi+z/lvLyPSCoNtFCpqjy0S4kPbirhpTMwl8BkW9X4=
172-
github.com/spf13/viper v1.20.1/go.mod h1:P9Mdzt1zoHIG8m2eZQinpiBjo6kCmZSKBClNNqjJvu4=
173145
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
174146
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
175147
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
176-
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
177-
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
178148
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
179149
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
180150
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
181151
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
182152
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
183153
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
184154
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
185-
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
186-
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
187155
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
188156
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
189157
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
@@ -200,8 +168,6 @@ github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65E
200168
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
201169
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
202170
github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=
203-
go.mongodb.org/mongo-driver v1.14.0 h1:P98w8egYRjYe3XDjxhYJagTokP/H6HzlsnojRgZRd80=
204-
go.mongodb.org/mongo-driver v1.14.0/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c=
205171
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
206172
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
207173
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=

internal/auth/auth.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package auth
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
"go.uber.org/zap"
8+
9+
"github.com/mcp-ecosystem/mcp-gateway/internal/common/config"
10+
"github.com/mcp-ecosystem/mcp-gateway/internal/common/errorx"
11+
)
12+
13+
// Auth defines the authentication oauth interface
14+
type Auth interface {
15+
OAuth2
16+
IsOAuth2Enabled() bool
17+
GetOAuth2CORS() *config.CORSConfig
18+
}
19+
20+
type OAuth2 interface {
21+
// ServerMetadata returns the server metadata
22+
ServerMetadata(r *http.Request) map[string]interface{}
23+
24+
// Authorize handles the authorization request
25+
Authorize(ctx context.Context, r *http.Request) (*AuthorizationResponse, error)
26+
27+
// Token handles the token request
28+
Token(ctx context.Context, r *http.Request) (*TokenResponse, error)
29+
30+
// Register handles client registration
31+
Register(ctx context.Context, r *http.Request) (*ClientRegistrationResponse, error)
32+
33+
// Revoke handles token revocation
34+
Revoke(ctx context.Context, r *http.Request) error
35+
36+
// ValidateToken validates an access token
37+
ValidateToken(ctx context.Context, token string) error
38+
}
39+
40+
// AuthorizationResponse represents the response from the authorization endpoint
41+
type AuthorizationResponse struct {
42+
Code string
43+
State string
44+
}
45+
46+
// TokenResponse represents the response from the token endpoint
47+
type TokenResponse struct {
48+
AccessToken string `json:"access_token"`
49+
TokenType string `json:"token_type"`
50+
ExpiresIn int64 `json:"expires_in,omitempty"`
51+
RefreshToken string `json:"refresh_token,omitempty"`
52+
Scope string `json:"scope,omitempty"`
53+
}
54+
55+
// ClientRegistrationResponse represents the response from the client registration endpoint
56+
type ClientRegistrationResponse struct {
57+
ClientID string `json:"client_id"`
58+
ClientSecret string `json:"client_secret,omitempty"`
59+
RedirectURIs []string `json:"redirect_uris"`
60+
GrantTypes []string `json:"grant_types"`
61+
ResponseTypes []string `json:"response_types"`
62+
TokenEndpointAuthMethod string `json:"token_endpoint_auth_method"`
63+
Scope string `json:"scope"`
64+
}
65+
66+
type auth struct {
67+
OAuth2
68+
cfg config.AuthConfig
69+
}
70+
71+
// NewAuth creates a new auth oauth based on the configuration
72+
func NewAuth(logger *zap.Logger, cfg config.AuthConfig) (Auth, error) {
73+
a := &auth{
74+
cfg: cfg,
75+
}
76+
if cfg.OAuth2 != nil {
77+
oauth2, err := newOAuth(logger, *cfg.OAuth2)
78+
if err != nil {
79+
return nil, err
80+
}
81+
a.OAuth2 = oauth2
82+
}
83+
return a, nil
84+
}
85+
86+
// IsOAuth2Enabled returns true if OAuth2 is enabled
87+
func (a *auth) IsOAuth2Enabled() bool {
88+
return a.cfg.OAuth2 != nil
89+
}
90+
91+
// GetOAuth2CORS returns the OAuth2 CORS configuration
92+
func (a *auth) GetOAuth2CORS() *config.CORSConfig {
93+
return a.cfg.CORS
94+
}
95+
96+
// ValidateToken validates an access token
97+
func (a *auth) ValidateToken(ctx context.Context, token string) error {
98+
if a.OAuth2 == nil {
99+
return errorx.ErrOAuth2NotEnabled
100+
}
101+
102+
return a.OAuth2.ValidateToken(ctx, token)
103+
}

0 commit comments

Comments
 (0)