Skip to content

Commit bed668c

Browse files
committed
provide both possible authentication solutions
Signed-off-by: David Schneiderbauer <[email protected]>
1 parent a4cd461 commit bed668c

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

routers/repo/http.go

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,30 @@ func HTTP(ctx *context.Context) {
139139
}
140140

141141
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
149161
}
150-
return
151162
}
152163

153164
// Assume password is a token.
154-
token, err := models.GetAccessTokenBySHA(authPasswd)
165+
token, err := models.GetAccessTokenBySHA(authToken)
155166
if err != nil {
156167
if models.IsErrAccessTokenNotExist(err) || models.IsErrAccessTokenEmpty(err) {
157168
ctx.HandleText(http.StatusUnauthorized, "invalid credentials")
@@ -161,16 +172,22 @@ func HTTP(ctx *context.Context) {
161172
return
162173
}
163174

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+
}
167185
}
168186

169187
token.Updated = time.Now()
170188
if err = models.UpdateAccessToken(token); err != nil {
171189
ctx.Handle(http.StatusInternalServerError, "UpdateAccessToken", err)
172190
}
173-
174191
} else {
175192
_, err = models.GetTwoFactorByUID(authUser.ID)
176193

0 commit comments

Comments
 (0)