services/mobileid/MobileIdentitySmsMoMtVerificationFlow.jsm
author Armen Zambrano Gasparnian <armenzg@mozilla.com>
Tue, 17 Jun 2014 09:50:16 -0400
changeset 209909 c257e034da91f94e6b63b19a180816db8306d9f9
parent 206648 e3dc24f425734635f3f6d663d2339c4234027d86
child 218162 5af3884cea83e35eac396428cd9357f81ca7538b
permissions -rw-r--r--
Bug 989583 - Set self.testRoot and self.testRootAbs in only one place (runtests.py). r=jmaher

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

this.EXPORTED_SYMBOLS = ["MobileIdentitySmsMoMtVerificationFlow"];

const { classes: Cc, interfaces: Ci, utils: Cu } = Components;

Cu.import("resource://gre/modules/MobileIdentityCommon.jsm");
Cu.import("resource://gre/modules/MobileIdentitySmsVerificationFlow.jsm");
Cu.import("resource://gre/modules/Promise.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");

XPCOMUtils.defineLazyServiceGetter(this, "smsService",
                                   "@mozilla.org/sms/smsservice;1",
                                   "nsISmsService");

// In order to send messages through nsISmsService, we need to implement
// nsIMobileMessageCallback, as the WebSMS API implementation is not usable
// from JS.
function SilentSmsRequest(aDeferred) {
  this.deferred = aDeferred;
}
SilentSmsRequest.prototype = {
  QueryInterface: XPCOMUtils.generateQI([Ci.nsIMobileMessageCallback]),

  classID: Components.ID("{ff46f1a8-e040-4ff4-98a7-d5a5b86a2c3e}"),

  notifyMessageSent: function notifyMessageSent(aMessage) {
    log.debug("Silent message successfully sent");
    this.deferred.resolve(aMessage);
  },

  notifySendMessageFailed: function notifySendMessageFailed(aError) {
    log.error("Error sending silent message " + aError);
    this.deferred.reject(aError);
  }
};

this.MobileIdentitySmsMoMtVerificationFlow = function(aOrigin,
                                                      aServiceId,
                                                      aIccId,
                                                      aMtSender,
                                                      aMoVerifier,
                                                      aUI,
                                                      aClient) {

  log.debug("MobileIdentitySmsMoMtVerificationFlow");

  MobileIdentitySmsVerificationFlow.call(this,
                                         aOrigin,
                                         null, //msisdn
                                         aIccId,
                                         aServiceId,
                                         false, // external
                                         aMtSender,
                                         aMoVerifier,
                                         aUI,
                                         aClient,
                                         this.smsVerifyStrategy);
};

this.MobileIdentitySmsMoMtVerificationFlow.prototype = {

  __proto__: MobileIdentitySmsVerificationFlow.prototype,

  smsVerifyStrategy: function() {
    // In the MO+MT flow we need to send an SMS to the given moVerifier number
    // so the server can find out our phone number to send an SMS back with a
    // verification code.
    let deferred = Promise.defer();
    let silentSmsRequest = new SilentSmsRequest(deferred);

    // The MO SMS body that the server expects contains the API endpoint for
    // the MO verify request and the HAWK ID parameter derived via HKDF from
    // the session token. These parameters should go unnamed and space limited.
    let body = SMS_MO_MT_VERIFY + " " +
               this.client._deriveHawkCredentials(this.sessionToken).id;
    smsService.send(this.verificationOptions.serviceId,
                    this.verificationOptions.moVerifier,
                    body,
                    true, // silent
                    silentSmsRequest);
    log.debug("Sending " + body + " to " + this.verificationOptions.moVerifier);
    return deferred.promise;
  }
};