Skip to content

Instantly share code, notes, and snippets.

@elbuo8
Created October 17, 2014 16:56
Show Gist options
  • Save elbuo8/ee75d2944a7f877bea8b to your computer and use it in GitHub Desktop.
Save elbuo8/ee75d2944a7f877bea8b to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/codegangsta/negroni"
jwt "github.com/dgrijalva/jwt-go"
"github.com/gorilla/mux"
)
var (
signingKey = "HELLO GO-MIAMI"
privateKey []byte
publicKey []byte
)
func init() {
privateKey, _ = ioutil.ReadFile("demo.rsa")
publicKey, _ = ioutil.ReadFile("demo.rsa.pub")
}
func AuthMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
token, err := jwt.ParseFromRequest(r, func(token *jwt.Token) (interface{}, error) {
return publicKey, nil
})
if err == nil && token.Valid {
next(w, r)
} else {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, "GO HOME SON")
}
}
func APIHandler(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "WIN")
}
func main() {
router := mux.NewRouter()
n := negroni.Classic()
router.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
token := jwt.New(jwt.GetSigningMethod("RS256"))
tokenString, _ := token.SignedString(privateKey)
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, tokenString)
})
router.Handle("/api", negroni.New(negroni.HandlerFunc(AuthMiddleware), negroni.HandlerFunc(APIHandler)))
n.UseHandler(router)
http.ListenAndServe(":3000", n)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment