Skip to content

Commit

Permalink
Merge pull request #42 from NJUPT-SAST/dev-windpo
Browse files Browse the repository at this point in the history
Dev windpo
  • Loading branch information
windpo authored Sep 24, 2023
2 parents 9ce95cb + 7901bba commit 405e3c6
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ go.work

# Config
dev-*.toml
Dockerfile
.idea/
2 changes: 1 addition & 1 deletion api/v1/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func Login(ctx *gin.Context) {
return
}
// Set Token with expire time and return
token, err := util.GenerateTokenWithExp(model.LoginJWTSubKey(username), model.LOGIN_TOKEN_EXP)
token, err := util.GenerateTokenWithExp(ctx, model.LoginJWTSubKey(username), model.LOGIN_TOKEN_EXP)
if err != nil {
ctx.JSON(http.StatusOK, result.Failed(result.GenerateToken))
}
Expand Down
6 changes: 3 additions & 3 deletions service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func VerifyAccountRegister(ctx *gin.Context, username string) (string, error) {
return "", result.UserIsExist
} else { // user is not exist and can register
// generate token and set expire time
ticket, err := util.GenerateTokenWithExp(model.RegisterJWTSubKey(username), model.REGISTER_TICKET_EXP)
ticket, err := util.GenerateTokenWithExp(ctx, model.RegisterJWTSubKey(username), model.REGISTER_TICKET_EXP)
if err != nil {
return "", err
}
Expand All @@ -111,7 +111,7 @@ func VerifyAccountLogin(ctx *gin.Context, username string) (string, error) {
}
// user is existed and can login
if exist {
ticket, err := util.GenerateTokenWithExp(model.LoginTicketJWTSubKey(username), model.LOGIN_TICKET_EXP)
ticket, err := util.GenerateTokenWithExp(ctx, model.LoginTicketJWTSubKey(username), model.LOGIN_TICKET_EXP)
if err != nil {
return "", err
}
Expand All @@ -125,7 +125,7 @@ func VerifyAccountLogin(ctx *gin.Context, username string) (string, error) {
return "", err
}
if uidExist {
ticket, err := util.GenerateTokenWithExp(model.LoginTicketJWTSubKey(username), model.LOGIN_TICKET_EXP)
ticket, err := util.GenerateTokenWithExp(ctx, model.LoginTicketJWTSubKey(username), model.LOGIN_TICKET_EXP)
if err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions util/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ func GenerateToken(username string) (string, error) {
}

// GenerateToken with expireTime
func GenerateTokenWithExp(username string, expireTime time.Duration) (string, error) {
func GenerateTokenWithExp(ctx context.Context, username string, expireTime time.Duration) (string, error) {
signingKey := []byte(jwtSigningKey)
gen := NewJWTAccessGenerate("", signingKey, jwt.SigningMethodHS256)
access, _, err := gen.Token(context.Background(), username, expireTime, false)
access, _, err := gen.Token(ctx, username, expireTime, false)
return access, err
}

Expand Down
2 changes: 1 addition & 1 deletion util/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestJWT(t *testing.T) {
Convey("Test JWT Access Generate", t, func() {
token, err := GenerateTokenWithExp("[email protected]", time.Minute*3)
token, err := GenerateTokenWithExp(nil, "[email protected]", time.Minute*3)
So(err, ShouldBeNil)
fmt.Println("token:", token)
So(token, ShouldNotBeEmpty)
Expand Down

0 comments on commit 405e3c6

Please sign in to comment.