You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.1 KiB
112 lines
3.1 KiB
7 years ago
|
// Copyright 2018 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 setting
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
4 years ago
|
"net/http"
|
||
7 years ago
|
|
||
|
"code.gitea.io/gitea/models"
|
||
|
"code.gitea.io/gitea/modules/context"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/log"
|
||
7 years ago
|
"code.gitea.io/gitea/modules/setting"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/web"
|
||
4 years ago
|
"code.gitea.io/gitea/services/forms"
|
||
7 years ago
|
|
||
|
"github.com/tstranex/u2f"
|
||
|
)
|
||
|
|
||
|
// U2FRegister initializes the u2f registration procedure
|
||
4 years ago
|
func U2FRegister(ctx *context.Context) {
|
||
4 years ago
|
form := web.GetForm(ctx).(*forms.U2FRegistrationForm)
|
||
7 years ago
|
if form.Name == "" {
|
||
4 years ago
|
ctx.Error(http.StatusConflict)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
challenge, err := u2f.NewChallenge(setting.U2F.AppID, setting.U2F.TrustedFacets)
|
||
|
if err != nil {
|
||
|
ctx.ServerError("NewChallenge", err)
|
||
|
return
|
||
|
}
|
||
5 years ago
|
if err := ctx.Session.Set("u2fChallenge", challenge); err != nil {
|
||
|
ctx.ServerError("Unable to set session key for u2fChallenge", err)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
regs, err := models.GetU2FRegistrationsByUID(ctx.User.ID)
|
||
|
if err != nil {
|
||
|
ctx.ServerError("GetU2FRegistrationsByUID", err)
|
||
|
return
|
||
|
}
|
||
|
for _, reg := range regs {
|
||
|
if reg.Name == form.Name {
|
||
4 years ago
|
ctx.Error(http.StatusConflict, "Name already taken")
|
||
7 years ago
|
return
|
||
|
}
|
||
|
}
|
||
5 years ago
|
if err := ctx.Session.Set("u2fName", form.Name); err != nil {
|
||
|
ctx.ServerError("Unable to set session key for u2fName", err)
|
||
6 years ago
|
return
|
||
|
}
|
||
5 years ago
|
// Here we're just going to try to release the session early
|
||
|
if err := ctx.Session.Release(); err != nil {
|
||
|
// we'll tolerate errors here as they *should* get saved elsewhere
|
||
|
log.Error("Unable to save changes to the session: %v", err)
|
||
|
}
|
||
4 years ago
|
ctx.JSON(http.StatusOK, u2f.NewWebRegisterRequest(challenge, regs.ToRegistrations()))
|
||
7 years ago
|
}
|
||
|
|
||
|
// U2FRegisterPost receives the response of the security key
|
||
4 years ago
|
func U2FRegisterPost(ctx *context.Context) {
|
||
|
response := web.GetForm(ctx).(*u2f.RegisterResponse)
|
||
7 years ago
|
challSess := ctx.Session.Get("u2fChallenge")
|
||
|
u2fName := ctx.Session.Get("u2fName")
|
||
|
if challSess == nil || u2fName == nil {
|
||
|
ctx.ServerError("U2FRegisterPost", errors.New("not in U2F session"))
|
||
|
return
|
||
|
}
|
||
|
challenge := challSess.(*u2f.Challenge)
|
||
|
name := u2fName.(string)
|
||
|
config := &u2f.Config{
|
||
|
// Chrome 66+ doesn't return the device's attestation
|
||
|
// certificate by default.
|
||
|
SkipAttestationVerify: true,
|
||
|
}
|
||
4 years ago
|
reg, err := u2f.Register(*response, *challenge, config)
|
||
7 years ago
|
if err != nil {
|
||
|
ctx.ServerError("u2f.Register", err)
|
||
|
return
|
||
|
}
|
||
|
if _, err = models.CreateRegistration(ctx.User, name, reg); err != nil {
|
||
|
ctx.ServerError("u2f.Register", err)
|
||
|
return
|
||
|
}
|
||
|
ctx.Status(200)
|
||
|
}
|
||
|
|
||
|
// U2FDelete deletes an security key by id
|
||
4 years ago
|
func U2FDelete(ctx *context.Context) {
|
||
4 years ago
|
form := web.GetForm(ctx).(*forms.U2FDeleteForm)
|
||
7 years ago
|
reg, err := models.GetU2FRegistrationByID(form.ID)
|
||
|
if err != nil {
|
||
|
if models.IsErrU2FRegistrationNotExist(err) {
|
||
|
ctx.Status(200)
|
||
|
return
|
||
|
}
|
||
|
ctx.ServerError("GetU2FRegistrationByID", err)
|
||
|
return
|
||
|
}
|
||
|
if reg.UserID != ctx.User.ID {
|
||
|
ctx.Status(401)
|
||
|
return
|
||
|
}
|
||
|
if err := models.DeleteRegistration(reg); err != nil {
|
||
|
ctx.ServerError("DeleteRegistration", err)
|
||
|
return
|
||
|
}
|
||
4 years ago
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
||
7 years ago
|
"redirect": setting.AppSubURL + "/user/settings/security",
|
||
|
})
|
||
|
}
|