mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
31 lines
841 B
Go
31 lines
841 B
Go
package helpers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"github.com/muety/wakapi/config"
|
|
"github.com/muety/wakapi/models"
|
|
"net/http"
|
|
)
|
|
|
|
func ExtractCookieAuth(r *http.Request, config *config.Config) (username *string, err error) {
|
|
cookie, err := r.Cookie(models.AuthCookieKey)
|
|
if err != nil {
|
|
return nil, errors.New("missing authentication")
|
|
}
|
|
|
|
if err := config.Security.SecureCookie.Decode(models.AuthCookieKey, cookie.Value, &username); err != nil {
|
|
return nil, errors.New("cookie is invalid")
|
|
}
|
|
|
|
return username, nil
|
|
}
|
|
|
|
func RespondJSON(w http.ResponseWriter, r *http.Request, status int, object interface{}) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
if err := json.NewEncoder(w).Encode(object); err != nil {
|
|
config.Log().Request(r).Error("error while writing json response: %v", err)
|
|
}
|
|
}
|