Skip to content

Commit ba12463

Browse files
techknowlogicklunny
authored andcommitted
backport #6674 - API OTP Context (#6676)
1 parent 0acaa6b commit ba12463

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

modules/auth/auth.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,10 @@ func SignedInUser(ctx *macaron.Context, sess session.Store) (*models.User, bool)
214214
}
215215
return nil, false
216216
}
217+
} else {
218+
ctx.Data["IsApiToken"] = true
217219
}
218220

219-
ctx.Data["IsApiToken"] = true
220221
return u, true
221222
}
222223
}

modules/context/api.go

+22
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,28 @@ func (ctx *APIContext) RequireCSRF() {
110110
}
111111
}
112112

113+
// CheckForOTP validateds OTP
114+
func (ctx *APIContext) CheckForOTP() {
115+
otpHeader := ctx.Req.Header.Get("X-Gitea-OTP")
116+
twofa, err := models.GetTwoFactorByUID(ctx.Context.User.ID)
117+
if err != nil {
118+
if models.IsErrTwoFactorNotEnrolled(err) {
119+
return // No 2FA enrollment for this user
120+
}
121+
ctx.Context.Error(500)
122+
return
123+
}
124+
ok, err := twofa.ValidateTOTP(otpHeader)
125+
if err != nil {
126+
ctx.Context.Error(500)
127+
return
128+
}
129+
if !ok {
130+
ctx.Context.Error(401)
131+
return
132+
}
133+
}
134+
113135
// APIContexter returns apicontext as macaron middleware
114136
func APIContexter() macaron.Handler {
115137
return func(c *Context) {

modules/context/auth.go

+24
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2019 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

56
package context
67

78
import (
9+
"code.gitea.io/gitea/models"
810
"code.gitea.io/gitea/modules/auth"
911
"code.gitea.io/gitea/modules/log"
1012
"code.gitea.io/gitea/modules/setting"
@@ -88,6 +90,28 @@ func Toggle(options *ToggleOptions) macaron.Handler {
8890
ctx.HTML(200, "user/auth/activate")
8991
return
9092
}
93+
if ctx.IsSigned && auth.IsAPIPath(ctx.Req.URL.Path) && ctx.IsBasicAuth {
94+
twofa, err := models.GetTwoFactorByUID(ctx.User.ID)
95+
if err != nil {
96+
if models.IsErrTwoFactorNotEnrolled(err) {
97+
return // No 2FA enrollment for this user
98+
}
99+
ctx.Error(500)
100+
return
101+
}
102+
otpHeader := ctx.Req.Header.Get("X-Gitea-OTP")
103+
ok, err := twofa.ValidateTOTP(otpHeader)
104+
if err != nil {
105+
ctx.Error(500)
106+
return
107+
}
108+
if !ok {
109+
ctx.JSON(403, map[string]string{
110+
"message": "Only signed in user is allowed to call APIs.",
111+
})
112+
return
113+
}
114+
}
91115
}
92116

93117
// Redirect to log in page if auto-signin info is provided and has not signed in.

routers/api/v1/api.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ func reqToken() macaron.Handler {
172172
if true == ctx.Data["IsApiToken"] {
173173
return
174174
}
175+
if ctx.Context.IsBasicAuth {
176+
ctx.CheckForOTP()
177+
return
178+
}
175179
if ctx.IsSigned {
176180
ctx.RequireCSRF()
177181
return
@@ -181,11 +185,12 @@ func reqToken() macaron.Handler {
181185
}
182186

183187
func reqBasicAuth() macaron.Handler {
184-
return func(ctx *context.Context) {
185-
if !ctx.IsBasicAuth {
186-
ctx.Error(401)
188+
return func(ctx *context.APIContext) {
189+
if !ctx.Context.IsBasicAuth {
190+
ctx.Context.Error(401)
187191
return
188192
}
193+
ctx.CheckForOTP()
189194
}
190195
}
191196

0 commit comments

Comments
 (0)