mirror of
https://github.com/fiatjaf/khatru.git
synced 2025-03-17 13:22:56 +01:00
i.e. if there has been an auth and for some reason the client tried to auth again after RequestAuth() has been called again.
52 lines
1.0 KiB
Go
52 lines
1.0 KiB
Go
package khatru
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/nbd-wtf/go-nostr"
|
|
"github.com/sebest/xff"
|
|
)
|
|
|
|
const (
|
|
wsKey = iota
|
|
subscriptionIdKey
|
|
)
|
|
|
|
func RequestAuth(ctx context.Context) {
|
|
ws := GetConnection(ctx)
|
|
ws.authLock.Lock()
|
|
if ws.Authed == nil {
|
|
ws.Authed = make(chan struct{})
|
|
}
|
|
ws.authLock.Unlock()
|
|
ws.WriteJSON(nostr.AuthEnvelope{Challenge: &ws.Challenge})
|
|
}
|
|
|
|
func GetConnection(ctx context.Context) *WebSocket {
|
|
return ctx.Value(wsKey).(*WebSocket)
|
|
}
|
|
|
|
func GetAuthed(ctx context.Context) string {
|
|
return GetConnection(ctx).AuthedPublicKey
|
|
}
|
|
|
|
func GetIP(ctx context.Context) string {
|
|
return xff.GetRemoteAddr(GetConnection(ctx).Request)
|
|
}
|
|
|
|
func GetSubscriptionID(ctx context.Context) string {
|
|
return ctx.Value(subscriptionIdKey).(string)
|
|
}
|
|
|
|
func GetOpenSubscriptions(ctx context.Context) []nostr.Filter {
|
|
if subs, ok := listeners.Load(GetConnection(ctx)); ok {
|
|
res := make([]nostr.Filter, 0, listeners.Size()*2)
|
|
subs.Range(func(_ string, sub *Listener) bool {
|
|
res = append(res, sub.filters...)
|
|
return true
|
|
})
|
|
return res
|
|
}
|
|
return nil
|
|
}
|