Skip to content

Commit cee4d5e

Browse files
Add rate limits to token endpoint
ref DEV-2917 ref DEV-2919 ref DEV-2907
2 parents 1909055 + 04a9c8c commit cee4d5e

17 files changed

Lines changed: 587 additions & 22 deletions

pkg/auth/deps.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ var DependencySet = wire.NewSet(
7171
deps.CommonDependencySet,
7272

7373
nonce.DependencySet,
74+
75+
wire.Bind(new(oauthhandler.TokenHandlerAppDatabase), new(*appdb.Handle)),
76+
7477
wire.Bind(new(interaction.NonceService), new(*nonce.Service)),
7578

7679
wire.Bind(new(webapp.SessionMiddlewareOAuthSessionService), new(*oauthsession.StoreRedis)),

pkg/auth/handler/oauth/token.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,10 @@ func (h *TokenHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
4343

4444
var result httputil.Result
4545
ctx := r.Context()
46-
err = h.Database.WithTx(ctx, func(ctx context.Context) error {
47-
result = h.TokenHandler.Handle(ctx, rw, r, req)
48-
if result.IsInternalError() {
49-
return errAuthzInternalError
50-
}
51-
return nil
52-
})
46+
result = h.TokenHandler.Handle(ctx, rw, r, req)
47+
if result.IsInternalError() {
48+
err = errAuthzInternalError
49+
}
5350

5451
if err == nil || errors.Is(err, errAuthzInternalError) {
5552
result.WriteResponse(rw, r)

pkg/auth/wire_gen.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/lib/config/configsource/resources.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,14 @@ func (d AuthgearYAMLDescriptor) validateOAuthClients(validationCtx *validation.C
307307
if len(addedClientIds) > 0 && len(removedClientIds) > 0 {
308308
validationCtx.Child("oauth", "clients").EmitErrorMessage("client ids cannot be changed")
309309
}
310+
311+
// Validate the final clients length <= 50
312+
const maxFinalOAuthClients = 50
313+
if len(incoming.OAuth.Clients) > maxFinalOAuthClients {
314+
validationCtx.Child("oauth", "clients").EmitErrorMessage(
315+
fmt.Sprintf("exceed the maximum number of oauth clients, actual: %d, expected: %d", len(incoming.OAuth.Clients), maxFinalOAuthClients),
316+
)
317+
}
310318
}
311319

312320
func (d AuthgearYAMLDescriptor) validateBasedOnFeatureConfig(appConfig *config.AppConfig, fc *config.FeatureConfig) error {

pkg/lib/config/oauth.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ var _ = Schema.Add("OAuthConfig", `
55
"type": "object",
66
"additionalProperties": false,
77
"properties": {
8-
"clients": { "type": "array", "items": { "$ref": "#/$defs/OAuthClientConfig" } }
8+
"clients": {
9+
"type": "array",
10+
"items": { "$ref": "#/$defs/OAuthClientConfig" }
11+
}
912
}
1013
}
1114
`)
@@ -220,6 +223,23 @@ var _ = Schema.Add("OAuthClientConfig", `
220223
"then": {
221224
"required": ["redirect_uris"]
222225
}
226+
},
227+
{
228+
"if": {
229+
"properties": {
230+
"x_application_type": {
231+
"enum": ["m2m"]
232+
}
233+
},
234+
"required": ["x_application_type"]
235+
},
236+
"then": {
237+
"properties": {
238+
"access_token_lifetime_seconds": {
239+
"maximum": 2419200
240+
}
241+
}
242+
}
223243
}
224244
]
225245
}

pkg/lib/config/testdata/config_tests.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,3 +1388,20 @@ config:
13881388
- name: Test Client
13891389
client_id: test-client
13901390
x_application_type: m2m
1391+
1392+
---
1393+
name: oauth-m2m-client-access-token-lifetime-exceeds-max
1394+
error: |-
1395+
invalid configuration:
1396+
/oauth/clients/0/access_token_lifetime_seconds: maximum
1397+
map[actual:3e+06 maximum:2.4192e+06]
1398+
config:
1399+
id: test
1400+
http:
1401+
public_origin: http://test
1402+
oauth:
1403+
clients:
1404+
- name: Test Client M2M
1405+
client_id: test-client-m2m
1406+
x_application_type: m2m
1407+
access_token_lifetime_seconds: 3000000

pkg/lib/deps/deps_common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ var CommonDependencySet = wire.NewSet(
540540
wire.Bind(new(otp.RateLimiter), new(*ratelimit.Limiter)),
541541
wire.Bind(new(messaging.RateLimiter), new(*ratelimit.Limiter)),
542542
wire.Bind(new(mfa.RateLimiter), new(*ratelimit.Limiter)),
543+
wire.Bind(new(oauthhandler.TokenHandlerRateLimiter), new(*ratelimit.Limiter)),
543544
),
544545

545546
wire.NewSet(

pkg/lib/oauth/handler/handler_authz.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,12 +645,13 @@ func (h *AuthorizationHandler) finishSettingsAction(
645645
redirectURI *url.URL,
646646
r protocol.AuthorizationRequest,
647647
cookies []*http.Cookie,
648+
userID string,
648649
) (httputil.Result, error) {
649650
resp := protocol.AuthorizationResponse{}
650651
responseType := r.ResponseType()
651652
switch {
652653
case responseType.Equal(SettingsActonResponseType):
653-
err := h.generateSettingsActionResponse(ctx, redirectURI.String(), r, resp)
654+
err := h.generateSettingsActionResponse(ctx, redirectURI.String(), r, resp, userID)
654655
if err != nil {
655656
return nil, err
656657
}
@@ -780,12 +781,14 @@ func (h *AuthorizationHandler) doHandleConsentRequest(
780781
responseType := opts.ConsentRequest.OAuthSessionEntry.T.AuthorizationRequest.ResponseType()
781782
switch {
782783
case responseType.Equal(SettingsActonResponseType):
784+
userID := opts.ConsentRequest.AuthInfoEntry.T.UserID
783785
return h.finishSettingsAction(
784786
ctx,
785787
opts.ConsentRequest.Client,
786788
opts.ConsentRequest.RedirectURI,
787789
opts.ConsentRequest.OAuthSessionEntry.T.AuthorizationRequest,
788790
[]*http.Cookie{},
791+
userID,
789792
)
790793
default:
791794
_, uiInfoByProduct, err := h.UIInfoResolver.ResolveForAuthorizationEndpoint(
@@ -930,10 +933,12 @@ func (h *AuthorizationHandler) generateSettingsActionResponse(
930933
redirectURI string,
931934
r protocol.AuthorizationRequest,
932935
resp protocol.AuthorizationResponse,
936+
userID string,
933937
) error {
934938
code, _, err := h.SettingsActionGrantService.CreateSettingsActionGrant(ctx, &CreateSettingsActionGrantOptions{
935939
RedirectURI: redirectURI,
936940
AuthorizationRequest: r,
941+
UserID: userID,
937942
})
938943
if err != nil {
939944
return err

0 commit comments

Comments
 (0)