From bdfd035bf33710b5cde111e1873e6098fe07196c Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Thu, 25 Jan 2018 23:16:55 +0100 Subject: [PATCH] Handshake minor refactor --- component.go | 45 ++++++++++++++++++++++++++------------------- component_test.go | 4 ++++ 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/component.go b/component.go index 702c7d7..5077066 100644 --- a/component.go +++ b/component.go @@ -28,23 +28,7 @@ type Component struct { decoder *xml.Decoder } -// handshake generates an authentication token based on StreamID and shared secret. -func (c *Component) handshake(streamId string) string { - // 1. Concatenate the Stream ID received from the server with the shared secret. - concatStr := streamId + c.Secret - - // 2. Hash the concatenated string according to the SHA1 algorithm, i.e., SHA1( concat (sid, password)). - h := sha1.New() - h.Write([]byte(concatStr)) - hash := h.Sum(nil) - - // 3. Ensure that the hash output is in hexadecimal format, not binary or base64. - // 4. Convert the hash output to all lowercase characters. - encodedStr := hex.EncodeToString(hash) - - return encodedStr -} - +// Connect triggers component connection to XMPP server component port. // TODO Helper to prepare connection string func (c *Component) Connect(connStr string) error { var conn net.Conn @@ -106,17 +90,40 @@ func (c *Component) Send(packet Packet) error { return nil } -// ============================================================================ -// Handshake Packet +// handshake generates an authentication token based on StreamID and shared secret. +func (c *Component) handshake(streamId string) string { + // 1. Concatenate the Stream ID received from the server with the shared secret. + concatStr := streamId + c.Secret + // 2. Hash the concatenated string according to the SHA1 algorithm, i.e., SHA1( concat (sid, password)). + h := sha1.New() + h.Write([]byte(concatStr)) + hash := h.Sum(nil) + + // 3. Ensure that the hash output is in hexadecimal format, not binary or base64. + // 4. Convert the hash output to all lowercase characters. + encodedStr := hex.EncodeToString(hash) + + return encodedStr +} + +// ============================================================================ +// Handshake Stanza + +// Handshake is a stanza used by XMPP components to authenticate on XMPP +// component port. type Handshake struct { XMLName xml.Name `xml:"jabber:component:accept handshake"` + // TODO Add handshake value with test for proper serialization + // Value string `xml:",innerxml"` } func (Handshake) Name() string { return "component:handshake" } +// Handshake decoding wrapper + type handshakeDecoder struct{} var handshake handshakeDecoder diff --git a/component_test.go b/component_test.go index 76e3849..e3b6817 100644 --- a/component_test.go +++ b/component_test.go @@ -16,3 +16,7 @@ func TestHandshake(t *testing.T) { t.Errorf("incorrect handshake calculation '%s' != '%s'", result, expected) } } + +func TestGenerateHandshake(t *testing.T) { + // TODO +}