@@ -113,24 +113,24 @@ func HTTP(ctx *context.Context) {
113
113
return
114
114
}
115
115
116
- authUser , err = models .UserSignIn (authUsername , authPasswd )
117
- if err != nil {
118
- if ! models .IsErrUserNotExist (err ) {
119
- ctx .ServerError ("UserSignIn error: %v" , err )
120
- return
121
- }
116
+ // Check if username or password is a token
117
+ isUsernameToken := len (authPasswd ) == 0 || authPasswd == "x-oauth-basic"
118
+ // Assume username is token
119
+ authToken := authUsername
120
+ if ! isUsernameToken {
121
+ // Assume password is token
122
+ authToken = authPasswd
122
123
}
123
-
124
- if authUser == nil {
125
- isUsernameToken := len (authPasswd ) == 0 || authPasswd == "x-oauth-basic"
126
-
127
- // Assume username is token
128
- authToken := authUsername
129
-
130
- if ! isUsernameToken {
131
- // Assume password is token
132
- authToken = authPasswd
133
-
124
+ // Assume password is a token.
125
+ token , err := models .GetAccessTokenBySHA (authToken )
126
+ if err == nil {
127
+ if isUsernameToken {
128
+ authUser , err = models .GetUserByID (token .UID )
129
+ if err != nil {
130
+ ctx .ServerError ("GetUserByID" , err )
131
+ return
132
+ }
133
+ } else {
134
134
authUser , err = models .GetUserByName (authUsername )
135
135
if err != nil {
136
136
if models .IsErrUserNotExist (err ) {
@@ -140,37 +140,37 @@ func HTTP(ctx *context.Context) {
140
140
}
141
141
return
142
142
}
143
- }
144
-
145
- // Assume password is a token.
146
- token , err := models .GetAccessTokenBySHA (authToken )
147
- if err != nil {
148
- if models .IsErrAccessTokenNotExist (err ) || models .IsErrAccessTokenEmpty (err ) {
143
+ if authUser .ID != token .UID {
149
144
ctx .HandleText (http .StatusUnauthorized , "invalid credentials" )
150
- } else {
151
- ctx .ServerError ("GetAccessTokenBySha" , err )
145
+ return
152
146
}
153
- return
154
147
}
148
+ token .UpdatedUnix = util .TimeStampNow ()
149
+ if err = models .UpdateAccessToken (token ); err != nil {
150
+ ctx .ServerError ("UpdateAccessToken" , err )
151
+ }
152
+ } else {
153
+ if ! models .IsErrAccessTokenNotExist (err ) && ! models .IsErrAccessTokenEmpty (err ) {
154
+ log .Error (4 , "GetAccessTokenBySha: %v" , err )
155
+ }
156
+ }
155
157
156
- if isUsernameToken {
157
- authUser , err = models .GetUserByID (token .UID )
158
- if err != nil {
159
- ctx .ServerError ("GetUserByID" , err )
158
+ if authUser == nil {
159
+ // Check username and password
160
+ authUser , err = models .UserSignIn (authUsername , authPasswd )
161
+ if err != nil {
162
+ if ! models .IsErrUserNotExist (err ) {
163
+ ctx .ServerError ("UserSignIn error: %v" , err )
160
164
return
161
165
}
162
- } else if authUser .ID != token .UID {
166
+ }
167
+
168
+ if authUser == nil {
163
169
ctx .HandleText (http .StatusUnauthorized , "invalid credentials" )
164
170
return
165
171
}
166
172
167
- token .UpdatedUnix = util .TimeStampNow ()
168
- if err = models .UpdateAccessToken (token ); err != nil {
169
- ctx .ServerError ("UpdateAccessToken" , err )
170
- }
171
- } else {
172
173
_ , err = models .GetTwoFactorByUID (authUser .ID )
173
-
174
174
if err == nil {
175
175
// 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
176
176
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