@@ -139,19 +139,30 @@ func HTTP(ctx *context.Context) {
139
139
}
140
140
141
141
if authUser == nil {
142
- authUser , err = models .GetUserByName (authUsername )
143
-
144
- if err != nil {
145
- if models .IsErrUserNotExist (err ) {
146
- ctx .HandleText (http .StatusUnauthorized , "invalid credentials" )
147
- } else {
148
- ctx .Handle (http .StatusInternalServerError , "GetUserByName" , err )
142
+ var authToken string
143
+ isUsernameToken := len (authPasswd ) == 0 || authPasswd == "x-oauth-basic"
144
+
145
+
146
+ if isUsernameToken {
147
+ // Assume username is token
148
+ authToken = authUsername
149
+ } else {
150
+ // Assume password is token
151
+ authToken = authPasswd
152
+
153
+ authUser , err = models .GetUserByName (authUsername )
154
+ if err != nil {
155
+ if models .IsErrUserNotExist (err ) {
156
+ ctx .HandleText (http .StatusUnauthorized , "invalid credentials" )
157
+ } else {
158
+ ctx .Handle (http .StatusInternalServerError , "GetUserByName" , err )
159
+ }
160
+ return
149
161
}
150
- return
151
162
}
152
163
153
164
// Assume password is a token.
154
- token , err := models .GetAccessTokenBySHA (authPasswd )
165
+ token , err := models .GetAccessTokenBySHA (authToken )
155
166
if err != nil {
156
167
if models .IsErrAccessTokenNotExist (err ) || models .IsErrAccessTokenEmpty (err ) {
157
168
ctx .HandleText (http .StatusUnauthorized , "invalid credentials" )
@@ -161,16 +172,22 @@ func HTTP(ctx *context.Context) {
161
172
return
162
173
}
163
174
164
- if authUser .ID != token .UID {
165
- ctx .HandleText (http .StatusUnauthorized , "invalid credentials" )
166
- return
175
+ if isUsernameToken {
176
+ authUser , err = models .GetUserByID (token .UID )
177
+ if err != nil {
178
+ ctx .Handle (http .StatusInternalServerError , "GetUserByID" , err )
179
+ }
180
+ } else {
181
+ if authUser .ID != token .UID {
182
+ ctx .HandleText (http .StatusUnauthorized , "invalid credentials" )
183
+ return
184
+ }
167
185
}
168
186
169
187
token .Updated = time .Now ()
170
188
if err = models .UpdateAccessToken (token ); err != nil {
171
189
ctx .Handle (http .StatusInternalServerError , "UpdateAccessToken" , err )
172
190
}
173
-
174
191
} else {
175
192
_ , err = models .GetTwoFactorByUID (authUser .ID )
176
193
0 commit comments