Skip to content

Instantly share code, notes, and snippets.

@StephanDollberg
Created February 12, 2015 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StephanDollberg/b99dbbf3620b70cc0f0d to your computer and use it in GitHub Desktop.
Save StephanDollberg/b99dbbf3620b70cc0f0d to your computer and use it in GitHub Desktop.
jwt auth example
package main
import (
"github.com/ant0ine/go-json-rest/rest"
"log"
"net/http"
)
func handle_auth(w rest.ResponseWriter, r *rest.Request) {
w.WriteJson(map[string]string{"authed": r.Env["REMOTE_USER"].(string)})
}
func main() {
jwt_middleware := rest.JWTMiddleware{
Key: []byte("secret key"),
Realm: "jwt auth",
Authenticator: func(userId string, password string) bool {
if userId == "admin" && password == "admin" {
return true
}
return false
}}
login_api := rest.NewApi()
login_api.Use(rest.DefaultDevStack...)
login_router, _ := rest.MakeRouter(
&rest.Route{"POST", "/login", jwt_middleware.LoginHandler},
)
login_api.SetApp(login_router)
main_api := rest.NewApi()
main_api.Use(&jwt_middleware)
main_api.Use(rest.DefaultDevStack...)
main_api_router, _ := rest.MakeRouter(
&rest.Route{"GET", "/auth_test", handle_auth})
main_api.SetApp(main_api_router)
http.Handle("/", login_api.MakeHandler())
http.Handle("/api/", http.StripPrefix("/api", main_api.MakeHandler()))
log.Fatal(http.ListenAndServe(":20000", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment