package blossom import ( "encoding/base64" "net/http/httptest" "strconv" "strings" "testing" "time" "git.smesh.lol/orly/pkg/nostr/crypto/keys" "git.smesh.lol/orly/pkg/nostr/encoders/event" "git.smesh.lol/orly/pkg/nostr/encoders/tag" "git.smesh.lol/orly/pkg/nostr/interfaces/signer" "git.smesh.lol/orly/pkg/nostr/interfaces/signer/p8k" ) func authSigner(t *testing.T) signer.I { t.Helper() sk, err := keys.GenerateSecretKey() if err != nil { t.Fatal(err) } s, err := p8k.New() if err != nil { t.Fatal(err) } if err = s.InitSec(sk); err != nil { t.Fatal(err) } return s } type authToken struct { ev *event.E } func makeToken(t *testing.T, s signer.I, verb, x, server string, created, expires int64) *authToken { t.Helper() ev := event.New() ev.Kind = BlossomAuthKind ev.CreatedAt = created ev.Tags = tag.NewS() ev.Tags.Append(tag.NewFromBytesSlice([]byte("t"), []byte(verb))) if x != "" { ev.Tags.Append(tag.NewFromBytesSlice([]byte("x"), []byte(x))) } if server != "" { ev.Tags.Append(tag.NewFromBytesSlice([]byte("server"), []byte(server))) } ev.Tags.Append(tag.NewFromBytesSlice([]byte("expiration"), []byte(strconv.FormatInt(expires, 10)))) if err := ev.Sign(s); err != nil { t.Fatal(err) } return &authToken{ev: ev} } func (a *authToken) raw() []byte { return a.ev.Marshal(nil) } func (a *authToken) header(encoding *base64.Encoding) string { return "Nostr " + encoding.EncodeToString(a.raw()) } func TestBUD11UnpaddedBase64URL(t *testing.T) { s := authSigner(t) now := time.Now().Unix() tok := makeToken(t, s, "get", strings.Repeat("ab", 32), "blossom.example", now, now+300) r := httptest.NewRequest("GET", "/"+strings.Repeat("ab", 32), nil) r.Header.Set(AuthorizationHeader, tok.header(base64.RawURLEncoding)) authEv, err := ValidateAuthEventForGet(r, "https://blossom.example", []byte(strings.Repeat("ab", 32))) if err != nil { t.Fatalf("conformant unpadded token rejected: %v", err) } if authEv.Verb != "get" { t.Fatalf("verb = %q", authEv.Verb) } // Standard base64 from an older client is still accepted. r2 := httptest.NewRequest("GET", "/", nil) r2.Header.Set(AuthorizationHeader, tok.header(base64.StdEncoding)) if _, err = ValidateAuthEventForGet(r2, "https://blossom.example", []byte(strings.Repeat("ab", 32))); err != nil { t.Fatalf("legacy standard base64 rejected: %v", err) } } func TestBUD11ServerScope(t *testing.T) { s := authSigner(t) now := time.Now().Unix() tok := makeToken(t, s, "get", "", "other.example", now, now+300) r := httptest.NewRequest("GET", "/", nil) r.Header.Set(AuthorizationHeader, tok.header(base64.RawURLEncoding)) if _, err := ValidateAuthEventForGet(r, "https://blossom.example", nil); err == nil { t.Fatal("a token for another server was accepted") } // A token for this server authorizes a read with no x tag. tok2 := makeToken(t, s, "get", "", "https://blossom.example", now, now+300) r2 := httptest.NewRequest("GET", "/", nil) r2.Header.Set(AuthorizationHeader, tok2.header(base64.RawURLEncoding)) if _, err := ValidateAuthEventForGet(r2, "https://blossom.example", []byte(strings.Repeat("cd", 32))); err != nil { t.Fatalf("server-scoped token rejected: %v", err) } } func TestBUD11MaximumLifetime(t *testing.T) { s := authSigner(t) now := time.Now().Unix() x := strings.Repeat("ab", 32) tok := makeToken(t, s, "upload", x, "", now, now+int64(7*24*time.Hour/time.Second)) r := httptest.NewRequest("PUT", "/upload", nil) r.Header.Set(AuthorizationHeader, tok.header(base64.RawURLEncoding)) if _, err := ValidateAuthEvent(r, "upload", []byte(x)); err == nil { t.Fatal("a week-long token was accepted") } expired := makeToken(t, s, "upload", x, "", now-600, now-1) r2 := httptest.NewRequest("PUT", "/upload", nil) r2.Header.Set(AuthorizationHeader, expired.header(base64.RawURLEncoding)) if _, err := ValidateAuthEvent(r2, "upload", []byte(x)); err == nil { t.Fatal("an expired token was accepted") } } func TestBUD11XRequiredForUpload(t *testing.T) { s := authSigner(t) now := time.Now().Unix() tok := makeToken(t, s, "upload", "", "", now, now+300) r := httptest.NewRequest("PUT", "/upload", nil) r.Header.Set(AuthorizationHeader, tok.header(base64.RawURLEncoding)) if _, err := ValidateAuthEvent(r, "upload", []byte(strings.Repeat("ab", 32))); err == nil { t.Fatal("an upload token with no x scope was accepted") } } func TestBUD11VerbMismatch(t *testing.T) { s := authSigner(t) now := time.Now().Unix() x := strings.Repeat("ab", 32) tok := makeToken(t, s, "delete", x, "", now, now+300) r := httptest.NewRequest("PUT", "/upload", nil) r.Header.Set(AuthorizationHeader, tok.header(base64.RawURLEncoding)) if _, err := ValidateAuthEvent(r, "upload", []byte(x)); err == nil { t.Fatal("a delete token authorized an upload") } }