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