-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
42 lines (34 loc) · 1.11 KB
/
main.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
package main
import (
"fmt"
"net/http"
"os"
"github.com/auth0-community/auth0"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
jose "gopkg.in/square/go-jose.v2"
)
func main() {
r := mux.NewRouter()
r.Handle("/", http.FileServer(http.Dir("./views/")))
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
http.ListenAndServe(":8000", handlers.LoggingHandler(os.Stdout, r))
}
func authMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
secret := []byte("E3qcWNk0Di9xUkE01pOJyIUGyxWIib7u")
secretProvider := auth0.NewKeyProvider(secret)
audience := []string{"appleGo"}
configuration := auth0.NewConfiguration(secretProvider, audience, "https://dominickhera.auth0.com/", jose.HS256)
validator := auth0.NewValidator(configuration, nil)
token, err := validator.ValidateRequest(r)
if err != nil {
fmt.Println(err)
fmt.Println("Token is not valid:", token)
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorized"))
} else {
next.ServeHTTP(w, r)
}
})
}