|
| 1 | +package adapter |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "strconv" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/golang-jwt/jwt" |
| 10 | + "github.com/hervibest/be-yourmoments-backup/transaction-svc/internal/entity" |
| 11 | + "github.com/hervibest/be-yourmoments-backup/transaction-svc/internal/helper/utils" |
| 12 | + "github.com/oklog/ulid/v2" |
| 13 | +) |
| 14 | + |
| 15 | +type JWTAdapter interface { |
| 16 | + GenerateAccessToken(userId string) (*entity.AccessToken, error) |
| 17 | + GenerateRefreshToken(userId string) (*entity.RefreshToken, error) |
| 18 | + VerifyAccessToken(token string) (*entity.AccessToken, error) |
| 19 | + VerifyRefreshToken(token string) (*entity.RefreshToken, error) |
| 20 | +} |
| 21 | + |
| 22 | +type jwtAdapter struct { |
| 23 | + accessSecretByte []byte |
| 24 | + refreshSecretByte []byte |
| 25 | + accessExpireTime time.Duration |
| 26 | + refreshExpireTime time.Duration |
| 27 | +} |
| 28 | + |
| 29 | +func NewJWTAdapter() JWTAdapter { |
| 30 | + accessSecret := utils.GetEnv("ACCESS_TOKEN_SECRET") |
| 31 | + refreshSecret := utils.GetEnv("REFRESH_TOKEN_SECRET") |
| 32 | + |
| 33 | + accessExpireStr := utils.GetEnv("ACCESS_TOKEN_EXP_MINUTE") |
| 34 | + refreshExpireStr := utils.GetEnv("REFRESH_TOKEN_EXP_DAY") |
| 35 | + |
| 36 | + accessExpireInt, _ := strconv.Atoi(accessExpireStr) |
| 37 | + refreshExpirInt, _ := strconv.Atoi(refreshExpireStr) |
| 38 | + |
| 39 | + return &jwtAdapter{ |
| 40 | + accessSecretByte: []byte(accessSecret), |
| 41 | + refreshSecretByte: []byte(refreshSecret), |
| 42 | + accessExpireTime: time.Duration(accessExpireInt), |
| 43 | + refreshExpireTime: time.Duration(refreshExpirInt), |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (c *jwtAdapter) GenerateAccessToken(userId string) (*entity.AccessToken, error) { |
| 48 | + expirationTime := time.Now().Add(time.Minute * c.accessExpireTime) |
| 49 | + |
| 50 | + claims := jwt.MapClaims{} |
| 51 | + claims["authorized"] = true |
| 52 | + claims["user_id"] = userId |
| 53 | + claims["exp"] = expirationTime.Unix() |
| 54 | + |
| 55 | + token := jwt.NewWithClaims(jwt.SigningMethodHS512, claims) |
| 56 | + stringToken, err := token.SignedString(c.accessSecretByte) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + |
| 61 | + return &entity.AccessToken{ |
| 62 | + UserId: userId, |
| 63 | + Token: stringToken, |
| 64 | + ExpiresAt: expirationTime, |
| 65 | + }, nil |
| 66 | +} |
| 67 | + |
| 68 | +func (c *jwtAdapter) GenerateRefreshToken(userId string) (*entity.RefreshToken, error) { |
| 69 | + expirationTime := time.Now().Add(time.Hour * 24 * c.refreshExpireTime) |
| 70 | + |
| 71 | + claims := jwt.MapClaims{} |
| 72 | + claims["user_id"] = userId |
| 73 | + claims["exp"] = expirationTime.Unix() |
| 74 | + |
| 75 | + token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) |
| 76 | + stringToken, err := token.SignedString(c.refreshSecretByte) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + |
| 81 | + return &entity.RefreshToken{ |
| 82 | + UserId: userId, |
| 83 | + Token: stringToken, |
| 84 | + ExpiresAt: expirationTime, |
| 85 | + }, nil |
| 86 | +} |
| 87 | + |
| 88 | +func (c *jwtAdapter) VerifyAccessToken(token string) (*entity.AccessToken, error) { |
| 89 | + tokenClaims, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) { |
| 90 | + return c.accessSecretByte, nil |
| 91 | + }) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + |
| 96 | + accessTokenDetail := &entity.AccessToken{} |
| 97 | + claims, ok := tokenClaims.Claims.(jwt.MapClaims) |
| 98 | + if ok && tokenClaims.Valid { |
| 99 | + userIdStr, ok := claims["user_id"].(string) |
| 100 | + if !ok { |
| 101 | + log.Println("user_id not a string") |
| 102 | + return nil, fmt.Errorf("Invalid token claims") |
| 103 | + } |
| 104 | + |
| 105 | + authorized, ok := claims["authorized"].(bool) |
| 106 | + if !ok { |
| 107 | + log.Println("authorized is not a bool") |
| 108 | + return nil, fmt.Errorf("Invalid token claims") |
| 109 | + } |
| 110 | + |
| 111 | + if !authorized { |
| 112 | + log.Println("unathorize") |
| 113 | + return nil, fmt.Errorf("Invalid token claims") |
| 114 | + } |
| 115 | + |
| 116 | + _, err := ulid.Parse(userIdStr) |
| 117 | + if err != nil { |
| 118 | + log.Println("failed to parse ulid:", err) |
| 119 | + return nil, fmt.Errorf("Invalid token claims") |
| 120 | + } |
| 121 | + |
| 122 | + accessTokenDetail.UserId = userIdStr |
| 123 | + expFloat, ok := claims["exp"].(float64) |
| 124 | + if !ok { |
| 125 | + log.Println("exp is not a float") |
| 126 | + return nil, fmt.Errorf("Invalid exp in token claims") |
| 127 | + } |
| 128 | + |
| 129 | + expiresAt := time.Unix(int64(expFloat), 0) |
| 130 | + accessTokenDetail.ExpiresAt = expiresAt |
| 131 | + accessTokenDetail.Token = token |
| 132 | + } |
| 133 | + |
| 134 | + return accessTokenDetail, nil |
| 135 | + |
| 136 | +} |
| 137 | + |
| 138 | +func (c *jwtAdapter) VerifyRefreshToken(token string) (*entity.RefreshToken, error) { |
| 139 | + tokenClaims, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) { |
| 140 | + return c.refreshSecretByte, nil |
| 141 | + }) |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + |
| 146 | + refreshTokenDetail := &entity.RefreshToken{} |
| 147 | + claims, ok := tokenClaims.Claims.(jwt.MapClaims) |
| 148 | + if ok && tokenClaims.Valid { |
| 149 | + userIdStr, ok := claims["user_id"].(string) |
| 150 | + if !ok { |
| 151 | + log.Println("user_id not a string") |
| 152 | + return nil, fmt.Errorf("Invalid token claims") |
| 153 | + } |
| 154 | + |
| 155 | + _, err := ulid.Parse(userIdStr) |
| 156 | + if err != nil { |
| 157 | + log.Println("failed to parse ulid:", err) |
| 158 | + return nil, fmt.Errorf("Invalid token claims") |
| 159 | + } |
| 160 | + |
| 161 | + refreshTokenDetail.UserId = userIdStr |
| 162 | + expFloat, ok := claims["exp"].(float64) |
| 163 | + if !ok { |
| 164 | + log.Println("exp is not a float") |
| 165 | + return nil, fmt.Errorf("Invalid exp in token claims") |
| 166 | + } |
| 167 | + |
| 168 | + expiresAt := time.Unix(int64(expFloat), 0) |
| 169 | + refreshTokenDetail.ExpiresAt = expiresAt |
| 170 | + refreshTokenDetail.Token = token |
| 171 | + } |
| 172 | + |
| 173 | + return refreshTokenDetail, nil |
| 174 | +} |
0 commit comments