-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/guackamolly/zero-monitor
- Loading branch information
Showing
26 changed files
with
816 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package db | ||
|
||
type CredentialsTable CrudTable[CredentialsEntity, string] | ||
type CredentialsEntity struct { | ||
Username string | ||
Password string | ||
} | ||
|
||
func NewCredentialsEntity( | ||
username string, | ||
password string, | ||
) CredentialsEntity { | ||
return CredentialsEntity{ | ||
Username: username, | ||
Password: password, | ||
} | ||
} | ||
|
||
func (e CredentialsEntity) PK() string { | ||
return e.Username | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package db | ||
|
||
import "github.com/guackamolly/zero-monitor/internal/data/models" | ||
|
||
type UserTable CrudTable[UserEntity, string] | ||
type UserEntity struct { | ||
models.User | ||
} | ||
|
||
func NewUserEntity( | ||
user models.User, | ||
) UserEntity { | ||
return UserEntity{ | ||
User: user, | ||
} | ||
} | ||
|
||
func (e UserEntity) PK() string { | ||
return e.ID() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package models | ||
|
||
const ( | ||
AdminRole Role = iota + 1 | ||
GuestRole | ||
) | ||
|
||
type Role int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package models | ||
|
||
import "strings" | ||
|
||
type User struct { | ||
Role | ||
Username string | ||
} | ||
|
||
func NewAdminUser( | ||
username string, | ||
) User { | ||
return User{ | ||
Username: username, | ||
Role: AdminRole, | ||
} | ||
} | ||
|
||
func (u User) ID() string { | ||
return strings.ToLower(u.Username) | ||
} | ||
|
||
func (u User) IsAdmin() bool { | ||
return u.Role == AdminRole | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package models_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/guackamolly/zero-monitor/internal/data/models" | ||
) | ||
|
||
func TestUserIsAdmin(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
input models.User | ||
output bool | ||
}{ | ||
{ | ||
desc: "returns true if user has admin role", | ||
input: models.User{Role: models.AdminRole}, | ||
output: true, | ||
}, | ||
{ | ||
desc: "returns false if user has guest role", | ||
input: models.User{Role: models.GuestRole}, | ||
output: false, | ||
}, | ||
} | ||
for _, tC := range testCases { | ||
t.Run(tC.desc, func(t *testing.T) { | ||
if output := tC.input.IsAdmin(); output != tC.output { | ||
t.Errorf("expected %v but got %v", tC.output, output) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package repositories | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/guackamolly/zero-monitor/internal/data/db" | ||
"github.com/guackamolly/zero-monitor/internal/data/models" | ||
"github.com/guackamolly/zero-monitor/internal/logging" | ||
) | ||
|
||
type DatabaseAuthenticationRepository struct { | ||
authTable db.CredentialsTable | ||
userTable db.UserTable | ||
} | ||
|
||
func NewDatabaseAuthenticationRepository( | ||
authTable db.CredentialsTable, | ||
userTable db.UserTable, | ||
) *DatabaseAuthenticationRepository { | ||
return &DatabaseAuthenticationRepository{ | ||
authTable: authTable, | ||
userTable: userTable, | ||
} | ||
} | ||
|
||
func (r DatabaseAuthenticationRepository) SignIn(username string, password string) (models.User, error) { | ||
credsEntity, ok, err := r.authTable.Lookup(username) | ||
if !ok || err != nil { | ||
return models.User{}, fmt.Errorf("user credentials not found") | ||
} | ||
|
||
if credsEntity.Password != password { | ||
return models.User{}, fmt.Errorf("user credentials don't match") | ||
} | ||
|
||
userEntity, ok, err := r.userTable.Lookup(username) | ||
if !ok || err != nil { | ||
return models.User{}, fmt.Errorf("user not found") | ||
} | ||
|
||
return userEntity.User, nil | ||
} | ||
|
||
func (r DatabaseAuthenticationRepository) RegisterAdmin(username string, password string) (models.User, error) { | ||
if _, ok, _ := r.authTable.Lookup(username); ok { | ||
return models.User{}, fmt.Errorf("username already exists") | ||
} | ||
|
||
if _, ok, _ := r.userTable.Lookup(username); ok { | ||
return models.User{}, fmt.Errorf("username already exists") | ||
} | ||
|
||
user := models.NewAdminUser(username) | ||
err := r.userTable.Insert(db.NewUserEntity(user)) | ||
if err != nil { | ||
return models.User{}, fmt.Errorf("user table insert failed, %v", err) | ||
} | ||
|
||
err = r.authTable.Insert(db.NewCredentialsEntity(username, password)) | ||
if err == nil { | ||
return user, nil | ||
} | ||
|
||
delErr := r.userTable.Delete(db.NewUserEntity(user)) | ||
if delErr != nil { | ||
logging.LogWarning("failed to insert user credentials, and now can't delete user from user table!") | ||
} | ||
|
||
return models.User{}, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package repositories | ||
|
||
import "github.com/guackamolly/zero-monitor/internal/data/models" | ||
|
||
type AuthenticationRepository interface { | ||
SignIn(username string, password string) (models.User, error) | ||
RegisterAdmin(username string, password string) (models.User, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package repositories | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/guackamolly/zero-monitor/internal/data/db" | ||
) | ||
|
||
type DatabaseUserRepository struct { | ||
userTable db.UserTable | ||
} | ||
|
||
func NewDatabaseUserRepository( | ||
userTable db.UserTable, | ||
) *DatabaseUserRepository { | ||
return &DatabaseUserRepository{ | ||
userTable: userTable, | ||
} | ||
} | ||
|
||
func (r DatabaseUserRepository) AdminExists() (bool, error) { | ||
users, err := r.userTable.All() | ||
if err != nil && !strings.HasSuffix(err.Error(), "does not exist") { | ||
return false, err | ||
} | ||
|
||
if len(users) == 0 { | ||
return false, nil | ||
} | ||
|
||
for _, u := range users { | ||
if u.IsAdmin() { | ||
return true, nil | ||
} | ||
} | ||
|
||
return false, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package repositories | ||
|
||
type UserRepository interface { | ||
AdminExists() (bool, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package http | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
const ( | ||
tokenCookie = "token" | ||
) | ||
|
||
func NewCookie( | ||
ectx echo.Context, | ||
name string, | ||
value string, | ||
path string, | ||
expiry time.Time, | ||
) *http.Cookie { | ||
c := new(http.Cookie) | ||
c.Name = name | ||
c.Value = value | ||
c.Path = path | ||
c.Expires = expiry | ||
c.SameSite = http.SameSiteStrictMode | ||
c.Secure = ectx.IsTLS() | ||
|
||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.