Skip to content

Commit

Permalink
fix: type
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitiwat-owen committed Dec 29, 2023
1 parent 5f931df commit b8e5a6b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 73 deletions.
10 changes: 5 additions & 5 deletions src/internal/domain/dto/token/token.dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ type UserCredential struct {

type AuthPayload struct {
jwt.RegisteredClaims
UserID string `json:"user_id"`
Role constant.Role `json:"role"`
AuthSessionID string `json:"auth_session_id"`
UserID string `json:"user_id"`
AuthSessionID string `json:"auth_session_id"`
}

type AccessTokenCache struct {
Token string `json:"token"`
RefreshToken string `json:"refresh_token"`
Token string `json:"token"`
Role constant.Role `json:"role"`
RefreshToken string `json:"refresh_token"`
}

type RefreshTokenCache struct {
Expand Down
1 change: 0 additions & 1 deletion src/internal/service/jwt/jwt.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func (s *serviceImpl) SignAuth(userId string, role constant.Role, authSessionId
IssuedAt: s.jwtUtil.GetNumericDate(time.Now()),
},
UserID: userId,
Role: role,
AuthSessionID: authSessionId,
}

Expand Down
1 change: 0 additions & 1 deletion src/internal/service/jwt/jwt.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func (t *JwtServiceTest) SetupTest() {
IssuedAt: numericDate,
},
UserID: userId,
Role: role,
AuthSessionID: authSessionId,
}

Expand Down
16 changes: 9 additions & 7 deletions src/internal/service/token/token.service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package token

import (
_jwt "github.com/golang-jwt/jwt/v4"
"github.com/isd-sgcu/johnjud-auth/src/internal/constant"
tokenDto "github.com/isd-sgcu/johnjud-auth/src/internal/domain/dto/token"
"github.com/isd-sgcu/johnjud-auth/src/internal/utils"
Expand Down Expand Up @@ -39,6 +40,7 @@ func (s *serviceImpl) CreateCredential(userId string, role constant.Role, authSe

accessTokenCache := &tokenDto.AccessTokenCache{
Token: accessToken,
Role: role,
RefreshToken: refreshToken,
}
err = s.accessTokenCache.SetValue(authSessionId, accessTokenCache, jwtConf.ExpiresIn)
Expand Down Expand Up @@ -71,17 +73,17 @@ func (s *serviceImpl) Validate(token string) (*tokenDto.UserCredential, error) {
return nil, err
}

payloads := jwtToken.Claims.(tokenDto.AuthPayload)
if payloads.Issuer != s.jwtService.GetConfig().Issuer {
payloads := jwtToken.Claims.(_jwt.MapClaims)
if payloads["iss"] != s.jwtService.GetConfig().Issuer {
return nil, errors.New("invalid token")
}

if time.Unix(payloads.ExpiresAt.Unix(), 0).Before(time.Now()) {
if time.Unix(int64(payloads["exp"].(float64)), 0).Before(time.Now()) {
return nil, errors.New("expired token")
}

accessTokenCache := &tokenDto.AccessTokenCache{}
err = s.accessTokenCache.GetValue(payloads.AuthSessionID, accessTokenCache)
err = s.accessTokenCache.GetValue(payloads["auth_session_id"].(string), accessTokenCache)
if err != nil {
if err != redis.Nil {
return nil, err
Expand All @@ -94,9 +96,9 @@ func (s *serviceImpl) Validate(token string) (*tokenDto.UserCredential, error) {
}

userCredential := &tokenDto.UserCredential{
UserID: payloads.UserID,
Role: payloads.Role,
AuthSessionID: payloads.AuthSessionID,
UserID: payloads["user_id"].(string),
Role: accessTokenCache.Role,
AuthSessionID: payloads["auth_session_id"].(string),
RefreshToken: accessTokenCache.RefreshToken,
}
return userCredential, nil
Expand Down
103 changes: 44 additions & 59 deletions src/internal/service/token/token.service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (t *TokenServiceTest) SetupTest() {
func (t *TokenServiceTest) TestCreateCredentialSuccess() {
accessTokenCache := &tokenDto.AccessTokenCache{
Token: t.accessToken,
Role: t.role,
RefreshToken: t.refreshToken.String(),
}
refreshTokenCache := &tokenDto.RefreshTokenCache{
Expand Down Expand Up @@ -120,6 +121,7 @@ func (t *TokenServiceTest) TestCreateCredentialSignAuthFailed() {
func (t *TokenServiceTest) TestCreateCredentialSetAccessTokenFailed() {
accessTokenCache := &tokenDto.AccessTokenCache{
Token: t.accessToken,
Role: t.role,
RefreshToken: t.refreshToken.String(),
}
setCacheErr := errors.New("Internal server error")
Expand Down Expand Up @@ -147,6 +149,7 @@ func (t *TokenServiceTest) TestCreateCredentialSetAccessTokenFailed() {
func (t *TokenServiceTest) TestCreateCredentialSetRefreshTokenFailed() {
accessTokenCache := &tokenDto.AccessTokenCache{
Token: t.accessToken,
Role: t.role,
RefreshToken: t.refreshToken.String(),
}
refreshTokenCache := &tokenDto.RefreshTokenCache{
Expand Down Expand Up @@ -180,19 +183,16 @@ func (t *TokenServiceTest) TestCreateCredentialSetRefreshTokenFailed() {
func (t *TokenServiceTest) TestValidateSuccess() {
expected := &tokenDto.UserCredential{
UserID: t.userId,
Role: constant.USER,
Role: "",
AuthSessionID: t.authSessionId,
RefreshToken: "",
}
payloads := tokenDto.AuthPayload{
RegisteredClaims: _jwt.RegisteredClaims{
Issuer: t.jwtConfig.Issuer,
ExpiresAt: _jwt.NewNumericDate(time.Now().Add(time.Second * time.Duration(t.jwtConfig.ExpiresIn))),
IssuedAt: _jwt.NewNumericDate(time.Now()),
},
UserID: t.userId,
Role: t.role,
AuthSessionID: t.authSessionId,
payloads := _jwt.MapClaims{
"iss": t.jwtConfig.Issuer,
"exp": float64(_jwt.NewNumericDate(time.Now().Add(time.Second * time.Duration(t.jwtConfig.ExpiresIn))).Unix()),
"iat": float64(_jwt.NewNumericDate(time.Now()).Unix()),
"user_id": t.userId,
"auth_session_id": t.authSessionId,
}
jwtToken := &_jwt.Token{
Method: _jwt.SigningMethodHS256,
Expand All @@ -209,7 +209,7 @@ func (t *TokenServiceTest) TestValidateSuccess() {

jwtService.On("VerifyAuth", t.validateToken).Return(jwtToken, nil)
jwtService.On("GetConfig").Return(t.jwtConfig)
accessTokenRepo.EXPECT().GetValue(payloads.AuthSessionID, accessTokenCache).Return(nil)
accessTokenRepo.EXPECT().GetValue(payloads["auth_session_id"].(string), accessTokenCache).Return(nil)

tokenSvc := NewService(&jwtService, accessTokenRepo, refreshTokenRepo, &uuidUtil)
actual, err := tokenSvc.Validate(t.validateToken)
Expand All @@ -221,15 +221,12 @@ func (t *TokenServiceTest) TestValidateSuccess() {
func (t *TokenServiceTest) TestValidateInvalidIssuer() {
expected := errors.New("invalid token")

payloads := tokenDto.AuthPayload{
RegisteredClaims: _jwt.RegisteredClaims{
Issuer: "InvalidIssuer",
ExpiresAt: _jwt.NewNumericDate(time.Now().Add(time.Second * time.Duration(t.jwtConfig.ExpiresIn))),
IssuedAt: _jwt.NewNumericDate(time.Now()),
},
UserID: t.userId,
Role: t.role,
AuthSessionID: t.authSessionId,
payloads := _jwt.MapClaims{
"iss": "invalid issuer",
"exp": float64(_jwt.NewNumericDate(time.Now().Add(time.Second * time.Duration(t.jwtConfig.ExpiresIn))).Unix()),
"iat": float64(_jwt.NewNumericDate(time.Now()).Unix()),
"user_id": t.userId,
"auth_session_id": t.authSessionId,
}

jwtToken := &_jwt.Token{
Expand Down Expand Up @@ -257,15 +254,12 @@ func (t *TokenServiceTest) TestValidateInvalidIssuer() {
func (t *TokenServiceTest) TestValidateExpireToken() {
expected := errors.New("expired token")

payloads := tokenDto.AuthPayload{
RegisteredClaims: _jwt.RegisteredClaims{
Issuer: t.jwtConfig.Issuer,
ExpiresAt: _jwt.NewNumericDate(time.Now().Add(time.Second * (-time.Duration(t.jwtConfig.ExpiresIn)))),
IssuedAt: _jwt.NewNumericDate(time.Now()),
},
UserID: t.userId,
Role: t.role,
AuthSessionID: t.authSessionId,
payloads := _jwt.MapClaims{
"iss": t.jwtConfig.Issuer,
"exp": float64(_jwt.NewNumericDate(time.Now().Add(time.Second * (-time.Duration(t.jwtConfig.ExpiresIn)))).Unix()),
"iat": float64(_jwt.NewNumericDate(time.Now()).Unix()),
"user_id": t.userId,
"auth_session_id": t.authSessionId,
}
jwtToken := &_jwt.Token{
Method: _jwt.SigningMethodHS256,
Expand Down Expand Up @@ -311,15 +305,12 @@ func (t *TokenServiceTest) TestValidateVerifyFailed() {
func (t *TokenServiceTest) TestValidateGetCacheKeyNotFound() {
expected := errors.New("invalid token")

payloads := tokenDto.AuthPayload{
RegisteredClaims: _jwt.RegisteredClaims{
Issuer: t.jwtConfig.Issuer,
ExpiresAt: _jwt.NewNumericDate(time.Now().Add(time.Second * (time.Duration(t.jwtConfig.ExpiresIn)))),
IssuedAt: _jwt.NewNumericDate(time.Now()),
},
UserID: t.userId,
Role: t.role,
AuthSessionID: t.authSessionId,
payloads := _jwt.MapClaims{
"iss": t.jwtConfig.Issuer,
"exp": float64(_jwt.NewNumericDate(time.Now().Add(time.Second * time.Duration(t.jwtConfig.ExpiresIn))).Unix()),
"iat": float64(_jwt.NewNumericDate(time.Now()).Unix()),
"user_id": t.userId,
"auth_session_id": t.authSessionId,
}
jwtToken := &_jwt.Token{
Method: _jwt.SigningMethodHS256,
Expand All @@ -336,7 +327,7 @@ func (t *TokenServiceTest) TestValidateGetCacheKeyNotFound() {

jwtService.On("VerifyAuth", t.validateToken).Return(jwtToken, nil)
jwtService.On("GetConfig").Return(t.jwtConfig)
accessTokenRepo.EXPECT().GetValue(payloads.AuthSessionID, accessTokenCache).Return(redis.Nil)
accessTokenRepo.EXPECT().GetValue(payloads["auth_session_id"].(string), accessTokenCache).Return(redis.Nil)

tokenSvc := NewService(&jwtService, accessTokenRepo, refreshTokenRepo, &uuidUtil)
actual, err := tokenSvc.Validate(t.validateToken)
Expand All @@ -346,15 +337,12 @@ func (t *TokenServiceTest) TestValidateGetCacheKeyNotFound() {
}

func (t *TokenServiceTest) TestValidateGetCacheInternalFailed() {
payloads := tokenDto.AuthPayload{
RegisteredClaims: _jwt.RegisteredClaims{
Issuer: t.jwtConfig.Issuer,
ExpiresAt: _jwt.NewNumericDate(time.Now().Add(time.Second * (time.Duration(t.jwtConfig.ExpiresIn)))),
IssuedAt: _jwt.NewNumericDate(time.Now()),
},
UserID: t.userId,
Role: t.role,
AuthSessionID: t.authSessionId,
payloads := _jwt.MapClaims{
"iss": t.jwtConfig.Issuer,
"exp": float64(_jwt.NewNumericDate(time.Now().Add(time.Second * time.Duration(t.jwtConfig.ExpiresIn))).Unix()),
"iat": float64(_jwt.NewNumericDate(time.Now()).Unix()),
"user_id": t.userId,
"auth_session_id": t.authSessionId,
}
jwtToken := &_jwt.Token{
Method: _jwt.SigningMethodHS256,
Expand All @@ -374,7 +362,7 @@ func (t *TokenServiceTest) TestValidateGetCacheInternalFailed() {

jwtService.On("VerifyAuth", t.validateToken).Return(jwtToken, nil)
jwtService.On("GetConfig").Return(t.jwtConfig)
accessTokenRepo.EXPECT().GetValue(payloads.AuthSessionID, accessTokenCache).Return(getCacheErr)
accessTokenRepo.EXPECT().GetValue(payloads["auth_session_id"].(string), accessTokenCache).Return(getCacheErr)

tokenSvc := NewService(&jwtService, accessTokenRepo, refreshTokenRepo, &uuidUtil)
actual, err := tokenSvc.Validate(t.validateToken)
Expand All @@ -387,15 +375,12 @@ func (t *TokenServiceTest) TestValidateInvalidToken() {
invalidToken := faker.Word()
expected := errors.New("invalid token")

payloads := tokenDto.AuthPayload{
RegisteredClaims: _jwt.RegisteredClaims{
Issuer: t.jwtConfig.Issuer,
ExpiresAt: _jwt.NewNumericDate(time.Now().Add(time.Second * (time.Duration(t.jwtConfig.ExpiresIn)))),
IssuedAt: _jwt.NewNumericDate(time.Now()),
},
UserID: t.userId,
Role: t.role,
AuthSessionID: t.authSessionId,
payloads := _jwt.MapClaims{
"iss": t.jwtConfig.Issuer,
"exp": float64(_jwt.NewNumericDate(time.Now().Add(time.Second * time.Duration(t.jwtConfig.ExpiresIn))).Unix()),
"iat": float64(_jwt.NewNumericDate(time.Now()).Unix()),
"user_id": t.userId,
"auth_session_id": t.authSessionId,
}
jwtToken := &_jwt.Token{
Method: _jwt.SigningMethodHS256,
Expand All @@ -412,7 +397,7 @@ func (t *TokenServiceTest) TestValidateInvalidToken() {

jwtService.On("VerifyAuth", invalidToken).Return(jwtToken, nil)
jwtService.On("GetConfig").Return(t.jwtConfig)
accessTokenRepo.EXPECT().GetValue(payloads.AuthSessionID, accessTokenCache).Return(nil)
accessTokenRepo.EXPECT().GetValue(payloads["auth_session_id"].(string), accessTokenCache).Return(nil)

tokenSvc := NewService(&jwtService, accessTokenRepo, refreshTokenRepo, &uuidUtil)
actual, err := tokenSvc.Validate(invalidToken)
Expand Down

0 comments on commit b8e5a6b

Please sign in to comment.