Skip to content

Commit 11a3f27

Browse files
zeripathtechknowlogick
authored and
Yohann Delafollye
committed
Add option to increase provided OAuth2 token maximum size (go-gitea#11180)
Some OAuth2 providers return quite large structured tokens >32767 bytes. Gitea currently has a fixed maximum of 32767 bytes for these and unfortunately due to the convoluted nature of the dependent libraries the error returned is rather opaque. Here we manage the error a little better - detecting the rather opaque github.com/gorilla/securecookie.errEncodedValueTooLong and converting it to a more readable error. Further we provide a configurable option to increase the maximum size of the provided OAuth2 tokens. Fix go-gitea#9907 Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 74d907c commit 11a3f27

File tree

5 files changed

+12
-3
lines changed

5 files changed

+12
-3
lines changed

custom/conf/app.ini.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,8 @@ REFRESH_TOKEN_EXPIRATION_TIME=730
916916
INVALIDATE_REFRESH_TOKENS=false
917917
; OAuth2 authentication secret for access and refresh tokens, change this to a unique string.
918918
JWT_SECRET=Bk0yK7Y9g_p56v86KaHqjSbxvNvu3SbKoOdOt2ZcXvU
919+
; Maximum length of oauth2 token/cookie stored on server
920+
MAX_TOKEN_LENGTH=32767
919921

920922
[i18n]
921923
LANGS = en-US,zh-CN,zh-HK,zh-TW,de-DE,fr-FR,nl-NL,lv-LV,ru-RU,uk-UA,ja-JP,es-ES,pt-BR,pl-PL,bg-BG,it-IT,fi-FI,tr-TR,cs-CZ,sr-SP,sv-SE,ko-KR

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ NB: You must `REDIRECT_MACARON_LOG` and have `DISABLE_ROUTER_LOG` set to `false`
587587
- `REFRESH_TOKEN_EXPIRATION_TIME`: **730**: Lifetime of an OAuth2 access token in hours
588588
- `INVALIDATE_REFRESH_TOKEN`: **false**: Check if refresh token got already used
589589
- `JWT_SECRET`: **\<empty\>**: OAuth2 authentication secret for access and refresh tokens, change this a unique string.
590+
- `MAX_TOKEN_LENGTH`: **32767**: Maximum length of token/cookie to accept from OAuth2 provider
590591

591592
## i18n (`i18n`)
592593

modules/auth/oauth2/oauth2.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package oauth2
66

77
import (
8-
"math"
98
"net/http"
109

1110
"code.gitea.io/gitea/modules/log"
@@ -26,7 +25,7 @@ import (
2625
"github.com/markbates/goth/providers/openidConnect"
2726
"github.com/markbates/goth/providers/twitter"
2827
"github.com/markbates/goth/providers/yandex"
29-
"github.com/satori/go.uuid"
28+
uuid "github.com/satori/go.uuid"
3029
"xorm.io/xorm"
3130
)
3231

@@ -58,7 +57,7 @@ func Init(x *xorm.Engine) error {
5857
// when using OpenID Connect , since this can contain a large amount of extra information in the id_token
5958

6059
// Note, when using the FilesystemStore only the session.ID is written to a browser cookie, so this is explicit for the storage on disk
61-
store.MaxLength(math.MaxInt16)
60+
store.MaxLength(setting.OAuth2.MaxTokenLength)
6261
gothic.Store = store
6362

6463
gothic.SetState = func(req *http.Request) string {

modules/setting/setting.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"io"
1212
"io/ioutil"
13+
"math"
1314
"net"
1415
"net/url"
1516
"os"
@@ -323,11 +324,13 @@ var (
323324
InvalidateRefreshTokens bool
324325
JWTSecretBytes []byte `ini:"-"`
325326
JWTSecretBase64 string `ini:"JWT_SECRET"`
327+
MaxTokenLength int
326328
}{
327329
Enable: true,
328330
AccessTokenExpirationTime: 3600,
329331
RefreshTokenExpirationTime: 730,
330332
InvalidateRefreshTokens: false,
333+
MaxTokenLength: math.MaxInt16,
331334
}
332335

333336
U2F = struct {

routers/user/auth.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,10 @@ func oAuth2UserLoginCallback(loginSource *models.LoginSource, request *http.Requ
670670
gothUser, err := oauth2.ProviderCallback(loginSource.Name, request, response)
671671

672672
if err != nil {
673+
if err.Error() == "securecookie: the value is too long" {
674+
log.Error("OAuth2 Provider %s returned too long a token. Current max: %d. Either increase the [OAuth2] MAX_TOKEN_LENGTH or reduce the information returned from the OAuth2 provider", loginSource.Name, setting.OAuth2.MaxTokenLength)
675+
err = fmt.Errorf("OAuth2 Provider %s returned too long a token. Current max: %d. Either increase the [OAuth2] MAX_TOKEN_LENGTH or reduce the information returned from the OAuth2 provider", loginSource.Name, setting.OAuth2.MaxTokenLength)
676+
}
673677
return nil, goth.User{}, err
674678
}
675679

0 commit comments

Comments
 (0)