Skip to content

Commit

Permalink
chore: add cors headers on unauthorized request
Browse files Browse the repository at this point in the history
  • Loading branch information
ijo42 committed Aug 11, 2024
1 parent 9347481 commit 7a7d58f
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,16 @@ func (plugin *ICalMiddleware) validate(request *http.Request) (int, error) {
return http.StatusOK, nil
}

func (a *ICalMiddleware) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
_, err := a.validate(req)
func (plugin *ICalMiddleware) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
_, err := plugin.validate(req)
if err != nil {
http.Error(rw, "Unauthorized. Attach valid ICal ETIS token in "+a.headerName+" header", http.StatusUnauthorized)
rw.Header().Add("Cache-Control", "no-cache")
rw.Header().Add("Access-Control-Allow-Origin", "*")
rw.Header().Add("Access-Control-Allow-Headers", "*")
rw.Header().Add("Access-Control-Allow-Headers", "*")
rw.Header().Add("Access-Control-Max-Age", "0")
http.Error(rw, "Unauthorized. Attach valid ICal ETIS token in "+plugin.headerName+" header", http.StatusUnauthorized)
return
}
a.next.ServeHTTP(rw, req)
plugin.next.ServeHTTP(rw, req)
}

0 comments on commit 7a7d58f

Please sign in to comment.