neko/internal/session/auth.go

46 lines
839 B
Go
Raw Normal View History

2020-11-02 06:39:12 +13:00
package session
import (
"fmt"
"net/http"
2020-11-02 08:23:09 +13:00
"demodesk/neko/internal/types"
2020-11-02 06:39:12 +13:00
)
2020-11-02 08:23:09 +13:00
func (manager *SessionManagerCtx) Authenticate(r *http.Request) (types.Session, error) {
2020-11-29 03:22:04 +13:00
id, secret, ok := getAuthData(r)
2020-11-28 07:59:54 +13:00
if !ok {
return nil, fmt.Errorf("no authentication provided")
2020-11-02 06:39:12 +13:00
}
2020-11-28 07:59:54 +13:00
session, ok := manager.Get(id)
if !ok {
return nil, fmt.Errorf("member not found")
2020-11-02 06:39:12 +13:00
}
2020-11-28 07:59:54 +13:00
if !session.VerifySecret(secret) {
return nil, fmt.Errorf("invalid password provided")
2020-11-02 06:39:12 +13:00
}
2020-12-07 06:48:50 +13:00
if !session.CanLogin() {
return nil, fmt.Errorf("login disabled")
}
2020-11-28 07:59:54 +13:00
return session, nil
2020-11-02 06:39:12 +13:00
}
2020-11-29 03:22:04 +13:00
func getAuthData(r *http.Request) (string, string, bool) {
id, secret, ok := r.BasicAuth()
if ok {
return id, secret, true
}
id = r.URL.Query().Get("id")
secret = r.URL.Query().Get("secret")
if id != "" && secret != "" {
return id, secret, true
}
return "", "", false
}