Last active
January 15, 2021 00:57
-
-
Save brianpgerson/c9d688b512e029693c08251130a5e6ac to your computer and use it in GitHub Desktop.
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 ( | |
"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