-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
e599968
commit 4d909a4
Showing
8 changed files
with
145 additions
and
2 deletions.
There are no files selected for viewing
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
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,6 @@ | ||
package constants | ||
|
||
const ( | ||
AccessTokenLifetime = "ACCESS_TOKEN_LIFETIME_IN_MINUTES" | ||
RefreshTokenLifetime = "REFRESH_TOKEN_LIFETIME_IN_DAYS" | ||
) |
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,16 @@ | ||
package constants | ||
|
||
const ( | ||
AdminRole string = "admin" | ||
DefaultRole string = "default" | ||
|
||
AuthorizationHeaderKey string = "Authorization" | ||
UserIdKey string = "UserId" | ||
UserNameKey string = "UserName" | ||
FullNameKey string = "FullName" | ||
EmailKey string = "Email" | ||
RolesKey string = "Roles" | ||
ExpireTimeKey string = "ExpireTime" | ||
AccessTokenSecretKey string = "ACCESS_TOKEN_SECRET_KEY" | ||
RefreshTokenSecretKey string = "REFRESH_TOKEN_SECRET_KEY" | ||
) |
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,69 @@ | ||
package services | ||
|
||
import ( | ||
"github.com/alireza-fa/blog-go/src/api/dto" | ||
"github.com/alireza-fa/blog-go/src/constants" | ||
"github.com/alireza-fa/blog-go/src/pkg/logging" | ||
"github.com/golang-jwt/jwt/v5" | ||
"os" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type TokenService struct { | ||
logger logging.Logger | ||
} | ||
|
||
type TokenDto struct { | ||
UserId int | ||
FullName string | ||
UserName string | ||
Email string | ||
Roles []string | ||
} | ||
|
||
func NewTokenService() *TokenService { | ||
return &TokenService{ | ||
logger: logging.NewLogger(), | ||
} | ||
} | ||
|
||
func (s *TokenService) GenerateToken(token *TokenDto) (*dto.TokenDetail, error) { | ||
td := &dto.TokenDetail{} | ||
|
||
accessTokenDuration, _ := strconv.Atoi(os.Getenv(constants.AccessTokenLifetime)) | ||
td.AccessTokenExpireTime = time.Now().Add(time.Duration(accessTokenDuration) * time.Minute).Unix() | ||
|
||
refreshTokenDuration, _ := strconv.Atoi(os.Getenv(constants.RefreshTokenLifetime)) | ||
td.RefreshTokenExpireTime = time.Now().Add(time.Duration(refreshTokenDuration) * time.Hour * 24).Unix() | ||
|
||
act := jwt.MapClaims{} | ||
|
||
act[constants.UserIdKey] = token.UserId | ||
act[constants.FullNameKey] = token.FullName | ||
act[constants.UserNameKey] = token.UserName | ||
act[constants.EmailKey] = token.Email | ||
act[constants.RolesKey] = token.Roles | ||
act[constants.ExpireTimeKey] = td.AccessTokenExpireTime | ||
ac := jwt.NewWithClaims(jwt.SigningMethodHS256, act) | ||
|
||
var err error | ||
td.AccessToken, err = ac.SignedString([]byte(os.Getenv(constants.AccessTokenSecretKey))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rtc := jwt.MapClaims{} | ||
|
||
rtc[constants.UserIdKey] = token.UserId | ||
rtc[constants.ExpireTimeKey] = td.RefreshTokenExpireTime | ||
|
||
rt := jwt.NewWithClaims(jwt.SigningMethodHS256, rtc) | ||
|
||
td.RefreshToken, err = rt.SignedString([]byte(os.Getenv(constants.RefreshTokenSecretKey))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return td, err | ||
} |
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