register.js 3.57 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, no-var, prefer-rest-params, wrap-iife, no-else-return, quotes, quote-props, comma-dangle, one-var, one-var-declaration-per-line, max-len */
2 3 4 5
/* global u2f */
/* global U2FError */
/* global U2FUtil */

6 7 8 9
// Register U2F (universal 2nd factor) devices for users to authenticate with.
//
// State Flow #1: setup -> in_progress -> registered -> POST to server
// State Flow #2: setup -> in_progress -> error -> setup
Fatih Acet's avatar
Fatih Acet committed
10
(function() {
11
  var bind = function(fn, me) { return function() { return fn.apply(me, arguments); }; };
Fatih Acet's avatar
Fatih Acet committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

  this.U2FRegister = (function() {
    function U2FRegister(container, u2fParams) {
      this.container = container;
      this.renderNotSupported = bind(this.renderNotSupported, this);
      this.renderRegistered = bind(this.renderRegistered, this);
      this.renderError = bind(this.renderError, this);
      this.renderInProgress = bind(this.renderInProgress, this);
      this.renderSetup = bind(this.renderSetup, this);
      this.renderTemplate = bind(this.renderTemplate, this);
      this.register = bind(this.register, this);
      this.start = bind(this.start, this);
      this.appId = u2fParams.app_id;
      this.registerRequests = u2fParams.register_requests;
      this.signRequests = u2fParams.sign_requests;
    }

    U2FRegister.prototype.start = function() {
      if (U2FUtil.isU2FSupported()) {
        return this.renderSetup();
      } else {
        return this.renderNotSupported();
      }
    };

    U2FRegister.prototype.register = function() {
      return u2f.register(this.appId, this.registerRequests, this.signRequests, (function(_this) {
        return function(response) {
          var error;
          if (response.errorCode) {
42
            error = new U2FError(response.errorCode, 'register');
Fatih Acet's avatar
Fatih Acet committed
43 44 45 46 47 48 49 50
            return _this.renderError(error);
          } else {
            return _this.renderRegistered(JSON.stringify(response));
          }
        };
      })(this), 10);
    };

51
    // Rendering #
Fatih Acet's avatar
Fatih Acet committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    U2FRegister.prototype.templates = {
      "notSupported": "#js-register-u2f-not-supported",
      "setup": '#js-register-u2f-setup',
      "inProgress": '#js-register-u2f-in-progress',
      "error": '#js-register-u2f-error',
      "registered": '#js-register-u2f-registered'
    };

    U2FRegister.prototype.renderTemplate = function(name, params) {
      var template, templateString;
      templateString = $(this.templates[name]).html();
      template = _.template(templateString);
      return this.container.html(template(params));
    };

    U2FRegister.prototype.renderSetup = function() {
      this.renderTemplate('setup');
      return this.container.find('#js-setup-u2f-device').on('click', this.renderInProgress);
    };

    U2FRegister.prototype.renderInProgress = function() {
      this.renderTemplate('inProgress');
      return this.register();
    };

    U2FRegister.prototype.renderError = function(error) {
      this.renderTemplate('error', {
79 80
        error_message: error.message(),
        error_code: error.errorCode
Fatih Acet's avatar
Fatih Acet committed
81 82 83 84 85 86
      });
      return this.container.find('#js-u2f-try-again').on('click', this.renderSetup);
    };

    U2FRegister.prototype.renderRegistered = function(deviceResponse) {
      this.renderTemplate('registered');
87 88
      // Prefer to do this instead of interpolating using Underscore templates
      // because of JSON escaping issues.
Fatih Acet's avatar
Fatih Acet committed
89 90 91 92 93 94 95 96 97 98
      return this.container.find("#js-device-response").val(deviceResponse);
    };

    U2FRegister.prototype.renderNotSupported = function() {
      return this.renderTemplate('notSupported');
    };

    return U2FRegister;
  })();
}).call(this);