Compare commits
No commits in common. "27e7ea7d7a0bbf6c147ff625591acf6376c6c62d" and "f3172b0d31f844b95a0e64b3a5d821f71b0fbe07" have entirely different histories.
27e7ea7d7a
...
f3172b0d31
|
@ -14,7 +14,6 @@ import (
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type AccountController struct {
|
type AccountController struct {
|
||||||
|
@ -117,7 +116,7 @@ func (a *AccountController) PostRegister() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(r.Pubkey) > 0 {
|
if len(r.Pubkey) > 0 {
|
||||||
if err := tx.Create(&model.Key{Account: account, Pubkey: strings.ToLower(r.Pubkey)}).Error; err != nil {
|
if err := tx.Create(&model.Key{Account: account, Pubkey: r.Pubkey}).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,6 @@ import (
|
||||||
"github.com/kataras/jwt"
|
"github.com/kataras/jwt"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -161,13 +160,6 @@ func (a *AuthController) PostLogin() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if account.Password == nil || len(*account.Password) == 0 {
|
|
||||||
msg := "only pubkey login is supported"
|
|
||||||
logger.Get().Debug(msg)
|
|
||||||
a.Ctx.StopWithError(iris.StatusBadRequest, errors.New(msg))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify the provided password against the hashed password stored in the database.
|
// Verify the provided password against the hashed password stored in the database.
|
||||||
if err := verifyPassword(*account.Password, r.Password); err != nil {
|
if err := verifyPassword(*account.Password, r.Password); err != nil {
|
||||||
msg := "invalid email or password"
|
msg := "invalid email or password"
|
||||||
|
@ -193,7 +185,7 @@ func (a *AuthController) PostLogin() {
|
||||||
|
|
||||||
// PostChallenge handles the POST /api/auth/pubkey/challenge request to generate a challenge for a user's public key.
|
// PostChallenge handles the POST /api/auth/pubkey/challenge request to generate a challenge for a user's public key.
|
||||||
func (a *AuthController) PostPubkeyChallenge() {
|
func (a *AuthController) PostPubkeyChallenge() {
|
||||||
var r ChallengeRequest
|
var r LoginRequest
|
||||||
|
|
||||||
// Read the login request from the client.
|
// Read the login request from the client.
|
||||||
if err := a.Ctx.ReadJSON(&r); err != nil {
|
if err := a.Ctx.ReadJSON(&r); err != nil {
|
||||||
|
@ -202,17 +194,15 @@ func (a *AuthController) PostPubkeyChallenge() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r.Pubkey = strings.ToLower(r.Pubkey)
|
|
||||||
|
|
||||||
// Retrieve the account for the given email.
|
// Retrieve the account for the given email.
|
||||||
account := model.Key{}
|
account := model.Account{}
|
||||||
if err := db.Get().Where("pubkey = ?", r.Pubkey).First(&account).Error; err != nil {
|
if err := db.Get().Where("email = ?", r.Email).First(&account).Error; err != nil {
|
||||||
a.Ctx.StopWithError(iris.StatusBadRequest, errors.New("invalid pubkey"))
|
a.Ctx.StopWithError(iris.StatusBadRequest, errors.New("invalid email or password"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate a random challenge string.
|
// Generate a random challenge string.
|
||||||
challenge, err := generateAndSaveChallengeToken(account.AccountID, time.Minute)
|
challenge, err := generateAndSaveChallengeToken(account.ID, time.Minute)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.Ctx.StopWithError(iris.StatusInternalServerError, errors.New("failed to generate challenge"))
|
a.Ctx.StopWithError(iris.StatusInternalServerError, errors.New("failed to generate challenge"))
|
||||||
return
|
return
|
||||||
|
@ -236,12 +226,9 @@ func (a *AuthController) PostPubkeyLogin() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r.Pubkey = strings.ToLower(r.Pubkey)
|
|
||||||
r.Signature = strings.ToLower(r.Signature)
|
|
||||||
|
|
||||||
// Retrieve the key challenge for the given challenge.
|
// Retrieve the key challenge for the given challenge.
|
||||||
challenge := model.KeyChallenge{}
|
challenge := model.KeyChallenge{}
|
||||||
if err := db.Get().Where("challenge = ?", r.Challenge).First(&challenge).Error; err != nil {
|
if err := db.Get().Where("challenge = ?", r.Challenge).Preload("Key").First(&challenge).Error; err != nil {
|
||||||
msg := "invalid key challenge"
|
msg := "invalid key challenge"
|
||||||
logger.Get().Debug(msg, zap.Error(err), zap.String("challenge", r.Challenge))
|
logger.Get().Debug(msg, zap.Error(err), zap.String("challenge", r.Challenge))
|
||||||
a.Ctx.StopWithError(iris.StatusBadRequest, errorx.RejectedOperation.New(msg))
|
a.Ctx.StopWithError(iris.StatusBadRequest, errorx.RejectedOperation.New(msg))
|
||||||
|
|
Loading…
Reference in New Issue