MCPcopy Create free account
hub / github.com/gogs/gogs / LoginTwoFactorPost

Function LoginTwoFactorPost

internal/route/user/auth.go:207–248  ·  view source on GitHub ↗
(c *context.Context)

Source from the content-addressed store, hash-verified

205}
206
207func LoginTwoFactorPost(c *context.Context) {
208 userID, ok := c.Session.Get("twoFactorUserID").(int64)
209 if !ok {
210 c.NotFound()
211 return
212 }
213
214 t, err := database.Handle.TwoFactors().GetByUserID(c.Req.Context(), userID)
215 if err != nil {
216 c.Error(err, "get two factor by user ID")
217 return
218 }
219
220 passcode := c.Query("passcode")
221 valid, err := t.ValidateTOTP(passcode)
222 if err != nil {
223 c.Error(err, "validate TOTP")
224 return
225 } else if !valid {
226 c.Flash.Error(c.Tr("settings.two_factor_invalid_passcode"))
227 c.RedirectSubpath("/user/login/two_factor")
228 return
229 }
230
231 u, err := database.Handle.Users().GetByID(c.Req.Context(), userID)
232 if err != nil {
233 c.Error(err, "get user by ID")
234 return
235 }
236
237 // Prevent same passcode from being reused
238 if c.Cache.IsExist(userutil.TwoFactorCacheKey(u.ID, passcode)) {
239 c.Flash.Error(c.Tr("settings.two_factor_reused_passcode"))
240 c.RedirectSubpath("/user/login/two_factor")
241 return
242 }
243 if err = c.Cache.Put(userutil.TwoFactorCacheKey(u.ID, passcode), 1, 60); err != nil {
244 log.Error("Failed to put cache 'two factor passcode': %v", err)
245 }
246
247 afterLogin(c, u, c.Session.Get("twoFactorRemember").(bool))
248}
249
250func LoginTwoFactorRecoveryCode(c *context.Context) {
251 _, ok := c.Session.Get("twoFactorUserID").(int64)

Callers

nothing calls this directly

Calls 12

TwoFactorCacheKeyFunction · 0.92
afterLoginFunction · 0.85
GetByUserIDMethod · 0.80
TwoFactorsMethod · 0.80
ValidateTOTPMethod · 0.80
TrMethod · 0.80
RedirectSubpathMethod · 0.80
UsersMethod · 0.80
GetMethod · 0.65
NotFoundMethod · 0.65
GetByIDMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected