-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from DoWithLogic/feat/revoked-token-after-logout
✨ feat: revoked token after logout with redis
- Loading branch information
Showing
15 changed files
with
245 additions
and
78 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
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 |
---|---|---|
@@ -1,30 +1,48 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/DoWithLogic/golang-clean-architecture/internal/middleware" | ||
userV1 "github.com/DoWithLogic/golang-clean-architecture/internal/users/delivery/http/v1" | ||
userRepository "github.com/DoWithLogic/golang-clean-architecture/internal/users/repository" | ||
userUseCase "github.com/DoWithLogic/golang-clean-architecture/internal/users/usecase" | ||
"github.com/DoWithLogic/golang-clean-architecture/pkg/app_crypto" | ||
"github.com/DoWithLogic/golang-clean-architecture/pkg/app_jwt" | ||
"github.com/DoWithLogic/golang-clean-architecture/pkg/app_redis" | ||
"github.com/go-redis/redis/v8" | ||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func (app *App) startService() error { | ||
domain := app.echo.Group("/api/v1/users") | ||
domain := app.echo.Group("/api/v1") | ||
domain.GET("/ping", func(c echo.Context) error { | ||
return c.String(http.StatusOK, "Hello Word 👋") | ||
}) | ||
|
||
client := redis.NewClient(&redis.Options{ | ||
Addr: app.cfg.Redis.Addr, | ||
Password: app.cfg.Redis.Password, | ||
DB: app.cfg.Redis.DB, | ||
}) | ||
|
||
if err := client.Ping(context.Background()).Err(); err != nil { | ||
return err | ||
} | ||
|
||
var ( | ||
crypto = app_crypto.NewCrypto(app.cfg.Authentication.Key) | ||
appJwt = app_jwt.NewJWT(app.cfg.JWT) | ||
redis = app_redis.NewRedis(client) | ||
crypto = app_crypto.NewCrypto(app.cfg.Authentication.Key) | ||
jwt = app_jwt.NewJWT(app.cfg.JWT, redis) | ||
middleware = middleware.NewMiddleware(jwt) | ||
|
||
userRepo = userRepository.NewRepository(app.db) | ||
userUC = userUseCase.NewUseCase(userRepo, appJwt, crypto) | ||
userUC = userUseCase.NewUseCase(userRepo, jwt, crypto) | ||
userCTRL = userV1.NewHandlers(userUC) | ||
) | ||
|
||
return userCTRL.UserRoutes(domain, app.cfg) | ||
userCTRL.UserRoutes(domain, middleware) | ||
|
||
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 |
---|---|---|
@@ -1,67 +1,41 @@ | ||
package middleware | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/DoWithLogic/golang-clean-architecture/config" | ||
"github.com/DoWithLogic/golang-clean-architecture/pkg/app_jwt" | ||
"github.com/DoWithLogic/golang-clean-architecture/pkg/apperror" | ||
"github.com/DoWithLogic/golang-clean-architecture/pkg/constant" | ||
"github.com/DoWithLogic/golang-clean-architecture/pkg/response" | ||
"github.com/golang-jwt/jwt" | ||
"github.com/labstack/echo/v4" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type PayloadToken struct { | ||
Data *Data `json:"data"` | ||
jwt.StandardClaims | ||
var ( | ||
errMissingJwtToken = errors.New("Missing JWT token") | ||
errInvalidJwtToken = errors.New("Invalid JWT token") | ||
) | ||
|
||
type Middleware struct { | ||
jwt *app_jwt.JWT | ||
} | ||
|
||
type Data struct { | ||
UserID int64 `json:"user_id"` | ||
Email string `json:"email"` | ||
func NewMiddleware(jwt *app_jwt.JWT) *Middleware { | ||
return &Middleware{jwt: jwt} | ||
} | ||
|
||
// Middleware function to validate JWT token | ||
func JWTMiddleware(cfg config.Config) echo.MiddlewareFunc { | ||
func (m *Middleware) JWTMiddleware() echo.MiddlewareFunc { | ||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
tokenString := c.Request().Header.Get(constant.AuthorizationHeaderKey) | ||
if tokenString == "" { | ||
return response.ErrorBuilder(apperror.Unauthorized(errors.New("Missing JWT token"))).Send(c) | ||
token := c.Request().Header.Get(constant.AuthorizationHeaderKey) | ||
if token == "" { | ||
return response.ErrorBuilder(apperror.Unauthorized(errMissingJwtToken)).Send(c) | ||
} | ||
|
||
// Remove "Bearer " prefix from token string | ||
tokenString = tokenString[len("Bearer "):] | ||
|
||
token, err := jwt.ParseWithClaims(tokenString, &PayloadToken{}, func(token *jwt.Token) (interface{}, error) { | ||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { | ||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) | ||
} | ||
return []byte(cfg.JWT.Key), nil | ||
}) | ||
|
||
if err != nil { | ||
return response.ErrorBuilder(apperror.Unauthorized(errors.New("Invalid JWT token"))).Send(c) | ||
} | ||
if !token.Valid { | ||
return response.ErrorBuilder(apperror.Unauthorized(errors.New("JWT token is not valid"))).Send(c) | ||
if err := m.jwt.ValidateToken(c, token); err != nil { | ||
return err | ||
} | ||
|
||
// Store the token claims in the request context for later use | ||
claims := token.Claims.(*PayloadToken) | ||
c.Set(constant.AuthCredentialKey, claims) | ||
|
||
return next(c) | ||
} | ||
} | ||
} | ||
|
||
func NewTokenInformation(ctx echo.Context) (*PayloadToken, error) { | ||
tokenInformation, ok := ctx.Get(constant.AuthCredentialKey).(*PayloadToken) | ||
if !ok { | ||
return tokenInformation, apperror.Unauthorized(apperror.ErrFailedGetTokenInformation) | ||
} | ||
|
||
return tokenInformation, 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
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 |
---|---|---|
@@ -1,17 +1,20 @@ | ||
package v1 | ||
|
||
import ( | ||
"github.com/DoWithLogic/golang-clean-architecture/config" | ||
"github.com/DoWithLogic/golang-clean-architecture/internal/middleware" | ||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func (h *handlers) UserRoutes(domain *echo.Group, cfg config.Config) error { | ||
domain.POST("", h.CreateUser) | ||
domain.POST("/login", h.Login) | ||
domain.GET("/detail", h.UserDetail, middleware.JWTMiddleware(cfg)) | ||
domain.PATCH("/update", h.UpdateUser, middleware.JWTMiddleware(cfg)) | ||
domain.PUT("/update/status", h.UpdateUserStatus, middleware.JWTMiddleware(cfg)) | ||
func (h *handlers) UserRoutes(domain *echo.Group, mw *middleware.Middleware) { | ||
// privates | ||
{ | ||
private := domain.Group("/user", mw.JWTMiddleware()) | ||
private.GET("/detail", h.UserDetail) | ||
private.PATCH("/update", h.UpdateUser) | ||
private.PUT("/update/status", h.UpdateUserStatus) | ||
} | ||
|
||
return nil | ||
// publics | ||
domain.POST("/user", h.CreateUser) | ||
domain.POST("/user/login", h.Login) | ||
} |
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
Oops, something went wrong.