Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate OAuth2 Provider #5378

Merged
merged 92 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
fb1bab1
Add oauth2 application
jonasfranz Nov 12, 2018
8b9fb4c
Update index.css
kolaente Nov 12, 2018
4b18995
Merge remote-tracking branch 'origin/master' into feature/oauth2
jonasfranz Nov 19, 2018
8a2e6f1
Add grant and redirection
jonasfranz Nov 20, 2018
6752e1a
Small improvements
jonasfranz Nov 20, 2018
ee833d1
Add documentation and missing vendors
jonasfranz Nov 22, 2018
114cf36
Merge branch 'master' into feature/oauth2
jonasfranz Nov 22, 2018
cabbbd9
Revert unwanted changes
jonasfranz Nov 22, 2018
6c5ddfb
Merge remote-tracking branch 'origin/master' into feature/oauth2
jonasfranz Nov 22, 2018
ccb990a
Merge branch 'feature/oauth2' of github.com:JonasFranzDEV/gitea into …
jonasfranz Nov 22, 2018
04d3420
Merge branch 'feature/oauth2' of github.com:JonasFranzDEV/gitea into …
jonasfranz Nov 24, 2018
de7210f
Add copyright headers
jonasfranz Nov 24, 2018
25793e6
Add access token cleanup cronjob
jonasfranz Nov 24, 2018
e5435ed
Merge branch 'master' of https://github.com/go-gitea/gitea into featu…
jonasfranz Dec 21, 2018
bd311a6
Use JWT tokens instead of "access tokens"
jonasfranz Dec 22, 2018
fd162c1
Fix some lint and documentation problems
jonasfranz Dec 22, 2018
fb2735c
Fix misspells
jonasfranz Dec 22, 2018
0c07b84
Remove unsused access tokens model changes
jonasfranz Dec 22, 2018
51ffa8a
Add auth code invalidation
jonasfranz Dec 22, 2018
0648d41
Ensure gopkg
jonasfranz Dec 22, 2018
3899a2e
Add oauth2 ui
jonasfranz Dec 23, 2018
1d7316c
Check OAuth2 token
jonasfranz Dec 23, 2018
dce746a
Merge branch 'feature/oauth2' into feature/oauth2-ui
jonasfranz Dec 23, 2018
ee44838
Change authorize and access token endpoint
jonasfranz Dec 26, 2018
f8b099a
Merge remote-tracking branch 'jonas/feature/oauth2' into feature/oauth2
jonasfranz Dec 26, 2018
869589e
Merge branch 'feature/oauth2' into feature/oauth2-ui
jonasfranz Dec 26, 2018
e334f2a
Merge branch 'master' into feature/oauth2
jonasfranz Dec 26, 2018
8773029
Merge branch 'master' of https://github.com/go-gitea/gitea into featu…
jonasfranz Dec 31, 2018
f1ff7c9
Add counter for preventing using a refresh token twice
jonasfranz Dec 31, 2018
d4cc413
Merge branch 'feature/oauth2' into feature/oauth2-ui
jonasfranz Dec 31, 2018
ef14bf7
Improved the counter mechanics
jonasfranz Dec 31, 2018
31112ee
Merge branch 'feature/oauth2' into feature/oauth2-ui
jonasfranz Dec 31, 2018
df63692
Fix increase counter
jonasfranz Dec 31, 2018
f7b7173
Merge remote-tracking branch 'jonas/feature/oauth2' into feature/oauth2
jonasfranz Dec 31, 2018
68a0736
Add comments and fmt
jonasfranz Dec 31, 2018
8a95e72
Merge branch 'master' into feature/oauth2
jonasfranz Dec 31, 2018
72f4ec8
Merge branch 'master' into feature/oauth2
jonasfranz Jan 2, 2019
93c9994
Merge branch 'master' into feature/oauth2
kolaente Jan 6, 2019
a892670
Add PKCE support
jonasfranz Jan 7, 2019
8db5aba
Merge remote-tracking branch 'jonas/feature/oauth2' into feature/oauth2
jonasfranz Jan 7, 2019
a57014a
Merge branch 'master' into feature/oauth2
jonasfranz Jan 7, 2019
2b737a1
Add some unit tests for oauth2_applications
jonasfranz Jan 7, 2019
ddb4244
Merge remote-tracking branch 'jonas/feature/oauth2' into feature/oauth2
jonasfranz Jan 7, 2019
3116afc
Remove unused test
jonasfranz Jan 7, 2019
a89c4de
gofmt
jonasfranz Jan 7, 2019
6e96c77
Merge branch 'master' into feature/oauth2
jonasfranz Jan 15, 2019
14270e1
Add additional tests for OAuth2
jonasfranz Jan 29, 2019
0ab5390
Merge branch 'master' of https://github.com/go-gitea/gitea into featu…
jonasfranz Jan 29, 2019
a4bc672
Merge branch 'master' into feature/oauth2
jonasfranz Feb 1, 2019
84b927d
Merge branch 'jo/feature/oauth2' into jo/feature/oauth2-ui
kolaente Feb 1, 2019
d942c57
Add better secret
jonasfranz Feb 1, 2019
f874fc9
added new fancy design
kolaente Feb 1, 2019
e2919f8
Merge branch 'jo/feature/oauth2-ui' into jo/feature/oauth2
kolaente Feb 1, 2019
3a853e6
Merged ui branch
kolaente Feb 1, 2019
96fea41
Removed unnessecary types
kolaente Feb 1, 2019
72c734c
fixed type not working
kolaente Feb 1, 2019
edf900f
Removed unnessecary types
kolaente Feb 1, 2019
d1a2a02
Removed more unnecessary stuff
kolaente Feb 1, 2019
e0b9307
fixed tests
kolaente Feb 1, 2019
9ce3a3e
Add additional integration tests
jonasfranz Feb 1, 2019
7e7d524
Merge remote-tracking branch 'jonas/feature/oauth2' into feature/oauth2
jonasfranz Feb 1, 2019
e8baddb
UI improvements
kolaente Feb 1, 2019
d645c00
Fixed lint
kolaente Feb 1, 2019
124eab9
Show secrets after generating them
kolaente Feb 2, 2019
8143eeb
Merge branch 'master' into feature/oauth2
kolaente Feb 2, 2019
6e3b4c8
Merge remote-tracking branch 'jonas/feature/oauth2' into feature/oauth2
jonasfranz Feb 2, 2019
b26df39
Add additional integrations tests
jonasfranz Feb 2, 2019
f082e40
Merge branch 'master' of ssh://github.com/go-gitea/gitea into feature…
jonasfranz Feb 3, 2019
7250d4a
Add remove functionality to application settings
jonasfranz Feb 4, 2019
e58c161
Add edit functionality
jonasfranz Feb 4, 2019
792d8e5
Add copyright headers
jonasfranz Feb 4, 2019
3fcfdb5
Add regenerated stylessheet
jonasfranz Feb 4, 2019
7370e05
Merge branch 'master' into feature/oauth2
jonasfranz Feb 4, 2019
82102fa
Fix pgsql/mssql failures with increase counter
jonasfranz Feb 5, 2019
c193062
Merge branch 'master' into feature/oauth2
jonasfranz Feb 5, 2019
a428f1a
Fix wrong bearer detection
jonasfranz Feb 6, 2019
6278b7a
Merge remote-tracking branch 'jonas/feature/oauth2' into feature/oauth2
jonasfranz Feb 6, 2019
2b55fbb
Merge branch 'master' of https://github.com/go-gitea/gitea into featu…
jonasfranz Feb 6, 2019
c2243c9
Update CSS
jonasfranz Feb 6, 2019
5740c12
Apply changes by @lunny
jonasfranz Feb 27, 2019
2262795
Use session to delete applications
jonasfranz Feb 27, 2019
49e2fac
Merge branch 'master' of https://github.com/go-gitea/gitea into featu…
jonasfranz Feb 27, 2019
4bcc28b
Use separate json tag for grant_id
jonasfranz Feb 27, 2019
dc8dc55
Move session begin and commit
jonasfranz Mar 1, 2019
141ae6b
Add authorization error page
jonasfranz Mar 5, 2019
bdf6643
Merge remote-tracking branch 'jonas/feature/oauth2' into feature/oauth2
jonasfranz Mar 5, 2019
709e8d5
Add login as reserved username
jonasfranz Mar 5, 2019
5c31409
Merge branch 'master' of https://github.com/go-gitea/gitea into featu…
jonasfranz Mar 5, 2019
b3c6962
Merge branch 'master' into feature/oauth2
jonasfranz Mar 5, 2019
9e27fea
Merge branch 'master' into feature/oauth2
techknowlogick Mar 8, 2019
ff266f4
generate stylesheets
techknowlogick Mar 8, 2019
9d11393
status 400 instead of 500 because error is with user not server
techknowlogick Mar 8, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func runGenerateInternalToken(c *cli.Context) error {
}

func runGenerateLfsJwtSecret(c *cli.Context) error {
JWTSecretBase64, err := generate.NewLfsJwtSecret()
JWTSecretBase64, err := generate.NewJwtSecret()
if err != nil {
return err
}
Expand Down
10 changes: 10 additions & 0 deletions custom/conf/app.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,16 @@ DEFAULT_PAGING_NUM = 30
; Default and maximum number of items per page for git trees api
DEFAULT_GIT_TREES_PER_PAGE = 1000

[oauth2]
; Enables OAuth2 provider
ENABLED = true
; Lifetime of an OAuth2 access token in seconds
ACCESS_TOKEN_EXPIRATION_TIME=3600
; Lifetime of an OAuth2 access token in hours
REFRESH_TOKEN_EXPIRATION_TIME=730
; OAuth2 authentication secret for access and refresh tokens, change this a unique string.
JWT_SECRET=Bk0yK7Y9g_p56v86KaHqjSbxvNvu3SbKoOdOt2ZcXvU

[i18n]
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
NAMES = English,简体中文,繁體中文(香港),繁體中文(台灣),Deutsch,français,Nederlands,latviešu,русский,Українська,日本語,español,português do Brasil,polski,български,italiano,suomi,Türkçe,čeština,српски,svenska,한국어
Expand Down
7 changes: 7 additions & 0 deletions docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,13 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
- `DEFAULT_PAGING_NUM`: **30**: Default paging number of api.
- `DEFAULT_GIT_TREES_PER_PAGE`: **1000**: Default and maximum number of items per page for git trees api.

## OAuth2 (`oauth2`)

- `ENABLED`: **true**: Enables OAuth2 provider.
- `ACCESS_TOKEN_EXPIRATION_TIME`: **3600**: Lifetime of an OAuth2 access token in seconds
- `REFRESH_TOKEN_EXPIRATION_TIME`: **730**: Lifetime of an OAuth2 access token in hours
- `JWT_SECRET`: **\<empty\>**: OAuth2 authentication secret for access and refresh tokens, change this a unique string.

## i18n (`i18n`)

- `LANGS`: **en-US,zh-CN,zh-HK,zh-TW,de-DE,fr-FR,nl-NL,lv-LV,ru-RU,ja-JP,es-ES,pt-BR,pl-PL,bg-BG,it-IT,fi-FI,tr-TR,cs-CZ,sr-SP,sv-SE,ko-KR**: List of locales shown in language selector
Expand Down
138 changes: 138 additions & 0 deletions integrations/oauth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package integrations
jonasfranz marked this conversation as resolved.
Show resolved Hide resolved

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

const defaultAuthorize = "/login/oauth/authorize?client_id=da7da3ba-9a13-4167-856f-3899de0b0138&redirect_uri=a&response_type=code&state=thestate"

func TestNoClientID(t *testing.T) {
prepareTestEnv(t)
req := NewRequest(t, "GET", "/login/oauth/authorize")
ctx := loginUser(t, "user2")
ctx.MakeRequest(t, req, 400)
}

func TestLoginRedirect(t *testing.T) {
prepareTestEnv(t)
req := NewRequest(t, "GET", "/login/oauth/authorize")
assert.Contains(t, MakeRequest(t, req, 302).Body.String(), "/user/login")
}

func TestShowAuthorize(t *testing.T) {
prepareTestEnv(t)
req := NewRequest(t, "GET", defaultAuthorize)
ctx := loginUser(t, "user4")
resp := ctx.MakeRequest(t, req, 200)

htmlDoc := NewHTMLParser(t, resp.Body)
htmlDoc.AssertElement(t, "#authorize-app", true)
htmlDoc.GetCSRF()
}

func TestRedirectWithExistingGrant(t *testing.T) {
prepareTestEnv(t)
req := NewRequest(t, "GET", defaultAuthorize)
ctx := loginUser(t, "user1")
resp := ctx.MakeRequest(t, req, 302)
u, err := resp.Result().Location()
assert.NoError(t, err)
assert.Equal(t, "thestate", u.Query().Get("state"))
assert.Truef(t, len(u.Query().Get("code")) > 30, "authorization code '%s' should be longer then 30", u.Query().Get("code"))
}

func TestAccessTokenExchange(t *testing.T) {
prepareTestEnv(t)
req := NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
"grant_type": "authorization_code",
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
"client_secret": "4MK8Na6R55smdCY0WuCCumZ6hjRPnGY5saWVRHHjJiA=",
"redirect_uri": "a",
"code": "authcode",
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt", // test PKCE additionally
})
resp := MakeRequest(t, req, 200)
type response struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int64 `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
}
parsed := new(response)
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
assert.True(t, len(parsed.AccessToken) > 10)
assert.True(t, len(parsed.RefreshToken) > 10)
}

func TestAccessTokenExchangeWithoutPKCE(t *testing.T) {
prepareTestEnv(t)
req := NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
"grant_type": "authorization_code",
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
"client_secret": "4MK8Na6R55smdCY0WuCCumZ6hjRPnGY5saWVRHHjJiA=",
"redirect_uri": "a",
"code": "authcode",
})
MakeRequest(t, req, 400)
}

func TestAccessTokenExchangeWithInvalidCredentials(t *testing.T) {
prepareTestEnv(t)
// invalid client id
req := NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
"grant_type": "authorization_code",
"client_id": "???",
"client_secret": "4MK8Na6R55smdCY0WuCCumZ6hjRPnGY5saWVRHHjJiA=",
"redirect_uri": "a",
"code": "authcode",
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt", // test PKCE additionally
})
MakeRequest(t, req, 400)
// invalid client secret
req = NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
"grant_type": "authorization_code",
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
"client_secret": "???",
"redirect_uri": "a",
"code": "authcode",
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt", // test PKCE additionally
})
MakeRequest(t, req, 400)
// invalid redirect uri
req = NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
"grant_type": "authorization_code",
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
"client_secret": "4MK8Na6R55smdCY0WuCCumZ6hjRPnGY5saWVRHHjJiA=",
"redirect_uri": "???",
"code": "authcode",
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt", // test PKCE additionally
})
MakeRequest(t, req, 400)
// invalid authorization code
req = NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
"grant_type": "authorization_code",
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
"client_secret": "4MK8Na6R55smdCY0WuCCumZ6hjRPnGY5saWVRHHjJiA=",
"redirect_uri": "a",
"code": "???",
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt", // test PKCE additionally
})
MakeRequest(t, req, 400)
// invalid grant_type
req = NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
"grant_type": "???",
"client_id": "da7da3ba-9a13-4167-856f-3899de0b0138",
"client_secret": "4MK8Na6R55smdCY0WuCCumZ6hjRPnGY5saWVRHHjJiA=",
"redirect_uri": "a",
"code": "authcode",
"code_verifier": "N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt", // test PKCE additionally
})
MakeRequest(t, req, 400)
}
39 changes: 39 additions & 0 deletions models/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -1398,3 +1398,42 @@ func IsErrReviewNotExist(err error) bool {
func (err ErrReviewNotExist) Error() string {
return fmt.Sprintf("review does not exist [id: %d]", err.ID)
}

// ________ _____ __ .__
// \_____ \ / _ \ __ ___/ |_| |__
// / | \ / /_\ \| | \ __\ | \
// / | \/ | \ | /| | | Y \
// \_______ /\____|__ /____/ |__| |___| /
// \/ \/ \/

// ErrOAuthClientIDInvalid will be thrown if client id cannot be found
type ErrOAuthClientIDInvalid struct {
ClientID string
}

// IsErrOauthClientIDInvalid checks if an error is a ErrReviewNotExist.
func IsErrOauthClientIDInvalid(err error) bool {
_, ok := err.(ErrOAuthClientIDInvalid)
return ok
}

// Error returns the error message
func (err ErrOAuthClientIDInvalid) Error() string {
return fmt.Sprintf("Client ID invalid [Client ID: %s]", err.ClientID)
}

// ErrOAuthApplicationNotFound will be thrown if id cannot be found
type ErrOAuthApplicationNotFound struct {
ID int64
}

// IsErrOAuthApplicationNotFound checks if an error is a ErrReviewNotExist.
func IsErrOAuthApplicationNotFound(err error) bool {
_, ok := err.(ErrOAuthApplicationNotFound)
return ok
}

// Error returns the error message
func (err ErrOAuthApplicationNotFound) Error() string {
return fmt.Sprintf("OAuth application not found [ID: %d]", err.ID)
}
9 changes: 9 additions & 0 deletions models/fixtures/oauth2_application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-
id: 1
uid: 1
name: "Test"
client_id: "da7da3ba-9a13-4167-856f-3899de0b0138"
client_secret: "$2a$10$UYRgUSgekzBp6hYe8pAdc.cgB4Gn06QRKsORUnIYTYQADs.YR/uvi" # bcrypt of "4MK8Na6R55smdCY0WuCCumZ6hjRPnGY5saWVRHHjJiA=
redirect_uris: '["a"]'
created_unix: 1546869730
updated_unix: 1546869730
8 changes: 8 additions & 0 deletions models/fixtures/oauth2_authorization_code.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- id: 1
grant_id: 1
code: "authcode"
code_challenge: "CjvyTLSdR47G5zYenDA-eDWW4lRrO8yvjcWwbD_deOg" # Code Verifier: N1Zo9-8Rfwhkt68r1r29ty8YwIraXR8eh_1Qwxg7yQXsonBt
code_challenge_method: "S256"
redirect_uri: "a"
valid_until: 3546869730

6 changes: 6 additions & 0 deletions models/fixtures/oauth2_grant.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- id: 1
user_id: 1
application_id: 1
counter: 1
created_unix: 1546869730
updated_unix: 1546869730
3 changes: 3 additions & 0 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ func init() {
new(U2FRegistration),
new(TeamUnit),
new(Review),
new(OAuth2Application),
new(OAuth2AuthorizationCode),
new(OAuth2Grant),
)

gonicNames := []string{"SSL", "UID"}
Expand Down
Loading