2017-05-23 17:43:05 +01:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2017-07-17 17:20:57 +01:00
|
|
|
"net/http"
|
|
|
|
|
2017-05-23 17:43:05 +01:00
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth"
|
|
|
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
|
|
|
"github.com/matrix-org/util"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MakeAuthAPI turns a util.JSONRequestHandler function into an http.Handler which checks the access token in the request.
|
2017-07-17 17:20:57 +01:00
|
|
|
func MakeAuthAPI(metricsName string, deviceDB auth.DeviceDatabase, f func(*http.Request, *authtypes.Device) util.JSONResponse) http.Handler {
|
2017-05-23 17:43:05 +01:00
|
|
|
h := util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse {
|
|
|
|
device, resErr := auth.VerifyAccessToken(req, deviceDB)
|
|
|
|
if resErr != nil {
|
|
|
|
return *resErr
|
|
|
|
}
|
|
|
|
return f(req, device)
|
|
|
|
})
|
|
|
|
return prometheus.InstrumentHandler(metricsName, util.MakeJSONAPI(h))
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeAPI turns a util.JSONRequestHandler function into an http.Handler.
|
|
|
|
func MakeAPI(metricsName string, f func(*http.Request) util.JSONResponse) http.Handler {
|
|
|
|
h := util.NewJSONRequestHandler(f)
|
|
|
|
return prometheus.InstrumentHandler(metricsName, util.MakeJSONAPI(h))
|
|
|
|
}
|