fixed linter errors
This commit is contained in:
parent
d38a61307d
commit
1ae02b3bc6
15 changed files with 242 additions and 117 deletions
50
auth/auth.go
50
auth/auth.go
|
|
@ -1,45 +1,54 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/Bronku/iroon/crypto"
|
||||
"github.com/Bronku/iroon/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Authenticator struct {
|
||||
db *gorm.DB
|
||||
type Config struct {
|
||||
SessionExpiration time.Duration
|
||||
}
|
||||
|
||||
func New(db *gorm.DB) Authenticator {
|
||||
type Authenticator struct {
|
||||
db *gorm.DB
|
||||
config Config
|
||||
}
|
||||
|
||||
func New(db *gorm.DB, config Config) Authenticator {
|
||||
err := db.AutoMigrate(&models.User{}, &models.Token{})
|
||||
if err != nil {
|
||||
log.Fatal("failed to initialize authenticator", err)
|
||||
}
|
||||
return Authenticator{db}
|
||||
return Authenticator{db, config}
|
||||
}
|
||||
|
||||
// #todo only used once, maybe remove it entierly later
|
||||
var ErrUserNotFound = errors.New("user with this login doesn't exist")
|
||||
var ErrWrongCredentials = errors.New("wrong credentials")
|
||||
|
||||
// #todo only used once, maybe remove it entirely later
|
||||
func (a *Authenticator) verifyCredentials(login, password string) error {
|
||||
var user models.User
|
||||
|
||||
result := a.db.First(&user, "login = ?", login)
|
||||
if result.Error != nil {
|
||||
return errors.New("user with this login doesn't exist")
|
||||
return ErrUserNotFound
|
||||
}
|
||||
|
||||
hash := crypto.PasswordHash(password, user.Salt)
|
||||
if hash == user.Password {
|
||||
return nil
|
||||
}
|
||||
return errors.New("wrong credentials")
|
||||
|
||||
return ErrWrongCredentials
|
||||
}
|
||||
|
||||
func (a *Authenticator) Middleware(in http.Handler) http.Handler {
|
||||
func (a *Authenticator) Middleware(inner http.Handler) http.Handler {
|
||||
handler := http.NewServeMux()
|
||||
handler.HandleFunc("GET /login", getLogin)
|
||||
handler.HandleFunc("POST /login", a.login)
|
||||
|
|
@ -50,31 +59,20 @@ func (a *Authenticator) Middleware(in http.Handler) http.Handler {
|
|||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
in.ServeHTTP(w, r)
|
||||
inner.ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
return handler
|
||||
}
|
||||
|
||||
// func (s *Store) AddUser(login, password string) error {
|
||||
// _, exists := s.GetUser(login)
|
||||
// if exists {
|
||||
// return errors.New("the user already exists")
|
||||
// }
|
||||
// query := "insert into user (login, password, salt) values(?, ?, ?)"
|
||||
// salt := crypto.GenerateKey()
|
||||
// hash := crypto.PasswordHash(password, salt)
|
||||
// _, err := s.db.Exec(query, login, hash, salt)
|
||||
// if err == nil {
|
||||
// s.users[login] = models.User{Password: hash, Salt: salt}
|
||||
// }
|
||||
// return err
|
||||
// }
|
||||
var ErrUsernameTaken = errors.New("username taken")
|
||||
|
||||
func (a *Authenticator) AddUser(login, password string) error {
|
||||
var user models.User
|
||||
|
||||
result := a.db.First(&user, "login = ?", login)
|
||||
if result.Error != gorm.ErrRecordNotFound {
|
||||
return errors.New("user already exists")
|
||||
if !errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return ErrUsernameTaken
|
||||
}
|
||||
|
||||
salt := crypto.GenerateKey()
|
||||
|
|
|
|||
|
|
@ -13,6 +13,5 @@ var loginHTML string
|
|||
// returns a simple login page
|
||||
func getLogin(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("content-type", "text/html")
|
||||
fmt.Fprint(w, loginHTML)
|
||||
return
|
||||
_, _ = fmt.Fprint(w, loginHTML)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,25 +9,29 @@ import (
|
|||
"github.com/Bronku/iroon/models"
|
||||
)
|
||||
|
||||
var ErrNoCookie = errors.New("session cookie not found")
|
||||
var ErrSessionNotFound = errors.New("session not found")
|
||||
var ErrSessionExpired = errors.New("session has expired")
|
||||
|
||||
func (a *Authenticator) getSession(r *http.Request) (models.Token, error) {
|
||||
c, err := r.Cookie("token")
|
||||
cookie, err := r.Cookie("token")
|
||||
if err != nil {
|
||||
return models.Token{}, err
|
||||
return models.Token{}, ErrNoCookie
|
||||
}
|
||||
var session models.Token
|
||||
result := a.db.First(&session, "token = ?", c.Value)
|
||||
result := a.db.First(&session, "token = ?", cookie.Value)
|
||||
if result.Error != nil {
|
||||
return models.Token{}, errors.New("session not found")
|
||||
return models.Token{}, ErrSessionNotFound
|
||||
}
|
||||
if time.Since(session.Expiration) > 0 {
|
||||
a.db.Delete(&session)
|
||||
return models.Token{}, errors.New("session expired")
|
||||
return models.Token{}, ErrSessionExpired
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (a *Authenticator) login(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
_ = r.ParseForm()
|
||||
login := r.PostFormValue("login")
|
||||
password := r.PostFormValue("password")
|
||||
if a.verifyCredentials(login, password) != nil {
|
||||
|
|
@ -60,7 +64,7 @@ func (a *Authenticator) newSession(user string) http.Cookie {
|
|||
key := crypto.GenerateKey()
|
||||
session := models.Token{
|
||||
User: user,
|
||||
Expiration: time.Now().Add(time.Hour * 24),
|
||||
Expiration: time.Now().Add(a.config.SessionExpiration),
|
||||
Token: key,
|
||||
}
|
||||
a.db.Create(&session)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue