Config repository (#3988)
* WIP * fix(test): fix ap test failing * fix: fix unkeyed fields being used * chore(tests): clean up browser tests by splitting out federation UI tests
This commit is contained in:
15
persistence/authrepository/authrepository.go
Normal file
15
persistence/authrepository/authrepository.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package authrepository
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/owncast/owncast/models"
|
||||
)
|
||||
|
||||
type AuthRepository interface {
|
||||
CreateBanIPTable(db *sql.DB)
|
||||
BanIPAddress(address, note string) error
|
||||
IsIPAddressBanned(address string) (bool, error)
|
||||
GetIPAddressBans() ([]models.IPAddress, error)
|
||||
RemoveIPAddressBan(address string) error
|
||||
}
|
||||
65
persistence/authrepository/bans.go
Normal file
65
persistence/authrepository/bans.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package authrepository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"log"
|
||||
|
||||
"github.com/owncast/owncast/db"
|
||||
"github.com/owncast/owncast/models"
|
||||
)
|
||||
|
||||
// CreateBanIPTable will create the IP ban table if needed.
|
||||
func (r *SqlAuthRepository) CreateBanIPTable(db *sql.DB) {
|
||||
createTableSQL := ` CREATE TABLE IF NOT EXISTS ip_bans (
|
||||
"ip_address" TEXT NOT NULL PRIMARY KEY,
|
||||
"notes" TEXT,
|
||||
"created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);`
|
||||
|
||||
stmt, err := db.Prepare(createTableSQL)
|
||||
if err != nil {
|
||||
log.Fatal("error creating ip ban table", err)
|
||||
}
|
||||
defer stmt.Close()
|
||||
if _, err := stmt.Exec(); err != nil {
|
||||
log.Fatal("error creating ip ban table", err)
|
||||
}
|
||||
}
|
||||
|
||||
// BanIPAddress will persist a new IP address ban to the datastore.
|
||||
func (r *SqlAuthRepository) BanIPAddress(address, note string) error {
|
||||
return r.datastore.GetQueries().BanIPAddress(context.Background(), db.BanIPAddressParams{
|
||||
IpAddress: address,
|
||||
Notes: sql.NullString{String: note, Valid: true},
|
||||
})
|
||||
}
|
||||
|
||||
// IsIPAddressBanned will return if an IP address has been previously blocked.
|
||||
func (r *SqlAuthRepository) IsIPAddressBanned(address string) (bool, error) {
|
||||
blocked, error := r.datastore.GetQueries().IsIPAddressBlocked(context.Background(), address)
|
||||
return blocked > 0, error
|
||||
}
|
||||
|
||||
// GetIPAddressBans will return all the banned IP addresses.
|
||||
func (r *SqlAuthRepository) GetIPAddressBans() ([]models.IPAddress, error) {
|
||||
result, err := r.datastore.GetQueries().GetIPAddressBans(context.Background())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response := []models.IPAddress{}
|
||||
for _, ip := range result {
|
||||
response = append(response, models.IPAddress{
|
||||
IPAddress: ip.IpAddress,
|
||||
Notes: ip.Notes.String,
|
||||
CreatedAt: ip.CreatedAt.Time,
|
||||
})
|
||||
}
|
||||
return response, err
|
||||
}
|
||||
|
||||
// RemoveIPAddressBan will remove a previously banned IP address.
|
||||
func (r *SqlAuthRepository) RemoveIPAddressBan(address string) error {
|
||||
return r.datastore.GetQueries().RemoveIPAddressBan(context.Background(), address)
|
||||
}
|
||||
30
persistence/authrepository/sqlauthrepository.go
Normal file
30
persistence/authrepository/sqlauthrepository.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package authrepository
|
||||
|
||||
import (
|
||||
"github.com/owncast/owncast/core/data"
|
||||
)
|
||||
|
||||
type SqlAuthRepository struct {
|
||||
datastore *data.Datastore
|
||||
}
|
||||
|
||||
// NOTE: This is temporary during the transition period.
|
||||
var temporaryGlobalInstance AuthRepository
|
||||
|
||||
// Get will return the user repository.
|
||||
func Get() AuthRepository {
|
||||
if temporaryGlobalInstance == nil {
|
||||
i := New(data.GetDatastore())
|
||||
temporaryGlobalInstance = i
|
||||
}
|
||||
return temporaryGlobalInstance
|
||||
}
|
||||
|
||||
// New will create a new instance of the UserRepository.
|
||||
func New(datastore *data.Datastore) *SqlAuthRepository {
|
||||
r := &SqlAuthRepository{
|
||||
datastore: datastore,
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
Reference in New Issue
Block a user