Skip to content

Commit

Permalink
Merge pull request #18 from RobyFerro/fix-middleware
Browse files Browse the repository at this point in the history
Fix middleware
  • Loading branch information
RobyFerro committed Aug 27, 2021
2 parents d1161e8 + 91267f8 commit 2a3e3ae
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ http-client.env.json
.vscode
go-web
node_modules
*.test.json
*.test.json
alfred
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
All notable changes to the "Go-web" will be documented in this file.

## [Unreleased]
### Changed

## [v0.6.2-beta] - 2021-08-27
## Changed
- Updated gwf version
- Now evey middleware consists in an isolated struct.

## [v0.6.1-beta] - 2021-08-25
## Changed
Expand Down
8 changes: 2 additions & 6 deletions app/http/controller/auth.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package controller

import (
"github.com/RobyFerro/go-web-framework"
"github.com/RobyFerro/go-web/app/auth"
"github.com/labstack/gommon/log"
"net/http"
Expand All @@ -25,9 +24,7 @@ type Credentials struct {
}

// JWTAuthentication provides user authentication with JWT
func (c *AuthController) JWTAuthentication(db *gorm.DB) {
conf := foundation.RetrieveConfig()

func (c *AuthController) JWTAuthentication(db *gorm.DB, conf *kernel.Conf) {
var payload Credentials
var user *model.User
var jwt auth.JWTAuth
Expand Down Expand Up @@ -71,8 +68,7 @@ func (c *AuthController) JWTAuthentication(db *gorm.DB) {
}

// BasicAuthentication perform basic authentication method
func (c *AuthController) BasicAuthentication(db *gorm.DB) {
session := foundation.RetrieveCookieStore()
func (c *AuthController) BasicAuthentication(db *gorm.DB, session *sessions.CookieStore) {
var payload Credentials

if err := tool.DecodeJsonRequest(c.Request, &payload); err != nil {
Expand Down
2 changes: 0 additions & 2 deletions app/http/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package controller
import (
"github.com/RobyFerro/go-web-framework/kernel"
"github.com/RobyFerro/go-web-framework/tool"
"net/http"
)

type HomeController struct {
Expand All @@ -14,6 +13,5 @@ type HomeController struct {
// the method/properties declared in BaseController (controller.go).
// Of course you can edit this method with a custom logic.
func (c *HomeController) Main() {
c.Response.WriteHeader(http.StatusOK)
tool.View("index.html", c.Response, nil)
}
4 changes: 1 addition & 3 deletions app/http/controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package controller

import (
"encoding/json"
"github.com/RobyFerro/go-web-framework"
"github.com/RobyFerro/go-web-framework/kernel"
"github.com/RobyFerro/go-web-framework/tool"
jwt "github.com/RobyFerro/go-web/app/auth"
Expand Down Expand Up @@ -62,8 +61,7 @@ func (c *UserController) Insert(db *gorm.DB) {
}

// Profile method return information about the authenticated user.
func (c *UserController) Profile() {
conf := foundation.RetrieveConfig()
func (c *UserController) Profile(conf *kernel.Conf) {
var auth jwt.JWTAuth

if err := auth.GetUser(c.Request, conf.App.Key); err != nil {
Expand Down
12 changes: 8 additions & 4 deletions app/http/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ import (
"net/http"
)

// Auth checks if the JWT used by the request is valid.
type AuthMiddleware struct {
Name string
Description string
}

// Handle checks if the JWT used by the request is valid.
// This middleware must be used only with JWT authentication and will not work with the basic auth.
func (Middleware) Auth(next http.Handler) http.Handler {
var key string
func (AuthMiddleware) Handle(next http.Handler) http.Handler {
conf := foundation.RetrieveConfig()

if len(conf.App.Key) == 0 {
log.Fatal("HTTP server unable to start, expected an APP_KEY for JWT auth")
}
jwtMiddleware := New(Options{
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
return []byte(key), nil
return []byte(conf.App.Key), nil
},
SigningMethod: jwt.SigningMethodHS256,
})
Expand Down
9 changes: 7 additions & 2 deletions app/http/middleware/basic_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ import (
"net/http"
)

// BasicAuth used to check if the basic auth session is present.
type BasicAuthMiddleware struct {
Name string
Description string
}

// Handle used to check if the basic auth session is present.
// Use this middleware to protect resources with the basic authentication.
func (Middleware) BasicAuth(next http.Handler) http.Handler {
func (BasicAuthMiddleware) Handle(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
store := foundation.RetrieveCookieStore()
session, err := store.Get(r, "basic-auth")
Expand Down
9 changes: 7 additions & 2 deletions app/http/middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import (
"net/http"
)

// Logging log every actions printing used route
func (Middleware) Logging(next http.Handler) http.Handler {
type LoggingMiddleware struct {
Name string
Description string
}

// Handle log every actions printing used route
func (LoggingMiddleware) Handle(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Do stuff here
log.Println(r.RequestURI)
Expand Down
8 changes: 0 additions & 8 deletions app/http/middleware/middleware.go

This file was deleted.

9 changes: 7 additions & 2 deletions app/http/middleware/rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import (
"net/http"
)

// RateLimiter set a limit of request allowed in a specific time
func (Middleware) RateLimiter(next http.Handler) http.Handler {
type RateLimiterMiddleware struct {
Name string
Description string
}

// Handle set a limit of request allowed in a specific time
func (RateLimiterMiddleware) Handle(next http.Handler) http.Handler {
var limiter = rate.NewLimiter(1, 3)

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
9 changes: 7 additions & 2 deletions app/http/middleware/refresh_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import (
"net/http"
)

// RefreshToken return a new token in request response
func (Middleware) RefreshToken(next http.Handler) http.Handler {
type RefreshTokenMiddleware struct {
Name string
Description string
}

// Handle return a new token in request response
func (RefreshTokenMiddleware) Handle(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var a auth.JWTAuth
conf := foundation.RetrieveConfig()
Expand Down
3 changes: 1 addition & 2 deletions database/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ type User struct {
// Seed executes seeding in defined table
func (User) Seed(db *gorm.DB) {
for i := 0; i < 10; i++ {
password := gofakeit.Password(true, true, true, true, false, 32)
encryptedPassword, _ := bcrypt.GenerateFromPassword([]byte(password), 14)
encryptedPassword, _ := bcrypt.GenerateFromPassword([]byte("password"), 14)

user := User{
Name: gofakeit.FirstName(),
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module github.com/RobyFerro/go-web
go 1.16

// Only for development environment
//replace github.com/RobyFerro/go-web-framework => <your local gwf path>
// replace github.com/RobyFerro/go-web-framework => <replace with your local repo of gwf>

require (
github.com/RobyFerro/go-web-framework v0.6.1-beta
github.com/RobyFerro/go-web-framework v0.6.3-beta
github.com/auth0/go-jwt-middleware v0.0.0-20200810150920-a32d7af194d1
github.com/brianvoe/gofakeit/v4 v4.3.0
github.com/denisenkom/go-mssqldb v0.10.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/RobyFerro/dig v1.12.1-0.20210820090948-eef4b5ae40c2 h1:RaMKVl0nfJD/neflcaPmWJYyzt43HfX5kmN3gdWGjRw=
github.com/RobyFerro/dig v1.12.1-0.20210820090948-eef4b5ae40c2/go.mod h1:jfL50cNXPp4gqLYHPyZMyUS9D05UTUJCdkxNU6gftXc=
github.com/RobyFerro/go-web-framework v0.6.1-beta h1:ZQBg6fiz65Z2JnyrujczP8YWNHNDyXYymUmlf9WEvtw=
github.com/RobyFerro/go-web-framework v0.6.1-beta/go.mod h1:wB/7eaLLMAq+y4rGnKxvrRlw+c3RoO6i825jBCd44gU=
github.com/RobyFerro/go-web-framework v0.6.3-beta h1:060vVAsA92MRJeEgzlgvJqKxuiaDVeI8LOFqPHTizKo=
github.com/RobyFerro/go-web-framework v0.6.3-beta/go.mod h1:wB/7eaLLMAq+y4rGnKxvrRlw+c3RoO6i825jBCd44gU=
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/auth0/go-jwt-middleware v0.0.0-20200810150920-a32d7af194d1 h1:lnVadil6o8krZE47ms2PCxhXcki/UwoqiB0axOIV3mk=
github.com/auth0/go-jwt-middleware v0.0.0-20200810150920-a32d7af194d1/go.mod h1:mF0ip7kTEFtnhBJbd/gJe62US3jykNN+dcZoZakJCCA=
Expand Down
3 changes: 1 addition & 2 deletions register/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package register

import (
"github.com/RobyFerro/go-web-framework"
"github.com/RobyFerro/go-web/app/http/middleware"
)

// BaseEntities returns a struct that contains Go-Web base entities
Expand All @@ -14,6 +13,6 @@ func BaseEntities() foundation.BaseEntities {
SingletonServices: SingletonServices,
CommandServices: CommandServices,
Models: Models,
Middlewares: middleware.Middleware{},
Middlewares: Middleware,
}
}
25 changes: 25 additions & 0 deletions register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/RobyFerro/go-web/app"
"github.com/RobyFerro/go-web/app/console"
"github.com/RobyFerro/go-web/app/http/controller"
"github.com/RobyFerro/go-web/app/http/middleware"
"github.com/RobyFerro/go-web/database/model"
"github.com/RobyFerro/go-web/service"
)
Expand Down Expand Up @@ -64,4 +65,28 @@ var (
// Here is where you've to register your custom commands
},
}
Middleware = register.MiddlewareRegister{
List: []interface{}{
&middleware.AuthMiddleware{
Name: "Auth",
Description: "Provides JWT authentication",
},
&middleware.BasicAuthMiddleware{
Name: "BasicAuth",
Description: "Provides basic authentication",
},
&middleware.LoggingMiddleware{
Name: "Logging",
Description: "Logs every request in console",
},
&middleware.RateLimiterMiddleware{
Name: "RateLimiter",
Description: "Provides rate limit over HTTP requests",
},
&middleware.RefreshTokenMiddleware{
Name: "RefreshToken",
Description: "Refresh JWT token",
},
},
}
)

0 comments on commit 2a3e3ae

Please sign in to comment.