mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
login with secret.
This commit is contained in:
parent
a330a3cc76
commit
a90bf87e24
@ -3,51 +3,24 @@ package session
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"demodesk/neko/internal/types"
|
"demodesk/neko/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
token_name = "password"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (manager *SessionManagerCtx) Authenticate(r *http.Request) (types.Session, error) {
|
func (manager *SessionManagerCtx) Authenticate(r *http.Request) (types.Session, error) {
|
||||||
token := getToken(r)
|
id, secret, ok := r.BasicAuth()
|
||||||
if token == "" {
|
if !ok {
|
||||||
return nil, fmt.Errorf("no password provided")
|
return nil, fmt.Errorf("no authentication provided")
|
||||||
}
|
}
|
||||||
|
|
||||||
isAdmin := (token == manager.config.AdminPassword)
|
session, ok := manager.Get(id)
|
||||||
isUser := (token == manager.config.Password)
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("member not found")
|
||||||
if !isAdmin && !isUser {
|
|
||||||
return nil, fmt.Errorf("invalid password")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Enable persistent user autentication.
|
if !session.VerifySecret(secret) {
|
||||||
return manager.Create(types.MemberProfile{
|
return nil, fmt.Errorf("invalid password provided")
|
||||||
IsAdmin: isAdmin,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getToken(r *http.Request) string {
|
return session, nil
|
||||||
// Get token from query
|
|
||||||
if token := r.URL.Query().Get(token_name); token != "" {
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get token from authorization header
|
|
||||||
bearer := r.Header.Get("Authorization")
|
|
||||||
if len(bearer) > 7 && strings.ToUpper(bearer[0:6]) == "BEARER" {
|
|
||||||
return bearer[7:]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get token from cookie
|
|
||||||
cookie, err := r.Cookie(token_name)
|
|
||||||
if err == nil {
|
|
||||||
return cookie.Value
|
|
||||||
}
|
|
||||||
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func New(capture types.CaptureManager, config *config.Session) *SessionManagerCtx {
|
func New(capture types.CaptureManager, config *config.Session) *SessionManagerCtx {
|
||||||
return &SessionManagerCtx{
|
manager := &SessionManagerCtx{
|
||||||
logger: log.With().Str("module", "session").Logger(),
|
logger: log.With().Str("module", "session").Logger(),
|
||||||
host: nil,
|
host: nil,
|
||||||
hostMu: sync.Mutex{},
|
hostMu: sync.Mutex{},
|
||||||
@ -24,6 +24,22 @@ func New(capture types.CaptureManager, config *config.Session) *SessionManagerCt
|
|||||||
membersMu: sync.Mutex{},
|
membersMu: sync.Mutex{},
|
||||||
emmiter: events.New(),
|
emmiter: events.New(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create default admin account at startup
|
||||||
|
_ = manager.Create("admin", types.MemberProfile{
|
||||||
|
Secret: config.AdminPassword,
|
||||||
|
Name: "Administrator",
|
||||||
|
IsAdmin: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
// create default user account at startup
|
||||||
|
_ = manager.Create("user", types.MemberProfile{
|
||||||
|
Secret: config.Password,
|
||||||
|
Name: "User",
|
||||||
|
IsAdmin: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
return manager
|
||||||
}
|
}
|
||||||
|
|
||||||
type SessionManagerCtx struct {
|
type SessionManagerCtx struct {
|
||||||
@ -37,15 +53,10 @@ type SessionManagerCtx struct {
|
|||||||
emmiter events.EventEmmiter
|
emmiter events.EventEmmiter
|
||||||
}
|
}
|
||||||
|
|
||||||
func (manager *SessionManagerCtx) Create(profile types.MemberProfile) (types.Session, error) {
|
func (manager *SessionManagerCtx) Create(id string, profile types.MemberProfile) types.Session {
|
||||||
manager.membersMu.Lock()
|
manager.membersMu.Lock()
|
||||||
defer manager.membersMu.Unlock()
|
defer manager.membersMu.Unlock()
|
||||||
|
|
||||||
id, err := utils.NewUID(32)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
session := &SessionCtx{
|
session := &SessionCtx{
|
||||||
id: id,
|
id: id,
|
||||||
manager: manager,
|
manager: manager,
|
||||||
@ -54,7 +65,7 @@ func (manager *SessionManagerCtx) Create(profile types.MemberProfile) (types.Ses
|
|||||||
}
|
}
|
||||||
|
|
||||||
manager.members[id] = session
|
manager.members[id] = session
|
||||||
return session, nil
|
return session
|
||||||
}
|
}
|
||||||
|
|
||||||
func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) {
|
func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) {
|
||||||
|
@ -35,6 +35,10 @@ func (session *SessionCtx) IsHost() bool {
|
|||||||
return session.manager.host != nil && session.manager.host.ID() == session.ID()
|
return session.manager.host != nil && session.manager.host.ID() == session.ID()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (session *SessionCtx) VerifySecret(secret string) bool {
|
||||||
|
return session.profile.Secret == secret
|
||||||
|
}
|
||||||
|
|
||||||
func (session *SessionCtx) Connected() bool {
|
func (session *SessionCtx) Connected() bool {
|
||||||
return session.websocket_connected && session.webrtc_connected
|
return session.websocket_connected && session.webrtc_connected
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,8 @@ package types
|
|||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
type MemberProfile struct {
|
type MemberProfile struct {
|
||||||
//Token string
|
ID string
|
||||||
|
Secret string
|
||||||
Name string
|
Name string
|
||||||
IsAdmin bool
|
IsAdmin bool
|
||||||
//Enabled bool
|
//Enabled bool
|
||||||
@ -18,6 +19,7 @@ type Session interface {
|
|||||||
Admin() bool
|
Admin() bool
|
||||||
IsHost() bool
|
IsHost() bool
|
||||||
Connected() bool
|
Connected() bool
|
||||||
|
VerifySecret(secret string) bool
|
||||||
SetName(name string)
|
SetName(name string)
|
||||||
SetWebSocketPeer(websocket_peer WebSocketPeer)
|
SetWebSocketPeer(websocket_peer WebSocketPeer)
|
||||||
SetWebSocketConnected(connected bool)
|
SetWebSocketConnected(connected bool)
|
||||||
@ -29,7 +31,7 @@ type Session interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SessionManager interface {
|
type SessionManager interface {
|
||||||
Create(profile MemberProfile) (Session, error)
|
Create(id string, profile MemberProfile) Session
|
||||||
Get(id string) (Session, bool)
|
Get(id string) (Session, bool)
|
||||||
Delete(id string) error
|
Delete(id string) error
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user