65c194264c
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 10s
CI / integration (pull_request) Successful in 14s
CI / ui (pull_request) Successful in 1m0s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m19s
Mirror the Telegram Mini App wrapper for VK: the SPA loads at a new /vk/ entry, authenticates from VK's signed launch parameters, and provisions a 'vk' platform identity — the minimum to run the game in VK test mode. - Gateway verifies the launch signature in-process (internal/vkauth: HMAC-SHA256 over the sorted vk_* params under GATEWAY_VK_APP_SECRET, base64url) — a pure offline check, no side-service. New auth.vk op (gated on the secret), backendclient.VKAuth, /vk/ SPA mount. - Backend: KindVK + ProvisionVK/vkSeed, /sessions/vk handler, identity kind widened to include 'vk' (migration 00005, expand-contract). - UI: src/lib/vk.ts (VK Bridge, lazy-imported), bootVK + the /vk/ boot dispatch, encodeVKLogin + authVK across transport/client/mock. VK omits the name from the signed params, so the client reads it via VKWebAppGetUserInfo as an unsigned display seed. - Deploy: /vk in the edge Caddyfile, GATEWAY_VK_APP_SECRET wired through compose + .env.example + CI (TEST_) + prod-deploy (PROD_). - Admin console: surface the VK user id (link to the VK profile) next to the Telegram id on the user card. - Docs: ARCHITECTURE §12/§13, FUNCTIONAL (+ _ru), gateway README; VK integration reference under .claude/. Signature algorithm verified against dev.vk.com plus independent Node/Python references and a %2C edge-case vector.
112 lines
3.8 KiB
Go
112 lines
3.8 KiB
Go
package vkauth_test
|
|
|
|
import (
|
|
"errors"
|
|
"net/url"
|
|
"testing"
|
|
|
|
"scrabble/gateway/internal/vkauth"
|
|
)
|
|
|
|
const (
|
|
testSecret = "wv527nm6mvf3rgomfhd6"
|
|
// goldenSign is HMAC-SHA256(sorted vk_* query, testSecret) base64url without
|
|
// padding, computed independently from a Python reference over goldenParams — so a
|
|
// green test proves Verify matches VK's documented algorithm, not merely itself.
|
|
goldenSign = "x7RUe9sKN05Bigm-pBIl8gLmfMwZCloPrwZaZEQAdOA"
|
|
)
|
|
|
|
// goldenParams is the canonical VK launch-parameter set the golden signature covers
|
|
// (a representative real launch, including the empty vk_access_token_settings).
|
|
func goldenParams() url.Values {
|
|
return url.Values{
|
|
"vk_access_token_settings": {""},
|
|
"vk_app_id": {"6736218"},
|
|
"vk_are_notifications_enabled": {"0"},
|
|
"vk_is_app_user": {"1"},
|
|
"vk_is_favorite": {"0"},
|
|
"vk_language": {"ru"},
|
|
"vk_platform": {"android"},
|
|
"vk_ref": {"other"},
|
|
"vk_ts": {"1546961916"},
|
|
"vk_user_id": {"494075"},
|
|
}
|
|
}
|
|
|
|
func signedParams() url.Values {
|
|
p := goldenParams()
|
|
p.Set("sign", goldenSign)
|
|
return p
|
|
}
|
|
|
|
func TestVerifyValid(t *testing.T) {
|
|
id, err := vkauth.Verify(signedParams().Encode(), testSecret)
|
|
if err != nil {
|
|
t.Fatalf("Verify: unexpected error %v", err)
|
|
}
|
|
if id.ExternalID != "494075" || id.Language != "ru" {
|
|
t.Fatalf("identity = %+v, want ExternalID=494075 Language=ru", id)
|
|
}
|
|
}
|
|
|
|
// TestVerifyCommaValue locks the URL-encoding of the one realistic special-char VK
|
|
// value: vk_access_token_settings can carry a comma (e.g. "email,phone"), which VK's
|
|
// signer and our url.Values.Encode both escape to %2C. The golden sign was computed
|
|
// independently (Node crypto) over these params under testSecret — so a green test
|
|
// proves Go's encoding matches VK's for that character.
|
|
func TestVerifyCommaValue(t *testing.T) {
|
|
p := url.Values{
|
|
"vk_access_token_settings": {"email,phone"},
|
|
"vk_app_id": {"6736218"},
|
|
"vk_language": {"ru"},
|
|
"vk_platform": {"mobile_web"},
|
|
"vk_ts": {"1546961916"},
|
|
"vk_user_id": {"494075"},
|
|
"sign": {"6g9FKCAfHfT-fBUdBAcJ5QK1xUm-vKMvGZrQ63aPgnQ"},
|
|
}
|
|
id, err := vkauth.Verify(p.Encode(), testSecret)
|
|
if err != nil {
|
|
t.Fatalf("Verify: unexpected error %v", err)
|
|
}
|
|
if id.ExternalID != "494075" {
|
|
t.Fatalf("ExternalID = %q, want 494075", id.ExternalID)
|
|
}
|
|
}
|
|
|
|
// TestVerifyIgnoresForeignParams asserts a non-vk_ query parameter the client may
|
|
// append (e.g. a tracking tag) is outside the signed set and does not break the check.
|
|
func TestVerifyIgnoresForeignParams(t *testing.T) {
|
|
id, err := vkauth.Verify(signedParams().Encode()+"&utm_source=catalog", testSecret)
|
|
if err != nil {
|
|
t.Fatalf("Verify: unexpected error %v", err)
|
|
}
|
|
if id.ExternalID != "494075" {
|
|
t.Fatalf("ExternalID = %q, want 494075", id.ExternalID)
|
|
}
|
|
}
|
|
|
|
func TestVerifyRejects(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
raw string
|
|
secret string
|
|
}{
|
|
{name: "tampered param", secret: testSecret, raw: func() string {
|
|
p := signedParams()
|
|
p.Set("vk_user_id", "1") // user id changed; the golden sign no longer matches
|
|
return p.Encode()
|
|
}()},
|
|
{name: "wrong secret", secret: "not-the-secret", raw: signedParams().Encode()},
|
|
{name: "missing sign", secret: testSecret, raw: goldenParams().Encode()},
|
|
{name: "no vk params", secret: testSecret, raw: url.Values{"sign": {goldenSign}, "foo": {"bar"}}.Encode()},
|
|
{name: "malformed query", secret: testSecret, raw: "%zz=bad"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if _, err := vkauth.Verify(tt.raw, tt.secret); !errors.Is(err, vkauth.ErrInvalid) {
|
|
t.Fatalf("Verify error = %v, want ErrInvalid", err)
|
|
}
|
|
})
|
|
}
|
|
}
|