Last active
May 22, 2017 14:29
-
-
Save aalvesjr/36de019dba1759c96129719fdb8d34a0 to your computer and use it in GitHub Desktop.
Revisions
-
aalvesjr revised this gist
May 22, 2017 . 1 changed file with 12 additions and 9 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -12,6 +12,11 @@ import ( const secret = "secret" type DefaultClaims struct { UserID uint `json:"user_id"` RoleType string `json:"role_type"` } func main() { router := mux.NewRouter() router.HandleFunc("/", Authorizate(DefaultHandler, secret)).Methods("POST") @@ -23,17 +28,12 @@ func DefaultHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) d, ok := r.Context().Value("defaultClaims").(DefaultClaims) if !ok { fmt.Println("doesn't has the key user_id") } fmt.Printf("UserID: %d, role: %s\n", d.UserID, d.RoleType) json.NewEncoder(w).Encode(map[string]string{ "message": "OK", @@ -49,8 +49,11 @@ func Authorizate(next http.HandlerFunc, secret string) http.HandlerFunc { if validateToken(token, secret) { ctx := r.Context() d := DefaultClaims{ UserID: 123, RoleType: "SUPPORT", } ctx = context.WithValue(ctx, "defaultClaims", d) next.ServeHTTP(w, r.WithContext(ctx)) } else { -
aalvesjr revised this gist
May 22, 2017 . 1 changed file with 1 addition and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -10,10 +10,7 @@ import ( "github.com/gorilla/mux" ) const secret = "secret" func main() { router := mux.NewRouter() -
aalvesjr revised this gist
May 22, 2017 . 1 changed file with 22 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ package main import ( "context" "encoding/json" "fmt" "net/http" @@ -9,7 +10,10 @@ import ( "github.com/gorilla/mux" ) const ( secret = "secret" userID = "usera_id" ) func main() { router := mux.NewRouter() @@ -21,7 +25,18 @@ func main() { func DefaultHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) id, ok := r.Context().Value("user_id").(int64) if !ok { fmt.Println("doesn't has the key user_id") } role, ok := r.Context().Value("role_type").(string) if !ok { fmt.Println("doesn't has the key role_type") } fmt.Printf("UserID: %d, role: %s\n", id, role) json.NewEncoder(w).Encode(map[string]string{ "message": "OK", @@ -35,9 +50,12 @@ func Authorizate(next http.HandlerFunc, secret string) http.HandlerFunc { // validateToken must return a 'token.DefaultClaims' and bool if validateToken(token, secret) { ctx := r.Context() ctx = context.WithValue(ctx, "user_id", int64(31)) ctx = context.WithValue(ctx, "role_type", "SUPPORT") next.ServeHTTP(w, r.WithContext(ctx)) } else { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusForbidden) -
aalvesjr created this gist
May 22, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,55 @@ package main import ( "encoding/json" "fmt" "net/http" "strings" "github.com/gorilla/mux" ) const secret = "secret" func main() { router := mux.NewRouter() router.HandleFunc("/", Authorizate(DefaultHandler, secret)).Methods("POST") http.ListenAndServe(":8090", router) } func DefaultHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) fmt.Println("CustomHeader:", r.Header.Get("CustomHeader")) json.NewEncoder(w).Encode(map[string]string{ "message": "OK", }) } func Authorizate(next http.HandlerFunc, secret string) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { authHeader := strings.Split(r.Header.Get("Authorization"), " ") token := authHeader[len(authHeader)-1] // validateToken must return a 'token.DefaultClaims' and bool if validateToken(token, secret) { r.Header.Set("CustomHeader", "Allowed Request") next.ServeHTTP(w, r) } else { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusForbidden) json.NewEncoder(w).Encode(map[string]string{ "message": "Forbidden", }) } }) } func validateToken(token, secret string) bool { // check if is a valid the [user|service] token using secret return true }