gitstack

grindlemire/gothem-stack code browser

610 B Go 20 lines 2024-07-19 · 7d26781 raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package auth

import (
	"github.com/labstack/echo/v4"
	"github.com/pkg/errors"
)

// Middleware is a simple middleware that checks the request for authentication
func Middleware() echo.MiddlewareFunc {
	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) (err error) {
			// obviously this is not real authentication and is just illustrative of what you can do here
			username, _, ok := c.Request().BasicAuth()
			if ok && username == "reject" {
				return echo.ErrUnauthorized.SetInternal(errors.Errorf("user; %s is not authorized", username))
			}
			return next(c)
		}
	}
}