services/fxaccounts/FxAccountsCommon.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/fxaccounts/FxAccountsCommon.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,176 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const { interfaces: Ci, utils: Cu } = Components;
     1.9 +
    1.10 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.11 +Cu.import("resource://gre/modules/Services.jsm");
    1.12 +Cu.import("resource://gre/modules/Log.jsm");
    1.13 +
    1.14 +// loglevel should be one of "Fatal", "Error", "Warn", "Info", "Config",
    1.15 +// "Debug", "Trace" or "All". If none is specified, "Debug" will be used by
    1.16 +// default.  Note "Debug" is usually appropriate so that when this log is
    1.17 +// included in the Sync file logs we get verbose output.
    1.18 +const PREF_LOG_LEVEL = "identity.fxaccounts.loglevel";
    1.19 +// The level of messages that will be dumped to the console.  If not specified,
    1.20 +// "Error" will be used.
    1.21 +const PREF_LOG_LEVEL_DUMP = "identity.fxaccounts.log.appender.dump";
    1.22 +
    1.23 +// A pref that can be set so "sensitive" information (eg, personally
    1.24 +// identifiable info, credentials, etc) will be logged.
    1.25 +const PREF_LOG_SENSITIVE_DETAILS = "identity.fxaccounts.log.sensitive";
    1.26 +
    1.27 +XPCOMUtils.defineLazyGetter(this, 'log', function() {
    1.28 +  let log = Log.repository.getLogger("FirefoxAccounts");
    1.29 +  // We set the log level to debug, but the default dump appender is set to
    1.30 +  // the level reflected in the pref.  Other code that consumes FxA may then
    1.31 +  // choose to add another appender at a different level.
    1.32 +  log.level = Log.Level.Debug;
    1.33 +  let appender = new Log.DumpAppender();
    1.34 +  appender.level = Log.Level.Error;
    1.35 +
    1.36 +  log.addAppender(appender);
    1.37 +  try {
    1.38 +    // The log itself.
    1.39 +    let level =
    1.40 +      Services.prefs.getPrefType(PREF_LOG_LEVEL) == Ci.nsIPrefBranch.PREF_STRING
    1.41 +      && Services.prefs.getCharPref(PREF_LOG_LEVEL);
    1.42 +    log.level = Log.Level[level] || Log.Level.Debug;
    1.43 +
    1.44 +    // The appender.
    1.45 +    level =
    1.46 +      Services.prefs.getPrefType(PREF_LOG_LEVEL_DUMP) == Ci.nsIPrefBranch.PREF_STRING
    1.47 +      && Services.prefs.getCharPref(PREF_LOG_LEVEL_DUMP);
    1.48 +    appender.level = Log.Level[level] || Log.Level.Error;
    1.49 +  } catch (e) {
    1.50 +    log.error(e);
    1.51 +  }
    1.52 +
    1.53 +  return log;
    1.54 +});
    1.55 +
    1.56 +// A boolean to indicate if personally identifiable information (or anything
    1.57 +// else sensitive, such as credentials) should be logged.
    1.58 +XPCOMUtils.defineLazyGetter(this, 'logPII', function() {
    1.59 +  try {
    1.60 +    return Services.prefs.getBoolPref(PREF_LOG_SENSITIVE_DETAILS);
    1.61 +  } catch (_) {
    1.62 +    return false;
    1.63 +  }
    1.64 +});
    1.65 +
    1.66 +this.DATA_FORMAT_VERSION = 1;
    1.67 +this.DEFAULT_STORAGE_FILENAME = "signedInUser.json";
    1.68 +
    1.69 +// Token life times.
    1.70 +// Having this parameter be short has limited security value and can cause
    1.71 +// spurious authentication values if the client's clock is skewed and
    1.72 +// we fail to adjust. See Bug 983256.
    1.73 +this.ASSERTION_LIFETIME = 1000 * 3600 * 24 * 365 * 25; // 25 years
    1.74 +// This is a time period we want to guarantee that the assertion will be
    1.75 +// valid after we generate it (e.g., the signed cert won't expire in this
    1.76 +// period).
    1.77 +this.ASSERTION_USE_PERIOD = 1000 * 60 * 5; // 5 minutes
    1.78 +this.CERT_LIFETIME      = 1000 * 3600 * 6;  // 6 hours
    1.79 +this.KEY_LIFETIME       = 1000 * 3600 * 12; // 12 hours
    1.80 +
    1.81 +// Polling timings.
    1.82 +this.POLL_SESSION       = 1000 * 60 * 5;    // 5 minutes
    1.83 +this.POLL_STEP          = 1000 * 3;         // 3 seconds
    1.84 +
    1.85 +// Observer notifications.
    1.86 +this.ONLOGIN_NOTIFICATION = "fxaccounts:onlogin";
    1.87 +this.ONVERIFIED_NOTIFICATION = "fxaccounts:onverified";
    1.88 +this.ONLOGOUT_NOTIFICATION = "fxaccounts:onlogout";
    1.89 +
    1.90 +// UI Requests.
    1.91 +this.UI_REQUEST_SIGN_IN_FLOW = "signInFlow";
    1.92 +this.UI_REQUEST_REFRESH_AUTH = "refreshAuthentication";
    1.93 +
    1.94 +// Server errno.
    1.95 +// From https://github.com/mozilla/fxa-auth-server/blob/master/docs/api.md#response-format
    1.96 +this.ERRNO_ACCOUNT_ALREADY_EXISTS         = 101;
    1.97 +this.ERRNO_ACCOUNT_DOES_NOT_EXIST         = 102;
    1.98 +this.ERRNO_INCORRECT_PASSWORD             = 103;
    1.99 +this.ERRNO_UNVERIFIED_ACCOUNT             = 104;
   1.100 +this.ERRNO_INVALID_VERIFICATION_CODE      = 105;
   1.101 +this.ERRNO_NOT_VALID_JSON_BODY            = 106;
   1.102 +this.ERRNO_INVALID_BODY_PARAMETERS        = 107;
   1.103 +this.ERRNO_MISSING_BODY_PARAMETERS        = 108;
   1.104 +this.ERRNO_INVALID_REQUEST_SIGNATURE      = 109;
   1.105 +this.ERRNO_INVALID_AUTH_TOKEN             = 110;
   1.106 +this.ERRNO_INVALID_AUTH_TIMESTAMP         = 111;
   1.107 +this.ERRNO_MISSING_CONTENT_LENGTH         = 112;
   1.108 +this.ERRNO_REQUEST_BODY_TOO_LARGE         = 113;
   1.109 +this.ERRNO_TOO_MANY_CLIENT_REQUESTS       = 114;
   1.110 +this.ERRNO_INVALID_AUTH_NONCE             = 115;
   1.111 +this.ERRNO_ENDPOINT_NO_LONGER_SUPPORTED   = 116;
   1.112 +this.ERRNO_INCORRECT_LOGIN_METHOD         = 117;
   1.113 +this.ERRNO_INCORRECT_KEY_RETRIEVAL_METHOD = 118;
   1.114 +this.ERRNO_INCORRECT_API_VERSION          = 119;
   1.115 +this.ERRNO_INCORRECT_EMAIL_CASE           = 120;
   1.116 +this.ERRNO_SERVICE_TEMP_UNAVAILABLE       = 201;
   1.117 +this.ERRNO_UNKNOWN_ERROR                  = 999;
   1.118 +
   1.119 +// Errors.
   1.120 +this.ERROR_ACCOUNT_ALREADY_EXISTS         = "ACCOUNT_ALREADY_EXISTS";
   1.121 +this.ERROR_ACCOUNT_DOES_NOT_EXIST         = "ACCOUNT_DOES_NOT_EXIST ";
   1.122 +this.ERROR_ALREADY_SIGNED_IN_USER         = "ALREADY_SIGNED_IN_USER";
   1.123 +this.ERROR_ENDPOINT_NO_LONGER_SUPPORTED   = "ENDPOINT_NO_LONGER_SUPPORTED";
   1.124 +this.ERROR_INCORRECT_API_VERSION          = "INCORRECT_API_VERSION";
   1.125 +this.ERROR_INCORRECT_EMAIL_CASE           = "INCORRECT_EMAIL_CASE";
   1.126 +this.ERROR_INCORRECT_KEY_RETRIEVAL_METHOD = "INCORRECT_KEY_RETRIEVAL_METHOD";
   1.127 +this.ERROR_INCORRECT_LOGIN_METHOD         = "INCORRECT_LOGIN_METHOD";
   1.128 +this.ERROR_INVALID_ACCOUNTID              = "INVALID_ACCOUNTID";
   1.129 +this.ERROR_INVALID_AUDIENCE               = "INVALID_AUDIENCE";
   1.130 +this.ERROR_INVALID_AUTH_TOKEN             = "INVALID_AUTH_TOKEN";
   1.131 +this.ERROR_INVALID_AUTH_TIMESTAMP         = "INVALID_AUTH_TIMESTAMP";
   1.132 +this.ERROR_INVALID_AUTH_NONCE             = "INVALID_AUTH_NONCE";
   1.133 +this.ERROR_INVALID_BODY_PARAMETERS        = "INVALID_BODY_PARAMETERS";
   1.134 +this.ERROR_INVALID_PASSWORD               = "INVALID_PASSWORD";
   1.135 +this.ERROR_INVALID_VERIFICATION_CODE      = "INVALID_VERIFICATION_CODE";
   1.136 +this.ERROR_INVALID_REFRESH_AUTH_VALUE     = "INVALID_REFRESH_AUTH_VALUE";
   1.137 +this.ERROR_INVALID_REQUEST_SIGNATURE      = "INVALID_REQUEST_SIGNATURE";
   1.138 +this.ERROR_INTERNAL_INVALID_USER          = "INTERNAL_ERROR_INVALID_USER";
   1.139 +this.ERROR_MISSING_BODY_PARAMETERS        = "MISSING_BODY_PARAMETERS";
   1.140 +this.ERROR_MISSING_CONTENT_LENGTH         = "MISSING_CONTENT_LENGTH";
   1.141 +this.ERROR_NO_TOKEN_SESSION               = "NO_TOKEN_SESSION";
   1.142 +this.ERROR_NOT_VALID_JSON_BODY            = "NOT_VALID_JSON_BODY";
   1.143 +this.ERROR_OFFLINE                        = "OFFLINE";
   1.144 +this.ERROR_REQUEST_BODY_TOO_LARGE         = "REQUEST_BODY_TOO_LARGE";
   1.145 +this.ERROR_SERVER_ERROR                   = "SERVER_ERROR";
   1.146 +this.ERROR_TOO_MANY_CLIENT_REQUESTS       = "TOO_MANY_CLIENT_REQUESTS";
   1.147 +this.ERROR_SERVICE_TEMP_UNAVAILABLE       = "SERVICE_TEMPORARY_UNAVAILABLE";
   1.148 +this.ERROR_UI_ERROR                       = "UI_ERROR";
   1.149 +this.ERROR_UI_REQUEST                     = "UI_REQUEST";
   1.150 +this.ERROR_UNKNOWN                        = "UNKNOWN_ERROR";
   1.151 +this.ERROR_UNVERIFIED_ACCOUNT             = "UNVERIFIED_ACCOUNT";
   1.152 +
   1.153 +// Error matching.
   1.154 +this.SERVER_ERRNO_TO_ERROR = {};
   1.155 +SERVER_ERRNO_TO_ERROR[ERRNO_ACCOUNT_ALREADY_EXISTS]         = ERROR_ACCOUNT_ALREADY_EXISTS;
   1.156 +SERVER_ERRNO_TO_ERROR[ERRNO_ACCOUNT_DOES_NOT_EXIST]         = ERROR_ACCOUNT_DOES_NOT_EXIST;
   1.157 +SERVER_ERRNO_TO_ERROR[ERRNO_INCORRECT_PASSWORD]             = ERROR_INVALID_PASSWORD;
   1.158 +SERVER_ERRNO_TO_ERROR[ERRNO_UNVERIFIED_ACCOUNT]             = ERROR_UNVERIFIED_ACCOUNT;
   1.159 +SERVER_ERRNO_TO_ERROR[ERRNO_INVALID_VERIFICATION_CODE]      = ERROR_INVALID_VERIFICATION_CODE;
   1.160 +SERVER_ERRNO_TO_ERROR[ERRNO_NOT_VALID_JSON_BODY]            = ERROR_NOT_VALID_JSON_BODY;
   1.161 +SERVER_ERRNO_TO_ERROR[ERRNO_INVALID_BODY_PARAMETERS]        = ERROR_INVALID_BODY_PARAMETERS;
   1.162 +SERVER_ERRNO_TO_ERROR[ERRNO_MISSING_BODY_PARAMETERS]        = ERROR_MISSING_BODY_PARAMETERS;
   1.163 +SERVER_ERRNO_TO_ERROR[ERRNO_INVALID_REQUEST_SIGNATURE]      = ERROR_INVALID_REQUEST_SIGNATURE;
   1.164 +SERVER_ERRNO_TO_ERROR[ERRNO_INVALID_AUTH_TOKEN]             = ERROR_INVALID_AUTH_TOKEN;
   1.165 +SERVER_ERRNO_TO_ERROR[ERRNO_INVALID_AUTH_TIMESTAMP]         = ERROR_INVALID_AUTH_TIMESTAMP;
   1.166 +SERVER_ERRNO_TO_ERROR[ERRNO_MISSING_CONTENT_LENGTH]         = ERROR_MISSING_CONTENT_LENGTH;
   1.167 +SERVER_ERRNO_TO_ERROR[ERRNO_REQUEST_BODY_TOO_LARGE]         = ERROR_REQUEST_BODY_TOO_LARGE;
   1.168 +SERVER_ERRNO_TO_ERROR[ERRNO_TOO_MANY_CLIENT_REQUESTS]       = ERROR_TOO_MANY_CLIENT_REQUESTS;
   1.169 +SERVER_ERRNO_TO_ERROR[ERRNO_INVALID_AUTH_NONCE]             = ERROR_INVALID_AUTH_NONCE;
   1.170 +SERVER_ERRNO_TO_ERROR[ERRNO_ENDPOINT_NO_LONGER_SUPPORTED]   = ERROR_ENDPOINT_NO_LONGER_SUPPORTED;
   1.171 +SERVER_ERRNO_TO_ERROR[ERRNO_INCORRECT_LOGIN_METHOD]         = ERROR_INCORRECT_LOGIN_METHOD;
   1.172 +SERVER_ERRNO_TO_ERROR[ERRNO_INCORRECT_KEY_RETRIEVAL_METHOD] = ERROR_INCORRECT_KEY_RETRIEVAL_METHOD;
   1.173 +SERVER_ERRNO_TO_ERROR[ERRNO_INCORRECT_API_VERSION]          = ERROR_INCORRECT_API_VERSION;
   1.174 +SERVER_ERRNO_TO_ERROR[ERRNO_INCORRECT_EMAIL_CASE]           = ERROR_INCORRECT_EMAIL_CASE;
   1.175 +SERVER_ERRNO_TO_ERROR[ERRNO_SERVICE_TEMP_UNAVAILABLE]       = ERROR_SERVICE_TEMP_UNAVAILABLE;
   1.176 +SERVER_ERRNO_TO_ERROR[ERRNO_UNKNOWN_ERROR]                  = ERROR_UNKNOWN;
   1.177 +
   1.178 +// Allow this file to be imported via Components.utils.import().
   1.179 +this.EXPORTED_SYMBOLS = Object.keys(this);

mercurial