mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
refactor: use cookie-based login
feat: add login page
This commit is contained in:
65
utils/auth.go
Normal file
65
utils/auth.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"github.com/muety/wakapi/models"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ExtractBasicAuth(r *http.Request) (username, password string, err error) {
|
||||
authHeader := strings.Split(r.Header.Get("Authorization"), " ")
|
||||
if len(authHeader) != 2 || authHeader[0] != "Basic" {
|
||||
return username, password, errors.New("failed to extract API key")
|
||||
}
|
||||
|
||||
hash, err := base64.StdEncoding.DecodeString(authHeader[1])
|
||||
userKey := strings.TrimSpace(string(hash))
|
||||
if err != nil {
|
||||
return username, password, err
|
||||
}
|
||||
|
||||
re := regexp.MustCompile(`^(.+):(.+)$`)
|
||||
groups := re.FindAllStringSubmatch(userKey, -1)
|
||||
if len(groups) == 0 || len(groups[0]) != 3 {
|
||||
return username, password, errors.New("failed to parse user agent string")
|
||||
}
|
||||
username, password = groups[0][1], groups[0][2]
|
||||
return username, password, err
|
||||
}
|
||||
|
||||
func ExtractBearerAuth(r *http.Request) (key string, err error) {
|
||||
authHeader := strings.Split(r.Header.Get("Authorization"), " ")
|
||||
if len(authHeader) != 2 || authHeader[0] != "Basic" {
|
||||
return key, errors.New("failed to extract API key")
|
||||
}
|
||||
|
||||
keyBytes, err := base64.StdEncoding.DecodeString(authHeader[1])
|
||||
return string(keyBytes), err
|
||||
}
|
||||
|
||||
func ExtractCookieAuth(r *http.Request, config *models.Config) (login *models.Login, err error) {
|
||||
cookie, err := r.Cookie(models.AuthCookieKey)
|
||||
if err != nil {
|
||||
return nil, errors.New("missing authentication")
|
||||
}
|
||||
|
||||
if err := config.SecureCookie.Decode(models.AuthCookieKey, cookie.Value, &login); err != nil {
|
||||
return nil, errors.New("invalid parameters")
|
||||
}
|
||||
|
||||
return login, nil
|
||||
}
|
||||
|
||||
func CheckPassword(user *models.User, password string) bool {
|
||||
passwordHash := md5.Sum([]byte(password))
|
||||
passwordHashString := hex.EncodeToString(passwordHash[:])
|
||||
if passwordHashString == user.Password {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
@@ -12,3 +12,13 @@ func RespondJSON(w http.ResponseWriter, status int, object interface{}) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func ClearCookie(w http.ResponseWriter, name string) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: name,
|
||||
Value: "",
|
||||
Path: "/",
|
||||
Secure: true,
|
||||
HttpOnly: true,
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user