2023-10-01 21:44:49 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-11-03 07:39:58 +00:00
|
|
|
"github.com/1f349/mjwt"
|
|
|
|
"github.com/1f349/mjwt/auth"
|
2023-11-15 09:21:09 +00:00
|
|
|
"github.com/1f349/violet/utils"
|
2023-10-01 21:44:49 +01:00
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (h *HttpServer) verifyHandler(rw http.ResponseWriter, req *http.Request, _ httprouter.Params) {
|
|
|
|
// find bearer token
|
|
|
|
bearer := utils.GetBearer(req)
|
|
|
|
if bearer == "" {
|
|
|
|
http.Error(rw, "Missing bearer", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// after this mjwt is considered valid
|
|
|
|
_, b, err := mjwt.ExtractClaims[auth.AccessTokenClaims](h.signer, bearer)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, "Invalid token", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check issuer against config
|
2023-11-15 09:21:09 +00:00
|
|
|
if b.Issuer != h.conf.Load().Issuer {
|
2023-10-01 21:44:49 +01:00
|
|
|
http.Error(rw, "Invalid issuer", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}
|