Sha256: 34e695a57c2723e51291808f44db941294bcf1caeb25bc84bc7c22c28b42df9e

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

function FayeAuthentication(endpoint) {
  this._endpoint = endpoint || '/faye/auth';
  this._signatures = {};
}

FayeAuthentication.prototype.endpoint = function() {
  return (this._endpoint);
};

FayeAuthentication.prototype.signMessage = function(message, callback) {
  var channel = message.subscription || message.channel;
  var clientId = message.clientId;

  if (!this._signatures[clientId])
    this._signatures[clientId] = {};
  if (this._signatures[clientId][channel]) {
    this._signatures[clientId][channel].then(function(signature) {
      message.signature = signature;
      callback(message);
    });
  } else {
    var self = this;
    self._signatures[clientId][channel] = new Faye.Promise(function(success, failure) {
      $.post(self.endpoint(), {message: {channel: channel, clientId: clientId}}, function(response) {
        success(response.signature);
      }, 'json').fail(function(xhr, textStatus, e) {
        success(null);
      });
    });
    self._signatures[clientId][channel].then(function(signature) {
      message.signature = signature;
      callback(message);
    });
  }
}

FayeAuthentication.prototype.outgoing = function(message, callback) {
  if (message.channel === '/meta/subscribe') {
    this.signMessage(message, callback);
  }
  else if (/^\/meta\/(.*)/.exec(message.channel) === null) { // Publish
    this.signMessage(message, callback);
  }
  else
    callback(message);
};

FayeAuthentication.prototype.incoming = function(message, callback) {
  if (message.error === 'Invalid signature')
    this._signatures = {};
  callback(message);
};

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
faye-authentication-0.1.0 app/assets/javascripts/faye-authentication.js