Handshake minor refactor

This commit is contained in:
Mickael Remond 2018-01-25 23:16:55 +01:00
parent 4173d9ee70
commit bdfd035bf3
No known key found for this signature in database
GPG Key ID: E6F6045D79965AA3
2 changed files with 30 additions and 19 deletions

View File

@ -28,23 +28,7 @@ type Component struct {
decoder *xml.Decoder decoder *xml.Decoder
} }
// handshake generates an authentication token based on StreamID and shared secret. // Connect triggers component connection to XMPP server component port.
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
}
// TODO Helper to prepare connection string // TODO Helper to prepare connection string
func (c *Component) Connect(connStr string) error { func (c *Component) Connect(connStr string) error {
var conn net.Conn var conn net.Conn
@ -106,17 +90,40 @@ func (c *Component) Send(packet Packet) error {
return nil return nil
} }
// ============================================================================ // handshake generates an authentication token based on StreamID and shared secret.
// Handshake Packet 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 { type Handshake struct {
XMLName xml.Name `xml:"jabber:component:accept handshake"` 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 { func (Handshake) Name() string {
return "component:handshake" return "component:handshake"
} }
// Handshake decoding wrapper
type handshakeDecoder struct{} type handshakeDecoder struct{}
var handshake handshakeDecoder var handshake handshakeDecoder

View File

@ -16,3 +16,7 @@ func TestHandshake(t *testing.T) {
t.Errorf("incorrect handshake calculation '%s' != '%s'", result, expected) t.Errorf("incorrect handshake calculation '%s' != '%s'", result, expected)
} }
} }
func TestGenerateHandshake(t *testing.T) {
// TODO
}