-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
195 additions
and
89 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package model | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/team-nerd-planet/api-server/internal/entity" | ||
) | ||
|
||
type TokenType int | ||
|
||
const ( | ||
SUBSCRIBE TokenType = iota | ||
RESUBSCRIBE | ||
UNSUBSCRIBE | ||
) | ||
|
||
type EmailToken struct { | ||
TokenType TokenType `json:"token_type"` | ||
Subscription entity.Subscription `json:"subscription"` | ||
jwt.RegisteredClaims | ||
} | ||
|
||
func NewEmailToken(tokenType TokenType, subscription entity.Subscription) EmailToken { | ||
return EmailToken{ | ||
TokenType: tokenType, | ||
Subscription: subscription, | ||
RegisteredClaims: jwt.RegisteredClaims{ | ||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(1 * time.Hour)), | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package token | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/team-nerd-planet/api-server/infra/config" | ||
) | ||
|
||
var ( | ||
errTokenExpired = errors.New("token has invalid claims: token is expired") | ||
errUnexpectedSigningMethod = errors.New("unexpected signing method: HMAC-SHA") | ||
errSignatureInvalid = errors.New("token signature is invalid: signature is invalid") | ||
) | ||
|
||
type TokenUsecase struct { | ||
conf *config.Config | ||
} | ||
|
||
func NewTokenUsecase(conf *config.Config) TokenUsecase { | ||
return TokenUsecase{ | ||
conf: conf, | ||
} | ||
} | ||
|
||
func (tu TokenUsecase) GenerateToken(claims jwt.Claims) (string, error) { | ||
newToken := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) | ||
return newToken.SignedString([]byte(tu.conf.Jwt.SecretKey)) | ||
} | ||
|
||
func (tu TokenUsecase) VerifyToken(tokenString string, claims jwt.Claims) (err error) { | ||
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) { | ||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { | ||
return nil, errUnexpectedSigningMethod | ||
} | ||
expiration, err := token.Claims.GetExpirationTime() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if expiration.Time.Unix() < time.Now().Unix() { | ||
return nil, errTokenExpired | ||
} | ||
|
||
return []byte(tu.conf.Jwt.SecretKey), nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !token.Valid { | ||
return errSignatureInvalid | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package token | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/lib/pq" | ||
"github.com/team-nerd-planet/api-server/infra/config" | ||
"github.com/team-nerd-planet/api-server/internal/entity" | ||
"github.com/team-nerd-planet/api-server/internal/usecase/token/model" | ||
) | ||
|
||
func Test_GenerateAndVerifyEmailToken(t *testing.T) { | ||
tokenUsecase := NewTokenUsecase(&config.Config{ | ||
Jwt: config.Jwt{ | ||
SecretKey: "test_key", | ||
}, | ||
}) | ||
|
||
name := "name" | ||
division := "division" | ||
token1 := model.EmailToken{ | ||
Subscription: entity.Subscription{ | ||
Email: "email", | ||
Name: &name, | ||
Division: &division, | ||
Published: time.Now(), | ||
PreferredCompanyArr: pq.Int64Array{}, | ||
PreferredCompanySizeArr: pq.Int64Array{}, | ||
PreferredJobArr: pq.Int64Array{}, | ||
PreferredSkillArr: pq.Int64Array{}, | ||
}, | ||
RegisteredClaims: jwt.RegisteredClaims{ | ||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(1 * time.Hour)), | ||
}, | ||
} | ||
|
||
str, err := tokenUsecase.GenerateToken(token1) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
return | ||
|
||
} | ||
|
||
var token2 model.EmailToken | ||
err = tokenUsecase.VerifyToken(str, &token2) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
return | ||
} | ||
|
||
if token1.Subscription.Email != token2.Subscription.Email || | ||
*token1.Subscription.Name != *token2.Subscription.Name || | ||
*token1.Subscription.Division != *token2.Subscription.Division || | ||
token1.Subscription.Published.Compare(token2.Subscription.Published) != 0 { | ||
t.Errorf("not same") | ||
return | ||
} | ||
|
||
t.Log("PASS") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters