|
1 | 1 | // Copyright 2014 The Gogs Authors. All rights reserved.
|
| 2 | +// Copyright 2019 The Gitea Authors. All rights reserved. |
2 | 3 | // Use of this source code is governed by a MIT-style
|
3 | 4 | // license that can be found in the LICENSE file.
|
4 | 5 |
|
@@ -54,7 +55,7 @@ func SignedInID(ctx *macaron.Context, sess session.Store) int64 {
|
54 | 55 | // Let's see if token is valid.
|
55 | 56 | if len(tokenSHA) > 0 {
|
56 | 57 | if strings.Contains(tokenSHA, ".") {
|
57 |
| - uid := checkOAuthAccessToken(tokenSHA) |
| 58 | + uid := CheckOAuthAccessToken(tokenSHA) |
58 | 59 | if uid != 0 {
|
59 | 60 | ctx.Data["IsApiToken"] = true
|
60 | 61 | }
|
@@ -85,7 +86,8 @@ func SignedInID(ctx *macaron.Context, sess session.Store) int64 {
|
85 | 86 | return 0
|
86 | 87 | }
|
87 | 88 |
|
88 |
| -func checkOAuthAccessToken(accessToken string) int64 { |
| 89 | +// CheckOAuthAccessToken returns uid of user from oauth token token |
| 90 | +func CheckOAuthAccessToken(accessToken string) int64 { |
89 | 91 | // JWT tokens require a "."
|
90 | 92 | if !strings.Contains(accessToken, ".") {
|
91 | 93 | return 0
|
@@ -178,6 +180,18 @@ func SignedInUser(ctx *macaron.Context, sess session.Store) (*models.User, bool)
|
178 | 180 | // Assume password is token
|
179 | 181 | authToken = passwd
|
180 | 182 | }
|
| 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 | + } |
181 | 195 | token, err := models.GetAccessTokenBySHA(authToken)
|
182 | 196 | if err == nil {
|
183 | 197 | if isUsernameToken {
|
|
0 commit comments