Skip to content

Commit

Permalink
Fix incorrect {Client,Server}AuthMsg
Browse files Browse the repository at this point in the history
  • Loading branch information
high-moctane committed Aug 12, 2024
1 parent 437cf2b commit 7a9b406
Showing 6 changed files with 82 additions and 74 deletions.
76 changes: 41 additions & 35 deletions message.go
Original file line number Diff line number Diff line change
@@ -330,16 +330,24 @@ func (msg *ClientCloseMsg) Valid() bool { return msg != nil }
var _ ClientMsg = (*ClientAuthMsg)(nil)

type ClientAuthMsg struct {
Challenge string
Event *Event
}

func NewClientAuthMsg(event *Event) (*ClientAuthMsg, error) {
if event == nil {
return nil, errors.New("server auth msg event must be non nil value")
}

return &ClientAuthMsg{Event: event}, nil
}

func (*ClientAuthMsg) ClientMsgLabel() string { return MsgLabelAuth }

func (msg ClientAuthMsg) MarshalJSON() ([]byte, error) {
v := [2]string{MsgLabelAuth, msg.Challenge}
v := [2]any{MsgLabelAuth, msg.Event}
ret, err := json.Marshal(&v)
if err != nil {
return nil, fmt.Errorf("failed to marshal client auth msg: %w", err)
return nil, fmt.Errorf("failed to marshal server auth msg: %w", err)
}

return ret, nil
@@ -350,24 +358,36 @@ func (msg *ClientAuthMsg) UnmarshalJSON(b []byte) error {
return nil
}

var elems []string
var elems []json.RawMessage
if err := json.Unmarshal(b, &elems); err != nil {
return fmt.Errorf("not a json array: %w", err)
}
if len(elems) != 2 {
return fmt.Errorf("client auth msg length must be 2 but got %d", len(elems))
return fmt.Errorf("server auth msg length must be 2 but got %d", len(elems))
}

if elems[0] != MsgLabelAuth {
return fmt.Errorf(`client auth msg label must be %q but got %q`, MsgLabelAuth, elems[0])
var label string
if err := json.Unmarshal(elems[0], &label); err != nil {
return fmt.Errorf("label must be string: %w", err)
}
if label != MsgLabelAuth {
return fmt.Errorf(`server auth msg label must be %q but got %q`, MsgLabelAuth, elems[0])
}

msg.Challenge = strings.Clone(elems[1])
var ret ClientAuthMsg
ret.Event = new(Event)
if err := ret.Event.UnmarshalJSON(elems[1]); err != nil {
return fmt.Errorf("failed to unmarshal event json: %w", err)
}

*msg = ret

return nil
}

func (msg *ClientAuthMsg) Valid() bool { return msg != nil }
func (msg *ClientAuthMsg) Valid() bool {
return msg != nil && msg.Event.Valid()
}

var _ ClientMsg = (*ClientCountMsg)(nil)

@@ -956,25 +976,19 @@ func (msg *ServerOKMsg) UnmarshalJSON(b []byte) error {
return nil
}

type ServerAuthMsg struct {
Event *Event
}
var _ ServerMsg = (*ServerAuthMsg)(nil)

func NewServerAuthMsg(event *Event) (*ServerAuthMsg, error) {
if event == nil {
return nil, errors.New("server auth msg event must be non nil value")
}

return &ServerAuthMsg{Event: event}, nil
type ServerAuthMsg struct {
Challenge string
}

func (*ServerAuthMsg) ServerMsgLabel() string { return MsgLabelAuth }

func (msg ServerAuthMsg) MarshalJSON() ([]byte, error) {
v := [2]any{MsgLabelAuth, msg.Event}
v := [2]string{MsgLabelAuth, msg.Challenge}
ret, err := json.Marshal(&v)
if err != nil {
return nil, fmt.Errorf("failed to marshal server auth msg: %w", err)
return nil, fmt.Errorf("failed to marshal client auth msg: %w", err)
}

return ret, nil
@@ -985,33 +999,25 @@ func (msg *ServerAuthMsg) UnmarshalJSON(b []byte) error {
return nil
}

var elems []json.RawMessage
var elems []string
if err := json.Unmarshal(b, &elems); err != nil {
return fmt.Errorf("not a json array: %w", err)
}
if len(elems) != 2 {
return fmt.Errorf("server auth msg length must be 2 but got %d", len(elems))
}

var label string
if err := json.Unmarshal(elems[0], &label); err != nil {
return fmt.Errorf("label must be string: %w", err)
}
if label != MsgLabelAuth {
return fmt.Errorf(`server auth msg label must be %q but got %q`, MsgLabelAuth, elems[0])
return fmt.Errorf("client auth msg length must be 2 but got %d", len(elems))
}

var ret ServerAuthMsg
ret.Event = new(Event)
if err := ret.Event.UnmarshalJSON(elems[1]); err != nil {
return fmt.Errorf("failed to unmarshal event json: %w", err)
if elems[0] != MsgLabelAuth {
return fmt.Errorf(`client auth msg label must be %q but got %q`, MsgLabelAuth, elems[0])
}

*msg = ret
msg.Challenge = strings.Clone(elems[1])

return nil
}

func (msg *ServerAuthMsg) Valid() bool { return msg != nil }

type ServerCountMsg struct {
SubscriptionID string
Count uint64
58 changes: 30 additions & 28 deletions message_test.go
Original file line number Diff line number Diff line change
@@ -178,8 +178,10 @@ func TestParseClientMsg(t *testing.T) {
},
},
{
Name: "ok: client auth message",
Input: []byte(`["AUTH","cf9ee89f-a07d-4ed6-9cc9-66ff6ef319f4"]`),
Name: "ok: client auth message",
Input: []byte(
`["AUTH",{"id":"37b9219808f916a0ab60cf40daf836cfd73ab6051d536e5453cadf8d424df829","pubkey":"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798","created_at":1723280486,"kind":22242,"tags":[["relay","example.com"],["challenge","challengemsg"]],"content":"","sig":"a5b7f83c7d65a5de41a81a688a450fa5c58bd86d7c106ec71721e64fb0a78d62e7413fbcbee17b0c6f3d7eeefbcf6fd482945942617f69b5a91e6d189ff62feb"}]`,
),
Expect: Expect{
MsgType: new(ClientAuthMsg),
IsErr: false,
@@ -665,7 +667,17 @@ func TestClientAuthMsg_MarshalJSON(t *testing.T) {
want []byte
}{
{
in: ClientAuthMsg{Challenge: "challenge"},
in: ClientAuthMsg{
Event: &Event{
ID: "37b9219808f916a0ab60cf40daf836cfd73ab6051d536e5453cadf8d424df829",
Pubkey: "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
CreatedAt: 1723280486,
Kind: 22242,
Tags: []Tag{{"relay", "example.com"}, {"challenge", "challengemsg"}},
Content: "",
Sig: "a5b7f83c7d65a5de41a81a688a450fa5c58bd86d7c106ec71721e64fb0a78d62e7413fbcbee17b0c6f3d7eeefbcf6fd482945942617f69b5a91e6d189ff62feb",
},
},
want: jsons[0],
},
}
@@ -692,8 +704,18 @@ func TestClientAuthMsg_UnmarshalJSON(t *testing.T) {
want ClientAuthMsg
}{
{
in: jsons[0],
want: ClientAuthMsg{Challenge: "challenge"},
in: jsons[0],
want: ClientAuthMsg{
Event: &Event{
ID: "37b9219808f916a0ab60cf40daf836cfd73ab6051d536e5453cadf8d424df829",
Pubkey: "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
CreatedAt: 1723280486,
Kind: 22242,
Tags: []Tag{{"relay", "example.com"}, {"challenge", "challengemsg"}},
Content: "",
Sig: "a5b7f83c7d65a5de41a81a688a450fa5c58bd86d7c106ec71721e64fb0a78d62e7413fbcbee17b0c6f3d7eeefbcf6fd482945942617f69b5a91e6d189ff62feb",
},
},
},
}

@@ -1473,17 +1495,7 @@ func TestServerAuthMsg_MarshalJSON(t *testing.T) {
want []byte
}{
{
in: ServerAuthMsg{
Event: &Event{
ID: "37b9219808f916a0ab60cf40daf836cfd73ab6051d536e5453cadf8d424df829",
Pubkey: "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
CreatedAt: 1723280486,
Kind: 22242,
Tags: []Tag{{"relay", "example.com"}, {"challenge", "challengemsg"}},
Content: "",
Sig: "a5b7f83c7d65a5de41a81a688a450fa5c58bd86d7c106ec71721e64fb0a78d62e7413fbcbee17b0c6f3d7eeefbcf6fd482945942617f69b5a91e6d189ff62feb",
},
},
in: ServerAuthMsg{Challenge: "challenge"},
want: jsons[0],
},
}
@@ -1510,18 +1522,8 @@ func TestServerAuthMsg_UnmarshalJSON(t *testing.T) {
want ServerAuthMsg
}{
{
in: jsons[0],
want: ServerAuthMsg{
Event: &Event{
ID: "37b9219808f916a0ab60cf40daf836cfd73ab6051d536e5453cadf8d424df829",
Pubkey: "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
CreatedAt: 1723280486,
Kind: 22242,
Tags: []Tag{{"relay", "example.com"}, {"challenge", "challengemsg"}},
Content: "",
Sig: "a5b7f83c7d65a5de41a81a688a450fa5c58bd86d7c106ec71721e64fb0a78d62e7413fbcbee17b0c6f3d7eeefbcf6fd482945942617f69b5a91e6d189ff62feb",
},
},
in: jsons[0],
want: ServerAuthMsg{Challenge: "challenge"},
},
}

9 changes: 4 additions & 5 deletions testdata/clientauthmsgs_invalid.jsonl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
["AUTH","challenge"
["AUT","challenge"]
["AUTH"]
["AUTH","challenge1","challenge2"]
["AUTH",1]
["AUTH",{"id":"37b9219808f916a0ab60cf40daf836cfd73ab6051d536e5453cadf8d424df829","pubkey":"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798","created_at":1723280486,"kind":22242,"tags":[["relay","example.com"],["challenge","challengemsg"]],"content":"","sig":"a5b7f83c7d65a5de41a81a688a450fa5c58bd86d7c106ec71721e64fb0a78d62e7413fbcbee17b0c6f3d7eeefbcf6fd482945942617f69b5a91e6d189ff62feb"}
["AUT",{"id":"37b9219808f916a0ab60cf40daf836cfd73ab6051d536e5453cadf8d424df829","pubkey":"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798","created_at":1723280486,"kind":22242,"tags":[["relay","example.com"],["challenge","challengemsg"]],"content":"","sig":"a5b7f83c7d65a5de41a81a688a450fa5c58bd86d7c106ec71721e64fb0a78d62e7413fbcbee17b0c6f3d7eeefbcf6fd482945942617f69b5a91e6d189ff62feb"}]
["AUTH",{"id":"37b9219808f916a0ab60cf40daf836cfd73ab6051d536e5453cadf8d424df829","pubkey":"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798","created_at":1723280486,"kind":22242,"tags":[["relay","example.com"],["challenge","challengemsg"]],"content":"","sig":"a5b7f83c7d65a5de41a81a688a450fa5c58bd86d7c106ec71721e64fb0a78d62e7413fbcbee17b0c6f3d7eeefbcf6fd482945942617f69b5a91e6d189ff62feb"},1]
["AUTH",{"invalid":1}]
2 changes: 1 addition & 1 deletion testdata/clientauthmsgs_valid.jsonl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
["AUTH","challenge"]
["AUTH",{"id":"37b9219808f916a0ab60cf40daf836cfd73ab6051d536e5453cadf8d424df829","pubkey":"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798","created_at":1723280486,"kind":22242,"tags":[["relay","example.com"],["challenge","challengemsg"]],"content":"","sig":"a5b7f83c7d65a5de41a81a688a450fa5c58bd86d7c106ec71721e64fb0a78d62e7413fbcbee17b0c6f3d7eeefbcf6fd482945942617f69b5a91e6d189ff62feb"}]
9 changes: 5 additions & 4 deletions testdata/serverauthmsgs_invalid.jsonl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
["AUTH",{"id":"37b9219808f916a0ab60cf40daf836cfd73ab6051d536e5453cadf8d424df829","pubkey":"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798","created_at":1723280486,"kind":22242,"tags":[["relay","example.com"],["challenge","challengemsg"]],"content":"","sig":"a5b7f83c7d65a5de41a81a688a450fa5c58bd86d7c106ec71721e64fb0a78d62e7413fbcbee17b0c6f3d7eeefbcf6fd482945942617f69b5a91e6d189ff62feb"}
["AUT",{"id":"37b9219808f916a0ab60cf40daf836cfd73ab6051d536e5453cadf8d424df829","pubkey":"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798","created_at":1723280486,"kind":22242,"tags":[["relay","example.com"],["challenge","challengemsg"]],"content":"","sig":"a5b7f83c7d65a5de41a81a688a450fa5c58bd86d7c106ec71721e64fb0a78d62e7413fbcbee17b0c6f3d7eeefbcf6fd482945942617f69b5a91e6d189ff62feb"}]
["AUTH",{"id":"37b9219808f916a0ab60cf40daf836cfd73ab6051d536e5453cadf8d424df829","pubkey":"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798","created_at":1723280486,"kind":22242,"tags":[["relay","example.com"],["challenge","challengemsg"]],"content":"","sig":"a5b7f83c7d65a5de41a81a688a450fa5c58bd86d7c106ec71721e64fb0a78d62e7413fbcbee17b0c6f3d7eeefbcf6fd482945942617f69b5a91e6d189ff62feb"},1]
["AUTH",{"invalid":1}]
["AUTH","challenge"
["AUT","challenge"]
["AUTH"]
["AUTH","challenge1","challenge2"]
["AUTH",1]
2 changes: 1 addition & 1 deletion testdata/serverauthmsgs_valid.jsonl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
["AUTH",{"id":"37b9219808f916a0ab60cf40daf836cfd73ab6051d536e5453cadf8d424df829","pubkey":"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798","created_at":1723280486,"kind":22242,"tags":[["relay","example.com"],["challenge","challengemsg"]],"content":"","sig":"a5b7f83c7d65a5de41a81a688a450fa5c58bd86d7c106ec71721e64fb0a78d62e7413fbcbee17b0c6f3d7eeefbcf6fd482945942617f69b5a91e6d189ff62feb"}]
["AUTH","challenge"]

0 comments on commit 7a9b406

Please sign in to comment.