check content-type in login endpoint
Signed-off-by: Tobias Erbshäußer <tobias@tesoft.dev>
This commit is contained in:
+13
-1
@@ -17,8 +17,15 @@ type ApiHandler struct {
|
|||||||
|
|
||||||
const authTokenCookieName = "auth-token"
|
const authTokenCookieName = "auth-token"
|
||||||
const isAuthorizedContextKey = "is-authorized"
|
const isAuthorizedContextKey = "is-authorized"
|
||||||
|
const contentTypeHeaderKey = "Content-Type"
|
||||||
|
const JsonMimeType = "application/json"
|
||||||
|
|
||||||
func (h *ApiHandler) ServeLoginPost(writer http.ResponseWriter, request *http.Request) {
|
func (h *ApiHandler) ServeLoginPost(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
if !HasContentType(request, JsonMimeType) {
|
||||||
|
WriteError(writer, http.StatusBadRequest, "expected json body", nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
bodyReader := request.Body
|
bodyReader := request.Body
|
||||||
body, err := io.ReadAll(bodyReader)
|
body, err := io.ReadAll(bodyReader)
|
||||||
_ = bodyReader.Close()
|
_ = bodyReader.Close()
|
||||||
@@ -108,7 +115,7 @@ func IsAuthorized(request *http.Request) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func WriteResponse(writer http.ResponseWriter, code int, body any) {
|
func WriteResponse(writer http.ResponseWriter, code int, body any) {
|
||||||
writer.Header().Set("Content-Type", "application/json")
|
writer.Header().Set(contentTypeHeaderKey, "application/json")
|
||||||
writer.WriteHeader(code)
|
writer.WriteHeader(code)
|
||||||
_ = json.NewEncoder(writer).Encode(body)
|
_ = json.NewEncoder(writer).Encode(body)
|
||||||
}
|
}
|
||||||
@@ -122,3 +129,8 @@ func WriteError(writer http.ResponseWriter, code int, message string, err error)
|
|||||||
"message": message,
|
"message": message,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HasContentType(request *http.Request, mimeType string) bool {
|
||||||
|
contentType := request.Header.Get(contentTypeHeaderKey)
|
||||||
|
return contentType == mimeType
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user