-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth0.go
110 lines (90 loc) · 2.79 KB
/
auth0.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package goechoauth0middleware
import (
"log"
"net/http"
"net/url"
"strings"
"time"
"github.com/auth0/go-jwt-middleware/v2/jwks"
"github.com/auth0/go-jwt-middleware/v2/validator"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
type (
Auth0Config struct {
// Skipper defines a function to skip middleware.
Skipper middleware.Skipper
Issuer string `yaml:"issuer"`
Audience []string `yaml:"audience"`
SignatureAlgorithm string `yaml:"signature_algorithm"`
CacheDuration time.Duration `yaml:"cache_duration"`
}
)
var (
// DefaultAuth0Config is the default Auth0 middleware config.
DefaultAuth0Config = Auth0Config{
Skipper: middleware.DefaultSkipper,
Issuer: "",
Audience: []string{},
SignatureAlgorithm: "RS256",
CacheDuration: 5 * time.Minute,
}
)
func Auth0() echo.MiddlewareFunc {
return Auth0WithConfig(DefaultAuth0Config)
}
func Auth0WithConfig(config Auth0Config) echo.MiddlewareFunc {
// Defaults
if config.Skipper == nil {
config.Skipper = DefaultAuth0Config.Skipper
}
if config.Issuer == "" {
config.Issuer = DefaultAuth0Config.Issuer
}
if len(config.Audience) == 0 {
config.Audience = DefaultAuth0Config.Audience
}
if config.SignatureAlgorithm == "" {
config.SignatureAlgorithm = DefaultAuth0Config.SignatureAlgorithm
}
if config.CacheDuration == 0 {
config.CacheDuration = DefaultAuth0Config.CacheDuration
}
issuerURL, err := url.Parse(config.Issuer)
if err != nil {
log.Fatalf("failed to parse the issuer url: %v", err)
}
provider := jwks.NewCachingProvider(issuerURL, config.CacheDuration)
// Set up the validator.
jwtValidator, err := validator.New(
provider.KeyFunc,
validator.SignatureAlgorithm(config.SignatureAlgorithm),
issuerURL.String(),
config.Audience,
)
if err != nil {
log.Fatalf("failed to set up the validator: %v", err)
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
authorization := c.Request().Header.Get("Authorization")
if authorization == "" {
return echo.NewHTTPError(http.StatusUnauthorized, "No Authorization Header")
}
// check if authorization header has bearer prefix
if !strings.HasPrefix(authorization, "Bearer ") {
return echo.NewHTTPError(http.StatusUnauthorized, "Invalid Authorization Header")
}
// get token from header
token := strings.TrimPrefix(authorization, "Bearer ")
// Get the JWT token from the request header.
claims, err := jwtValidator.ValidateToken(c.Request().Context(), token)
if err != nil {
return echo.NewHTTPError(http.StatusUnauthorized, "Invalid Token")
}
// Set the claims in the context.
c.Set("claims", claims.(*validator.ValidatedClaims))
return next(c)
}
}
}