-
Notifications
You must be signed in to change notification settings - Fork 49
feat: add refresh token endpoint and related configurations #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
New Feature: -Implemented the RefreshToken method in the TokenUsecase to handle token refreshing. -The method retrieves the refresh token from the HTTP cookie, validates it, and generates a new access and refresh token pair. Details: -Extracts the refresh token from the cookie using c.Cookie. -Validates the refresh token and extracts claims using the GetClaims method. -Converts roles from []interface{} to []string for proper type handling. -Generates a new token pair using the GenerateToken method. Reason for Addition: -To provide functionality for refreshing expired access tokens while maintaining security through refresh tokens. -This is a critical feature for session management in the application. Benefits: -Enables secure token lifecycle management. -Improves user experience by allowing seamless token refresh without requiring re-login.
Thanks, @alielmi98 for these valuable pull requests. |
But i can't find your review messages? |
@@ -47,6 +56,9 @@ func (h *UsersHandler) LoginByUsername(c *gin.Context) { | |||
return | |||
} | |||
|
|||
// Set the refresh token in a cookie | |||
c.SetCookie(constant.RefreshTokenCookieName, token.RefreshToken, int(h.config.JWT.RefreshTokenExpireDuration*60), "/", h.config.Server.Domin, true, true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to set sameSite attribute for CSRF protection
c.SetCookie(constant.RefreshTokenCookieName, token.RefreshToken, int(h.config.JWT.RefreshTokenExpireDuration*60), "/", h.config.Server.Domin, true, true) | |
http.SetCookie(c.Writer, &http.Cookie{ | |
Name: constant.RefreshTokenCookieName, | |
Value: token.RefreshToken, | |
MaxAge: int(h.config.JWT.RefreshTokenExpireDuration * 60), | |
Path: "/", | |
Domain: h.config.Server.Domain, | |
Secure: true, | |
HttpOnly: true, | |
SameSite: http.SameSiteStrictMode, | |
}) |
@@ -105,6 +117,9 @@ func (h *UsersHandler) RegisterLoginByMobileNumber(c *gin.Context) { | |||
return | |||
} | |||
|
|||
// Set the refresh token in a cookie | |||
c.SetCookie(constant.RefreshTokenCookieName, token.RefreshToken, int(h.config.JWT.RefreshTokenExpireDuration*60), "/", h.config.Server.Domin, true, true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c.SetCookie(constant.RefreshTokenCookieName, token.RefreshToken, int(h.config.JWT.RefreshTokenExpireDuration*60), "/", h.config.Server.Domin, true, true) | |
http.SetCookie(c.Writer, &http.Cookie{ | |
Name: constant.RefreshTokenCookieName, | |
Value: token.RefreshToken, | |
MaxAge: int(h.config.JWT.RefreshTokenExpireDuration * 60), | |
Path: "/", | |
Domain: h.config.Server.Domain, | |
Secure: true, | |
HttpOnly: true, | |
SameSite: http.SameSiteStrictMode, | |
}) |
// @Success 200 {object} helper.BaseHttpResponse "Success" | ||
// @Failure 400 {object} helper.BaseHttpResponse "Failed" | ||
// @Failure 401 {object} helper.BaseHttpResponse "Failed" | ||
// @Router /v1/users/refresh-token [get] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that we are requesting a new token in refresh-token and not retrieving a persistent resource. so it's better to set method POST
// @Router /v1/users/refresh-token [get] | |
// @Router /v1/users/refresh-token [post] |
return | ||
} | ||
// Set the refresh token in a cookie | ||
c.SetCookie(constant.RefreshTokenCookieName, token.RefreshToken, int(h.config.JWT.RefreshTokenExpireDuration*60), "/", h.config.Server.Domin, true, true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as previous changes
c.SetCookie(constant.RefreshTokenCookieName, token.RefreshToken, int(h.config.JWT.RefreshTokenExpireDuration*60), "/", h.config.Server.Domin, true, true) | |
http.SetCookie(c.Writer, &http.Cookie{ | |
Name: constant.RefreshTokenCookieName, | |
Value: token.RefreshToken, | |
MaxAge: int(h.config.JWT.RefreshTokenExpireDuration * 60), | |
Path: "/", | |
Domain: h.config.Server.Domain, | |
Secure: true, | |
HttpOnly: true, | |
SameSite: http.SameSiteStrictMode, | |
}) |
@@ -14,4 +14,5 @@ func User(router *gin.RouterGroup, cfg *config.Config) { | |||
router.POST("/login-by-username", h.LoginByUsername) | |||
router.POST("/register-by-username", h.RegisterByUsername) | |||
router.POST("/login-by-mobile", h.RegisterLoginByMobileNumber) | |||
router.GET("/refresh-token", h.RefreshToken) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based previous comment
router.GET("/refresh-token", h.RefreshToken) | |
router.POST("/refresh-token", h.RefreshToken) |
@@ -2,6 +2,7 @@ server: | |||
internalPort: 5005 | |||
externalPort: 5005 | |||
runMode: debug | |||
domin: localhost |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
domin: localhost | |
domain: localhost |
@@ -2,6 +2,7 @@ server: | |||
internalPort: 5000 | |||
externalPort: 0 | |||
runMode: release | |||
domin: localhost |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
domin: localhost | |
domain: localhost |
@@ -2,6 +2,7 @@ server: | |||
internalPort: 5010 | |||
externalPort: 5010 | |||
runMode: release | |||
domin: localhost |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
domin: localhost | |
domain: localhost |
InternalPort string | ||
ExternalPort string | ||
RunMode string | ||
Domin string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Domin string | |
Domain string |
Please check. @alielmi98 |
New Feature:
-Implemented the RefreshToken method in the TokenUsecase to handle token refreshing. -The method retrieves the refresh token from the HTTP cookie, validates it, and generates a new access and refresh token pair.
Details:
-Extracts the refresh token from the cookie using c.Cookie. -Validates the refresh token and extracts claims using the GetClaims method. -Converts roles from []interface{} to []string for proper type handling. -Generates a new token pair using the GenerateToken method.
Reason for Addition:
-To provide functionality for refreshing expired access tokens while maintaining security through refresh tokens. -This is a critical feature for session management in the application.
Benefits:
-Enables secure token lifecycle management.
-Improves user experience by allowing seamless token refresh without requiring re-login.