diff --git a/mobile/android/base/background/fxa/FxAccountClient20.java b/mobile/android/base/background/fxa/FxAccountClient20.java index 477fce1d83a0..36f6cba91138 100644 --- a/mobile/android/base/background/fxa/FxAccountClient20.java +++ b/mobile/android/base/background/fxa/FxAccountClient20.java @@ -4,7 +4,10 @@ package org.mozilla.gecko.background.fxa; +import java.io.UnsupportedEncodingException; import java.net.URI; +import java.net.URISyntaxException; +import java.net.URLEncoder; import java.util.concurrent.Executor; import org.json.simple.JSONObject; @@ -94,13 +97,35 @@ public class FxAccountClient20 extends FxAccountClient10 implements FxAccountCli post(resource, body, delegate); } - public void createAccount(final byte[] emailUTF8, final byte[] quickStretchedPW, final boolean getKeys, final boolean preVerified, + /** + * Create account/create URI, encoding query parameters carefully. + *

+ * This is equivalent to android.net.Uri.Builder, which is not + * present in our JUnit 4 tests. + */ + protected URI getCreateAccountURI(final boolean getKeys, final String service) throws UnsupportedEncodingException, URISyntaxException { + if (service == null) { + throw new IllegalArgumentException("service must not be null"); + } + final StringBuilder sb = new StringBuilder(serverURI); // serverURI always has a trailing slash. + sb.append("account/create?service="); + // Be very careful that query parameters are encoded correctly! + sb.append(URLEncoder.encode(service, "UTF-8")); + if (getKeys) { + sb.append("&keys=true"); + } + return new URI(sb.toString()); + } + + public void createAccount(final byte[] emailUTF8, final byte[] quickStretchedPW, + final boolean getKeys, + final boolean preVerified, + final String service, final RequestDelegate delegate) { - BaseResource resource; - JSONObject body; - final String path = getKeys ? "account/create?keys=true" : "account/create"; + final BaseResource resource; + final JSONObject body; try { - resource = new BaseResource(new URI(serverURI + path)); + resource = new BaseResource(getCreateAccountURI(getKeys, service)); body = new FxAccount20CreateDelegate(emailUTF8, quickStretchedPW, preVerified).getCreateBody(); } catch (Exception e) { invokeHandleError(delegate, e); @@ -144,7 +169,7 @@ public class FxAccountClient20 extends FxAccountClient10 implements FxAccountCli public void createAccountAndGetKeys(byte[] emailUTF8, PasswordStretcher passwordStretcher, RequestDelegate delegate) { try { byte[] quickStretchedPW = passwordStretcher.getQuickStretchedPW(emailUTF8); - createAccount(emailUTF8, quickStretchedPW, true, false, delegate); + createAccount(emailUTF8, quickStretchedPW, true, false, "sync", delegate); } catch (Exception e) { invokeHandleError(delegate, e); return;