37 lines
650 B
Go
37 lines
650 B
Go
|
package web
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"net/http"
|
||
|
|
||
|
"code.chrissexton.org/cws/cabinet/auth"
|
||
|
"github.com/rs/zerolog/log"
|
||
|
)
|
||
|
|
||
|
func (web *Web) auth(w http.ResponseWriter, r *http.Request) {
|
||
|
name := r.Header.Get("X-user-name")
|
||
|
password := r.Header.Get("X-user-password")
|
||
|
|
||
|
user, err := auth.Get(web.db, name)
|
||
|
if err != nil {
|
||
|
w.WriteHeader(500)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if user.Validate(password) {
|
||
|
w.WriteHeader(200)
|
||
|
return
|
||
|
}
|
||
|
w.WriteHeader(401)
|
||
|
resp := struct {
|
||
|
Status bool
|
||
|
Message string
|
||
|
}{Message: "incorrect credentials"}
|
||
|
j, err := json.Marshal(resp)
|
||
|
if err != nil {
|
||
|
w.WriteHeader(500)
|
||
|
log.Error().Err(err).Msg("Error")
|
||
|
}
|
||
|
w.Write(j)
|
||
|
}
|