@@ -143,24 +143,24 @@ func HTTP(ctx *context.Context) {
143
143
return
144
144
}
145
145
146
- authUser , err = models .UserSignIn (authUsername , authPasswd )
147
- if err != nil {
148
- if ! models .IsErrUserNotExist (err ) {
149
- ctx .ServerError ("UserSignIn error: %v" , err )
150
- return
151
- }
146
+ // Check if username or password is a token
147
+ isUsernameToken := len (authPasswd ) == 0 || authPasswd == "x-oauth-basic"
148
+ // Assume username is token
149
+ authToken := authUsername
150
+ if ! isUsernameToken {
151
+ // Assume password is token
152
+ authToken = authPasswd
152
153
}
153
-
154
- if authUser == nil {
155
- isUsernameToken := len (authPasswd ) == 0 || authPasswd == "x-oauth-basic"
156
-
157
- // Assume username is token
158
- authToken := authUsername
159
-
160
- if ! isUsernameToken {
161
- // Assume password is token
162
- authToken = authPasswd
163
-
154
+ // Assume password is a token.
155
+ token , err := models .GetAccessTokenBySHA (authToken )
156
+ if err == nil {
157
+ if isUsernameToken {
158
+ authUser , err = models .GetUserByID (token .UID )
159
+ if err != nil {
160
+ ctx .ServerError ("GetUserByID" , err )
161
+ return
162
+ }
163
+ } else {
164
164
authUser , err = models .GetUserByName (authUsername )
165
165
if err != nil {
166
166
if models .IsErrUserNotExist (err ) {
@@ -170,37 +170,37 @@ func HTTP(ctx *context.Context) {
170
170
}
171
171
return
172
172
}
173
- }
174
-
175
- // Assume password is a token.
176
- token , err := models .GetAccessTokenBySHA (authToken )
177
- if err != nil {
178
- if models .IsErrAccessTokenNotExist (err ) || models .IsErrAccessTokenEmpty (err ) {
173
+ if authUser .ID != token .UID {
179
174
ctx .HandleText (http .StatusUnauthorized , "invalid credentials" )
180
- } else {
181
- ctx .ServerError ("GetAccessTokenBySha" , err )
175
+ return
182
176
}
183
- return
184
177
}
178
+ token .UpdatedUnix = util .TimeStampNow ()
179
+ if err = models .UpdateAccessToken (token ); err != nil {
180
+ ctx .ServerError ("UpdateAccessToken" , err )
181
+ }
182
+ } else {
183
+ if ! models .IsErrAccessTokenNotExist (err ) && ! models .IsErrAccessTokenEmpty (err ) {
184
+ log .Error (4 , "GetAccessTokenBySha: %v" , err )
185
+ }
186
+ }
185
187
186
- if isUsernameToken {
187
- authUser , err = models .GetUserByID (token .UID )
188
- if err != nil {
189
- ctx .ServerError ("GetUserByID" , err )
188
+ if authUser == nil {
189
+ // Check username and password
190
+ authUser , err = models .UserSignIn (authUsername , authPasswd )
191
+ if err != nil {
192
+ if ! models .IsErrUserNotExist (err ) {
193
+ ctx .ServerError ("UserSignIn error: %v" , err )
190
194
return
191
195
}
192
- } else if authUser .ID != token .UID {
196
+ }
197
+
198
+ if authUser == nil {
193
199
ctx .HandleText (http .StatusUnauthorized , "invalid credentials" )
194
200
return
195
201
}
196
202
197
- token .UpdatedUnix = util .TimeStampNow ()
198
- if err = models .UpdateAccessToken (token ); err != nil {
199
- ctx .ServerError ("UpdateAccessToken" , err )
200
- }
201
- } else {
202
203
_ , err = models .GetTwoFactorByUID (authUser .ID )
203
-
204
204
if err == nil {
205
205
// TODO: This response should be changed to "invalid credentials" for security reasons once the expectation behind it (creating an app token to authenticate) is properly documented
206
206
ctx .HandleText (http .StatusUnauthorized , "Users with two-factor authentication enabled cannot perform HTTP/HTTPS operations via plain username and password. Please create and use a personal access token on the user settings page" )
0 commit comments