Last active
March 7, 2018 13:21
-
-
Save AshCoolman/6f95d710d09b751a5f453d4fa3a3b648 to your computer and use it in GitHub Desktop.
auth0 in go HelloWorld
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/base64" | |
"encoding/json" | |
"fmt" | |
"net/http" | |
"os" | |
"github.com/auth0-community/auth0" | |
"github.com/gorilla/handlers" | |
"github.com/gorilla/mux" | |
jose "gopkg.in/square/go-jose.v2" | |
) | |
type Product struct { | |
ID int | |
Name string | |
Slug string | |
Description string | |
} | |
/* We will create our catalog of VR experiences and store them in a slice. */ | |
var products = []Product{ | |
Product{ID: 1, Name: "Hover Shooters", Slug: "hover-shooters", Description: "Shoot your way to the top on 14 different hoverboards"}, | |
Product{ID: 2, Name: "Ocean Explorer", Slug: "ocean-explorer", Description: "Explore the depths of the sea in this one of a kind underwater experience"}, | |
Product{ID: 3, Name: "Dinosaur Park", Slug: "dinosaur-park", Description: "Go back 65 million years in the past and rIDe a T-Rex"}, | |
Product{ID: 4, Name: "Cars VR", Slug: "cars-vr", Description: "Get behind the wheel of the fastest cars in the world."}, | |
Product{ID: 5, Name: "Robin Hood", Slug: "robin-hood", Description: "Pick up the bow and arrow and master the art of archery"}, | |
Product{ID: 6, Name: "Real World VR", Slug: "real-world-vr", Description: "Explore the seven wonders of the world in VR"}, | |
} | |
func main() { | |
r := mux.NewRouter() | |
r.Handle("/", http.FileServer(http.Dir("./views/"))) | |
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))) | |
r.Handle("/products", authMiddleware(ProductsHandler)).Methods("GET") | |
r.Handle("/products/{slug}/feedback", authMiddleware(AddFeedbackHandler)).Methods("POST") | |
http.ListenAndServe(":3000", handlers.LoggingHandler(os.Stdout, r)) | |
} | |
func authMiddleware(next http.Handler) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
// Creates a configuration with the Auth0 information | |
secret, _ := base64.URLEncoding.DecodeString(os.Getenv("AUTH0_CLIENT_SECRET")) | |
secretProvider := auth0.NewKeyProvider(secret) | |
audience := os.Getenv("AUTH0_CLIENT_ID") | |
configuration := auth0.NewConfiguration(secretProvider, []string{audience}, "https://comparator.eu.auth0.com/", jose.HS256) | |
validator := auth0.NewValidator(configuration) | |
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) | |
} | |
}) | |
} | |
var ProductsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
payload, _ := json.Marshal(products) | |
w.Header().Set("Content-Type", "application/json") | |
w.Write([]byte(payload)) | |
}) | |
var AddFeedbackHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
var product Product | |
vars := mux.Vars(r) | |
slug := vars["slug"] | |
for _, p := range products { | |
if p.Slug == slug { | |
product = p | |
} | |
} | |
w.Header().Set("Content-Type", "application/json") | |
if product.Slug != "" { | |
payload, _ := json.Marshal(product) | |
w.Write([]byte(payload)) | |
} else { | |
w.Write([]byte("Product Not Found")) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment