Add an id validation

master
Araozu 2024-05-08 13:50:03 -05:00
parent af51a0acd5
commit 6b1a5c948f
1 changed files with 19 additions and 0 deletions

19
main.go
View File

@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"strings"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/nrednav/cuid2" "github.com/nrednav/cuid2"
@ -28,6 +29,7 @@ func main() {
users = make(map[string]string) users = make(map[string]string)
router.HandleFunc("/register", Register) router.HandleFunc("/register", Register)
router.HandleFunc("/validate", ValidateId)
port, ok := os.LookupEnv("PORT") port, ok := os.LookupEnv("PORT")
if !ok { if !ok {
@ -92,5 +94,22 @@ func Register(writer http.ResponseWriter, request *http.Request) {
} }
func ValidateId(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)
} }