diff --git a/ox.go b/ox.go index 4ba87e5..d7ff055 100644 --- a/ox.go +++ b/ox.go @@ -177,35 +177,11 @@ func oxPublishPubKey(jid string, client *xmpp.Client, iqc chan xmpp.IQ, return err } pubKeyBase64 := base64.StdEncoding.EncodeToString(keySerialized) - root := etree.NewDocument() - pubsub := root.CreateElement("pubsub") - pubsub.CreateAttr("xmlns", nsPubsub) - publish := pubsub.CreateElement("publish") - publish.CreateAttr("node", nsOxPubKeys+":"+fingerprint) - item := publish.CreateElement("item") - item.CreateAttr("id", keyCreated) - pubkey := item.CreateElement("pubkey") - pubkey.CreateAttr("xmlns", nsOx) - data := pubkey.CreateElement("data") - data.CreateText(pubKeyBase64) - publishoptions := pubsub.CreateElement("publish-options") - x := publishoptions.CreateElement("x") - x.CreateAttr("xmlns", nsJabberData) - x.CreateAttr("type", "submit") - field := x.CreateElement("field") - field.CreateAttr("var", "FORM_TYPE") - field.CreateAttr("type", "hidden") - value := field.CreateElement("value") - value.CreateText(pubsubPubOptions) - field = x.CreateElement("field") - field.CreateAttr("var", "pubsub#access_model") - value = field.CreateElement("value") - value.CreateText("open") - xmlstring, err := root.WriteToString() + ppk, err := oxPublishPubKeyRequest(pubKeyBase64, fingerprint, keyCreated) if err != nil { return err } - iqReply, err := sendIQ(client, iqc, jid, "set", xmlstring) + iqReply, err := sendIQ(client, iqc, jid, "set", ppk) if err != nil { return err } @@ -224,35 +200,11 @@ func oxPublishPubKey(jid string, client *xmpp.Client, iqc chan xmpp.IQ, if pubKeyBase64 != base64.StdEncoding.EncodeToString(ownPubKeyFromPubsubSerialized) { return errors.New("couldn't successfully verify public key upload") } - root = etree.NewDocument() - pubsub = root.CreateElement("pubsub") - pubsub.CreateAttr("xmlns", nsPubsub) - publish = pubsub.CreateElement("publish") - publish.CreateAttr("node", nsOxPubKeys) - item = publish.CreateElement("item") - pubkeyslist := item.CreateElement("public-keys-list") - pubkeyslist.CreateAttr("xmlns", nsOx) - pubkeymeta := pubkeyslist.CreateElement("pubkey-metadata") - pubkeymeta.CreateAttr("v4-fingerprint", fingerprint) - pubkeymeta.CreateAttr("date", keyCreated) - publishoptions = pubsub.CreateElement("publish-options") - x = publishoptions.CreateElement("x") - x.CreateAttr("xmlns", nsJabberData) - x.CreateAttr("type", "submit") - field = x.CreateElement("field") - field.CreateAttr("var", "FORM_TYPE") - field.CreateAttr("type", "hidden") - value = field.CreateElement("value") - value.CreateText(pubsubPubOptions) - field = x.CreateElement("field") - field.CreateAttr("var", "pubsub#access_model") - value = field.CreateElement("value") - value.CreateText("open") - xmlstring, err = root.WriteToString() + ppkl, err := oxPublishPubKeyListRequest(fingerprint, keyCreated) if err != nil { return err } - iqReply, err = sendIQ(client, iqc, jid, "set", xmlstring) + iqReply, err = sendIQ(client, iqc, jid, "set", ppkl) if err != nil { return err } @@ -427,17 +379,11 @@ func oxGenPrivKey(jid string, client *xmpp.Client, iqc chan xmpp.IQ, func oxRecvPublicKeys(client *xmpp.Client, iqc chan xmpp.IQ, recipient string, fingerprint string) (*crypto.KeyRing, error) { - opkr := etree.NewDocument() - opkrPs := opkr.CreateElement("pubsub") - opkrPs.CreateAttr("xmlns", nsPubsub) - opkrPsItems := opkrPs.CreateElement("items") - opkrPsItems.CreateAttr("node", nsOxPubKeys+":"+fingerprint) - opkrPsItems.CreateAttr("max_items", "1") - opkrString, err := opkr.WriteToString() + opkr, err := oxRecvPubKeysRequest(fingerprint) if err != nil { return nil, err } - oxPublicKey, err := sendIQ(client, iqc, recipient, "get", opkrString) + oxPublicKey, err := sendIQ(client, iqc, recipient, "get", opkr) if err != nil { return nil, err } diff --git a/oxstanzas.go b/oxstanzas.go index bd0917a..4e7ed3f 100644 --- a/oxstanzas.go +++ b/oxstanzas.go @@ -99,3 +99,82 @@ func oxParseSignCrypt(message string) (oxSigncrypt, error) { output.body = body.Text() return output, nil } + +func oxPublishPubKeyRequest(pubKeyBase64 string, fingerprint string, + keyCreated string) (string, error) { + root := etree.NewDocument() + pubsub := root.CreateElement("pubsub") + pubsub.CreateAttr("xmlns", nsPubsub) + publish := pubsub.CreateElement("publish") + publish.CreateAttr("node", nsOxPubKeys+":"+fingerprint) + item := publish.CreateElement("item") + item.CreateAttr("id", keyCreated) + pubkey := item.CreateElement("pubkey") + pubkey.CreateAttr("xmlns", nsOx) + data := pubkey.CreateElement("data") + data.CreateText(pubKeyBase64) + publishoptions := pubsub.CreateElement("publish-options") + x := publishoptions.CreateElement("x") + x.CreateAttr("xmlns", nsJabberData) + x.CreateAttr("type", "submit") + field := x.CreateElement("field") + field.CreateAttr("var", "FORM_TYPE") + field.CreateAttr("type", "hidden") + value := field.CreateElement("value") + value.CreateText(pubsubPubOptions) + field = x.CreateElement("field") + field.CreateAttr("var", "pubsub#access_model") + value = field.CreateElement("value") + value.CreateText("open") + output, err := root.WriteToString() + if err != nil { + return output, err + } + return output, nil +} + +func oxPublishPubKeyListRequest(fingerprint string, keyCreated string) (string, error) { + root := etree.NewDocument() + pubsub := root.CreateElement("pubsub") + pubsub.CreateAttr("xmlns", nsPubsub) + publish := pubsub.CreateElement("publish") + publish.CreateAttr("node", nsOxPubKeys) + item := publish.CreateElement("item") + pubkeyslist := item.CreateElement("public-keys-list") + pubkeyslist.CreateAttr("xmlns", nsOx) + pubkeymeta := pubkeyslist.CreateElement("pubkey-metadata") + pubkeymeta.CreateAttr("v4-fingerprint", fingerprint) + pubkeymeta.CreateAttr("date", keyCreated) + publishoptions := pubsub.CreateElement("publish-options") + x := publishoptions.CreateElement("x") + x.CreateAttr("xmlns", nsJabberData) + x.CreateAttr("type", "submit") + field := x.CreateElement("field") + field.CreateAttr("var", "FORM_TYPE") + field.CreateAttr("type", "hidden") + value := field.CreateElement("value") + value.CreateText(pubsubPubOptions) + field = x.CreateElement("field") + field.CreateAttr("var", "pubsub#access_model") + value = field.CreateElement("value") + value.CreateText("open") + output, err := root.WriteToString() + if err != nil { + return output, err + } + return output, nil +} + +func oxRecvPubKeysRequest(fingerprint string) (string, error) { + opkr := etree.NewDocument() + opkrPs := opkr.CreateElement("pubsub") + opkrPs.CreateAttr("xmlns", nsPubsub) + opkrPsItems := opkrPs.CreateElement("items") + opkrPsItems.CreateAttr("node", nsOxPubKeys+":"+fingerprint) + opkrPsItems.CreateAttr("max_items", "1") + output, err := opkr.WriteToString() + if err != nil { + return output, err + } + return output, nil +}