databag/net/server/internal/authUtil.go

218 lines
5.9 KiB
Go
Raw Normal View History

2022-01-17 05:11:24 +00:00
package databag
import (
"errors"
"strings"
2022-01-18 05:48:42 +00:00
"time"
2022-01-17 05:11:24 +00:00
"net/http"
"encoding/base64"
2022-01-20 23:19:26 +00:00
"gorm.io/gorm"
2022-01-17 05:11:24 +00:00
"golang.org/x/crypto/bcrypt"
"databag/internal/store"
)
2022-01-18 05:48:42 +00:00
type accountLogin struct {
ID uint
Guid string
2022-01-18 05:48:42 +00:00
Password []byte
}
2022-01-19 20:07:57 +00:00
func AdminLogin(r *http.Request) error {
2022-01-17 05:11:24 +00:00
// extract request auth
2022-01-19 20:07:57 +00:00
username, password, ok := r.BasicAuth()
2022-01-17 05:11:24 +00:00
if !ok || username == "" || password == "" {
2022-01-19 20:07:57 +00:00
return errors.New("invalid credentials")
2022-01-17 05:11:24 +00:00
}
// nothing to do if not configured
if !getBoolConfigValue(CONFIG_CONFIGURED, false) {
2022-01-19 20:07:57 +00:00
return errors.New("node not configured")
2022-01-17 05:11:24 +00:00
}
// compare username
if getStrConfigValue(CONFIG_USERNAME, "") != username {
2022-01-19 20:07:57 +00:00
return errors.New("admin username error")
2022-01-17 05:11:24 +00:00
}
// compare password
p := getBinConfigValue(CONFIG_PASSWORD, nil);
if bcrypt.CompareHashAndPassword(p, []byte(password)) != nil {
2022-01-19 20:07:57 +00:00
return errors.New("admin password error")
2022-01-17 05:11:24 +00:00
}
2022-01-19 20:07:57 +00:00
return nil
2022-01-17 05:11:24 +00:00
}
2022-01-18 05:48:42 +00:00
func AccountLogin(r *http.Request) (uint, error) {
// extract request auth
username, password, ok := r.BasicAuth();
if !ok || username == "" || password == "" {
return 0, errors.New("invalid login")
}
// find account
var account accountLogin
2022-01-18 06:56:00 +00:00
if store.DB.Model(&store.Account{}).Where("Username = ?", username).First(&account).Error != nil {
2022-01-18 05:48:42 +00:00
return 0, errors.New("username not found");
}
// compare password
if bcrypt.CompareHashAndPassword(account.Password, []byte(password)) != nil {
return 0, errors.New("invalid password");
}
return account.ID, nil
}
2022-01-17 21:42:17 +00:00
func BearerAccountToken(r *http.Request) (store.AccountToken, error) {
2022-01-17 05:11:24 +00:00
// parse bearer authentication
auth := r.Header.Get("Authorization")
token := strings.TrimSpace(strings.TrimPrefix(auth, "Bearer"))
// find token record
var accountToken store.AccountToken
if err := store.DB.Preload("Account").Where("token = ?", token).First(&accountToken).Error; err != nil {
2022-01-19 20:07:57 +00:00
return accountToken, err
}
2022-01-18 05:48:42 +00:00
if accountToken.Expires < time.Now().Unix() {
return accountToken, errors.New("expired token")
}
2022-01-19 20:07:57 +00:00
return accountToken, nil
2022-01-17 05:11:24 +00:00
}
2022-01-20 23:19:26 +00:00
func BearerAppToken(r *http.Request, detail bool) (*store.Account, int, error) {
// parse bearer authentication
auth := r.Header.Get("Authorization")
token := strings.TrimSpace(strings.TrimPrefix(auth, "Bearer"))
// find token record
var app store.App
if detail {
2022-01-19 20:07:57 +00:00
if err := store.DB.Preload("Account.AccountDetail").Where("token = ?", token).First(&app).Error; err != nil {
2022-01-20 23:19:26 +00:00
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, http.StatusNotFound, err
} else {
return nil, http.StatusInternalServerError, err
}
}
} else {
2022-01-19 20:07:57 +00:00
if err := store.DB.Preload("Account").Where("token = ?", token).First(&app).Error; err != nil {
2022-01-20 23:19:26 +00:00
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, http.StatusNotFound, err
} else {
return nil, http.StatusInternalServerError, err
}
}
}
2022-01-20 23:19:26 +00:00
if app.Account.Disabled {
return nil, http.StatusGone, errors.New("account is inactive")
}
return &app.Account, http.StatusOK, nil
}
2022-01-22 19:04:29 +00:00
func BearerAppyToken(r *http.Request, detail bool) (*store.Account, int, error) {
// parse bearer authentication
auth := r.Header.Get("Authorization")
token := strings.TrimSpace(strings.TrimPrefix(auth, "Bearer"))
target, access, err := ParseToken(token)
if err != nil {
return nil, http.StatusBadRequest, err
}
// find token record
var app store.App
if detail {
if err := store.DB.Preload("Account.AccountDetail").Where("account_id = ? AND token = ?", target, access).First(&app).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, http.StatusNotFound, err
} else {
return nil, http.StatusInternalServerError, err
}
}
} else {
if err := store.DB.Preload("Account").Where("token = ?", token).First(&app).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, http.StatusNotFound, err
} else {
return nil, http.StatusInternalServerError, err
}
}
}
if app.Account.Disabled {
return nil, http.StatusGone, errors.New("account is inactive")
}
return &app.Account, http.StatusOK, nil
}
2022-01-22 18:45:39 +00:00
func ParseToken(token string) (string, string, error) {
2022-01-22 19:04:29 +00:00
split := strings.Split(token, ".")
2022-01-22 18:45:39 +00:00
if len(split) != 2 {
return "", "", errors.New("invalid token format")
}
return split[0], split[1], nil
}
2022-01-22 07:00:47 +00:00
func BearerContactToken(r *http.Request) (*store.Card, int, error) {
// parse bearer authentication
auth := r.Header.Get("Authorization")
token := strings.TrimSpace(strings.TrimPrefix(auth, "Bearer"))
2022-01-22 18:45:39 +00:00
target, access, err := ParseToken(token)
if err != nil {
return nil, http.StatusBadRequest, err
}
2022-01-22 07:00:47 +00:00
// find token record
var card store.Card
2022-01-22 18:45:39 +00:00
if err := store.DB.Preload("Account").Where("account_id = ? AND InToken = ?", target, access).First(&card).Error; err != nil {
2022-01-22 07:00:47 +00:00
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, http.StatusNotFound, err
} else {
return nil, http.StatusInternalServerError, err
}
}
if card.Account.Disabled {
return nil, http.StatusGone, errors.New("account is inactive")
}
return &card, http.StatusOK, nil
}
2022-01-17 21:42:17 +00:00
func BasicCredentials(r *http.Request) (string, []byte, error) {
2022-01-17 05:11:24 +00:00
var username string
var password []byte
// parse bearer authentication
auth := r.Header.Get("Credentials")
token := strings.TrimSpace(strings.TrimPrefix(auth, "Basic"))
// decode basic auth
credentials, err := base64.StdEncoding.DecodeString(token)
if err != nil {
return username, password, err
}
// parse credentials
login := strings.Split(string(credentials), ":");
if login[0] == "" || login[1] == "" {
return username, password, errors.New("invalid credentials")
}
username = login[0]
// hash password
password, err = bcrypt.GenerateFromPassword([]byte(login[1]), bcrypt.DefaultCost)
if err != nil {
2022-01-18 05:48:42 +00:00
return username, password, err
2022-01-17 05:11:24 +00:00
}
return username, password, nil
}