PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/ember-simple-auth/0.6.3/simple-auth-devise.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 322 lines | 175 code | 32 blank | 115 comment | 23 complexity | 171304ed28c37ee7f9c3be523e046e39 MD5 | raw file
  1. (function(global) {
  2. var define, requireModule;
  3. (function() {
  4. var registry = {}, seen = {};
  5. define = function(name, deps, callback) {
  6. registry[name] = { deps: deps, callback: callback };
  7. };
  8. requireModule = function(name) {
  9. if (seen.hasOwnProperty(name)) { return seen[name]; }
  10. seen[name] = {};
  11. if (!registry[name]) {
  12. throw new Error("Could not find module " + name);
  13. }
  14. var mod = registry[name],
  15. deps = mod.deps,
  16. callback = mod.callback,
  17. reified = [],
  18. exports;
  19. for (var i=0, l=deps.length; i<l; i++) {
  20. if (deps[i] === 'exports') {
  21. reified.push(exports = {});
  22. } else {
  23. reified.push(requireModule(resolve(deps[i])));
  24. }
  25. }
  26. var value = callback.apply(this, reified);
  27. return seen[name] = exports || value;
  28. function resolve(child) {
  29. if (child.charAt(0) !== '.') { return child; }
  30. var parts = child.split("/");
  31. var parentBase = name.split("/").slice(0, -1);
  32. for (var i=0, l=parts.length; i<l; i++) {
  33. var part = parts[i];
  34. if (part === '..') { parentBase.pop(); }
  35. else if (part === '.') { continue; }
  36. else { parentBase.push(part); }
  37. }
  38. return parentBase.join("/");
  39. }
  40. };
  41. requireModule.registry = registry;
  42. })();
  43. define("simple-auth-devise/authenticators/devise",
  44. ["simple-auth/authenticators/base","simple-auth/utils/is-secure-url","simple-auth/utils/get-global-config","exports"],
  45. function(__dependency1__, __dependency2__, __dependency3__, __exports__) {
  46. "use strict";
  47. var Base = __dependency1__["default"];
  48. var isSecureUrl = __dependency2__["default"];
  49. var getGlobalConfig = __dependency3__["default"];
  50. var global = (typeof window !== 'undefined') ? window : {},
  51. Ember = global.Ember;
  52. /**
  53. Authenticator that works with the Ruby gem
  54. [Devise](https://github.com/plataformatec/devise).
  55. __As token authentication is not actually part of devise anymore, the server
  56. needs to implement some customizations__ to work with this authenticator -
  57. see the README and
  58. [discussion here](https://gist.github.com/josevalim/fb706b1e933ef01e4fb6).
  59. _The factory for this authenticator is registered as
  60. `'simple-auth-authenticator:devise'` in Ember's container._
  61. @class Devise
  62. @namespace SimpleAuth.Authenticators
  63. @module simple-auth-devise/authenticators/devise
  64. @extends Base
  65. */
  66. __exports__["default"] = Base.extend({
  67. /**
  68. The endpoint on the server the authenticator acquires the auth token
  69. and email from.
  70. This value can be configured via the global environment object:
  71. ```js
  72. window.ENV = window.ENV || {};
  73. window.ENV['simple-auth-devise'] = {
  74. serverTokenEndpoint: '/some/other/endpoint'
  75. }
  76. ```
  77. @property serverTokenEndpoint
  78. @type String
  79. @default '/users/sign_in'
  80. */
  81. serverTokenEndpoint: '/users/sign_in',
  82. /**
  83. The devise resource name
  84. This value can be configured via the global environment object:
  85. ```js
  86. window.ENV = window.ENV || {};
  87. window.ENV['simple-auth-devise'] = {
  88. resourceName: 'account'
  89. }
  90. ```
  91. @property resourceName
  92. @type String
  93. @default 'user'
  94. */
  95. resourceName: 'user',
  96. /**
  97. @method init
  98. @private
  99. */
  100. init: function() {
  101. var globalConfig = getGlobalConfig('simple-auth-devise');
  102. this.serverTokenEndpoint = globalConfig.serverTokenEndpoint || this.serverTokenEndpoint;
  103. this.resourceName = globalConfig.resourceName || this.resourceName;
  104. },
  105. /**
  106. Restores the session from a set of session properties; __will return a
  107. resolving promise when there's a non-empty `user_token` and a non-empty
  108. `user_email` in the `properties`__ and a rejecting promise otherwise.
  109. @method restore
  110. @param {Object} properties The properties to restore the session from
  111. @return {Ember.RSVP.Promise} A promise that when it resolves results in the session being authenticated
  112. */
  113. restore: function(properties) {
  114. return new Ember.RSVP.Promise(function(resolve, reject) {
  115. if (!Ember.isEmpty(properties.user_token) && !Ember.isEmpty(properties.user_email)) {
  116. resolve(properties);
  117. } else {
  118. reject();
  119. }
  120. });
  121. },
  122. /**
  123. Authenticates the session with the specified `credentials`; the credentials
  124. are `POST`ed to the
  125. [`Authenticators.Devise#serverTokenEndpoint`](#SimpleAuth-Authenticators-Devise-serverTokenEndpoint)
  126. and if they are valid the server returns an auth token and email in
  127. response. __If the credentials are valid and authentication succeeds, a
  128. promise that resolves with the server's response is returned__, otherwise a
  129. promise that rejects with the server error is returned.
  130. @method authenticate
  131. @param {Object} options The credentials to authenticate the session with
  132. @return {Ember.RSVP.Promise} A promise that resolves when an auth token and email is successfully acquired from the server and rejects otherwise
  133. */
  134. authenticate: function(credentials) {
  135. var _this = this;
  136. return new Ember.RSVP.Promise(function(resolve, reject) {
  137. var data = {};
  138. data[_this.resourceName] = {
  139. email: credentials.identification,
  140. password: credentials.password
  141. };
  142. _this.makeRequest(data).then(function(response) {
  143. Ember.run(function() {
  144. resolve(response);
  145. });
  146. }, function(xhr, status, error) {
  147. Ember.run(function() {
  148. reject(xhr.responseJSON || xhr.responseText);
  149. });
  150. });
  151. });
  152. },
  153. /**
  154. Does nothing
  155. @method invalidate
  156. @return {Ember.RSVP.Promise} A resolving promise
  157. */
  158. invalidate: function() {
  159. return Ember.RSVP.resolve();
  160. },
  161. /**
  162. @method makeRequest
  163. @private
  164. */
  165. makeRequest: function(data, resolve, reject) {
  166. if (!isSecureUrl(this.serverTokenEndpoint)) {
  167. Ember.Logger.warn('Credentials are transmitted via an insecure connection - use HTTPS to keep them secure.');
  168. }
  169. return Ember.$.ajax({
  170. url: this.serverTokenEndpoint,
  171. type: 'POST',
  172. data: data,
  173. dataType: 'json',
  174. beforeSend: function(xhr, settings) {
  175. xhr.setRequestHeader('Accept', settings.accepts.json);
  176. }
  177. });
  178. }
  179. });
  180. });
  181. define("simple-auth-devise/authorizers/devise",
  182. ["simple-auth/authorizers/base","simple-auth/utils/is-secure-url","exports"],
  183. function(__dependency1__, __dependency2__, __exports__) {
  184. "use strict";
  185. var Base = __dependency1__["default"];
  186. var isSecureUrl = __dependency2__["default"];
  187. var global = (typeof window !== 'undefined') ? window : {},
  188. Ember = global.Ember;
  189. /**
  190. Authenticator that works with the Ruby gem
  191. [Devise](https://github.com/plataformatec/devise) by sending the `user_token`
  192. and `user_email` properties from the session in the `Authorization` header.
  193. __As token authentication is not actually part of devise anymore, the server
  194. needs to implement some customizations__ to work with this authenticator -
  195. see the README for more information.
  196. _The factory for this authorizer is registered as
  197. `'simple-auth-authorizer:devise'` in Ember's container._
  198. @class Devise
  199. @namespace SimpleAuth.Authorizers
  200. @module simple-auth-devise/authorizers/devise
  201. @extends Base
  202. */
  203. __exports__["default"] = Base.extend({
  204. /**
  205. Authorizes an XHR request by sending the `user_token` and `user_email`
  206. properties from the session in the `Authorization` header:
  207. ```
  208. Authorization: Token token="<user_token>", user_email="<user_email>"
  209. ```
  210. @method authorize
  211. @param {jqXHR} jqXHR The XHR request to authorize (see http://api.jquery.com/jQuery.ajax/#jqXHR)
  212. @param {Object} requestOptions The options as provided to the `$.ajax` method (see http://api.jquery.com/jQuery.ajaxPrefilter/)
  213. */
  214. authorize: function(jqXHR, requestOptions) {
  215. var userToken = this.get('session.user_token');
  216. var userEmail = this.get('session.user_email');
  217. if (this.get('session.isAuthenticated') && !Ember.isEmpty(userToken) && !Ember.isEmpty(userEmail)) {
  218. if (!isSecureUrl(requestOptions.url)) {
  219. Ember.Logger.warn('Credentials are transmitted via an insecure connection - use HTTPS to keep them secure.');
  220. }
  221. var authData = 'token="' + userToken + '", user_email="' + userEmail + '"';
  222. jqXHR.setRequestHeader('Authorization', 'Token ' + authData);
  223. }
  224. }
  225. });
  226. });
  227. define("simple-auth-devise/ember",
  228. ["./initializer"],
  229. function(__dependency1__) {
  230. "use strict";
  231. var global = (typeof window !== 'undefined') ? window : {},
  232. Ember = global.Ember;
  233. var initializer = __dependency1__["default"];
  234. Ember.onLoad('Ember.Application', function(Application) {
  235. Application.initializer(initializer);
  236. });
  237. });
  238. define("simple-auth-devise/initializer",
  239. ["simple-auth-devise/authenticators/devise","simple-auth-devise/authorizers/devise","exports"],
  240. function(__dependency1__, __dependency2__, __exports__) {
  241. "use strict";
  242. var global = (typeof window !== 'undefined') ? window : {},
  243. Ember = global.Ember;
  244. var Authenticator = __dependency1__["default"];
  245. var Authorizer = __dependency2__["default"];
  246. __exports__["default"] = {
  247. name: 'simple-auth-devise',
  248. before: 'simple-auth',
  249. initialize: function(container, application) {
  250. container.register('simple-auth-authorizer:devise', Authorizer);
  251. container.register('simple-auth-authenticator:devise', Authenticator);
  252. }
  253. };
  254. });
  255. define('simple-auth/authenticators/base', ['exports'], function(__exports__) {
  256. __exports__['default'] = global.SimpleAuth.Authenticators.Base;
  257. });
  258. define('simple-auth/authorizers/base', ['exports'], function(__exports__) {
  259. __exports__['default'] = global.SimpleAuth.Authorizers.Base;
  260. });
  261. define('simple-auth/utils/is-secure-url', ['exports'], function(__exports__) {
  262. __exports__['default'] = global.SimpleAuth.Utils.isSecureUrl;
  263. });
  264. define('simple-auth/utils/get-global-config', ['exports'], function(__exports__) {
  265. __exports__['default'] = global.SimpleAuth.Utils.getGlobalConfig;
  266. });
  267. var initializer = requireModule('simple-auth-devise/initializer').default;
  268. var Authenticator = requireModule('simple-auth-devise/authenticators/devise').default;
  269. var Authorizer = requireModule('simple-auth-devise/authorizers/devise').default;
  270. global.SimpleAuth.Authenticators.Devise = Authenticator;
  271. global.SimpleAuth.Authorizers.Devise = Authorizer;
  272. requireModule('simple-auth-devise/ember');
  273. })((typeof global !== 'undefined') ? global : window);