2024-08-18 11:20:06 +02:00
|
|
|
package calls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
b "streifling.com/jason/cpolis/cmd/backend"
|
|
|
|
)
|
|
|
|
|
|
|
|
// tokenIsVerified verifies that a request is authorized. It returns a bool.
|
2024-08-23 21:45:10 +02:00
|
|
|
func tokenIsVerified(w http.ResponseWriter, r *http.Request, c *b.Config) bool {
|
2024-08-18 11:20:06 +02:00
|
|
|
idToken := r.Header.Get("Authorization")
|
|
|
|
if idToken == "" {
|
|
|
|
log.Println("Authorization header missing")
|
|
|
|
http.Error(w, "Authorization header missing", http.StatusUnauthorized)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-08-23 21:45:10 +02:00
|
|
|
client, err := b.NewClient(c)
|
2024-08-18 11:20:06 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = client.Verify(idToken)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|