1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 this.EXPORTED_SYMBOLS = ["MobileIdentitySmsVerificationFlow"];
9 const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
11 Cu.import("resource://gre/modules/MobileIdentityCommon.jsm");
12 Cu.import("resource://gre/modules/MobileIdentityVerificationFlow.jsm");
13 Cu.import("resource://gre/modules/Promise.jsm");
14 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
17 XPCOMUtils.defineLazyServiceGetter(this, "smsService",
18 "@mozilla.org/sms/smsservice;1",
22 this.MobileIdentitySmsVerificationFlow = function(aVerificationOptions,
27 // SMS MT or SMS MO+MT specific verify strategy.
28 this.smsVerifyStrategy = aVerifyStrategy;
30 log.debug("aVerificationOptions ${}", aVerificationOptions);
31 MobileIdentityVerificationFlow.call(this, aVerificationOptions, aUI, aClient,
32 this._verifyStrategy, this._cleanupStrategy);
35 this.MobileIdentitySmsVerificationFlow.prototype = {
37 __proto__: MobileIdentityVerificationFlow.prototype,
39 observedSilentNumber: null,
43 _verifyStrategy: function() {
44 if (!this.smsVerifyStrategy) {
45 return Promise.reject(ERROR_INTERNAL_UNEXPECTED);
48 // Even if the user selection is given to us as a possible external phone
49 // number, it is also possible that the phone number introduced by the
50 // user belongs to one of the SIMs inserted in the device which MSISDN
51 // is unknown for us, so we always observe for incoming messages coming
52 // from the given mtSender.
55 this.observedSilentNumber = this.verificationOptions.mtSender;
57 smsService.addSilentNumber(this.observedSilentNumber);
59 log.warn("We are already listening for that number");
62 this.onSilentSms = (function(aSubject, aTopic, aData) {
63 log.debug("Got silent message " + aSubject.sender + " - " + aSubject.body);
64 // We might have observed a notification of an incoming silent message
65 // for other number. In that case, we just bail out.
66 if (aSubject.sender != this.observedSilentNumber) {
70 // We got the SMS containing the verification code.
72 // If the phone number we are trying to verify is or can be an external
73 // phone number (meaning that it doesn't belong to any of the inserted
74 // SIMs) we will be receiving an human readable SMS containing a short
75 // verification code. In this case we need to parse the SMS body to
76 // extract the verification code.
77 // Otherwise, we just use the whole SMS body as it should contain a long
79 let verificationCode = aSubject.body;
80 if (this.verificationOptions.external) {
81 // We just take the numerical characters from the body.
82 verificationCode = aSubject.body.replace(/[^0-9]/g,'');
85 log.debug("Verification code: " + verificationCode);
87 this.verificationCodeDeferred.resolve(verificationCode);
90 Services.obs.addObserver(this.onSilentSms,
91 SILENT_SMS_RECEIVED_TOPIC,
93 log.debug("Observing messages from " + this.observedSilentNumber);
96 return this.smsVerifyStrategy();
99 _cleanupStrategy: function() {
101 smsService.removeSilentNumber(this.observedSilentNumber);
102 Services.obs.removeObserver(this.onSilentSms,
103 SILENT_SMS_RECEIVED_TOPIC);
104 this.observedSilentNumber = null;
105 this.onSilentSms = null;