Search in sources :

Example 1 with AndroidCryptoProvider

use of org.flyve.mdm.agent.security.AndroidCryptoProvider in project android-mdm-agent by flyve-mdm.

the class EnrollmentModel method enroll.

@Override
public void enroll(final Activity activity, final List<UserData.EmailsData> arrEmails, final String firstName, final String lastName, final String phone, final String phone2, final String mobilePhone, final String inventory, final String photo, final String language, final String administrativeNumber) {
    StringBuilder errMsg = new StringBuilder(activity.getResources().getString(R.string.validate_error));
    boolean allow = true;
    Helpers.hideKeyboard(activity);
    if (arrEmails.isEmpty() || arrEmails.get(0).getEmail().equals("")) {
        errMsg.append(activity.getResources().getString(R.string.validate_email_at_least_one));
        allow = false;
    }
    if (firstName.trim().equals("")) {
        errMsg.append(activity.getResources().getString(R.string.validate_first_name));
        allow = false;
    }
    if (lastName.trim().equals("")) {
        errMsg.append(activity.getResources().getString(R.string.validate_last_name));
        allow = false;
    }
    if (inventory.contains("fail")) {
        errMsg.append(activity.getResources().getString(R.string.validate_inventory));
        allow = false;
    }
    // inventory running
    if (inventory.equals("")) {
        errMsg.append(activity.getResources().getString(R.string.inventory_not_exists));
        allow = false;
    }
    if (!allow) {
        presenter.showSnackError(CommonErrorType.ENROLLMENT_FIELD_VALIDATION, activity.getResources().getString(R.string.validate_check_details));
        presenter.showDetailError(CommonErrorType.ENROLLMENT_FIELD_VALIDATION, errMsg.toString());
        return;
    }
    try {
        AndroidCryptoProvider csr = new AndroidCryptoProvider(activity);
        String requestCSR = "";
        if (csr.getlCsr() != null) {
            requestCSR = URLEncoder.encode(csr.getlCsr(), "UTF-8");
        }
        MqttData cache = new MqttData(activity);
        String invitationToken = cache.getInvitationToken();
        JSONObject payload = new JSONObject();
        String mInventory = Helpers.base64encode(inventory.trim());
        // get first email
        payload.put("_email", arrEmails.get(0).getEmail());
        payload.put("_invitation_token", invitationToken);
        payload.put("_serial", Helpers.getDeviceSerial());
        payload.put("_uuid", new Hardware(activity).getUUID());
        payload.put("csr", requestCSR);
        payload.put("firstname", firstName);
        payload.put("lastname", lastName);
        payload.put("phone", phone);
        payload.put("version", BuildConfig.VERSION_NAME);
        payload.put("type", "android");
        payload.put("has_system_permission", Helpers.isSystemApp(activity));
        payload.put("inventory", mInventory);
        // could be mqtt or fcm
        payload.put("notification_type", "mqtt");
        // this is the token get from fcm register
        payload.put("notification_token", "");
        FlyveLog.d(mInventory);
        EnrollmentHelper enroll = new EnrollmentHelper(activity);
        enroll.enrollment(payload, new EnrollmentHelper.EnrollCallBack() {

            @Override
            public void onSuccess(String data) {
                // -------------------------------
                // Store user information
                // -------------------------------
                UserData userData = new UserData(activity);
                userData.setFirstName(firstName);
                userData.setLastName(lastName);
                userData.setEmails(arrEmails);
                userData.setPicture(photo);
                userData.setLanguage(language);
                userData.setAdministrativeNumber(administrativeNumber);
                // -------------------------------
                // Remove Deeplink information
                // -------------------------------
                MqttData cache = new MqttData(activity);
                cache.setInvitationToken("");
                presenter.enrollSuccess();
            }

            @Override
            public void onError(int type, String error) {
                presenter.showSnackError(type, error);
            }
        });
    } catch (Exception ex) {
        presenter.showSnackError(CommonErrorType.ENROLLMENT_REQUEST_EXCEPTION, ex.getMessage());
    }
}
Also used : MqttData(org.flyve.mdm.agent.data.database.MqttData) UserData(org.flyve.mdm.agent.data.localstorage.UserData) AndroidCryptoProvider(org.flyve.mdm.agent.security.AndroidCryptoProvider) JSONObject(org.json.JSONObject) Hardware(org.flyve.inventory.categories.Hardware)

Example 2 with AndroidCryptoProvider

use of org.flyve.mdm.agent.security.AndroidCryptoProvider in project android-mdm-agent by flyve-mdm.

the class EnrollmentHelper method createX509cert.

/**
 * Create X509 certificate
 */
public void createX509cert(final EnrollCallBack callback) {
    new Thread(new Runnable() {

        public void run() {
            try {
                AndroidCryptoProvider createCertificate = new AndroidCryptoProvider(context);
                createCertificate.generateRequest(new AndroidCryptoProvider.GenerateCallback() {

                    @Override
                    public void onGenerate(final boolean work) {
                        EnrollmentHelper.runOnUI(new Runnable() {

                            public void run() {
                                if (work) {
                                    callback.onSuccess("true");
                                }
                            }
                        });
                    }
                });
            } catch (Exception ex) {
                FlyveLog.e(this.getClass().getName() + ", createX509cert", ex.getMessage());
                EnrollmentHelper.runOnUI(new Runnable() {

                    public void run() {
                        callback.onError(CommonErrorType.ENROLLMENT_HELPER_X509CERTIFICATION, "false");
                    }
                });
            }
        }
    }).start();
}
Also used : AndroidCryptoProvider(org.flyve.mdm.agent.security.AndroidCryptoProvider)

Aggregations

AndroidCryptoProvider (org.flyve.mdm.agent.security.AndroidCryptoProvider)2 Hardware (org.flyve.inventory.categories.Hardware)1 MqttData (org.flyve.mdm.agent.data.database.MqttData)1 UserData (org.flyve.mdm.agent.data.localstorage.UserData)1 JSONObject (org.json.JSONObject)1