2023-08-10 14:32:11 -03:00
|
|
|
package khatru
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-12-09 08:19:37 -03:00
|
|
|
|
|
|
|
"github.com/nbd-wtf/go-nostr"
|
2023-08-10 14:32:11 -03:00
|
|
|
)
|
|
|
|
|
2023-12-25 09:14:09 -03:00
|
|
|
const (
|
|
|
|
wsKey = iota
|
|
|
|
subscriptionIdKey
|
2024-07-09 00:11:07 -03:00
|
|
|
nip86HeaderAuthKey
|
2023-12-25 09:14:09 -03:00
|
|
|
)
|
|
|
|
|
2023-12-25 09:30:13 -03:00
|
|
|
func RequestAuth(ctx context.Context) {
|
|
|
|
ws := GetConnection(ctx)
|
2023-12-27 12:30:23 -03:00
|
|
|
ws.authLock.Lock()
|
2023-12-27 13:05:29 -03:00
|
|
|
if ws.Authed == nil {
|
|
|
|
ws.Authed = make(chan struct{})
|
|
|
|
}
|
2023-12-27 12:30:23 -03:00
|
|
|
ws.authLock.Unlock()
|
2023-12-25 09:30:13 -03:00
|
|
|
ws.WriteJSON(nostr.AuthEnvelope{Challenge: &ws.Challenge})
|
|
|
|
}
|
|
|
|
|
2023-08-10 14:32:11 -03:00
|
|
|
func GetConnection(ctx context.Context) *WebSocket {
|
2024-07-03 22:16:44 -03:00
|
|
|
wsi := ctx.Value(wsKey)
|
|
|
|
if wsi != nil {
|
|
|
|
return wsi.(*WebSocket)
|
|
|
|
}
|
|
|
|
return nil
|
2023-08-10 14:32:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetAuthed(ctx context.Context) string {
|
2024-07-09 00:11:07 -03:00
|
|
|
if conn := GetConnection(ctx); conn != nil {
|
2024-07-03 22:16:44 -03:00
|
|
|
return conn.AuthedPublicKey
|
|
|
|
}
|
2024-07-09 00:11:07 -03:00
|
|
|
if nip86Auth := ctx.Value(nip86HeaderAuthKey); nip86Auth != nil {
|
|
|
|
return nip86Auth.(string)
|
|
|
|
}
|
2024-07-03 22:16:44 -03:00
|
|
|
return ""
|
2023-12-09 08:19:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetIP(ctx context.Context) string {
|
2024-09-11 23:02:01 -03:00
|
|
|
conn := GetConnection(ctx)
|
|
|
|
if conn == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetIPFromRequest(conn.Request)
|
2023-12-09 08:19:37 -03:00
|
|
|
}
|
|
|
|
|
2023-12-25 09:14:09 -03:00
|
|
|
func GetSubscriptionID(ctx context.Context) string {
|
|
|
|
return ctx.Value(subscriptionIdKey).(string)
|
|
|
|
}
|