Created
February 12, 2015 09:15
-
-
Save StephanDollberg/b99dbbf3620b70cc0f0d to your computer and use it in GitHub Desktop.
jwt auth example
This file contains 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 ( | |
"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