1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 12:02:05 +01:00

Improve OX's PubkeyElement

there is no reason why we want to store the bytes of the base64
encoded string. Simply store the string itself.

Also add a convenience method to get the decoded bytes of the PubKey.
This commit is contained in:
Florian Schmaus 2019-09-07 23:27:36 +02:00
parent 34f1c2b79e
commit a6a1142255
4 changed files with 40 additions and 29 deletions

View file

@ -636,7 +636,8 @@ public final class OpenPgpManager extends Manager {
* @return {@link PubkeyElement} containing the key * @return {@link PubkeyElement} containing the key
*/ */
private static PubkeyElement createPubkeyElement(byte[] bytes, Date date) { private static PubkeyElement createPubkeyElement(byte[] bytes, Date date) {
return new PubkeyElement(new PubkeyElement.PubkeyDataElement(Base64.encode(bytes)), date); String base64EncodedOpenPgpPubKey = Base64.encodeToString(bytes);
return new PubkeyElement(new PubkeyElement.PubkeyDataElement(base64EncodedOpenPgpPubKey), date);
} }
/** /**

View file

@ -16,12 +16,12 @@
*/ */
package org.jivesoftware.smackx.ox.element; package org.jivesoftware.smackx.ox.element;
import java.nio.charset.Charset;
import java.util.Date; import java.util.Date;
import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smack.util.XmlStringBuilder;
import org.jivesoftware.smack.util.stringencoder.Base64;
/** /**
* Class representing a pubkey element which is used to transport OpenPGP public keys. * Class representing a pubkey element which is used to transport OpenPGP public keys.
@ -88,21 +88,30 @@ public class PubkeyElement implements ExtensionElement {
public static final String ELEMENT = "data"; public static final String ELEMENT = "data";
private final byte[] b64Data; private final String b64Data;
public PubkeyDataElement(byte[] b64Data) { public PubkeyDataElement(String b64Data) {
this.b64Data = Objects.requireNonNull(b64Data); this.b64Data = Objects.requireNonNull(b64Data);
} }
/** /**
* Base64 encoded public key. * Base64 encoded public key.
* *
* @return public key bytes. * @return the base64 encoded version of the public key.
*/ */
public byte[] getB64Data() { public String getB64Data() {
return b64Data; return b64Data;
} }
private transient byte[] pubKeyBytesCache;
public byte[] getPubKeyBytes() {
if (pubKeyBytesCache == null) {
pubKeyBytesCache = Base64.decode(b64Data);
}
return pubKeyBytesCache.clone();
}
@Override @Override
public String getElementName() { public String getElementName() {
return ELEMENT; return ELEMENT;
@ -117,7 +126,7 @@ public class PubkeyElement implements ExtensionElement {
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace) XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace)
.rightAngleBracket() .rightAngleBracket()
.append(new String(b64Data, Charset.forName("UTF-8"))) .append(b64Data)
.closeElement(this); .closeElement(this);
return xml; return xml;
} }

View file

@ -17,7 +17,6 @@
package org.jivesoftware.smackx.ox.provider; package org.jivesoftware.smackx.ox.provider;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Date; import java.util.Date;
import org.jivesoftware.smack.packet.XmlEnvironment; import org.jivesoftware.smack.packet.XmlEnvironment;
@ -34,7 +33,7 @@ import org.jivesoftware.smackx.ox.element.PubkeyElement;
*/ */
public class PubkeyElementProvider extends ExtensionElementProvider<PubkeyElement> { public class PubkeyElementProvider extends ExtensionElementProvider<PubkeyElement> {
public static final PubkeyElementProvider TEST_INSTANCE = new PubkeyElementProvider(); public static final PubkeyElementProvider INSTANCE = new PubkeyElementProvider();
@Override @Override
public PubkeyElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackTextParseException { public PubkeyElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackTextParseException {
@ -46,11 +45,9 @@ public class PubkeyElementProvider extends ExtensionElementProvider<PubkeyElemen
if (tag == XmlPullParser.Event.START_ELEMENT) { if (tag == XmlPullParser.Event.START_ELEMENT) {
switch (name) { switch (name) {
case PubkeyElement.PubkeyDataElement.ELEMENT: case PubkeyElement.PubkeyDataElement.ELEMENT:
String data = parser.nextText(); String base64EncodedOpenPgpPubKey = parser.nextText();
if (data != null) { PubkeyElement.PubkeyDataElement pubkeyDataElement = new PubkeyElement.PubkeyDataElement(base64EncodedOpenPgpPubKey);
byte[] bytes = data.getBytes(Charset.forName("UTF-8")); return new PubkeyElement(pubkeyDataElement, date);
return new PubkeyElement(new PubkeyElement.PubkeyDataElement(bytes), date);
}
} }
} }
} }

View file

@ -17,44 +17,48 @@
package org.jivesoftware.smackx.ox; package org.jivesoftware.smackx.ox;
import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static org.jivesoftware.smack.test.util.XmlUnitUtils.assertXmlSimilar; import static org.jivesoftware.smack.test.util.XmlUnitUtils.assertXmlSimilar;
import java.nio.charset.Charset; import java.io.IOException;
import java.util.Arrays; import java.text.ParseException;
import java.util.Date; import java.util.Date;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.test.util.SmackTestSuite; import org.jivesoftware.smack.test.util.SmackTestSuite;
import org.jivesoftware.smack.test.util.TestUtils; import org.jivesoftware.smack.test.util.SmackTestUtil;
import org.jivesoftware.smack.xml.XmlPullParser; import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException;
import org.jivesoftware.smackx.ox.element.PubkeyElement; import org.jivesoftware.smackx.ox.element.PubkeyElement;
import org.jivesoftware.smackx.ox.provider.PubkeyElementProvider; import org.jivesoftware.smackx.ox.provider.PubkeyElementProvider;
import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.jxmpp.util.XmppDateTime; import org.jxmpp.util.XmppDateTime;
public class PubkeyElementTest extends SmackTestSuite { public class PubkeyElementTest extends SmackTestSuite {
@Test @ParameterizedTest
public void providerTest() throws Exception { @EnumSource(SmackTestUtil.XmlPullParserKind.class)
String expected = public void providerTest(SmackTestUtil.XmlPullParserKind parserKind)
throws ParseException, XmlPullParserException, IOException, SmackParsingException {
String base64EncodedOpenPgpPublicKey = "VGhpcyBpcyBqdXN0IGEgdGVzdA==";
String pubkeyElement =
"<pubkey xmlns='urn:xmpp:openpgp:0' date='2018-01-21T10:46:21.000+00:00'>" + "<pubkey xmlns='urn:xmpp:openpgp:0' date='2018-01-21T10:46:21.000+00:00'>" +
"<data>" + "<data>" +
"BASE64_OPENPGP_PUBLIC_KEY" + base64EncodedOpenPgpPublicKey +
"</data>" + "</data>" +
"</pubkey>"; "</pubkey>";
Date date = XmppDateTime.parseXEP0082Date("2018-01-21T10:46:21.000+00:00"); Date date = XmppDateTime.parseXEP0082Date("2018-01-21T10:46:21.000+00:00");
byte[] key = "BASE64_OPENPGP_PUBLIC_KEY".getBytes(Charset.forName("UTF-8")); PubkeyElement element = new PubkeyElement(new PubkeyElement.PubkeyDataElement(base64EncodedOpenPgpPublicKey), date);
PubkeyElement element = new PubkeyElement(new PubkeyElement.PubkeyDataElement(key), date);
assertXmlSimilar(expected, element.toXML().toString()); assertXmlSimilar(pubkeyElement, element.toXML().toString());
XmlPullParser parser = TestUtils.getParser(expected); XmlPullParser parser = SmackTestUtil.getParserFor(pubkeyElement, parserKind);
PubkeyElement parsed = PubkeyElementProvider.TEST_INSTANCE.parse(parser); PubkeyElement parsed = PubkeyElementProvider.INSTANCE.parse(parser);
assertEquals(element.getDate(), parsed.getDate()); assertEquals(element.getDate(), parsed.getDate());
assertTrue(Arrays.equals(element.getDataElement().getB64Data(), parsed.getDataElement().getB64Data())); assertEquals(element.getDataElement().getB64Data(), parsed.getDataElement().getB64Data());
} }
} }