rename auth service
This commit is contained in:
@@ -30,8 +30,5 @@ func getHome(w http.ResponseWriter, _ *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := fmt.Fprint(w, buf); err != nil {
|
||||
log.Err(err).Msg("unable to write to response")
|
||||
http.Error(w, "unexpected error occurred", http.StatusInternalServerError)
|
||||
}
|
||||
fmt.Fprint(w, buf)
|
||||
}
|
||||
|
||||
+43
-43
@@ -5,8 +5,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
@@ -14,16 +12,6 @@ import (
|
||||
"librapi/templates"
|
||||
)
|
||||
|
||||
var (
|
||||
adminUsername = sync.OnceValue[string](func() string {
|
||||
return os.Getenv("API_ADMIN_USERNAME")
|
||||
})
|
||||
|
||||
adminPassword = sync.OnceValue[string](func() string {
|
||||
return os.Getenv("API_ADMIN_PASSWORD")
|
||||
})
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidUsername = errors.New("username must not be empty")
|
||||
ErrInvalidPassword = errors.New("password must not be empty")
|
||||
@@ -63,17 +51,13 @@ func (lf *LoginForm) IsSuccess() bool {
|
||||
return lf.Method == http.MethodPost && lf.Error != nil && !lf.HasErrors()
|
||||
}
|
||||
|
||||
func (lf *LoginForm) ValidCredentials() bool {
|
||||
return lf.Username.Value == adminUsername() && lf.Password.Value == adminPassword()
|
||||
}
|
||||
|
||||
func Handler(s *services.SessionStore) func(http.ResponseWriter, *http.Request) {
|
||||
func Handler(a services.IAuthenticate) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
getLogin(w, r, s)
|
||||
getLogin(w, r, a)
|
||||
case http.MethodPost:
|
||||
postLogin(w, r, s)
|
||||
postLogin(w, r, a)
|
||||
default:
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
@@ -98,8 +82,20 @@ func extractLoginForm(r *http.Request) LoginForm {
|
||||
return lf
|
||||
}
|
||||
|
||||
func postLogin(w http.ResponseWriter, r *http.Request, s *services.SessionStore) {
|
||||
func postLogin(w http.ResponseWriter, r *http.Request, a services.IAuthenticate) {
|
||||
loginForm := templates.GetLoginForm()
|
||||
loginSuccess := templates.GetLoginSuccess()
|
||||
|
||||
if a.IsLogged(r) {
|
||||
buf := bytes.NewBufferString("")
|
||||
if err := loginSuccess.Execute(buf, nil); err != nil {
|
||||
log.Err(err).Msg("unable to generate template")
|
||||
http.Error(w, "unexpected error occurred", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, buf)
|
||||
return
|
||||
}
|
||||
|
||||
lf := extractLoginForm(r)
|
||||
if lf.HasErrors() {
|
||||
@@ -114,25 +110,25 @@ func postLogin(w http.ResponseWriter, r *http.Request, s *services.SessionStore)
|
||||
return
|
||||
}
|
||||
|
||||
if ok := lf.ValidCredentials(); !ok {
|
||||
log.Warn().Str("username", lf.Username.Value).Msg("bad credentials")
|
||||
lf.Error = ErrInvalidCredentials
|
||||
session, err := a.Authenticate(lf.Username.Value, lf.Password.Value)
|
||||
if err != nil {
|
||||
if errors.Is(err, services.ErrUnauthorized) {
|
||||
log.Warn().Str("username", lf.Username.Value).Msg("bad credentials")
|
||||
|
||||
buf := bytes.NewBufferString("")
|
||||
if err := loginForm.Execute(buf, &lf); err != nil {
|
||||
log.Err(err).Msg("unable to generate template")
|
||||
http.Error(w, "unexpected error occurred", http.StatusInternalServerError)
|
||||
lf.Error = ErrInvalidCredentials
|
||||
|
||||
buf := bytes.NewBufferString("")
|
||||
if err := loginForm.Execute(buf, &lf); err != nil {
|
||||
log.Err(err).Msg("unable to generate template")
|
||||
http.Error(w, "unexpected error occurred", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
fmt.Fprint(w, buf.String())
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
fmt.Fprint(w, buf.String())
|
||||
return
|
||||
}
|
||||
|
||||
session, err := s.NewSession()
|
||||
if err != nil {
|
||||
log.Err(err).Msg("unable to create a new session")
|
||||
http.Error(w, "unexpected error occurred", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -140,7 +136,6 @@ func postLogin(w http.ResponseWriter, r *http.Request, s *services.SessionStore)
|
||||
cookie := session.GenerateCookie()
|
||||
http.SetCookie(w, cookie)
|
||||
|
||||
loginSuccess := templates.GetLoginSuccess()
|
||||
buf := bytes.NewBufferString("")
|
||||
if err := loginSuccess.Execute(buf, nil); err != nil {
|
||||
log.Err(err).Msg("unable to generate template")
|
||||
@@ -151,12 +146,20 @@ func postLogin(w http.ResponseWriter, r *http.Request, s *services.SessionStore)
|
||||
fmt.Fprint(w, buf)
|
||||
}
|
||||
|
||||
func getLogin(w http.ResponseWriter, r *http.Request, s *services.SessionStore) {
|
||||
func getLogin(w http.ResponseWriter, r *http.Request, a services.IAuthenticate) {
|
||||
loginForm := templates.GetLoginForm()
|
||||
|
||||
if s.IsLogged(r) {
|
||||
if a.IsLogged(r) {
|
||||
loginSuccess := templates.GetLoginSuccess()
|
||||
fmt.Fprint(w, loginSuccess.Tree.Root.String())
|
||||
|
||||
buf := bytes.NewBufferString("")
|
||||
if err := loginSuccess.Execute(buf, nil); err != nil {
|
||||
log.Err(err).Msg("unable to generate template")
|
||||
http.Error(w, "unexpected error occurred", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprint(w, buf)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -167,8 +170,5 @@ func getLogin(w http.ResponseWriter, r *http.Request, s *services.SessionStore)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := fmt.Fprint(w, buf); err != nil {
|
||||
log.Err(err).Msg("unable to write to response")
|
||||
http.Error(w, "unexpected error occurred", http.StatusInternalServerError)
|
||||
}
|
||||
fmt.Fprint(w, buf)
|
||||
}
|
||||
|
||||
@@ -100,13 +100,13 @@ func (bf *BookForm) IsSuccess() bool {
|
||||
return bf.Method == http.MethodPost && bf.Error == "" && !bf.HasErrors()
|
||||
}
|
||||
|
||||
func Handler(s *services.SessionStore) func(http.ResponseWriter, *http.Request) {
|
||||
func Handler(a services.IAuthenticate) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
getUploadFile(w, r)
|
||||
case http.MethodPost:
|
||||
postUploadFile(w, r, s)
|
||||
postUploadFile(w, r, a)
|
||||
default:
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
@@ -164,10 +164,10 @@ func extractBookForm(r *http.Request) BookForm {
|
||||
return bf
|
||||
}
|
||||
|
||||
func postUploadFile(w http.ResponseWriter, r *http.Request, s *services.SessionStore) {
|
||||
func postUploadFile(w http.ResponseWriter, r *http.Request, a services.IAuthenticate) {
|
||||
uploadForm := templates.GetUploadForm()
|
||||
|
||||
if !s.IsLogged(r) {
|
||||
if !a.IsLogged(r) {
|
||||
buf := bytes.NewBufferString("")
|
||||
if err := uploadForm.Execute(buf, &BookForm{Error: services.ErrUnauthorized.Error()}); err != nil {
|
||||
log.Err(err).Msg("unable to generate template")
|
||||
@@ -179,8 +179,8 @@ func postUploadFile(w http.ResponseWriter, r *http.Request, s *services.SessionS
|
||||
return
|
||||
}
|
||||
|
||||
buf := bytes.NewBufferString("")
|
||||
bf := extractBookForm(r)
|
||||
buf := bytes.NewBufferString("")
|
||||
if err := uploadForm.Execute(buf, &bf); err != nil {
|
||||
log.Err(err).Msg("unable to generate template")
|
||||
http.Error(w, "unexpected error occurred", http.StatusInternalServerError)
|
||||
@@ -241,8 +241,5 @@ func getUploadFile(w http.ResponseWriter, _ *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := fmt.Fprint(w, buf); err != nil {
|
||||
log.Err(err).Msg("unable to write to response")
|
||||
http.Error(w, "unexpected error occurred", http.StatusInternalServerError)
|
||||
}
|
||||
fmt.Fprint(w, buf)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user