Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API OTP Context #6674

Merged
merged 11 commits into from
Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion modules/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,10 @@ func SignedInUser(ctx *macaron.Context, sess session.Store) (*models.User, bool)
}
return nil, false
}
} else {
ctx.Data["IsApiToken"] = true
}

ctx.Data["IsApiToken"] = true
return u, true
}
}
Expand Down
25 changes: 25 additions & 0 deletions modules/context/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ func (ctx *APIContext) RequireCSRF() {
}
}

// CheckForOTP validateds OTP
func (ctx *APIContext) CheckForOTP() {
if ctx.Data["IsApiToken"] == true {
return
}
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) {
Expand Down
11 changes: 7 additions & 4 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,26 @@ func repoAssignment() macaron.Handler {
// Contexter middleware already checks token for user sign in process.
func reqToken() macaron.Handler {
return func(ctx *context.APIContext) {
if true == ctx.Data["IsApiToken"] {
if true == ctx.Data["IsApiToken"] || ctx.Context.IsBasicAuth {
ctx.CheckForOTP()
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
return
}
if ctx.IsSigned {
ctx.RequireCSRF()
ctx.CheckForOTP()
return
}
ctx.Context.Error(401)
}
}

func reqBasicAuth() macaron.Handler {
return func(ctx *context.Context) {
if !ctx.IsBasicAuth {
ctx.Error(401)
return func(ctx *context.APIContext) {
if !ctx.Context.IsBasicAuth {
ctx.Context.Error(401)
return
}
ctx.CheckForOTP()
}
}

Expand Down