diff options
author | Michael Hunteman <huntemanmt@gmail.com> | 2025-02-02 18:57:30 -0600 |
---|---|---|
committer | Michael Hunteman <huntemanmt@gmail.com> | 2025-02-02 19:04:24 -0600 |
commit | 5fffbba3b851f6cebfd0e616bef2ff6f0c520c3d (patch) | |
tree | fe8b7a5ba77f83f7b82753d5cc58cba51596da2b /server/admin/handler.go | |
parent | 23bcef02052c45089358d22d0645ceac858de3bb (diff) |
Diffstat (limited to 'server/admin/handler.go')
-rw-r--r-- | server/admin/handler.go | 41 |
1 files changed, 11 insertions, 30 deletions
diff --git a/server/admin/handler.go b/server/admin/handler.go index b8f1d7f..2ae0b0d 100644 --- a/server/admin/handler.go +++ b/server/admin/handler.go @@ -12,15 +12,15 @@ import ( ) type AdminHandler struct { - adminStore adminStore + adminStore AdminStore guestStore guest.GuestStore } -type adminStore interface { - Find(admin Admin) (Admin, error) +type AdminStore interface { + Find(Admin) (Admin, error) } -func NewAdminHandler(a adminStore, g guest.GuestStore) *AdminHandler { +func NewAdminHandler(a AdminStore, g guest.GuestStore) *AdminHandler { return &AdminHandler{a, g} } @@ -45,21 +45,19 @@ func (a *AdminHandler) handleLogIn(w http.ResponseWriter, r *http.Request) { } func (a *AdminHandler) logIn(r *http.Request) ([]byte, *errors.AppError) { - requestAdmin, err := a.decodeCredentials(r) + admin, err := a.decodeCredentials(r) if err != nil { return nil, errors.NewAppError(http.StatusBadRequest, err.Error()) } - _, err = a.adminStore.Find(requestAdmin) + _, err = a.adminStore.Find(admin) if err != nil { return nil, errors.NewAppError(http.StatusUnauthorized, err.Error()) } - expirationTime := a.setExpirationTime() - claims := a.createClaims(requestAdmin, expirationTime) key, err := a.readKey() if err != nil { return nil, errors.NewAppError(http.StatusInternalServerError, err.Error()) } - token, err := a.createToken(claims, key) + token, err := a.newToken(NewClaims(admin, a.setExpirationTime()), key) if err != nil { return nil, errors.NewAppError(http.StatusInternalServerError, err.Error()) } @@ -85,31 +83,14 @@ func (a *AdminHandler) setExpirationTime() time.Time { return time.Now().Add(15 * time.Minute) } -func (a *AdminHandler) createClaims(admin Admin, expirationTime time.Time) *Claims { - return &Claims{ - admin, - jwt.RegisteredClaims{ - ExpiresAt: jwt.NewNumericDate(expirationTime), - }, - } -} - func (a *AdminHandler) readKey() ([]byte, error) { return os.ReadFile(os.Getenv("ADMIN_KEY")) } -func (a *AdminHandler) createToken(claims *Claims, key []byte) (string, error) { - token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) - return token.SignedString(key) -} - -func (a *AdminHandler) marshalResponse(guests []guest.Guest, - token string) ([]byte, error) { - loginResponse := a.createLoginResponse(guests, token) - return json.Marshal(loginResponse) +func (a *AdminHandler) newToken(claims *Claims, key []byte) (string, error) { + return jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString(key) } -func (a *AdminHandler) createLoginResponse(guests []guest.Guest, - token string) *Login { - return &Login{guests, token} +func (a *AdminHandler) marshalResponse(guests []guest.Guest, token string) ([]byte, error) { + return json.Marshal(NewLogin(guests, token)) } |