fix: remove redundante names
This commit is contained in:
@@ -5,9 +5,9 @@ issue-845-fix: True
|
|||||||
packages:
|
packages:
|
||||||
me-fit/service:
|
me-fit/service:
|
||||||
interfaces:
|
interfaces:
|
||||||
RandomService:
|
Random:
|
||||||
ClockService:
|
Clock:
|
||||||
MailService:
|
Mail:
|
||||||
me-fit/db:
|
me-fit/db:
|
||||||
interfaces:
|
interfaces:
|
||||||
AuthDb:
|
Auth:
|
||||||
|
|||||||
34
db/auth.go
34
db/auth.go
@@ -78,7 +78,7 @@ func NewToken(userId uuid.UUID, token string, tokenType string, createdAt time.T
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthDb interface {
|
type Auth interface {
|
||||||
InsertUser(user *User) error
|
InsertUser(user *User) error
|
||||||
UpdateUser(user *User) error
|
UpdateUser(user *User) error
|
||||||
GetUserByEmail(email string) (*User, error)
|
GetUserByEmail(email string) (*User, error)
|
||||||
@@ -96,15 +96,15 @@ type AuthDb interface {
|
|||||||
DeleteOldSessions(userId uuid.UUID) error
|
DeleteOldSessions(userId uuid.UUID) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthDbSqlite struct {
|
type AuthSqlite struct {
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAuthDbSqlite(db *sql.DB) *AuthDbSqlite {
|
func NewAuthSqlite(db *sql.DB) *AuthSqlite {
|
||||||
return &AuthDbSqlite{db: db}
|
return &AuthSqlite{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db AuthDbSqlite) InsertUser(user *User) error {
|
func (db AuthSqlite) InsertUser(user *User) error {
|
||||||
_, err := db.db.Exec(`
|
_, err := db.db.Exec(`
|
||||||
INSERT INTO user (user_uuid, email, email_verified, email_verified_at, is_admin, password, salt, created_at)
|
INSERT INTO user (user_uuid, email, email_verified, email_verified_at, is_admin, password, salt, created_at)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||||
@@ -122,7 +122,7 @@ func (db AuthDbSqlite) InsertUser(user *User) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db AuthDbSqlite) UpdateUser(user *User) error {
|
func (db AuthSqlite) UpdateUser(user *User) error {
|
||||||
_, err := db.db.Exec(`
|
_, err := db.db.Exec(`
|
||||||
UPDATE user
|
UPDATE user
|
||||||
SET email_verified = ?, email_verified_at = ?, password = ?
|
SET email_verified = ?, email_verified_at = ?, password = ?
|
||||||
@@ -137,7 +137,7 @@ func (db AuthDbSqlite) UpdateUser(user *User) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db AuthDbSqlite) GetUserByEmail(email string) (*User, error) {
|
func (db AuthSqlite) GetUserByEmail(email string) (*User, error) {
|
||||||
var (
|
var (
|
||||||
userId uuid.UUID
|
userId uuid.UUID
|
||||||
emailVerified bool
|
emailVerified bool
|
||||||
@@ -164,7 +164,7 @@ func (db AuthDbSqlite) GetUserByEmail(email string) (*User, error) {
|
|||||||
return NewUser(userId, email, emailVerified, emailVerifiedAt, isAdmin, password, salt, createdAt), nil
|
return NewUser(userId, email, emailVerified, emailVerifiedAt, isAdmin, password, salt, createdAt), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db AuthDbSqlite) GetUser(userId uuid.UUID) (*User, error) {
|
func (db AuthSqlite) GetUser(userId uuid.UUID) (*User, error) {
|
||||||
var (
|
var (
|
||||||
email string
|
email string
|
||||||
emailVerified bool
|
emailVerified bool
|
||||||
@@ -191,7 +191,7 @@ func (db AuthDbSqlite) GetUser(userId uuid.UUID) (*User, error) {
|
|||||||
return NewUser(userId, email, emailVerified, emailVerifiedAt, isAdmin, password, salt, createdAt), nil
|
return NewUser(userId, email, emailVerified, emailVerifiedAt, isAdmin, password, salt, createdAt), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db AuthDbSqlite) DeleteUser(userId uuid.UUID) error {
|
func (db AuthSqlite) DeleteUser(userId uuid.UUID) error {
|
||||||
|
|
||||||
tx, err := db.db.Begin()
|
tx, err := db.db.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -236,7 +236,7 @@ func (db AuthDbSqlite) DeleteUser(userId uuid.UUID) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db AuthDbSqlite) InsertToken(token *Token) error {
|
func (db AuthSqlite) InsertToken(token *Token) error {
|
||||||
_, err := db.db.Exec(`
|
_, err := db.db.Exec(`
|
||||||
INSERT INTO user_token (user_uuid, type, token, created_at, expires_at)
|
INSERT INTO user_token (user_uuid, type, token, created_at, expires_at)
|
||||||
VALUES (?, ?, ?, ?, ?)`, token.UserId, token.Type, token.Token, token.CreatedAt, token.ExpiresAt)
|
VALUES (?, ?, ?, ?, ?)`, token.UserId, token.Type, token.Token, token.CreatedAt, token.ExpiresAt)
|
||||||
@@ -249,7 +249,7 @@ func (db AuthDbSqlite) InsertToken(token *Token) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db AuthDbSqlite) GetToken(token string) (*Token, error) {
|
func (db AuthSqlite) GetToken(token string) (*Token, error) {
|
||||||
var (
|
var (
|
||||||
userId uuid.UUID
|
userId uuid.UUID
|
||||||
tokenType string
|
tokenType string
|
||||||
@@ -290,7 +290,7 @@ func (db AuthDbSqlite) GetToken(token string) (*Token, error) {
|
|||||||
return NewToken(userId, token, tokenType, createdAt, expiresAt), nil
|
return NewToken(userId, token, tokenType, createdAt, expiresAt), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db AuthDbSqlite) GetTokensByUserIdAndType(userId uuid.UUID, tokenType string) ([]*Token, error) {
|
func (db AuthSqlite) GetTokensByUserIdAndType(userId uuid.UUID, tokenType string) ([]*Token, error) {
|
||||||
|
|
||||||
query, err := db.db.Query(`
|
query, err := db.db.Query(`
|
||||||
SELECT token, created_at, expires_at
|
SELECT token, created_at, expires_at
|
||||||
@@ -338,7 +338,7 @@ func (db AuthDbSqlite) GetTokensByUserIdAndType(userId uuid.UUID, tokenType stri
|
|||||||
return tokens, nil
|
return tokens, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db AuthDbSqlite) DeleteToken(token string) error {
|
func (db AuthSqlite) DeleteToken(token string) error {
|
||||||
_, err := db.db.Exec("DELETE FROM user_token WHERE token = ?", token)
|
_, err := db.db.Exec("DELETE FROM user_token WHERE token = ?", token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Could not delete token: %v", err)
|
log.Error("Could not delete token: %v", err)
|
||||||
@@ -347,7 +347,7 @@ func (db AuthDbSqlite) DeleteToken(token string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db AuthDbSqlite) InsertSession(session *Session) error {
|
func (db AuthSqlite) InsertSession(session *Session) error {
|
||||||
|
|
||||||
_, err := db.db.Exec(`
|
_, err := db.db.Exec(`
|
||||||
INSERT INTO session (session_id, user_uuid, created_at)
|
INSERT INTO session (session_id, user_uuid, created_at)
|
||||||
@@ -361,7 +361,7 @@ func (db AuthDbSqlite) InsertSession(session *Session) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db AuthDbSqlite) GetSession(sessionId string) (*Session, error) {
|
func (db AuthSqlite) GetSession(sessionId string) (*Session, error) {
|
||||||
|
|
||||||
var (
|
var (
|
||||||
userId uuid.UUID
|
userId uuid.UUID
|
||||||
@@ -381,7 +381,7 @@ func (db AuthDbSqlite) GetSession(sessionId string) (*Session, error) {
|
|||||||
return NewSession(sessionId, userId, sessionCreatedAt), nil
|
return NewSession(sessionId, userId, sessionCreatedAt), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db AuthDbSqlite) DeleteOldSessions(userId uuid.UUID) error {
|
func (db AuthSqlite) DeleteOldSessions(userId uuid.UUID) error {
|
||||||
// Delete old inactive sessions
|
// Delete old inactive sessions
|
||||||
_, err := db.db.Exec("DELETE FROM session WHERE created_at < datetime('now','-8 hours') AND user_uuid = ?", userId)
|
_, err := db.db.Exec("DELETE FROM session WHERE created_at < datetime('now','-8 hours') AND user_uuid = ?", userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -391,7 +391,7 @@ func (db AuthDbSqlite) DeleteOldSessions(userId uuid.UUID) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db AuthDbSqlite) DeleteSession(sessionId string) error {
|
func (db AuthSqlite) DeleteSession(sessionId string) error {
|
||||||
if sessionId != "" {
|
if sessionId != "" {
|
||||||
|
|
||||||
_, err := db.db.Exec("DELETE FROM session WHERE session_id = ?", sessionId)
|
_, err := db.db.Exec("DELETE FROM session WHERE session_id = ?", sessionId)
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ func TestUser(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
db := setupDb(t)
|
db := setupDb(t)
|
||||||
|
|
||||||
underTest := AuthDbSqlite{db: db}
|
underTest := AuthSqlite{db: db}
|
||||||
|
|
||||||
_, err := underTest.GetUserByEmail("someNonExistentEmail")
|
_, err := underTest.GetUserByEmail("someNonExistentEmail")
|
||||||
assert.Equal(t, ErrNotFound, err)
|
assert.Equal(t, ErrNotFound, err)
|
||||||
@@ -43,7 +43,7 @@ func TestUser(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
db := setupDb(t)
|
db := setupDb(t)
|
||||||
|
|
||||||
underTest := AuthDbSqlite{db: db}
|
underTest := AuthSqlite{db: db}
|
||||||
|
|
||||||
verifiedAt := time.Date(2020, 1, 5, 13, 0, 0, 0, time.UTC)
|
verifiedAt := time.Date(2020, 1, 5, 13, 0, 0, 0, time.UTC)
|
||||||
createAt := time.Date(2020, 1, 5, 12, 0, 0, 0, time.UTC)
|
createAt := time.Date(2020, 1, 5, 12, 0, 0, 0, time.UTC)
|
||||||
@@ -62,7 +62,7 @@ func TestUser(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
db := setupDb(t)
|
db := setupDb(t)
|
||||||
|
|
||||||
underTest := AuthDbSqlite{db: db}
|
underTest := AuthSqlite{db: db}
|
||||||
|
|
||||||
verifiedAt := time.Date(2020, 1, 5, 13, 0, 0, 0, time.UTC)
|
verifiedAt := time.Date(2020, 1, 5, 13, 0, 0, 0, time.UTC)
|
||||||
createAt := time.Date(2020, 1, 5, 12, 0, 0, 0, time.UTC)
|
createAt := time.Date(2020, 1, 5, 12, 0, 0, 0, time.UTC)
|
||||||
@@ -83,7 +83,7 @@ func TestEmailVerification(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
db := setupDb(t)
|
db := setupDb(t)
|
||||||
|
|
||||||
underTest := AuthDbSqlite{db: db}
|
underTest := AuthSqlite{db: db}
|
||||||
|
|
||||||
token, err := underTest.GetToken("someNonExistentToken")
|
token, err := underTest.GetToken("someNonExistentToken")
|
||||||
|
|
||||||
@@ -94,7 +94,7 @@ func TestEmailVerification(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
db := setupDb(t)
|
db := setupDb(t)
|
||||||
|
|
||||||
underTest := AuthDbSqlite{db: db}
|
underTest := AuthSqlite{db: db}
|
||||||
tokenStr := "some secure token"
|
tokenStr := "some secure token"
|
||||||
createdAt := time.Date(2020, 1, 5, 13, 0, 0, 0, time.UTC)
|
createdAt := time.Date(2020, 1, 5, 13, 0, 0, 0, time.UTC)
|
||||||
|
|
||||||
|
|||||||
@@ -13,23 +13,23 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HandlerAuth interface {
|
type Auth interface {
|
||||||
Handle(router *http.ServeMux)
|
Handle(router *http.ServeMux)
|
||||||
}
|
}
|
||||||
|
|
||||||
type HandlerAuthImpl struct {
|
type AuthImpl struct {
|
||||||
service service.AuthService
|
service service.Auth
|
||||||
render *Render
|
render *Render
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHandlerAuth(service service.AuthService, render *Render) HandlerAuth {
|
func NewAuth(service service.Auth, render *Render) Auth {
|
||||||
return HandlerAuthImpl{
|
return AuthImpl{
|
||||||
service: service,
|
service: service,
|
||||||
render: render,
|
render: render,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) Handle(router *http.ServeMux) {
|
func (handler AuthImpl) Handle(router *http.ServeMux) {
|
||||||
router.Handle("/auth/signin", handler.handleSignInPage())
|
router.Handle("/auth/signin", handler.handleSignInPage())
|
||||||
router.Handle("/api/auth/signin", handler.handleSignIn())
|
router.Handle("/api/auth/signin", handler.handleSignIn())
|
||||||
|
|
||||||
@@ -56,7 +56,7 @@ var (
|
|||||||
securityWaitDuration = 250 * time.Millisecond
|
securityWaitDuration = 250 * time.Millisecond
|
||||||
)
|
)
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleSignInPage() http.HandlerFunc {
|
func (handler AuthImpl) handleSignInPage() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
user, _ := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
user, _ := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
if user != nil {
|
if user != nil {
|
||||||
@@ -74,7 +74,7 @@ func (handler HandlerAuthImpl) handleSignInPage() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleSignIn() http.HandlerFunc {
|
func (handler AuthImpl) handleSignIn() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
user, err := utils.WaitMinimumTime(securityWaitDuration, func() (*service.User, error) {
|
user, err := utils.WaitMinimumTime(securityWaitDuration, func() (*service.User, error) {
|
||||||
var email = r.FormValue("email")
|
var email = r.FormValue("email")
|
||||||
@@ -119,7 +119,7 @@ func (handler HandlerAuthImpl) handleSignIn() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleSignUpPage() http.HandlerFunc {
|
func (handler AuthImpl) handleSignUpPage() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
user, _ := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
user, _ := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
|
|
||||||
@@ -137,7 +137,7 @@ func (handler HandlerAuthImpl) handleSignUpPage() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleSignUpVerifyPage() http.HandlerFunc {
|
func (handler AuthImpl) handleSignUpVerifyPage() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
user, _ := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
user, _ := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
if user == nil {
|
if user == nil {
|
||||||
@@ -155,7 +155,7 @@ func (handler HandlerAuthImpl) handleSignUpVerifyPage() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleVerifyResendComp() http.HandlerFunc {
|
func (handler AuthImpl) handleVerifyResendComp() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -172,7 +172,7 @@ func (handler HandlerAuthImpl) handleVerifyResendComp() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleSignUpVerifyResponsePage() http.HandlerFunc {
|
func (handler AuthImpl) handleSignUpVerifyResponsePage() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
token := r.URL.Query().Get("token")
|
token := r.URL.Query().Get("token")
|
||||||
@@ -187,7 +187,7 @@ func (handler HandlerAuthImpl) handleSignUpVerifyResponsePage() http.HandlerFunc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleSignUp() http.HandlerFunc {
|
func (handler AuthImpl) handleSignUp() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var email = r.FormValue("email")
|
var email = r.FormValue("email")
|
||||||
var password = r.FormValue("password")
|
var password = r.FormValue("password")
|
||||||
@@ -217,7 +217,7 @@ func (handler HandlerAuthImpl) handleSignUp() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleSignOut() http.HandlerFunc {
|
func (handler AuthImpl) handleSignOut() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
err := handler.service.SignOut(utils.GetSessionID(r))
|
err := handler.service.SignOut(utils.GetSessionID(r))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -241,7 +241,7 @@ func (handler HandlerAuthImpl) handleSignOut() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleDeleteAccountPage() http.HandlerFunc {
|
func (handler AuthImpl) handleDeleteAccountPage() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
// An unverified email should be able to delete their account
|
// An unverified email should be able to delete their account
|
||||||
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
@@ -254,7 +254,7 @@ func (handler HandlerAuthImpl) handleDeleteAccountPage() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleDeleteAccountComp() http.HandlerFunc {
|
func (handler AuthImpl) handleDeleteAccountComp() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -280,7 +280,7 @@ func (handler HandlerAuthImpl) handleDeleteAccountComp() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleChangePasswordPage() http.HandlerFunc {
|
func (handler AuthImpl) handleChangePasswordPage() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
isPasswordReset := r.URL.Query().Has("token")
|
isPasswordReset := r.URL.Query().Has("token")
|
||||||
@@ -297,7 +297,7 @@ func (handler HandlerAuthImpl) handleChangePasswordPage() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleChangePasswordComp() http.HandlerFunc {
|
func (handler AuthImpl) handleChangePasswordComp() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
@@ -319,7 +319,7 @@ func (handler HandlerAuthImpl) handleChangePasswordComp() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleResetPasswordPage() http.HandlerFunc {
|
func (handler AuthImpl) handleResetPasswordPage() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
@@ -333,7 +333,7 @@ func (handler HandlerAuthImpl) handleResetPasswordPage() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleForgotPasswordComp() http.HandlerFunc {
|
func (handler AuthImpl) handleForgotPasswordComp() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
email := r.FormValue("email")
|
email := r.FormValue("email")
|
||||||
@@ -351,7 +351,7 @@ func (handler HandlerAuthImpl) handleForgotPasswordComp() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler HandlerAuthImpl) handleForgotPasswordResponseComp() http.HandlerFunc {
|
func (handler AuthImpl) handleForgotPasswordResponseComp() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
pageUrl, err := url.Parse(r.Header.Get("HX-Current-URL"))
|
pageUrl, err := url.Parse(r.Header.Get("HX-Current-URL"))
|
||||||
|
|||||||
@@ -10,27 +10,27 @@ import (
|
|||||||
"github.com/a-h/templ"
|
"github.com/a-h/templ"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IndexHandler interface {
|
type Index interface {
|
||||||
Handle(router *http.ServeMux)
|
Handle(router *http.ServeMux)
|
||||||
}
|
}
|
||||||
|
|
||||||
type IndexHandlerImpl struct {
|
type IndexImpl struct {
|
||||||
service service.AuthService
|
service service.Auth
|
||||||
render *Render
|
render *Render
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIndexHandler(service service.AuthService, render *Render) IndexHandler {
|
func NewIndex(service service.Auth, render *Render) Index {
|
||||||
return IndexHandlerImpl{
|
return IndexImpl{
|
||||||
service: service,
|
service: service,
|
||||||
render: render,
|
render: render,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler IndexHandlerImpl) Handle(router *http.ServeMux) {
|
func (handler IndexImpl) Handle(router *http.ServeMux) {
|
||||||
router.Handle("/", handler.handleIndexAnd404())
|
router.Handle("/", handler.handleIndexAnd404())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler IndexHandlerImpl) handleIndexAnd404() http.HandlerFunc {
|
func (handler IndexImpl) handleIndexAnd404() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
user, _ := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
user, _ := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Cors(serverSettings *types.ServerSettings) func(http.Handler) http.Handler {
|
func Cors(serverSettings *types.Settings) func(http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Access-Control-Allow-Origin", serverSettings.BaseUrl)
|
w.Header().Set("Access-Control-Allow-Origin", serverSettings.BaseUrl)
|
||||||
|
|||||||
@@ -13,12 +13,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Render struct {
|
type Render struct {
|
||||||
serverSettings *types.ServerSettings
|
settings *types.Settings
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRender(serverSettings *types.ServerSettings) *Render {
|
func NewRender(settings *types.Settings) *Render {
|
||||||
return &Render{
|
return &Render{
|
||||||
serverSettings: serverSettings,
|
settings: settings,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ func (render *Render) Render(r *http.Request, w http.ResponseWriter, comp templ.
|
|||||||
|
|
||||||
func (render *Render) RenderLayout(r *http.Request, w http.ResponseWriter, slot templ.Component, user *service.User) {
|
func (render *Render) RenderLayout(r *http.Request, w http.ResponseWriter, slot templ.Component, user *service.User) {
|
||||||
userComp := render.getUserComp(user)
|
userComp := render.getUserComp(user)
|
||||||
layout := template.Layout(slot, userComp, render.serverSettings.Environment)
|
layout := template.Layout(slot, userComp, render.settings.Environment)
|
||||||
|
|
||||||
render.Render(r, w, layout)
|
render.Render(r, w, layout)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,32 +11,32 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WorkoutHandler interface {
|
type Workout interface {
|
||||||
Handle(router *http.ServeMux)
|
Handle(router *http.ServeMux)
|
||||||
}
|
}
|
||||||
|
|
||||||
type WorkoutHandlerImpl struct {
|
type WorkoutImpl struct {
|
||||||
service service.WorkoutService
|
service service.Workout
|
||||||
auth service.AuthService
|
auth service.Auth
|
||||||
render *Render
|
render *Render
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWorkoutHandler(service service.WorkoutService, auth service.AuthService, render *Render) WorkoutHandler {
|
func NewWorkout(service service.Workout, auth service.Auth, render *Render) Workout {
|
||||||
return WorkoutHandlerImpl{
|
return WorkoutImpl{
|
||||||
service: service,
|
service: service,
|
||||||
auth: auth,
|
auth: auth,
|
||||||
render: render,
|
render: render,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler WorkoutHandlerImpl) Handle(router *http.ServeMux) {
|
func (handler WorkoutImpl) Handle(router *http.ServeMux) {
|
||||||
router.Handle("/workout", handler.handleWorkoutPage())
|
router.Handle("/workout", handler.handleWorkoutPage())
|
||||||
router.Handle("POST /api/workout", handler.handleAddWorkout())
|
router.Handle("POST /api/workout", handler.handleAddWorkout())
|
||||||
router.Handle("GET /api/workout", handler.handleGetWorkout())
|
router.Handle("GET /api/workout", handler.handleGetWorkout())
|
||||||
router.Handle("DELETE /api/workout/{id}", handler.handleDeleteWorkout())
|
router.Handle("DELETE /api/workout/{id}", handler.handleDeleteWorkout())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler WorkoutHandlerImpl) handleWorkoutPage() http.HandlerFunc {
|
func (handler WorkoutImpl) handleWorkoutPage() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
user, err := handler.auth.GetUserFromSessionId(utils.GetSessionID(r))
|
user, err := handler.auth.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -50,7 +50,7 @@ func (handler WorkoutHandlerImpl) handleWorkoutPage() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler WorkoutHandlerImpl) handleAddWorkout() http.HandlerFunc {
|
func (handler WorkoutImpl) handleAddWorkout() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
user, err := handler.auth.GetUserFromSessionId(utils.GetSessionID(r))
|
user, err := handler.auth.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -77,7 +77,7 @@ func (handler WorkoutHandlerImpl) handleAddWorkout() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler WorkoutHandlerImpl) handleGetWorkout() http.HandlerFunc {
|
func (handler WorkoutImpl) handleGetWorkout() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
user, err := handler.auth.GetUserFromSessionId(utils.GetSessionID(r))
|
user, err := handler.auth.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -100,7 +100,7 @@ func (handler WorkoutHandlerImpl) handleGetWorkout() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler WorkoutHandlerImpl) handleDeleteWorkout() http.HandlerFunc {
|
func (handler WorkoutImpl) handleDeleteWorkout() http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
user, err := handler.auth.GetUserFromSessionId(utils.GetSessionID(r))
|
user, err := handler.auth.GetUserFromSessionId(utils.GetSessionID(r))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
28
main.go
28
main.go
@@ -44,7 +44,7 @@ func run(ctx context.Context, database *sql.DB, env func(string) string) {
|
|||||||
log.Info("Starting server...")
|
log.Info("Starting server...")
|
||||||
|
|
||||||
// init server settings
|
// init server settings
|
||||||
serverSettings := types.NewServerSettingsFromEnv(env)
|
serverSettings := types.NewSettingsFromEnv(env)
|
||||||
|
|
||||||
// init db
|
// init db
|
||||||
err := db.RunMigrations(database, "")
|
err := db.RunMigrations(database, "")
|
||||||
@@ -101,33 +101,31 @@ func shutdownServer(s *http.Server, ctx context.Context, wg *sync.WaitGroup) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createHandler(d *sql.DB, serverSettings *types.ServerSettings) http.Handler {
|
func createHandler(d *sql.DB, serverSettings *types.Settings) http.Handler {
|
||||||
var router = http.NewServeMux()
|
var router = http.NewServeMux()
|
||||||
|
|
||||||
authDb := db.NewAuthDbSqlite(d)
|
authDb := db.NewAuthSqlite(d)
|
||||||
workoutDb := db.NewWorkoutDbSqlite(d)
|
workoutDb := db.NewWorkoutDbSqlite(d)
|
||||||
|
|
||||||
randomService := service.NewRandomServiceImpl()
|
randomService := service.NewRandomImpl()
|
||||||
clockService := service.NewClockServiceImpl()
|
clockService := service.NewClockImpl()
|
||||||
mailService := service.NewMailServiceImpl(serverSettings)
|
mailService := service.NewMailImpl(serverSettings)
|
||||||
|
|
||||||
authService := service.NewAuthServiceImpl(authDb, randomService, clockService, mailService, serverSettings)
|
authService := service.NewAuthImpl(authDb, randomService, clockService, mailService, serverSettings)
|
||||||
workoutService := service.NewWorkoutServiceImpl(workoutDb, randomService, clockService, mailService, serverSettings)
|
workoutService := service.NewWorkoutImpl(workoutDb, randomService, clockService, mailService, serverSettings)
|
||||||
|
|
||||||
render := handler.NewRender(serverSettings)
|
render := handler.NewRender(serverSettings)
|
||||||
indexHandler := handler.NewIndexHandler(authService, render)
|
indexHandler := handler.NewIndex(authService, render)
|
||||||
authHandler := handler.NewHandlerAuth(authService, render)
|
authHandler := handler.NewAuth(authService, render)
|
||||||
workoutHandler := handler.NewWorkoutHandler(workoutService, authService, render)
|
workoutHandler := handler.NewWorkout(workoutService, authService, render)
|
||||||
|
|
||||||
indexHandler.Handle(router)
|
indexHandler.Handle(router)
|
||||||
|
workoutHandler.Handle(router)
|
||||||
|
authHandler.Handle(router)
|
||||||
|
|
||||||
// Serve static files (CSS, JS and images)
|
// Serve static files (CSS, JS and images)
|
||||||
router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
|
router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
|
||||||
|
|
||||||
workoutHandler.Handle(router)
|
|
||||||
|
|
||||||
authHandler.Handle(router)
|
|
||||||
|
|
||||||
return middleware.Wrapper(
|
return middleware.Wrapper(
|
||||||
router,
|
router,
|
||||||
middleware.Log,
|
middleware.Log,
|
||||||
|
|||||||
110
service/auth.go
110
service/auth.go
@@ -53,7 +53,7 @@ func NewSession(session *db.Session, user *User) *Session {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthService interface {
|
type Auth interface {
|
||||||
SignUp(email string, password string) (*User, error)
|
SignUp(email string, password string) (*User, error)
|
||||||
SendVerificationMail(userId uuid.UUID, email string)
|
SendVerificationMail(userId uuid.UUID, email string)
|
||||||
VerifyUserEmail(token string) error
|
VerifyUserEmail(token string) error
|
||||||
@@ -71,26 +71,26 @@ type AuthService interface {
|
|||||||
GetUserFromSessionId(sessionId string) (*User, error)
|
GetUserFromSessionId(sessionId string) (*User, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthServiceImpl struct {
|
type AuthImpl struct {
|
||||||
dbAuth db.AuthDb
|
db db.Auth
|
||||||
randomGenerator RandomService
|
random Random
|
||||||
clock ClockService
|
clock Clock
|
||||||
mailService MailService
|
mail Mail
|
||||||
serverSettings *types.ServerSettings
|
serverSettings *types.Settings
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAuthServiceImpl(dbAuth db.AuthDb, randomGenerator RandomService, clock ClockService, mailService MailService, serverSettings *types.ServerSettings) *AuthServiceImpl {
|
func NewAuthImpl(db db.Auth, random Random, clock Clock, mail Mail, serverSettings *types.Settings) *AuthImpl {
|
||||||
return &AuthServiceImpl{
|
return &AuthImpl{
|
||||||
dbAuth: dbAuth,
|
db: db,
|
||||||
randomGenerator: randomGenerator,
|
random: random,
|
||||||
clock: clock,
|
clock: clock,
|
||||||
mailService: mailService,
|
mail: mail,
|
||||||
serverSettings: serverSettings,
|
serverSettings: serverSettings,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service AuthServiceImpl) SignIn(email string, password string) (*Session, error) {
|
func (service AuthImpl) SignIn(email string, password string) (*Session, error) {
|
||||||
user, err := service.dbAuth.GetUserByEmail(email)
|
user, err := service.db.GetUserByEmail(email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, db.ErrNotFound) {
|
if errors.Is(err, db.ErrNotFound) {
|
||||||
return nil, ErrInvaidCredentials
|
return nil, ErrInvaidCredentials
|
||||||
@@ -113,13 +113,13 @@ func (service AuthServiceImpl) SignIn(email string, password string) (*Session,
|
|||||||
return NewSession(session, NewUser(user)), nil
|
return NewSession(session, NewUser(user)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service AuthServiceImpl) createSession(userId uuid.UUID) (*db.Session, error) {
|
func (service AuthImpl) createSession(userId uuid.UUID) (*db.Session, error) {
|
||||||
sessionId, err := service.randomGenerator.String(32)
|
sessionId, err := service.random.String(32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, types.ErrInternal
|
return nil, types.ErrInternal
|
||||||
}
|
}
|
||||||
|
|
||||||
err = service.dbAuth.DeleteOldSessions(userId)
|
err = service.db.DeleteOldSessions(userId)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, types.ErrInternal
|
return nil, types.ErrInternal
|
||||||
@@ -127,7 +127,7 @@ func (service AuthServiceImpl) createSession(userId uuid.UUID) (*db.Session, err
|
|||||||
|
|
||||||
session := db.NewSession(sessionId, userId, service.clock.Now())
|
session := db.NewSession(sessionId, userId, service.clock.Now())
|
||||||
|
|
||||||
err = service.dbAuth.InsertSession(session)
|
err = service.db.InsertSession(session)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, types.ErrInternal
|
return nil, types.ErrInternal
|
||||||
}
|
}
|
||||||
@@ -135,7 +135,7 @@ func (service AuthServiceImpl) createSession(userId uuid.UUID) (*db.Session, err
|
|||||||
return session, nil
|
return session, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service AuthServiceImpl) SignUp(email string, password string) (*User, error) {
|
func (service AuthImpl) SignUp(email string, password string) (*User, error) {
|
||||||
_, err := mail.ParseAddress(email)
|
_, err := mail.ParseAddress(email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ErrInvalidEmail
|
return nil, ErrInvalidEmail
|
||||||
@@ -145,12 +145,12 @@ func (service AuthServiceImpl) SignUp(email string, password string) (*User, err
|
|||||||
return nil, ErrInvalidPassword
|
return nil, ErrInvalidPassword
|
||||||
}
|
}
|
||||||
|
|
||||||
userId, err := service.randomGenerator.UUID()
|
userId, err := service.random.UUID()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, types.ErrInternal
|
return nil, types.ErrInternal
|
||||||
}
|
}
|
||||||
|
|
||||||
salt, err := service.randomGenerator.Bytes(16)
|
salt, err := service.random.Bytes(16)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, types.ErrInternal
|
return nil, types.ErrInternal
|
||||||
}
|
}
|
||||||
@@ -159,7 +159,7 @@ func (service AuthServiceImpl) SignUp(email string, password string) (*User, err
|
|||||||
|
|
||||||
dbUser := db.NewUser(userId, email, false, nil, false, hash, salt, service.clock.Now())
|
dbUser := db.NewUser(userId, email, false, nil, false, hash, salt, service.clock.Now())
|
||||||
|
|
||||||
err = service.dbAuth.InsertUser(dbUser)
|
err = service.db.InsertUser(dbUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == db.ErrUserExists {
|
if err == db.ErrUserExists {
|
||||||
return nil, ErrAccountExists
|
return nil, ErrAccountExists
|
||||||
@@ -171,9 +171,9 @@ func (service AuthServiceImpl) SignUp(email string, password string) (*User, err
|
|||||||
return NewUser(dbUser), nil
|
return NewUser(dbUser), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service AuthServiceImpl) SendVerificationMail(userId uuid.UUID, email string) {
|
func (service AuthImpl) SendVerificationMail(userId uuid.UUID, email string) {
|
||||||
|
|
||||||
tokens, err := service.dbAuth.GetTokensByUserIdAndType(userId, db.TokenTypeEmailVerify)
|
tokens, err := service.db.GetTokensByUserIdAndType(userId, db.TokenTypeEmailVerify)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -185,14 +185,14 @@ func (service AuthServiceImpl) SendVerificationMail(userId uuid.UUID, email stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
if token == nil {
|
if token == nil {
|
||||||
newTokenStr, err := service.randomGenerator.String(32)
|
newTokenStr, err := service.random.String(32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
token = db.NewToken(userId, newTokenStr, db.TokenTypeEmailVerify, service.clock.Now(), service.clock.Now().Add(24*time.Hour))
|
token = db.NewToken(userId, newTokenStr, db.TokenTypeEmailVerify, service.clock.Now(), service.clock.Now().Add(24*time.Hour))
|
||||||
|
|
||||||
err = service.dbAuth.InsertToken(token)
|
err = service.db.InsertToken(token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -205,21 +205,21 @@ func (service AuthServiceImpl) SendVerificationMail(userId uuid.UUID, email stri
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
service.mailService.SendMail(email, "Welcome to ME-FIT", w.String())
|
service.mail.SendMail(email, "Welcome to ME-FIT", w.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service AuthServiceImpl) VerifyUserEmail(tokenStr string) error {
|
func (service AuthImpl) VerifyUserEmail(tokenStr string) error {
|
||||||
|
|
||||||
if tokenStr == "" {
|
if tokenStr == "" {
|
||||||
return types.ErrInternal
|
return types.ErrInternal
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := service.dbAuth.GetToken(tokenStr)
|
token, err := service.db.GetToken(tokenStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.ErrInternal
|
return types.ErrInternal
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := service.dbAuth.GetUser(token.UserId)
|
user, err := service.db.GetUser(token.UserId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.ErrInternal
|
return types.ErrInternal
|
||||||
}
|
}
|
||||||
@@ -237,31 +237,31 @@ func (service AuthServiceImpl) VerifyUserEmail(tokenStr string) error {
|
|||||||
user.EmailVerified = true
|
user.EmailVerified = true
|
||||||
user.EmailVerifiedAt = &now
|
user.EmailVerifiedAt = &now
|
||||||
|
|
||||||
err = service.dbAuth.UpdateUser(user)
|
err = service.db.UpdateUser(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.ErrInternal
|
return types.ErrInternal
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = service.dbAuth.DeleteToken(token.Token)
|
_ = service.db.DeleteToken(token.Token)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service AuthServiceImpl) SignOut(sessionId string) error {
|
func (service AuthImpl) SignOut(sessionId string) error {
|
||||||
|
|
||||||
return service.dbAuth.DeleteSession(sessionId)
|
return service.db.DeleteSession(sessionId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service AuthServiceImpl) GetUserFromSessionId(sessionId string) (*User, error) {
|
func (service AuthImpl) GetUserFromSessionId(sessionId string) (*User, error) {
|
||||||
if sessionId == "" {
|
if sessionId == "" {
|
||||||
return nil, ErrSessionIdInvalid
|
return nil, ErrSessionIdInvalid
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := service.dbAuth.GetSession(sessionId)
|
session, err := service.db.GetSession(sessionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, types.ErrInternal
|
return nil, types.ErrInternal
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := service.dbAuth.GetUser(session.UserId)
|
user, err := service.db.GetUser(session.UserId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, types.ErrInternal
|
return nil, types.ErrInternal
|
||||||
}
|
}
|
||||||
@@ -273,19 +273,19 @@ func (service AuthServiceImpl) GetUserFromSessionId(sessionId string) (*User, er
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service AuthServiceImpl) DeleteAccount(user *User) error {
|
func (service AuthImpl) DeleteAccount(user *User) error {
|
||||||
|
|
||||||
err := service.dbAuth.DeleteUser(user.Id)
|
err := service.db.DeleteUser(user.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
go service.mailService.SendMail(user.Email, "Account deleted", "Your account has been deleted")
|
go service.mail.SendMail(user.Email, "Account deleted", "Your account has been deleted")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service AuthServiceImpl) ChangePassword(user *User, currPass, newPass string) error {
|
func (service AuthImpl) ChangePassword(user *User, currPass, newPass string) error {
|
||||||
|
|
||||||
if !isPasswordValid(newPass) {
|
if !isPasswordValid(newPass) {
|
||||||
return ErrInvalidPassword
|
return ErrInvalidPassword
|
||||||
@@ -300,7 +300,7 @@ func (service AuthServiceImpl) ChangePassword(user *User, currPass, newPass stri
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
userDb, err := service.dbAuth.GetUser(user.Id)
|
userDb, err := service.db.GetUser(user.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -309,7 +309,7 @@ func (service AuthServiceImpl) ChangePassword(user *User, currPass, newPass stri
|
|||||||
|
|
||||||
userDb.Password = newHash
|
userDb.Password = newHash
|
||||||
|
|
||||||
err = service.dbAuth.UpdateUser(userDb)
|
err = service.db.UpdateUser(userDb)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -317,14 +317,14 @@ func (service AuthServiceImpl) ChangePassword(user *User, currPass, newPass stri
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service AuthServiceImpl) SendForgotPasswordMail(email string) error {
|
func (service AuthImpl) SendForgotPasswordMail(email string) error {
|
||||||
|
|
||||||
tokenStr, err := service.randomGenerator.String(32)
|
tokenStr, err := service.random.String(32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := service.dbAuth.GetUserByEmail(email)
|
user, err := service.db.GetUserByEmail(email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == db.ErrNotFound {
|
if err == db.ErrNotFound {
|
||||||
return nil
|
return nil
|
||||||
@@ -335,7 +335,7 @@ func (service AuthServiceImpl) SendForgotPasswordMail(email string) error {
|
|||||||
|
|
||||||
token := db.NewToken(user.Id, tokenStr, db.TokenTypePasswordReset, service.clock.Now(), service.clock.Now().Add(15*time.Minute))
|
token := db.NewToken(user.Id, tokenStr, db.TokenTypePasswordReset, service.clock.Now(), service.clock.Now().Add(15*time.Minute))
|
||||||
|
|
||||||
err = service.dbAuth.InsertToken(token)
|
err = service.db.InsertToken(token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.ErrInternal
|
return types.ErrInternal
|
||||||
}
|
}
|
||||||
@@ -346,28 +346,28 @@ func (service AuthServiceImpl) SendForgotPasswordMail(email string) error {
|
|||||||
log.Error("Could not render reset password email: %v", err)
|
log.Error("Could not render reset password email: %v", err)
|
||||||
return types.ErrInternal
|
return types.ErrInternal
|
||||||
}
|
}
|
||||||
go service.mailService.SendMail(email, "Reset Password", mail.String())
|
go service.mail.SendMail(email, "Reset Password", mail.String())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service AuthServiceImpl) ForgotPassword(tokenStr string, newPass string) error {
|
func (service AuthImpl) ForgotPassword(tokenStr string, newPass string) error {
|
||||||
|
|
||||||
if !isPasswordValid(newPass) {
|
if !isPasswordValid(newPass) {
|
||||||
return ErrInvalidPassword
|
return ErrInvalidPassword
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := service.dbAuth.GetToken(tokenStr)
|
token, err := service.db.GetToken(tokenStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = service.dbAuth.DeleteToken(tokenStr)
|
err = service.db.DeleteToken(tokenStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := service.dbAuth.GetUser(token.UserId)
|
user, err := service.db.GetUser(token.UserId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Could not get user from token: %v", err)
|
log.Error("Could not get user from token: %v", err)
|
||||||
return types.ErrInternal
|
return types.ErrInternal
|
||||||
@@ -376,7 +376,7 @@ func (service AuthServiceImpl) ForgotPassword(tokenStr string, newPass string) e
|
|||||||
passHash := GetHashPassword(newPass, user.Salt)
|
passHash := GetHashPassword(newPass, user.Salt)
|
||||||
|
|
||||||
user.Password = passHash
|
user.Password = passHash
|
||||||
err = service.dbAuth.UpdateUser(user)
|
err = service.db.UpdateUser(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,17 +35,17 @@ func TestSignIn(t *testing.T) {
|
|||||||
|
|
||||||
dbSession := db.NewSession("sessionId", user.Id, time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
|
dbSession := db.NewSession("sessionId", user.Id, time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
|
||||||
|
|
||||||
mockAuthDb := mocks.NewMockAuthDb(t)
|
mockAuthDb := mocks.NewMockAuth(t)
|
||||||
mockAuthDb.EXPECT().GetUserByEmail("test@test.de").Return(user, nil)
|
mockAuthDb.EXPECT().GetUserByEmail("test@test.de").Return(user, nil)
|
||||||
mockAuthDb.EXPECT().DeleteOldSessions(user.Id).Return(nil)
|
mockAuthDb.EXPECT().DeleteOldSessions(user.Id).Return(nil)
|
||||||
mockAuthDb.EXPECT().InsertSession(dbSession).Return(nil)
|
mockAuthDb.EXPECT().InsertSession(dbSession).Return(nil)
|
||||||
mockRandom := mocks.NewMockRandomService(t)
|
mockRandom := mocks.NewMockRandom(t)
|
||||||
mockRandom.EXPECT().String(32).Return("sessionId", nil)
|
mockRandom.EXPECT().String(32).Return("sessionId", nil)
|
||||||
mockClock := mocks.NewMockClockService(t)
|
mockClock := mocks.NewMockClock(t)
|
||||||
mockClock.EXPECT().Now().Return(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
|
mockClock.EXPECT().Now().Return(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
|
||||||
mockMail := mocks.NewMockMailService(t)
|
mockMail := mocks.NewMockMail(t)
|
||||||
|
|
||||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||||
|
|
||||||
actualSession, err := underTest.SignIn(user.Email, "password")
|
actualSession, err := underTest.SignIn(user.Email, "password")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
@@ -70,13 +70,13 @@ func TestSignIn(t *testing.T) {
|
|||||||
time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
|
time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||||
)
|
)
|
||||||
|
|
||||||
mockAuthDb := mocks.NewMockAuthDb(t)
|
mockAuthDb := mocks.NewMockAuth(t)
|
||||||
mockAuthDb.EXPECT().GetUserByEmail(user.Email).Return(user, nil)
|
mockAuthDb.EXPECT().GetUserByEmail(user.Email).Return(user, nil)
|
||||||
mockRandom := mocks.NewMockRandomService(t)
|
mockRandom := mocks.NewMockRandom(t)
|
||||||
mockClock := mocks.NewMockClockService(t)
|
mockClock := mocks.NewMockClock(t)
|
||||||
mockMail := mocks.NewMockMailService(t)
|
mockMail := mocks.NewMockMail(t)
|
||||||
|
|
||||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||||
|
|
||||||
_, err := underTest.SignIn("test@test.de", "wrong password")
|
_, err := underTest.SignIn("test@test.de", "wrong password")
|
||||||
|
|
||||||
@@ -85,13 +85,13 @@ func TestSignIn(t *testing.T) {
|
|||||||
t.Run("should return ErrInvalidCretentials if user has not been found", func(t *testing.T) {
|
t.Run("should return ErrInvalidCretentials if user has not been found", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
mockAuthDb := mocks.NewMockAuthDb(t)
|
mockAuthDb := mocks.NewMockAuth(t)
|
||||||
mockAuthDb.EXPECT().GetUserByEmail("test").Return(nil, db.ErrNotFound)
|
mockAuthDb.EXPECT().GetUserByEmail("test").Return(nil, db.ErrNotFound)
|
||||||
mockRandom := mocks.NewMockRandomService(t)
|
mockRandom := mocks.NewMockRandom(t)
|
||||||
mockClock := mocks.NewMockClockService(t)
|
mockClock := mocks.NewMockClock(t)
|
||||||
mockMail := mocks.NewMockMailService(t)
|
mockMail := mocks.NewMockMail(t)
|
||||||
|
|
||||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||||
|
|
||||||
_, err := underTest.SignIn("test", "test")
|
_, err := underTest.SignIn("test", "test")
|
||||||
assert.Equal(t, ErrInvaidCredentials, err)
|
assert.Equal(t, ErrInvaidCredentials, err)
|
||||||
@@ -99,13 +99,13 @@ func TestSignIn(t *testing.T) {
|
|||||||
t.Run("should forward ErrInternal on any other error", func(t *testing.T) {
|
t.Run("should forward ErrInternal on any other error", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
mockAuthDb := mocks.NewMockAuthDb(t)
|
mockAuthDb := mocks.NewMockAuth(t)
|
||||||
mockAuthDb.EXPECT().GetUserByEmail("test").Return(nil, errors.New("Some undefined error"))
|
mockAuthDb.EXPECT().GetUserByEmail("test").Return(nil, errors.New("Some undefined error"))
|
||||||
mockRandom := mocks.NewMockRandomService(t)
|
mockRandom := mocks.NewMockRandom(t)
|
||||||
mockClock := mocks.NewMockClockService(t)
|
mockClock := mocks.NewMockClock(t)
|
||||||
mockMail := mocks.NewMockMailService(t)
|
mockMail := mocks.NewMockMail(t)
|
||||||
|
|
||||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||||
|
|
||||||
_, err := underTest.SignIn("test", "test")
|
_, err := underTest.SignIn("test", "test")
|
||||||
|
|
||||||
@@ -118,12 +118,12 @@ func TestSignUp(t *testing.T) {
|
|||||||
t.Run("should check for correct email address", func(t *testing.T) {
|
t.Run("should check for correct email address", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
mockAuthDb := mocks.NewMockAuthDb(t)
|
mockAuthDb := mocks.NewMockAuth(t)
|
||||||
mockRandom := mocks.NewMockRandomService(t)
|
mockRandom := mocks.NewMockRandom(t)
|
||||||
mockClock := mocks.NewMockClockService(t)
|
mockClock := mocks.NewMockClock(t)
|
||||||
mockMail := mocks.NewMockMailService(t)
|
mockMail := mocks.NewMockMail(t)
|
||||||
|
|
||||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||||
|
|
||||||
_, err := underTest.SignUp("invalid email address", "SomeStrongPassword123!")
|
_, err := underTest.SignUp("invalid email address", "SomeStrongPassword123!")
|
||||||
|
|
||||||
@@ -132,12 +132,12 @@ func TestSignUp(t *testing.T) {
|
|||||||
t.Run("should check for password complexity", func(t *testing.T) {
|
t.Run("should check for password complexity", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
mockAuthDb := mocks.NewMockAuthDb(t)
|
mockAuthDb := mocks.NewMockAuth(t)
|
||||||
mockRandom := mocks.NewMockRandomService(t)
|
mockRandom := mocks.NewMockRandom(t)
|
||||||
mockClock := mocks.NewMockClockService(t)
|
mockClock := mocks.NewMockClock(t)
|
||||||
mockMail := mocks.NewMockMailService(t)
|
mockMail := mocks.NewMockMail(t)
|
||||||
|
|
||||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||||
|
|
||||||
weakPasswords := []string{
|
weakPasswords := []string{
|
||||||
"123!ab", // too short
|
"123!ab", // too short
|
||||||
@@ -154,10 +154,10 @@ func TestSignUp(t *testing.T) {
|
|||||||
t.Run("should signup correctly", func(t *testing.T) {
|
t.Run("should signup correctly", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
mockAuthDb := mocks.NewMockAuthDb(t)
|
mockAuthDb := mocks.NewMockAuth(t)
|
||||||
mockRandom := mocks.NewMockRandomService(t)
|
mockRandom := mocks.NewMockRandom(t)
|
||||||
mockClock := mocks.NewMockClockService(t)
|
mockClock := mocks.NewMockClock(t)
|
||||||
mockMail := mocks.NewMockMailService(t)
|
mockMail := mocks.NewMockMail(t)
|
||||||
|
|
||||||
expected := User{
|
expected := User{
|
||||||
Id: uuid.New(),
|
Id: uuid.New(),
|
||||||
@@ -165,7 +165,7 @@ func TestSignUp(t *testing.T) {
|
|||||||
EmailVerified: false,
|
EmailVerified: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
random := NewRandomServiceImpl()
|
random := NewRandomImpl()
|
||||||
salt, err := random.Bytes(16)
|
salt, err := random.Bytes(16)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
password := "SomeStrongPassword123!"
|
password := "SomeStrongPassword123!"
|
||||||
@@ -179,7 +179,7 @@ func TestSignUp(t *testing.T) {
|
|||||||
|
|
||||||
mockAuthDb.EXPECT().InsertUser(db.NewUser(expected.Id, expected.Email, false, nil, false, GetHashPassword(password, salt), salt, createTime)).Return(nil)
|
mockAuthDb.EXPECT().InsertUser(db.NewUser(expected.Id, expected.Email, false, nil, false, GetHashPassword(password, salt), salt, createTime)).Return(nil)
|
||||||
|
|
||||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||||
|
|
||||||
actual, err := underTest.SignUp(expected.Email, password)
|
actual, err := underTest.SignUp(expected.Email, password)
|
||||||
|
|
||||||
@@ -190,17 +190,17 @@ func TestSignUp(t *testing.T) {
|
|||||||
t.Run("should return ErrAccountExists", func(t *testing.T) {
|
t.Run("should return ErrAccountExists", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
mockAuthDb := mocks.NewMockAuthDb(t)
|
mockAuthDb := mocks.NewMockAuth(t)
|
||||||
mockRandom := mocks.NewMockRandomService(t)
|
mockRandom := mocks.NewMockRandom(t)
|
||||||
mockClock := mocks.NewMockClockService(t)
|
mockClock := mocks.NewMockClock(t)
|
||||||
mockMail := mocks.NewMockMailService(t)
|
mockMail := mocks.NewMockMail(t)
|
||||||
|
|
||||||
user := User{
|
user := User{
|
||||||
Id: uuid.New(),
|
Id: uuid.New(),
|
||||||
Email: "some@valid.email",
|
Email: "some@valid.email",
|
||||||
}
|
}
|
||||||
|
|
||||||
random := NewRandomServiceImpl()
|
random := NewRandomImpl()
|
||||||
salt, err := random.Bytes(16)
|
salt, err := random.Bytes(16)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
password := "SomeStrongPassword123!"
|
password := "SomeStrongPassword123!"
|
||||||
@@ -214,7 +214,7 @@ func TestSignUp(t *testing.T) {
|
|||||||
|
|
||||||
mockAuthDb.EXPECT().InsertUser(db.NewUser(user.Id, user.Email, false, nil, false, GetHashPassword(password, salt), salt, createTime)).Return(db.ErrUserExists)
|
mockAuthDb.EXPECT().InsertUser(db.NewUser(user.Id, user.Email, false, nil, false, GetHashPassword(password, salt), salt, createTime)).Return(db.ErrUserExists)
|
||||||
|
|
||||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||||
|
|
||||||
_, err = underTest.SignUp(user.Email, password)
|
_, err = underTest.SignUp(user.Email, password)
|
||||||
assert.Equal(t, ErrAccountExists, err)
|
assert.Equal(t, ErrAccountExists, err)
|
||||||
@@ -233,10 +233,10 @@ func TestSendVerificationMail(t *testing.T) {
|
|||||||
email := "some@email.de"
|
email := "some@email.de"
|
||||||
userId := uuid.New()
|
userId := uuid.New()
|
||||||
|
|
||||||
mockAuthDb := mocks.NewMockAuthDb(t)
|
mockAuthDb := mocks.NewMockAuth(t)
|
||||||
mockRandom := mocks.NewMockRandomService(t)
|
mockRandom := mocks.NewMockRandom(t)
|
||||||
mockClock := mocks.NewMockClockService(t)
|
mockClock := mocks.NewMockClock(t)
|
||||||
mockMail := mocks.NewMockMailService(t)
|
mockMail := mocks.NewMockMail(t)
|
||||||
|
|
||||||
mockAuthDb.EXPECT().GetTokensByUserIdAndType(userId, db.TokenTypeEmailVerify).Return(tokens, nil)
|
mockAuthDb.EXPECT().GetTokensByUserIdAndType(userId, db.TokenTypeEmailVerify).Return(tokens, nil)
|
||||||
|
|
||||||
@@ -244,7 +244,7 @@ func TestSendVerificationMail(t *testing.T) {
|
|||||||
return strings.Contains(message, token.Token)
|
return strings.Contains(message, token.Token)
|
||||||
})).Return()
|
})).Return()
|
||||||
|
|
||||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||||
|
|
||||||
underTest.SendVerificationMail(userId, email)
|
underTest.SendVerificationMail(userId, email)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -2,16 +2,16 @@ package service
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
type ClockService interface {
|
type Clock interface {
|
||||||
Now() time.Time
|
Now() time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClockServiceImpl struct{}
|
type ClockImpl struct{}
|
||||||
|
|
||||||
func NewClockServiceImpl() ClockService {
|
func NewClockImpl() Clock {
|
||||||
return &ClockServiceImpl{}
|
return &ClockImpl{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ClockServiceImpl) Now() time.Time {
|
func (c *ClockImpl) Now() time.Time {
|
||||||
return time.Now()
|
return time.Now()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,25 +8,25 @@ import (
|
|||||||
"net/smtp"
|
"net/smtp"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MailService interface {
|
type Mail interface {
|
||||||
// Sending an email is a fire and forget operation. Thus no error handling
|
// Sending an email is a fire and forget operation. Thus no error handling
|
||||||
SendMail(to string, subject string, message string)
|
SendMail(to string, subject string, message string)
|
||||||
}
|
}
|
||||||
|
|
||||||
type MailServiceImpl struct {
|
type MailImpl struct {
|
||||||
serverSettings *types.ServerSettings
|
server *types.Settings
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMailServiceImpl(serverSettings *types.ServerSettings) MailServiceImpl {
|
func NewMailImpl(server *types.Settings) MailImpl {
|
||||||
return MailServiceImpl{serverSettings: serverSettings}
|
return MailImpl{server: server}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m MailServiceImpl) SendMail(to string, subject string, message string) {
|
func (m MailImpl) SendMail(to string, subject string, message string) {
|
||||||
if m.serverSettings.Smtp == nil {
|
if m.server.Smtp == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s := m.serverSettings.Smtp
|
s := m.server.Smtp
|
||||||
|
|
||||||
auth := smtp.PlainAuth("", s.User, s.Pass, s.Host)
|
auth := smtp.PlainAuth("", s.User, s.Pass, s.Host)
|
||||||
|
|
||||||
|
|||||||
@@ -10,20 +10,20 @@ import (
|
|||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RandomService interface {
|
type Random interface {
|
||||||
Bytes(size int) ([]byte, error)
|
Bytes(size int) ([]byte, error)
|
||||||
String(size int) (string, error)
|
String(size int) (string, error)
|
||||||
UUID() (uuid.UUID, error)
|
UUID() (uuid.UUID, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type RandomServiceImpl struct {
|
type RandomImpl struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRandomServiceImpl() *RandomServiceImpl {
|
func NewRandomImpl() *RandomImpl {
|
||||||
return &RandomServiceImpl{}
|
return &RandomImpl{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RandomServiceImpl) Bytes(size int) ([]byte, error) {
|
func (r *RandomImpl) Bytes(size int) ([]byte, error) {
|
||||||
b := make([]byte, 32)
|
b := make([]byte, 32)
|
||||||
_, err := rand.Read(b)
|
_, err := rand.Read(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -34,7 +34,7 @@ func (r *RandomServiceImpl) Bytes(size int) ([]byte, error) {
|
|||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RandomServiceImpl) String(size int) (string, error) {
|
func (r *RandomImpl) String(size int) (string, error) {
|
||||||
bytes, err := r.Bytes(size)
|
bytes, err := r.Bytes(size)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", types.ErrInternal
|
return "", types.ErrInternal
|
||||||
@@ -43,6 +43,6 @@ func (r *RandomServiceImpl) String(size int) (string, error) {
|
|||||||
return base64.StdEncoding.EncodeToString(bytes), nil
|
return base64.StdEncoding.EncodeToString(bytes), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RandomServiceImpl) UUID() (uuid.UUID, error) {
|
func (r *RandomImpl) UUID() (uuid.UUID, error) {
|
||||||
return uuid.NewRandom()
|
return uuid.NewRandom()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,27 +9,27 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WorkoutService interface {
|
type Workout interface {
|
||||||
AddWorkout(user *User, workoutDto *WorkoutDto) (*WorkoutDto, error)
|
AddWorkout(user *User, workoutDto *WorkoutDto) (*WorkoutDto, error)
|
||||||
DeleteWorkout(user *User, rowId int) error
|
DeleteWorkout(user *User, rowId int) error
|
||||||
GetWorkouts(user *User) ([]*WorkoutDto, error)
|
GetWorkouts(user *User) ([]*WorkoutDto, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type WorkoutServiceImpl struct {
|
type WorkoutImpl struct {
|
||||||
dbWorkout db.WorkoutDb
|
db db.WorkoutDb
|
||||||
randomGenerator RandomService
|
random Random
|
||||||
clock ClockService
|
clock Clock
|
||||||
mailService MailService
|
mail Mail
|
||||||
serverSettings *types.ServerSettings
|
settings *types.Settings
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWorkoutServiceImpl(dbWorkout db.WorkoutDb, randomGenerator RandomService, clock ClockService, mailService MailService, serverSettings *types.ServerSettings) WorkoutService {
|
func NewWorkoutImpl(db db.WorkoutDb, random Random, clock Clock, mail Mail, settings *types.Settings) Workout {
|
||||||
return WorkoutServiceImpl{
|
return WorkoutImpl{
|
||||||
dbWorkout: dbWorkout,
|
db: db,
|
||||||
randomGenerator: randomGenerator,
|
random: random,
|
||||||
clock: clock,
|
clock: clock,
|
||||||
mailService: mailService,
|
mail: mail,
|
||||||
serverSettings: serverSettings,
|
settings: settings,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +64,7 @@ var (
|
|||||||
ErrInputValues = errors.New("invalid input values")
|
ErrInputValues = errors.New("invalid input values")
|
||||||
)
|
)
|
||||||
|
|
||||||
func (service WorkoutServiceImpl) AddWorkout(user *User, workoutDto *WorkoutDto) (*WorkoutDto, error) {
|
func (service WorkoutImpl) AddWorkout(user *User, workoutDto *WorkoutDto) (*WorkoutDto, error) {
|
||||||
|
|
||||||
if workoutDto.Date == "" || workoutDto.Type == "" || workoutDto.Sets == "" || workoutDto.Reps == "" {
|
if workoutDto.Date == "" || workoutDto.Type == "" || workoutDto.Sets == "" || workoutDto.Reps == "" {
|
||||||
return nil, ErrInputValues
|
return nil, ErrInputValues
|
||||||
@@ -87,7 +87,7 @@ func (service WorkoutServiceImpl) AddWorkout(user *User, workoutDto *WorkoutDto)
|
|||||||
|
|
||||||
workoutInsert := db.NewWorkoutInsert(date, workoutDto.Type, sets, reps)
|
workoutInsert := db.NewWorkoutInsert(date, workoutDto.Type, sets, reps)
|
||||||
|
|
||||||
workout, err := service.dbWorkout.InsertWorkout(user.Id, workoutInsert)
|
workout, err := service.db.InsertWorkout(user.Id, workoutInsert)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -95,20 +95,20 @@ func (service WorkoutServiceImpl) AddWorkout(user *User, workoutDto *WorkoutDto)
|
|||||||
return NewWorkoutDtoFromDb(workout), nil
|
return NewWorkoutDtoFromDb(workout), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service WorkoutServiceImpl) DeleteWorkout(user *User, rowId int) error {
|
func (service WorkoutImpl) DeleteWorkout(user *User, rowId int) error {
|
||||||
if user == nil {
|
if user == nil {
|
||||||
return types.ErrInternal
|
return types.ErrInternal
|
||||||
}
|
}
|
||||||
|
|
||||||
return service.dbWorkout.DeleteWorkout(user.Id, rowId)
|
return service.db.DeleteWorkout(user.Id, rowId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service WorkoutServiceImpl) GetWorkouts(user *User) ([]*WorkoutDto, error) {
|
func (service WorkoutImpl) GetWorkouts(user *User) ([]*WorkoutDto, error) {
|
||||||
if user == nil {
|
if user == nil {
|
||||||
return nil, types.ErrInternal
|
return nil, types.ErrInternal
|
||||||
}
|
}
|
||||||
|
|
||||||
workouts, err := service.dbWorkout.GetWorkouts(user.Id)
|
workouts, err := service.db.GetWorkouts(user.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"me-fit/log"
|
"me-fit/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServerSettings struct {
|
type Settings struct {
|
||||||
Port string
|
Port string
|
||||||
PrometheusEnabled bool
|
PrometheusEnabled bool
|
||||||
|
|
||||||
@@ -22,7 +22,7 @@ type SmtpSettings struct {
|
|||||||
FromName string
|
FromName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServerSettingsFromEnv(env func(string) string) *ServerSettings {
|
func NewSettingsFromEnv(env func(string) string) *Settings {
|
||||||
|
|
||||||
var smtp *SmtpSettings
|
var smtp *SmtpSettings
|
||||||
if env("SMTP_ENABLED") == "true" {
|
if env("SMTP_ENABLED") == "true" {
|
||||||
@@ -55,7 +55,7 @@ func NewServerSettingsFromEnv(env func(string) string) *ServerSettings {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
settings := &ServerSettings{
|
settings := &Settings{
|
||||||
Port: env("PORT"),
|
Port: env("PORT"),
|
||||||
PrometheusEnabled: env("PROMETHEUS_ENABLED") == "true",
|
PrometheusEnabled: env("PROMETHEUS_ENABLED") == "true",
|
||||||
BaseUrl: env("BASE_URL"),
|
BaseUrl: env("BASE_URL"),
|
||||||
|
|||||||
Reference in New Issue
Block a user