khatru/utils.go
fiatjaf b00e5b2b3f
only reset ws.Authed if it's nil.
i.e. if there has been an auth and for some reason the client tried to auth again
after RequestAuth() has been called again.
2023-12-27 13:05:31 -03:00

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
}