Rewrite JWT for Echo
continuous-integration/drone/tag Build is passing Details

This commit is contained in:
Ymnuk 2023-10-06 10:44:13 +03:00
parent 7e78aa60bb
commit 81587ddbcb
12 changed files with 52 additions and 44 deletions

View File

@ -42,7 +42,7 @@ java -jar swagger-codegen-cli-2.4.32.jar generate -i docs/swagger.json -l typesc
# REST
Если в пути указаны параметры, например */geocoder/tile/:z/:y:x*, то *z*, *x* и *y* являются переменными и они вставляются в параметры path. Все что дальше идет по пути оставляется как есть, а директория в route формируется до переменных и, как в примере, имеет вид *geocoder/tile*
Если в пути указаны параметры, например */geocoder/tile/:z/:x/:y*, то *z*, *x* и *y* являются переменными и они вставляются в параметры path. Все что дальше идет по пути оставляется как есть, а директория в route формируется до переменных и, как в примере, имеет вид *geocoder/tile*
## data.name

View File

@ -8,6 +8,7 @@ github.com/golang-jwt/jwt
github.com/alexflint/go-arg
github.com/labstack/echo/v4
github.com/labstack/echo/v4/middleware
github.com/golang-jwt/jwt/v5
github.com/go-ldap/ldap/v3
github.com/swaggo/echo-swagger
github.com/swaggo/echo-swagger/example/docs

View File

@ -1,3 +1,3 @@
module {{ .Name }}
go 1.20
go 1.21

View File

@ -5,13 +5,11 @@ import (
"{{ .Name }}/route/api/safed"
"{{ .Name }}/route/api/unsafed"
"{{ .Name }}/route/api/user"
"{{ .Name }}/structs"
{{ range $index, $table := .Backend.Rest }}
{{ packageName $index}} "{{ $.Name }}/route/api/{{ backendFsPath $index }}"
{{ end }}
{{ packageName $index}} "{{ $.Name }}/route/api/{{ backendFsPath $index }}"{{ end }}
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
echojwt "github.com/labstack/echo-jwt/v4"
)
func Init(e *echo.Group) {
@ -27,27 +25,32 @@ func Init(e *echo.Group) {
{{ end }}
{{ end }}
config := middleware.JWTConfig{
/* config := middleware.JWTConfig{
Claims: &structs.JwtCustomClaims{},
SigningKey: []byte("secret"),
} */
config := echojwt.Config{
SigningKey: []byte("secret"),
}
r := e.Group("/user")
r.Use(middleware.JWTWithConfig(config))
r.Use(echojwt.WithConfig(config))
user.Init(r)
{{/* Динамическое формирование роутов*/}}
{{ range $index, $table := .Backend.Rest }}
{{ if not $table.Unsafe }}
r = e.Group("{{ $index }}")
r.Use(middleware.JWTWithConfig(config))
r.Use(echojwt.WithConfig(config))
{{ packageName $index}}.Init(r)
{{ end }}
{{ end }}
// Безопасные (безопасные, пройденные аутентификацию) роуты
r = e.Group("/unsafe")
r.Use(middleware.JWTWithConfig(config))
//r.Use(middleware.JWTWithConfig(config))
r.Use(echojwt.WithConfig(config))
safed.Init(r)
}

View File

@ -6,7 +6,8 @@ import (
"{{ .Project.Name }}/structs"
"net/http"
"github.com/golang-jwt/jwt"
uuid "github.com/satori/go.uuid"
"github.com/golang-jwt/jwt/v5"
{{ end }}
"github.com/labstack/echo/v4"
)
@ -39,8 +40,8 @@ func Init(c *echo.Group) {
func list(c echo.Context) error {
{{ if not .Rest.Unsafe }}
user := c.Get("user").(*jwt.Token)
claims := user.Claims.(*structs.JwtCustomClaims)
userID := claims.ID
claims := user.Claims.(jwt.MapClaims)
userID := uuid.FromStringOrNil(claims["id"].(string))
if !middleware.InsRole(userID, []string{
{{ if isMethod .Rest.Roles "LIST" }}
@ -76,8 +77,8 @@ func list(c echo.Context) error {
func count(c echo.Context) error {
{{ if not .Rest.Unsafe }}
user := c.Get("user").(*jwt.Token)
claims := user.Claims.(*structs.JwtCustomClaims)
userID := claims.ID
claims := user.Claims.(jwt.MapClaims)
userID := uuid.FromStringOrNil(claims["id"].(string))
if !middleware.InsRole(userID, []string{
{{ if isMethod .Rest.Roles "LIST" }}
@ -113,8 +114,8 @@ func count(c echo.Context) error {
func get(c echo.Context) error {
{{ if not .Rest.Unsafe }}
user := c.Get("user").(*jwt.Token)
claims := user.Claims.(*structs.JwtCustomClaims)
userID := claims.ID
claims := user.Claims.(jwt.MapClaims)
userID := uuid.FromStringOrNil(claims["id"].(string))
if !middleware.InsRole(userID, []string{
{{ if isMethod .Rest.Roles "GET" }}
@ -151,8 +152,8 @@ func get(c echo.Context) error {
func post(c echo.Context) error {
{{ if not .Rest.Unsafe }}
user := c.Get("user").(*jwt.Token)
claims := user.Claims.(*structs.JwtCustomClaims)
userID := claims.ID
claims := user.Claims.(jwt.MapClaims)
userID := uuid.FromStringOrNil(claims["id"].(string))
if !middleware.InsRole(userID, []string{
{{ if isMethod .Rest.Roles "POST" }}
@ -189,8 +190,8 @@ func post(c echo.Context) error {
func put(c echo.Context) error {
{{ if not .Rest.Unsafe }}
user := c.Get("user").(*jwt.Token)
claims := user.Claims.(*structs.JwtCustomClaims)
userID := claims.ID
claims := user.Claims.(jwt.MapClaims)
userID := uuid.FromStringOrNil(claims["id"].(string))
if !middleware.InsRole(userID, []string{
{{ if isMethod .Rest.Roles "PUT" }}
@ -226,8 +227,8 @@ func put(c echo.Context) error {
func delete(c echo.Context) error {
{{ if not .Rest.Unsafe }}
user := c.Get("user").(*jwt.Token)
claims := user.Claims.(*structs.JwtCustomClaims)
userID := claims.ID
claims := user.Claims.(jwt.MapClaims)
userID := uuid.FromStringOrNil(claims["id"].(string))
if !middleware.InsRole(userID, []string{
{{ if isMethod .Rest.Roles "DELETE" }}

View File

@ -8,7 +8,7 @@ import (
"{{ .Name }}/middleware"
"{{ .Name }}/structs"
"github.com/golang-jwt/jwt"
"github.com/golang-jwt/jwt/v5"
"github.com/labstack/echo/v4"
uuid "github.com/satori/go.uuid"
)
@ -30,8 +30,8 @@ import (
func delete(c echo.Context) error {
user := c.Get("user").(*jwt.Token)
claims := user.Claims.(*structs.JwtCustomClaims)
userID := claims.ID
claims := user.Claims.(jwt.MapClaims)
userID := uuid.FromStringOrNil(claims["id"].(string))
if !middleware.InRole(userID, "SEC_ADMIN") {
return c.JSON(http.StatusForbidden, structs.Result{

View File

@ -9,8 +9,9 @@ import (
"{{ .Name }}/middleware"
"{{ .Name }}/structs"
"github.com/golang-jwt/jwt"
"github.com/golang-jwt/jwt/v5"
"github.com/labstack/echo/v4"
uuid "github.com/satori/go.uuid"
ldap "github.com/go-ldap/ldap/v3"
)
@ -30,8 +31,8 @@ import (
// @Security BearerAuth
func searchLdap(c echo.Context) error {
user := c.Get("user").(*jwt.Token)
claims := user.Claims.(*structs.JwtCustomClaims)
userID := claims.ID
claims := user.Claims.(jwt.MapClaims)
userID := uuid.FromStringOrNil(claims["id"].(string))
if !middleware.InRole(userID, "SEC_ADMIN") {
return c.JSON(http.StatusForbidden, structs.Result{

View File

@ -8,8 +8,9 @@ import (
"{{ .Name }}/middleware"
"{{ .Name }}/structs"
"github.com/golang-jwt/jwt"
"github.com/golang-jwt/jwt/v5"
"github.com/labstack/echo/v4"
uuid "github.com/satori/go.uuid"
)
// ListUsers listUsers
@ -28,8 +29,8 @@ import (
func listUsers(c echo.Context) error {
user := c.Get("user").(*jwt.Token)
claims := user.Claims.(*structs.JwtCustomClaims)
userID := claims.ID
claims := user.Claims.(jwt.MapClaims)
userID := uuid.FromStringOrNil(claims["id"].(string))
if !middleware.InRole(userID, "SEC_ADMIN") {
return c.JSON(http.StatusForbidden, structs.Result{

View File

@ -8,7 +8,7 @@ import (
"{{ .Name }}/middleware"
"{{ .Name }}/structs"
"github.com/golang-jwt/jwt"
"github.com/golang-jwt/jwt/v5"
"github.com/labstack/echo/v4"
uuid "github.com/satori/go.uuid"
)
@ -30,8 +30,8 @@ import (
func lock(c echo.Context) error {
user := c.Get("user").(*jwt.Token)
claims := user.Claims.(*structs.JwtCustomClaims)
userID := claims.ID
claims := user.Claims.(jwt.MapClaims)
userID := uuid.FromStringOrNil(claims["id"].(string))
if !middleware.InRole(userID, "SEC_ADMIN") {
return c.JSON(http.StatusForbidden, structs.Result{

View File

@ -10,8 +10,9 @@ import (
"{{ .Name }}/middleware"
"{{ .Name }}/structs"
"github.com/golang-jwt/jwt"
"github.com/golang-jwt/jwt/v5"
"github.com/labstack/echo/v4"
uuid "github.com/satori/go.uuid"
)
// CreateUser createUser
@ -31,8 +32,8 @@ import (
func post(c echo.Context) error {
user := c.Get("user").(*jwt.Token)
claims := user.Claims.(*structs.JwtCustomClaims)
userID := claims.ID
claims := user.Claims.(jwt.MapClaims)
userID := uuid.FromStringOrNil(claims["id"].(string))
if !middleware.InRole(userID, "SEC_ADMIN") {
return c.JSON(http.StatusForbidden, structs.Result{

View File

@ -10,7 +10,7 @@ import (
"{{ .Name }}/middleware"
"{{ .Name }}/structs"
"github.com/golang-jwt/jwt"
"github.com/golang-jwt/jwt/v5"
"github.com/labstack/echo/v4"
uuid "github.com/satori/go.uuid"
)
@ -33,8 +33,8 @@ import (
func put(c echo.Context) error {
user := c.Get("user").(*jwt.Token)
claims := user.Claims.(*structs.JwtCustomClaims)
userID := claims.ID
claims := user.Claims.(jwt.MapClaims)
userID := uuid.FromStringOrNil(claims["id"].(string))
if !middleware.InRole(userID, "SEC_ADMIN") {
return c.JSON(http.StatusForbidden, structs.Result{

View File

@ -8,7 +8,7 @@ import (
"{{ .Name }}/middleware"
"{{ .Name }}/structs"
"github.com/golang-jwt/jwt"
"github.com/golang-jwt/jwt/v5"
"github.com/labstack/echo/v4"
uuid "github.com/satori/go.uuid"
)
@ -30,8 +30,8 @@ import (
func unlock(c echo.Context) error {
user := c.Get("user").(*jwt.Token)
claims := user.Claims.(*structs.JwtCustomClaims)
userID := claims.ID
claims := user.Claims.(jwt.MapClaims)
userID := uuid.FromStringOrNil(claims["id"].(string))
if !middleware.InRole(userID, "SEC_ADMIN") {
return c.JSON(http.StatusForbidden, structs.Result{