diff options
author | techknowlogick <matti@mdranta.net> | 2019-04-19 04:59:26 -0400 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2019-04-19 11:59:26 +0300 |
commit | 19ec2606e91610421a3e9cd87c94748ef07ca468 (patch) | |
tree | c1898300efe5ad0bd2fa07ab3268d21804c6b5ed /modules/context | |
parent | dae94e33be52ca8749421165ee662d7f1300d115 (diff) | |
download | gitea-19ec2606e91610421a3e9cd87c94748ef07ca468.tar.gz gitea-19ec2606e91610421a3e9cd87c94748ef07ca468.zip |
API OTP Context (#6674)
* API OTP Context
* Update api.go
* token
* token
* fix per discord
* copyright header
* remove check for token in OTP
* Update auth.go
* simplify
* Update api.go
Diffstat (limited to 'modules/context')
-rw-r--r-- | modules/context/api.go | 22 | ||||
-rw-r--r-- | modules/context/auth.go | 24 |
2 files changed, 46 insertions, 0 deletions
diff --git a/modules/context/api.go b/modules/context/api.go index 7e43d1f6bc..cbabfe40e1 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -114,6 +114,28 @@ func (ctx *APIContext) RequireCSRF() { } } +// CheckForOTP validateds OTP +func (ctx *APIContext) CheckForOTP() { + otpHeader := ctx.Req.Header.Get("X-Gitea-OTP") + twofa, err := models.GetTwoFactorByUID(ctx.Context.User.ID) + if err != nil { + if models.IsErrTwoFactorNotEnrolled(err) { + return // No 2FA enrollment for this user + } + ctx.Context.Error(500) + return + } + ok, err := twofa.ValidateTOTP(otpHeader) + if err != nil { + ctx.Context.Error(500) + return + } + if !ok { + ctx.Context.Error(401) + return + } +} + // APIContexter returns apicontext as macaron middleware func APIContexter() macaron.Handler { return func(c *Context) { diff --git a/modules/context/auth.go b/modules/context/auth.go index ca897de6ed..772403bda9 100644 --- a/modules/context/auth.go +++ b/modules/context/auth.go @@ -1,10 +1,12 @@ // Copyright 2014 The Gogs Authors. All rights reserved. +// Copyright 2019 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. package context import ( + "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/auth" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -88,6 +90,28 @@ func Toggle(options *ToggleOptions) macaron.Handler { ctx.HTML(200, "user/auth/activate") return } + if ctx.IsSigned && auth.IsAPIPath(ctx.Req.URL.Path) && ctx.IsBasicAuth { + twofa, err := models.GetTwoFactorByUID(ctx.User.ID) + if err != nil { + if models.IsErrTwoFactorNotEnrolled(err) { + return // No 2FA enrollment for this user + } + ctx.Error(500) + return + } + otpHeader := ctx.Req.Header.Get("X-Gitea-OTP") + ok, err := twofa.ValidateTOTP(otpHeader) + if err != nil { + ctx.Error(500) + return + } + if !ok { + ctx.JSON(403, map[string]string{ + "message": "Only signed in user is allowed to call APIs.", + }) + return + } + } } // Redirect to log in page if auto-signin info is provided and has not signed in. |