Skip to content

Integrate OAuth2 Provider #5378

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

Merged
merged 92 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix some lint and documentation problems
Signed-off-by: Jonas Franz <[email protected]>
  • Loading branch information
jonasfranz committed Dec 22, 2018
commit fd162c1e080c0b14cb588d0063983457ac12b9bb
2 changes: 1 addition & 1 deletion custom/conf/app.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ 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_PRIVATE=Bk0yK7Y9g_p56v86KaHqjSbxvNvu3SbKoOdOt2ZcXvU
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
Expand Down
2 changes: 1 addition & 1 deletion docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
- `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_PRIVATE`: **\<empty\>**: OAuth2 authentication secret for access and refresh tokens, change this a unique string.
- `JWT_SECRET`: **\<empty\>**: OAuth2 authentication secret for access and refresh tokens, change this a unique string.

## i18n (`i18n`)

Expand Down
12 changes: 8 additions & 4 deletions models/oauth2_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func getOAuth2AuthorizationByCode(e Engine, code string) (auth *OAuth2Authorizat
} else if !has {
return nil, nil
}
return auth, nil
return auth, nil
}

//////////////////////////////////////////////////////
Expand Down Expand Up @@ -224,19 +224,22 @@ func (grant *OAuth2Grant) generateNewAuthorizationCode(e Engine, redirectURI str
return code, nil
}


//////////////////////////////////////////////////////////////

// OAuth2TokenType represents the type of token for an oauth application
type OAuth2TokenType int

const (
// TypeAccessToken is a token with short lifetime to access the api
TypeAccessToken OAuth2TokenType = 0
// TypeRefreshToken is token wiht long lifetime to refresh access tokens obtained by the client
TypeRefreshToken = iota
)

// OAuth2Token represents a JWT token used to authenticate a client
type OAuth2Token struct {
GrantID int64 `json:"sub"`
Type OAuth2TokenType `json:"tt"`
GrantID int64 `json:"sub"`
Type OAuth2TokenType `json:"tt"`
jwt.StandardClaims
}

Expand Down Expand Up @@ -266,6 +269,7 @@ func ParseOAuth2Token(jwtToken string) (*OAuth2Token, error) {
return token, nil
}

// SignToken signs the token with the JWT secret
func (token *OAuth2Token) SignToken() (string, error) {
token.IssuedAt = time.Now().Unix()
jwtToken := jwt.NewWithClaims(jwt.SigningMethodHS512, token)
Expand Down
20 changes: 10 additions & 10 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,22 +549,22 @@ var (

// API settings
API = struct {
EnableSwagger bool
MaxResponseItems int
EnableSwagger bool
MaxResponseItems int
}{
EnableSwagger: true,
MaxResponseItems: 50,
EnableSwagger: true,
MaxResponseItems: 50,
}

OAuth2 = struct {
Enable bool
AccessTokenExpirationTime int64
Enable bool
AccessTokenExpirationTime int64
RefreshTokenExpirationTime int64
JWTSecretBytes []byte `ini:"-"`
JWTSecretBase64 string `ini:"JWT_SECRET"`
JWTSecretBytes []byte `ini:"-"`
JWTSecretBase64 string `ini:"JWT_SECRET"`
}{
Enable: true,
AccessTokenExpirationTime: 3600,
Enable: true,
AccessTokenExpirationTime: 3600,
RefreshTokenExpirationTime: 730,
}

Expand Down
49 changes: 32 additions & 17 deletions routers/user/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,20 @@ const (
type AuthorizeErrorCode string

const (
ErrorCodeInvalidRequest AuthorizeErrorCode = "invalid_request"
ErrorCodeUnauthorizedClient AuthorizeErrorCode = "unauthorized_client"
ErrorCodeAccessDenied AuthorizeErrorCode = "access_denied"
// ErrorCodeInvalidRequest represents the according error in RFC 6749
ErrorCodeInvalidRequest AuthorizeErrorCode = "invalid_request"
// ErrorCodeUnauthorizedClient represents the according error in RFC 6749
ErrorCodeUnauthorizedClient AuthorizeErrorCode = "unauthorized_client"
// ErrorCodeAccessDenied represents the according error in RFC 6749
ErrorCodeAccessDenied AuthorizeErrorCode = "access_denied"
// ErrorCodeUnsupportedResponseType represents the according error in RFC 6749
ErrorCodeUnsupportedResponseType AuthorizeErrorCode = "unsupported_response_type"
ErrorCodeInvalidScope AuthorizeErrorCode = "invalid_scope"
ErrorCodeServerError AuthorizeErrorCode = "server_error"
ErrorCodeTemporaryUnavailable AuthorizeErrorCode = "temporarily_unavailable"
// ErrorCodeInvalidScope represents the according error in RFC 6749
ErrorCodeInvalidScope AuthorizeErrorCode = "invalid_scope"
// ErrorCodeServerError represents the according error in RFC 6749
ErrorCodeServerError AuthorizeErrorCode = "server_error"
// ErrorCodeTemporaryUnavailable represents the according error in RFC 6749
ErrorCodeTemporaryUnavailable AuthorizeErrorCode = "temporarily_unavailable"
)

// AuthorizeError represents an error type specified in RFC 6749
Expand All @@ -54,12 +61,18 @@ func (err AuthorizeError) Error() string {
type AccessTokenErrorCode string

const (
AccessTokenErrorCodeInvalidRequest AccessTokenErrorCode = "invalid_request"
AccessTokenErrorCodeInvalidClient = "invalid_client"
AccessTokenErrorCodeInvalidGrant = "invalid_grant"
AccessTokenErrorCodeUnauthorizedClient = "unauthorized_client"
AccessTokenErrorCodeUnsupportedGrantType = "unsupported_grant_type"
AccessTokenErrorCodeInvalidScope = "invalid_scope"
// AccessTokenErrorCodeInvalidRequest represents an error code specified in RFC 6749
AccessTokenErrorCodeInvalidRequest AccessTokenErrorCode = "invalid_request"
// AccessTokenErrorCodeInvalidClient represents an error code specified in RFC 6749
AccessTokenErrorCodeInvalidClient = "invalid_client"
// AccessTokenErrorCodeInvalidGrant represents an error code specified in RFC 6749
AccessTokenErrorCodeInvalidGrant = "invalid_grant"
// AccessTokenErrorCodeUnauthorizedClient represents an error code specified in RFC 6749
AccessTokenErrorCodeUnauthorizedClient = "unauthorized_client"
// AccessTokenErrorCodeUnsupportedGrantType represents an error code specified in RFC 6749
AccessTokenErrorCodeUnsupportedGrantType = "unsupported_grant_type"
// AccessTokenErrorCodeInvalidScope represents an error code specified in RFC 6749
AccessTokenErrorCodeInvalidScope = "invalid_scope"
)

// AccessTokenError represents an error response specified in RFC 6749
Expand All @@ -77,8 +90,10 @@ func (err AccessTokenError) Error() string {
type TokenType string

const (
// TokenTypeBearer represents a token type specified in RFC 6749
TokenTypeBearer TokenType = "bearer"
TokenTypeMAC = "mac"
// TokenTypeMAC represents a token type specified in RFC 6749
TokenTypeMAC = "mac"
)

// AccessTokenResponse represents a successful access token response
Expand Down Expand Up @@ -207,13 +222,13 @@ func AccessTokenOAuth(ctx *context.Context, form auth.AccessTokenForm) {
})
return
}
/* if !app.ValidateClientSecret([]byte(form.ClientSecret)) {
if !app.ValidateClientSecret([]byte(form.ClientSecret)) {
handleAccessTokenError(ctx, AccessTokenError{
ErrorCode: AccessTokenErrorCodeUnauthorizedClient,
ErrorDescription: "client is not authorized",
})
return
}*/
}
authorizationCode, err := models.GetOAuth2AuthorizationByCode(form.Code)
if err != nil || authorizationCode == nil {
handleAccessTokenError(ctx, AccessTokenError{
Expand All @@ -240,7 +255,7 @@ func AccessTokenOAuth(ctx *context.Context, form auth.AccessTokenForm) {
signedAccessToken, err := accessToken.SignToken()
if err != nil {
handleAccessTokenError(ctx, AccessTokenError{
ErrorCode: AccessTokenErrorCodeInvalidRequest,
ErrorCode: AccessTokenErrorCodeInvalidRequest,
ErrorDescription: "cannot sign token",
})
return
Expand All @@ -257,7 +272,7 @@ func AccessTokenOAuth(ctx *context.Context, form auth.AccessTokenForm) {
signedRefreshToken, err := refreshToken.SignToken()
if err != nil {
handleAccessTokenError(ctx, AccessTokenError{
ErrorCode: AccessTokenErrorCodeInvalidRequest,
ErrorCode: AccessTokenErrorCodeInvalidRequest,
ErrorDescription: "cannot sign token",
})
return
Expand Down