diff --git a/main.go b/main.go index f6e5ffe..41ca94d 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "net/http" "net/url" "os" + "strings" "github.com/gorilla/mux" "github.com/nrednav/cuid2" @@ -28,6 +29,7 @@ func main() { users = make(map[string]string) router.HandleFunc("/register", Register) + router.HandleFunc("/validate", ValidateId) port, ok := os.LookupEnv("PORT") if !ok { @@ -92,5 +94,22 @@ func Register(writer http.ResponseWriter, request *http.Request) { } func ValidateId(writer http.ResponseWriter, request *http.Request) { + // (try to) get the Bearer token + reqToken := request.Header.Get("Authorization") + if !strings.HasPrefix(reqToken, "Bearer ") { + // return 401 + writer.WriteHeader(http.StatusUnauthorized) + } + bearerToken := reqToken[7:] + + // Check that the token is in the global map + _, ok := users[bearerToken] + if !ok { + // Return 401 + writer.WriteHeader(http.StatusUnauthorized) + } + + // Return Ok + writer.WriteHeader(http.StatusOK) }