-
Notifications
You must be signed in to change notification settings - Fork 0
/
middlewares.go
45 lines (38 loc) · 1.21 KB
/
middlewares.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
package server
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
"github.com/iron-io/functions/api/models"
"github.com/iron-io/functions/common"
"github.com/spf13/viper"
)
func SetupJwtAuth(funcServer *Server) {
// Add default JWT AUTH if env variable set
if jwtAuthKey := viper.GetString("jwt_auth_key"); jwtAuthKey != "" {
funcServer.AddMiddlewareFunc(func(ctx MiddlewareContext, w http.ResponseWriter, r *http.Request, app *models.App) error {
start := time.Now()
fmt.Println("JwtAuthMiddlewareFunc called at:", start)
ctx.Next()
fmt.Println("Duration:", (time.Now().Sub(start)))
return nil
})
funcServer.AddMiddleware(&JwtAuthMiddleware{})
}
}
type JwtAuthMiddleware struct {
}
func (h *JwtAuthMiddleware) Serve(ctx MiddlewareContext, w http.ResponseWriter, r *http.Request, app *models.App) error {
fmt.Println("JwtAuthMiddleware called")
jwtAuthKey := viper.GetString("jwt_auth_key")
if err := common.AuthJwt(jwtAuthKey, r); err != nil {
w.WriteHeader(http.StatusUnauthorized)
m := map[string]string{"error": "Invalid API Authorization token."}
json.NewEncoder(w).Encode(m)
return errors.New("Invalid API authorization token.")
}
fmt.Println("auth succeeded!")
return nil
}