autobrr/internal/http/auth.go
Ludvig Lundgren 4d40d41628
Refactor irc client (#19)
* refactor: update http handlers

* feat: add trace log level

* refactir: irc handler

* refactor(definitions): add irc settings and invite cmd:

* feat: add dft values to inputs

* refactor: indexer irc forms

* refactor(definitions): fix nickserv.password var:

* feat: pre fill indexer name field

* refactor: handle stopping and updates
2021-08-29 23:23:02 +02:00

96 lines
2.1 KiB
Go

package http
import (
"encoding/json"
"net/http"
"github.com/go-chi/chi"
"github.com/gorilla/sessions"
"github.com/autobrr/autobrr/internal/config"
"github.com/autobrr/autobrr/internal/domain"
)
type authService interface {
Login(username, password string) (*domain.User, error)
}
type authHandler struct {
encoder encoder
service authService
}
func newAuthHandler(encoder encoder, service authService) *authHandler {
return &authHandler{
encoder: encoder,
service: service,
}
}
var (
// key will only be valid as long as it's running.
key = []byte(config.Config.SessionSecret)
store = sessions.NewCookieStore(key)
)
func (h authHandler) Routes(r chi.Router) {
r.Post("/login", h.login)
r.Post("/logout", h.logout)
r.Get("/test", h.test)
}
func (h authHandler) login(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
data domain.User
)
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
// encode error
h.encoder.StatusResponse(ctx, w, nil, http.StatusBadRequest)
return
}
store.Options.Secure = true
store.Options.HttpOnly = true
store.Options.SameSite = http.SameSiteStrictMode
session, _ := store.Get(r, "user_session")
_, err := h.service.Login(data.Username, data.Password)
if err != nil {
h.encoder.StatusResponse(ctx, w, nil, http.StatusUnauthorized)
return
}
// Set user as authenticated
session.Values["authenticated"] = true
session.Save(r, w)
h.encoder.StatusResponse(ctx, w, nil, http.StatusNoContent)
}
func (h authHandler) logout(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
session, _ := store.Get(r, "user_session")
// Revoke users authentication
session.Values["authenticated"] = false
session.Save(r, w)
h.encoder.StatusResponse(ctx, w, nil, http.StatusNoContent)
}
func (h authHandler) test(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
session, _ := store.Get(r, "user_session")
// Check if user is authenticated
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
// send empty response as ok
h.encoder.StatusResponse(ctx, w, nil, http.StatusNoContent)
}