mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-28 20:45:17 +00:00
d83a3d71bc
Merge in RedPhone // FREEBIE
79 lines
2.8 KiB
Java
79 lines
2.8 KiB
Java
package org.thoughtcrime.redphone.signaling;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.squareup.okhttp.MediaType;
|
|
import com.squareup.okhttp.OkHttpClient;
|
|
import com.squareup.okhttp.Request;
|
|
import com.squareup.okhttp.RequestBody;
|
|
import com.squareup.okhttp.Response;
|
|
|
|
import org.thoughtcrime.securesms.util.Base64;
|
|
import org.whispersystems.textsecure.api.push.TrustStore;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.security.KeyManagementException;
|
|
import java.security.KeyStore;
|
|
import java.security.KeyStoreException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.cert.CertificateException;
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
import javax.net.ssl.TrustManager;
|
|
import javax.net.ssl.TrustManagerFactory;
|
|
|
|
public class RedPhoneAccountManager {
|
|
|
|
private final OkHttpClient client;
|
|
private final String baseUrl;
|
|
private final String login;
|
|
private final String password;
|
|
|
|
public RedPhoneAccountManager(String baseUrl, TrustStore trustStore, String login, String password) {
|
|
try {
|
|
TrustManager[] trustManagers = getTrustManager(trustStore);
|
|
SSLContext context = SSLContext.getInstance("TLS");
|
|
context.init(null, trustManagers, null);
|
|
|
|
this.baseUrl = baseUrl;
|
|
this.login = login;
|
|
this.password = password;
|
|
this.client = new OkHttpClient().setSslSocketFactory(context.getSocketFactory());
|
|
} catch (NoSuchAlgorithmException | KeyManagementException e) {
|
|
throw new AssertionError(e);
|
|
}
|
|
}
|
|
|
|
public void createAccount(String verificationToken, RedPhoneAccountAttributes attributes) throws IOException {
|
|
String body = new ObjectMapper().writeValueAsString(attributes);
|
|
|
|
Request request = new Request.Builder()
|
|
.url(baseUrl + "/api/v1/accounts/token/" + verificationToken)
|
|
.put(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), body))
|
|
.header("Authorization", "Basic " + Base64.encodeBytes((login + ":" + password).getBytes()))
|
|
.build();
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
if (!response.isSuccessful()) {
|
|
throw new IOException("Failed to create account");
|
|
}
|
|
}
|
|
|
|
public TrustManager[] getTrustManager(TrustStore trustStore) {
|
|
try {
|
|
InputStream keyStoreInputStream = trustStore.getKeyStoreInputStream();
|
|
KeyStore keyStore = KeyStore.getInstance("BKS");
|
|
|
|
keyStore.load(keyStoreInputStream, trustStore.getKeyStorePassword().toCharArray());
|
|
|
|
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("X509");
|
|
trustManagerFactory.init(keyStore);
|
|
|
|
return trustManagerFactory.getTrustManagers();
|
|
} catch (KeyStoreException | CertificateException | IOException | NoSuchAlgorithmException e) {
|
|
throw new AssertionError(e);
|
|
}
|
|
}
|
|
}
|