Skip to content

Instantly share code, notes, and snippets.

@aalvesjr
Last active May 22, 2017 14:29
Show Gist options
  • Save aalvesjr/36de019dba1759c96129719fdb8d34a0 to your computer and use it in GitHub Desktop.
Save aalvesjr/36de019dba1759c96129719fdb8d34a0 to your computer and use it in GitHub Desktop.

Revisions

  1. aalvesjr revised this gist May 22, 2017. 1 changed file with 12 additions and 9 deletions.
    21 changes: 12 additions & 9 deletions main.go
    Original 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)

    id, ok := r.Context().Value("user_id").(int64)
    d, ok := r.Context().Value("defaultClaims").(DefaultClaims)
    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)
    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()

    ctx = context.WithValue(ctx, "user_id", int64(31))
    ctx = context.WithValue(ctx, "role_type", "SUPPORT")
    d := DefaultClaims{
    UserID: 123,
    RoleType: "SUPPORT",
    }
    ctx = context.WithValue(ctx, "defaultClaims", d)

    next.ServeHTTP(w, r.WithContext(ctx))
    } else {
  2. aalvesjr revised this gist May 22, 2017. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -10,10 +10,7 @@ import (
    "github.com/gorilla/mux"
    )

    const (
    secret = "secret"
    userID = "usera_id"
    )
    const secret = "secret"

    func main() {
    router := mux.NewRouter()
  3. aalvesjr revised this gist May 22, 2017. 1 changed file with 22 additions and 4 deletions.
    26 changes: 22 additions & 4 deletions main.go
    Original 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"
    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)
    fmt.Println("CustomHeader:", r.Header.Get("CustomHeader"))

    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) {
    r.Header.Set("CustomHeader", "Allowed Request")
    ctx := r.Context()

    ctx = context.WithValue(ctx, "user_id", int64(31))
    ctx = context.WithValue(ctx, "role_type", "SUPPORT")

    next.ServeHTTP(w, r)
    next.ServeHTTP(w, r.WithContext(ctx))
    } else {
    w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(http.StatusForbidden)
  4. aalvesjr created this gist May 22, 2017.
    55 changes: 55 additions & 0 deletions main.go
    Original 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
    }