Skip to content

Instantly share code, notes, and snippets.

@brianpgerson
Last active January 15, 2021 00:57
Show Gist options
  • Save brianpgerson/c9d688b512e029693c08251130a5e6ac to your computer and use it in GitHub Desktop.
Save brianpgerson/c9d688b512e029693c08251130a5e6ac to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
type errResponse struct {
Type *string `json:"type,omitempty"`
Message *string `json:"message,omitempty"`
}
const requiredPermission = "permission6"
func handler(w http.ResponseWriter, req *http.Request) {
for name, headers := range req.Header {
for _, headerValues := range headers {
if strings.ToLower(name) == "my-credential-header" {
perms := strings.Split(headerValues, ",")
for _, strings.ToLower(perm) := range perms {
if perm == requiredPermission {
w.WriteHeader(http.StatusOK)
return
}
}
}
}
}
errType := "FORBIDDEN"
msg := "you are forbidden :("
err := &errResponse{
Type: &errType,
Message: &msg,
}
responseBytes, _ := json.MarshalIndent(err, "", " ")
w.WriteHeader(http.StatusForbidden)
w.Write([]byte(responseBytes))
}
func main() {
fmt.Println("LISTENING on 10003...")
http.HandleFunc("/", handler)
http.ListenAndServe(":10003", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment