Skip to content

Commit e6cd4f3

Browse files
OAuth2 token can be used in basic auth (go-gitea#6747) (go-gitea#6761)
1 parent 30226b4 commit e6cd4f3

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

modules/auth/auth.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2019 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -54,7 +55,7 @@ func SignedInID(ctx *macaron.Context, sess session.Store) int64 {
5455
// Let's see if token is valid.
5556
if len(tokenSHA) > 0 {
5657
if strings.Contains(tokenSHA, ".") {
57-
uid := checkOAuthAccessToken(tokenSHA)
58+
uid := CheckOAuthAccessToken(tokenSHA)
5859
if uid != 0 {
5960
ctx.Data["IsApiToken"] = true
6061
}
@@ -85,7 +86,8 @@ func SignedInID(ctx *macaron.Context, sess session.Store) int64 {
8586
return 0
8687
}
8788

88-
func checkOAuthAccessToken(accessToken string) int64 {
89+
// CheckOAuthAccessToken returns uid of user from oauth token token
90+
func CheckOAuthAccessToken(accessToken string) int64 {
8991
// JWT tokens require a "."
9092
if !strings.Contains(accessToken, ".") {
9193
return 0
@@ -178,6 +180,18 @@ func SignedInUser(ctx *macaron.Context, sess session.Store) (*models.User, bool)
178180
// Assume password is token
179181
authToken = passwd
180182
}
183+
184+
uid := CheckOAuthAccessToken(authToken)
185+
if uid != 0 {
186+
var err error
187+
ctx.Data["IsApiToken"] = true
188+
189+
u, err = models.GetUserByID(uid)
190+
if err != nil {
191+
log.Error(4, "GetUserByID: %v", err)
192+
return nil, false
193+
}
194+
}
181195
token, err := models.GetAccessTokenBySHA(authToken)
182196
if err == nil {
183197
if isUsernameToken {

routers/repo/http.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2019 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -18,6 +19,7 @@ import (
1819
"time"
1920

2021
"code.gitea.io/gitea/models"
22+
"code.gitea.io/gitea/modules/auth"
2123
"code.gitea.io/gitea/modules/base"
2224
"code.gitea.io/gitea/modules/context"
2325
"code.gitea.io/gitea/modules/log"
@@ -151,6 +153,16 @@ func HTTP(ctx *context.Context) {
151153
// Assume password is token
152154
authToken = authPasswd
153155
}
156+
uid := auth.CheckOAuthAccessToken(authToken)
157+
if uid != 0 {
158+
ctx.Data["IsApiToken"] = true
159+
160+
authUser, err = models.GetUserByID(uid)
161+
if err != nil {
162+
ctx.ServerError("GetUserByID", err)
163+
return
164+
}
165+
}
154166
// Assume password is a token.
155167
token, err := models.GetAccessTokenBySHA(authToken)
156168
if err == nil {

0 commit comments

Comments
 (0)