Refactor example

This commit is contained in:
Matthew Dillon 2015-04-30 16:19:14 -08:00
parent f3672c9a0c
commit 1d3c39bb49
2 changed files with 70 additions and 81 deletions

View file

@ -17,32 +17,32 @@ func dontProtectMe(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "not secured")
}
func auth(email string, password string) error {
// Hard-code a user
if email != "test" || password != "test" {
return errors.New("invalid credentials")
}
return nil
}
func setClaims(id string) (map[string]interface{}, error) {
currentTime := time.Now()
return map[string]interface{}{
"iat": currentTime.Unix(),
"exp": currentTime.Add(time.Minute * 60 * 24).Unix(),
}, nil
}
func verifyClaims([]byte) error {
// We don't really care about the claims, just approve as-is
return nil
}
func main() {
authFunc := func(email string, password string) error {
// Hard-code a user
if email != "test" || password != "test" {
return errors.New("invalid credentials")
}
return nil
}
claimsFunc := func(string) (map[string]interface{}, error) {
currentTime := time.Now()
return map[string]interface{}{
"iat": currentTime.Unix(),
"exp": currentTime.Add(time.Minute * 60 * 24).Unix(),
}, nil
}
verifyClaimsFunc := func([]byte) error {
// We don't really care about the claims, just approve as-is
return nil
}
config := &jwt.Config{
Secret: "password",
Auth: authFunc,
Claims: claimsFunc,
Auth: auth,
Claims: setClaims,
}
j, err := jwt.New(config)
@ -54,7 +54,7 @@ func main() {
dontProtect := http.HandlerFunc(dontProtectMe)
http.Handle("/authenticate", j.GenerateToken())
http.Handle("/secure", j.Secure(protect, verifyClaimsFunc))
http.Handle("/secure", j.Secure(protect, verifyClaims))
http.Handle("/insecure", dontProtect)
http.ListenAndServe(":8080", nil)
}