Add an id validation
This commit is contained in:
parent
af51a0acd5
commit
6b1a5c948f
19
main.go
19
main.go
@ -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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user