Skip to content

Commit

Permalink
fix resetPWD
Browse files Browse the repository at this point in the history
  • Loading branch information
windpo committed Nov 6, 2023
1 parent c63d2f7 commit 12458d8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
11 changes: 6 additions & 5 deletions model/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ const (
LOGIN_TOKEN_IN_REDIS = "LOGIN"

// For JWT
LOGIN_TOKEN_SUB = "loginToken"
LOGIN_TICKET_SUB = "loginTicket"
REGIST_TICKET_SUB = "registerTicket"
RESETPWD_TICKET_SUB = "resetPwdTicket"
LOGIN_TOKEN_SUB = "loginToken"
LOGIN_TICKET_SUB = "loginTicket"
REGIST_TICKET_SUB = "registerTicket"
RESETPWD_TICKET_SUB = "resetPwdTicket"
)

var (
Expand All @@ -43,7 +43,8 @@ func LoginTicketKey(username string) string {
func RegisterJWTSubKey(username string) string {
return fmt.Sprintf("%s-%s", username, REGIST_TICKET_SUB)
}
func ResetPwdJWTSubkey(username string) string {

func ResetPwdJWTSubKey(username string) string {
return fmt.Sprintf("%s-%s", username, RESETPWD_TICKET_SUB)
}

Expand Down
4 changes: 2 additions & 2 deletions model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ func CheckPassword(username string, password string) (string, error) {
return *user.Uid, err
}

func ChangePassword(username string, password string) error {
func ChangePassword(uid string, password string) error {
pwdEncrypted := util.ShaHashing(password)
err := Db.Model(&User{}).Where("uid = ?", username).Where("is_deleted = ?", false).Update("password", pwdEncrypted).Error
err := Db.Debug().Model(&User{}).Where("uid = ?", uid).Where("is_deleted = ?", false).Update("password", pwdEncrypted).Error
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func VerifyAccountResetPWD(ctx *gin.Context, username string) (string, error) {

// User exist and try to reset password
if user != nil {
ticket, err := util.GenerateTokenWithExp(ctx, model.ResetPwdJWTSubkey(username), model.RESETPWD_TICKET_EXP)
ticket, err := util.GenerateTokenWithExp(ctx, model.ResetPwdJWTSubKey(username), model.RESETPWD_TICKET_EXP)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -177,7 +177,12 @@ func ResetPassword(username, newPassword string) error {
if !CheckPasswordFormat(newPassword) {
return result.PasswordIllegal
}
cErr := model.ChangePassword(username, newPassword)

split := regexp.MustCompile(`@`)
uid := split.Split(username, 2)[0]
uid = strings.ToLower(uid)

cErr := model.ChangePassword(uid, newPassword)
if cErr != nil {
return cErr
}
Expand Down
13 changes: 10 additions & 3 deletions util/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"encoding/base64"
"errors"
"github.com/NJUPT-SAST/sast-link-backend/log"
"strings"
"time"

Expand All @@ -16,7 +17,10 @@ import (
"github.com/NJUPT-SAST/sast-link-backend/model/result"
)

var jwtSigningKey = config.Config.Sub("jwt").GetString("signing_key")
var (
utilLogger = log.Log
jwtSigningKey = config.Config.Sub("jwt").GetString("signing_key")
)

// GenerateToken
// token expireTime : not set, do this with redis
Expand Down Expand Up @@ -47,18 +51,21 @@ func GenerateTokenWithExp(ctx context.Context, username string, expireTime time.
func ParseToken(token string) (*JWTAccessClaims, error) {
tokenClaims, err := jwt.ParseWithClaims(token, &JWTAccessClaims{}, func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
utilLogger.Error(token + "-pasefail")
return nil, result.AuthParseTokenFail
}
return []byte(jwtSigningKey), nil
})
if err != nil {
return nil, result.AuthParseTokenFail.Wrap(err)
utilLogger.Error(token + "-pasefail-" + "msg:" + err.Error())
return nil, result.AuthParseTokenFail
}

if claims, ok := tokenClaims.Claims.(*JWTAccessClaims); ok && tokenClaims.Valid {
return claims, nil
} else {
return nil, result.AuthParseTokenFail.Wrap(err)
utilLogger.Error(token + "-pasefail-" + "msg:" + err.Error())
return nil, result.AuthParseTokenFail
}
}

Expand Down

0 comments on commit 12458d8

Please sign in to comment.