Skip to content

Commit f692f16

Browse files
authored
Merge branch 'main' into api_user-settings
2 parents 3009e3f + d5be9dd commit f692f16

File tree

20 files changed

+518
-62
lines changed

20 files changed

+518
-62
lines changed

cmd/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func runGenerateInternalToken(c *cli.Context) error {
7171
}
7272

7373
func runGenerateLfsJwtSecret(c *cli.Context) error {
74-
JWTSecretBase64, err := generate.NewJwtSecret()
74+
JWTSecretBase64, err := generate.NewJwtSecretBase64()
7575
if err != nil {
7676
return err
7777
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,9 @@ NB: You must have `DISABLE_ROUTER_LOG` set to `false` for this option to take ef
858858
- `ACCESS_TOKEN_EXPIRATION_TIME`: **3600**: Lifetime of an OAuth2 access token in seconds
859859
- `REFRESH_TOKEN_EXPIRATION_TIME`: **730**: Lifetime of an OAuth2 refresh token in hours
860860
- `INVALIDATE_REFRESH_TOKENS`: **false**: Check if refresh token has already been used
861-
- `JWT_SECRET`: **\<empty\>**: OAuth2 authentication secret for access and refresh tokens, change this a unique string.
861+
- `JWT_SIGNING_ALGORITHM`: **RS256**: Algorithm used to sign OAuth2 tokens. Valid values: \[`HS256`, `HS384`, `HS512`, `RS256`, `RS384`, `RS512`, `ES256`, `ES384`, `ES512`\]
862+
- `JWT_SECRET`: **\<empty\>**: OAuth2 authentication secret for access and refresh tokens, change this to a unique string. This setting is only needed if `JWT_SIGNING_ALGORITHM` is set to `HS256`, `HS384` or `HS512`.
863+
- `JWT_SIGNING_PRIVATE_KEY_FILE`: **jwt/private.pem**: Private key file path used to sign OAuth2 tokens. The path is relative to `CUSTOM_PATH`. This setting is only needed if `JWT_SIGNING_ALGORITHM` is set to `RS256`, `RS384`, `RS512`, `ES256`, `ES384` or `ES512`. The file must contain a RSA or ECDSA private key in the PKCS8 format.
862864
- `MAX_TOKEN_LENGTH`: **32767**: Maximum length of token/cookie to accept from OAuth2 provider
863865

864866
## i18n (`i18n`)

docs/content/doc/developers/oauth2-provider.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ Gitea supports acting as an OAuth2 provider to allow third party applications to
2323

2424
## Endpoints
2525

26-
| Endpoint | URL |
27-
| ---------------------- | --------------------------- |
28-
| Authorization Endpoint | `/login/oauth/authorize` |
29-
| Access Token Endpoint | `/login/oauth/access_token` |
26+
| Endpoint | URL |
27+
| ------------------------ | ----------------------------------- |
28+
| OpenID Connect Discovery | `/.well-known/openid-configuration` |
29+
| Authorization Endpoint | `/login/oauth/authorize` |
30+
| Access Token Endpoint | `/login/oauth/access_token` |
31+
| OpenID Connect UserInfo | `/login/oauth/userinfo` |
32+
| JSON Web Key Set | `/login/oauth/keys` |
3033

3134
## Supported OAuth2 Grants
3235

integrations/api_repo_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func TestAPIViewRepo(t *testing.T) {
223223
DecodeJSON(t, resp, &repo)
224224
assert.EqualValues(t, 1, repo.ID)
225225
assert.EqualValues(t, "repo1", repo.Name)
226-
assert.EqualValues(t, 3, repo.Releases)
226+
assert.EqualValues(t, 2, repo.Releases)
227227
assert.EqualValues(t, 1, repo.OpenIssues)
228228
assert.EqualValues(t, 3, repo.OpenPulls)
229229

@@ -466,7 +466,7 @@ func TestAPIRepoTransfer(t *testing.T) {
466466
session := loginUser(t, user.Name)
467467
token := getTokenForLoggedInUser(t, session)
468468
repoName := "moveME"
469-
repo := new(models.Repository)
469+
apiRepo := new(api.Repository)
470470
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/user/repos?token=%s", token), &api.CreateRepoOption{
471471
Name: repoName,
472472
Description: "repo move around",
@@ -475,12 +475,12 @@ func TestAPIRepoTransfer(t *testing.T) {
475475
AutoInit: true,
476476
})
477477
resp := session.MakeRequest(t, req, http.StatusCreated)
478-
DecodeJSON(t, resp, repo)
478+
DecodeJSON(t, resp, apiRepo)
479479

480480
//start testing
481481
for _, testCase := range testCases {
482482
user = models.AssertExistsAndLoadBean(t, &models.User{ID: testCase.ctxUserID}).(*models.User)
483-
repo = models.AssertExistsAndLoadBean(t, &models.Repository{ID: repo.ID}).(*models.Repository)
483+
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: apiRepo.ID}).(*models.Repository)
484484
session = loginUser(t, user.Name)
485485
token = getTokenForLoggedInUser(t, session)
486486
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/transfer?token=%s", repo.OwnerName, repo.Name, token), &api.TransferRepoOption{
@@ -491,7 +491,7 @@ func TestAPIRepoTransfer(t *testing.T) {
491491
}
492492

493493
//cleanup
494-
repo = models.AssertExistsAndLoadBean(t, &models.Repository{ID: repo.ID}).(*models.Repository)
494+
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: apiRepo.ID}).(*models.Repository)
495495
_ = models.DeleteRepository(user, repo.OwnerID, repo.ID)
496496
}
497497

models/oauth2.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ func GetActiveOAuth2Providers() ([]string, map[string]OAuth2Provider, error) {
132132

133133
// InitOAuth2 initialize the OAuth2 lib and register all active OAuth2 providers in the library
134134
func InitOAuth2() error {
135+
if err := oauth2.InitSigningKey(); err != nil {
136+
return err
137+
}
135138
if err := oauth2.Init(x); err != nil {
136139
return err
137140
}

models/oauth2_application.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"strings"
1313
"time"
1414

15+
"code.gitea.io/gitea/modules/auth/oauth2"
1516
"code.gitea.io/gitea/modules/secret"
16-
"code.gitea.io/gitea/modules/setting"
1717
"code.gitea.io/gitea/modules/timeutil"
1818
"code.gitea.io/gitea/modules/util"
1919

@@ -540,10 +540,10 @@ type OAuth2Token struct {
540540
// ParseOAuth2Token parses a singed jwt string
541541
func ParseOAuth2Token(jwtToken string) (*OAuth2Token, error) {
542542
parsedToken, err := jwt.ParseWithClaims(jwtToken, &OAuth2Token{}, func(token *jwt.Token) (interface{}, error) {
543-
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
543+
if token.Method == nil || token.Method.Alg() != oauth2.DefaultSigningKey.SigningMethod().Alg() {
544544
return nil, fmt.Errorf("unexpected signing algo: %v", token.Header["alg"])
545545
}
546-
return setting.OAuth2.JWTSecretBytes, nil
546+
return oauth2.DefaultSigningKey.VerifyKey(), nil
547547
})
548548
if err != nil {
549549
return nil, err
@@ -559,8 +559,9 @@ func ParseOAuth2Token(jwtToken string) (*OAuth2Token, error) {
559559
// SignToken signs the token with the JWT secret
560560
func (token *OAuth2Token) SignToken() (string, error) {
561561
token.IssuedAt = time.Now().Unix()
562-
jwtToken := jwt.NewWithClaims(jwt.SigningMethodHS512, token)
563-
return jwtToken.SignedString(setting.OAuth2.JWTSecretBytes)
562+
jwtToken := jwt.NewWithClaims(oauth2.DefaultSigningKey.SigningMethod(), token)
563+
oauth2.DefaultSigningKey.PreProcessToken(jwtToken)
564+
return jwtToken.SignedString(oauth2.DefaultSigningKey.SignKey())
564565
}
565566

566567
// OIDCToken represents an OpenID Connect id_token
@@ -583,8 +584,9 @@ type OIDCToken struct {
583584
}
584585

585586
// SignToken signs an id_token with the (symmetric) client secret key
586-
func (token *OIDCToken) SignToken(clientSecret string) (string, error) {
587+
func (token *OIDCToken) SignToken(signingKey oauth2.JWTSigningKey) (string, error) {
587588
token.IssuedAt = time.Now().Unix()
588-
jwtToken := jwt.NewWithClaims(jwt.SigningMethodHS256, token)
589-
return jwtToken.SignedString([]byte(clientSecret))
589+
jwtToken := jwt.NewWithClaims(signingKey.SigningMethod(), token)
590+
signingKey.PreProcessToken(jwtToken)
591+
return jwtToken.SignedString(signingKey.SignKey())
590592
}

0 commit comments

Comments
 (0)