ember_simple_auth-rails 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cfe2808004d5666ba68ebd9d4633afe78581d359
4
- data.tar.gz: dd554bd70fbf7be5edda64d7058792b1a732a063
3
+ metadata.gz: 022467e239fda4e5f6fa40982184389608076632
4
+ data.tar.gz: 5da86ed66985de41be4ba84ecff855235720f901
5
5
  SHA512:
6
- metadata.gz: 39ad83ffe1e8291c137d2269dbe8136e2a446748a42d3f5a72bd589c45e200937ed67a00d78deb78542011aefb6d29643ae8695f637226c5775d5d746c3c51c8
7
- data.tar.gz: 73fdb259e29c3e0066522affda571a2d35a56f539ac9b4b1b6884aafd6523e0350707476c33f37da3e4c248e1c9b2bb03a31e5c8486f6e3ddafce35ed02acf21
6
+ metadata.gz: d1f7f97d50614589d883b8d879e046ec5a6d9895292da0f9bd5f55d8dbd70ec88a57e07cde23d39a9ed026a785c100d254e519c78f77db6e84972e444a886ad5
7
+ data.tar.gz: b00e6ce324c7384c4114d24e5c85b661ff2304628ba1e1b48696f666d868ec4fe51fbd2c220cf4f427627b704359c0c78e550d517d8b49d0de35966726ef7cea
data/README.md CHANGED
@@ -18,7 +18,22 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ require the assets as you always do.
22
+
23
+ #### >= 0.3.0
24
+
25
+ ```
26
+ # require "ember-simple-auth"
27
+ # require "ember-simple-auth-cookie-store"
28
+ # require "ember-simple-auth-devise"
29
+ # require "ember-simple-auth-oauth2"
30
+ ```
31
+
32
+ #### < 0.3.0
33
+
34
+ ```
35
+ # require "ember-simple-auth"
36
+ ```
22
37
 
23
38
  ## Contributing
24
39
 
@@ -1,5 +1,5 @@
1
1
  module EmberSimpleAuth
2
2
  module Rails
3
- VERSION = "0.2.1"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -0,0 +1,225 @@
1
+ (function(global) {
2
+
3
+ var define, requireModule;
4
+
5
+ (function() {
6
+ var registry = {}, seen = {};
7
+
8
+ define = function(name, deps, callback) {
9
+ registry[name] = { deps: deps, callback: callback };
10
+ };
11
+
12
+ requireModule = function(name) {
13
+ if (seen.hasOwnProperty(name)) { return seen[name]; }
14
+ seen[name] = {};
15
+
16
+ if (!registry[name]) {
17
+ throw new Error("Could not find module " + name);
18
+ }
19
+
20
+ var mod = registry[name],
21
+ deps = mod.deps,
22
+ callback = mod.callback,
23
+ reified = [],
24
+ exports;
25
+
26
+ for (var i=0, l=deps.length; i<l; i++) {
27
+ if (deps[i] === 'exports') {
28
+ reified.push(exports = {});
29
+ } else {
30
+ reified.push(requireModule(resolve(deps[i])));
31
+ }
32
+ }
33
+
34
+ var value = callback.apply(this, reified);
35
+ return seen[name] = exports || value;
36
+
37
+ function resolve(child) {
38
+ if (child.charAt(0) !== '.') { return child; }
39
+ var parts = child.split("/");
40
+ var parentBase = name.split("/").slice(0, -1);
41
+
42
+ for (var i=0, l=parts.length; i<l; i++) {
43
+ var part = parts[i];
44
+
45
+ if (part === '..') { parentBase.pop(); }
46
+ else if (part === '.') { continue; }
47
+ else { parentBase.push(part); }
48
+ }
49
+
50
+ return parentBase.join("/");
51
+ }
52
+ };
53
+
54
+ requireModule.registry = registry;
55
+ })();
56
+
57
+ define("ember-simple-auth-cookie-store",
58
+ ["./ember-simple-auth-cookie-store/stores/cookie","exports"],
59
+ function(__dependency1__, __exports__) {
60
+ "use strict";
61
+ var Cookie = __dependency1__.Cookie;
62
+
63
+ __exports__["default"] = Cookie;
64
+ });
65
+ define("ember-simple-auth-cookie-store/stores/cookie",
66
+ ["exports"],
67
+ function(__exports__) {
68
+ "use strict";
69
+ var global = (typeof window !== 'undefined') ? window : {},
70
+ Ember = global.Ember;
71
+
72
+ /**
73
+ Store that saves its data in session cookies.
74
+
75
+ __In order to keep multiple tabs/windows of an application in sync, this
76
+ store has to periodically (every 500ms) check the cookies__ for changes as
77
+ there are no events that notify of changes in cookies. The recommended
78
+ alternative is `Ember.SimpleAuth.Stores.LocalStorage` that also persistently
79
+ stores data but instead of cookies relies on the `localStorage` API and does
80
+ not need to poll for external changes.
81
+
82
+ This store will trigger the `'updated'` event when any of its cookies is
83
+ changed from another tab or window.
84
+
85
+ _The factory for this store is registered as `'session-store:cookie'` in
86
+ Ember's container.
87
+
88
+ @class Cookie
89
+ @namespace Stores
90
+ @extends Stores.Base
91
+ */
92
+ var Cookie = Ember.SimpleAuth.Stores.Base.extend({
93
+ /**
94
+ The prefix to use for the store's cookie names so they can be distinguished
95
+ from other cookies.
96
+
97
+ @property cookieNamePrefix
98
+ @type String
99
+ @default 'ember_simple_auth:'
100
+ */
101
+ cookieNamePrefix: 'ember_simple_auth:',
102
+
103
+ /**
104
+ @property _secureCookies
105
+ @private
106
+ */
107
+ _secureCookies: window.location.protocol === 'https:',
108
+
109
+ /**
110
+ @property _syncDataTimeout
111
+ @private
112
+ */
113
+ _syncDataTimeout: null,
114
+
115
+ /**
116
+ @method init
117
+ @private
118
+ */
119
+ init: function() {
120
+ this.syncData();
121
+ },
122
+
123
+ /**
124
+ Persists the `data` in session cookies.
125
+
126
+ @method persist
127
+ @param {Object} data The data to persist
128
+ */
129
+ persist: function(data) {
130
+ for (var property in data) {
131
+ this.write(property, data[property], null);
132
+ }
133
+ this._lastData = this.restore();
134
+ },
135
+
136
+ /**
137
+ Restores all data currently saved in the session cookies identified by the
138
+ `cookieNamePrefix` (see
139
+ [Ember.SimpleAuth.Stores.Cookie#cookieNamePrefix](Ember-SimpleAuth-Stores-Cookie-cookieNamePrefix))
140
+ as a plain object.
141
+
142
+ @method restore
143
+ @return {Object} All data currently persisted in the session cookies
144
+ */
145
+ restore: function() {
146
+ var _this = this;
147
+ var data = {};
148
+ this.knownCookies().forEach(function(cookie) {
149
+ data[cookie] = _this.read(cookie);
150
+ });
151
+ return data;
152
+ },
153
+
154
+ /**
155
+ Clears the store by deleting all session cookies prefixed with the
156
+ `cookieNamePrefix` (see
157
+ [Ember.SimpleAuth.Stores.Cookie#cookieNamePrefix](Ember-SimpleAuth-Stores-Cookie-cookieNamePrefix)).
158
+
159
+ @method clear
160
+ */
161
+ clear: function() {
162
+ var _this = this;
163
+ this.knownCookies().forEach(function(cookie) {
164
+ _this.write(cookie, null, (new Date(0)).toGMTString());
165
+ });
166
+ this._lastData = null;
167
+ },
168
+
169
+ /**
170
+ @method read
171
+ @private
172
+ */
173
+ read: function(name) {
174
+ var value = document.cookie.match(new RegExp(this.cookieNamePrefix + name + '=([^;]+)')) || [];
175
+ return decodeURIComponent(value[1] || '');
176
+ },
177
+
178
+ /**
179
+ @method write
180
+ @private
181
+ */
182
+ write: function(name, value, expiration) {
183
+ var expires = Ember.isEmpty(expiration) ? '' : '; expires=' + expiration;
184
+ var secure = !!this._secureCookies ? ';secure' : '';
185
+ document.cookie = this.cookieNamePrefix + name + '=' + encodeURIComponent(value) + expires + secure;
186
+ },
187
+
188
+ /**
189
+ @method knownCookies
190
+ @private
191
+ */
192
+ knownCookies: function() {
193
+ var _this = this;
194
+ return Ember.A(document.cookie.split(/[=;\s]+/)).filter(function(element) {
195
+ return new RegExp('^' + _this.cookieNamePrefix).test(element);
196
+ }).map(function(cookie) {
197
+ return cookie.replace(_this.cookieNamePrefix, '');
198
+ });
199
+ },
200
+
201
+ /**
202
+ @method syncData
203
+ @private
204
+ */
205
+ syncData: function() {
206
+ var data = this.restore();
207
+ if (!Ember.SimpleAuth.Utils.flatObjectsAreEqual(data, this._lastData)) {
208
+ this._lastData = data;
209
+ this.trigger('updated', data);
210
+ }
211
+ if (!Ember.testing) {
212
+ Ember.run.cancel(this._syncDataTimeout);
213
+ this._syncDataTimeout = Ember.run.later(this, this.syncData, 500);
214
+ }
215
+ }
216
+ });
217
+
218
+ __exports__.Cookie = Cookie;
219
+ });
220
+ global.Ember.SimpleAuth.Stores.Cookie = requireModule('ember-simple-auth-cookie-store').default;
221
+
222
+ global.Ember.SimpleAuth.initializeExtension(function(container, application, options) {
223
+ container.register('session-store:cookie', global.Ember.SimpleAuth.Stores.Cookie);
224
+ });
225
+ })((typeof global !== 'undefined') ? global : window);
@@ -0,0 +1,244 @@
1
+ (function(global) {
2
+
3
+ var define, requireModule;
4
+
5
+ (function() {
6
+ var registry = {}, seen = {};
7
+
8
+ define = function(name, deps, callback) {
9
+ registry[name] = { deps: deps, callback: callback };
10
+ };
11
+
12
+ requireModule = function(name) {
13
+ if (seen.hasOwnProperty(name)) { return seen[name]; }
14
+ seen[name] = {};
15
+
16
+ if (!registry[name]) {
17
+ throw new Error("Could not find module " + name);
18
+ }
19
+
20
+ var mod = registry[name],
21
+ deps = mod.deps,
22
+ callback = mod.callback,
23
+ reified = [],
24
+ exports;
25
+
26
+ for (var i=0, l=deps.length; i<l; i++) {
27
+ if (deps[i] === 'exports') {
28
+ reified.push(exports = {});
29
+ } else {
30
+ reified.push(requireModule(resolve(deps[i])));
31
+ }
32
+ }
33
+
34
+ var value = callback.apply(this, reified);
35
+ return seen[name] = exports || value;
36
+
37
+ function resolve(child) {
38
+ if (child.charAt(0) !== '.') { return child; }
39
+ var parts = child.split("/");
40
+ var parentBase = name.split("/").slice(0, -1);
41
+
42
+ for (var i=0, l=parts.length; i<l; i++) {
43
+ var part = parts[i];
44
+
45
+ if (part === '..') { parentBase.pop(); }
46
+ else if (part === '.') { continue; }
47
+ else { parentBase.push(part); }
48
+ }
49
+
50
+ return parentBase.join("/");
51
+ }
52
+ };
53
+
54
+ requireModule.registry = registry;
55
+ })();
56
+
57
+ define("ember-simple-auth-devise",
58
+ ["./ember-simple-auth-devise/authenticators/devise","./ember-simple-auth-devise/authorizers/devise","exports"],
59
+ function(__dependency1__, __dependency2__, __exports__) {
60
+ "use strict";
61
+ var Authenticator = __dependency1__.Devise;
62
+ var Authorizer = __dependency2__.Devise;
63
+
64
+ __exports__.Authenticator = Authenticator;
65
+ __exports__.Authorizer = Authorizer;
66
+ });
67
+ define("ember-simple-auth-devise/authenticators/devise",
68
+ ["exports"],
69
+ function(__exports__) {
70
+ "use strict";
71
+ var global = (typeof window !== 'undefined') ? window : {},
72
+ Ember = global.Ember;
73
+
74
+ /**
75
+ Authenticator that works with the Ruby gem
76
+ [Devise](https://github.com/plataformatec/devise).
77
+
78
+ __As token authentication is not actually part of devise anymore, the server
79
+ needs to implement some customizations__ to work with this authenticator -
80
+ see the README and
81
+ [discussion here](https://gist.github.com/josevalim/fb706b1e933ef01e4fb6).
82
+
83
+ _The factory for this authenticator is registered as `'authenticator:devise'`
84
+ in Ember's container._
85
+
86
+ @class Devise
87
+ @namespace Authenticators
88
+ @extends Base
89
+ */
90
+ var Devise = Ember.SimpleAuth.Authenticators.Base.extend({
91
+ /**
92
+ The endpoint on the server the authenticator acquires the auth token
93
+ and email from.
94
+
95
+ @property serverTokenEndpoint
96
+ @type String
97
+ @default '/users/sign_in'
98
+ */
99
+ serverTokenEndpoint: '/users/sign_in',
100
+
101
+ /**
102
+ Restores the session from a set of session properties; __will return a
103
+ resolving promise when there's a non-empty `auth_token` and a non-empty
104
+ `auth_email` in the `properties`__ and a rejecting promise otherwise.
105
+
106
+ @method restore
107
+ @param {Object} properties The properties to restore the session from
108
+ @return {Ember.RSVP.Promise} A promise that when it resolves results in the session being authenticated
109
+ */
110
+ restore: function(properties) {
111
+ return new Ember.RSVP.Promise(function(resolve, reject) {
112
+ if (!Ember.isEmpty(properties.auth_token) && !Ember.isEmpty(properties.auth_email)) {
113
+ resolve(properties);
114
+ } else {
115
+ reject();
116
+ }
117
+ });
118
+ },
119
+
120
+ /**
121
+ Authenticates the session with the specified `credentials`; the credentials
122
+ are `POST`ed to the `serverTokenEndpoint` and if they are valid the server
123
+ returns an auth token and email in response . __If the credentials are
124
+ valid and authentication succeeds, a promise that resolves with the
125
+ server's response is returned__, otherwise a promise that rejects with the
126
+ error is returned.
127
+
128
+ @method authenticate
129
+ @param {Object} options The credentials to authenticate the session with
130
+ @return {Ember.RSVP.Promise} A promise that resolves when an auth token and email is successfully acquired from the server and rejects otherwise
131
+ */
132
+ authenticate: function(credentials) {
133
+ var _this = this;
134
+ return new Ember.RSVP.Promise(function(resolve, reject) {
135
+ var data = {
136
+ email: credentials.identification,
137
+ password: credentials.password
138
+ };
139
+ _this.makeRequest(data).then(function(response) {
140
+ Ember.run(function() {
141
+ resolve(response);
142
+ });
143
+ }, function(xhr, status, error) {
144
+ Ember.run(function() {
145
+ reject(xhr.responseJSON || xhr.responseText);
146
+ });
147
+ });
148
+ });
149
+ },
150
+
151
+ /**
152
+ Does nothing
153
+
154
+ @method invalidate
155
+ @return {Ember.RSVP.Promise} A resolving promise
156
+ */
157
+ invalidate: function() {
158
+ return Ember.RSVP.resolve();
159
+ },
160
+
161
+ /**
162
+ @method makeRequest
163
+ @private
164
+ */
165
+ makeRequest: function(data, resolve, reject) {
166
+ if (!Ember.SimpleAuth.Utils.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
+ contentType: 'application/x-www-form-urlencoded'
175
+ });
176
+ }
177
+ });
178
+
179
+ __exports__.Devise = Devise;
180
+ });
181
+ define("ember-simple-auth-devise/authorizers/devise",
182
+ ["exports"],
183
+ function(__exports__) {
184
+ "use strict";
185
+ var global = (typeof window !== 'undefined') ? window : {},
186
+ Ember = global.Ember;
187
+
188
+ /**
189
+ Authenticator that works with the Ruby gem
190
+ [Devise](https://github.com/plataformatec/devise) by adding `auth-token` and
191
+ `auth-email` headers to requests.
192
+
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 and
196
+ [discussion here](https://gist.github.com/josevalim/fb706b1e933ef01e4fb6).
197
+
198
+ _The factory for this authorizer is registered as `'authorizer:devise'` in
199
+ Ember's container._
200
+
201
+ @class Devise
202
+ @namespace Authorizers
203
+ @extends Base
204
+ */
205
+ var Devise = Ember.SimpleAuth.Authorizers.Base.extend({
206
+ /**
207
+ Authorizes an XHR request by sending the `auth_token` and `auth_email`
208
+ properties from the session in custom headers:
209
+
210
+ ```
211
+ auth-token: <auth_token>
212
+ auth-email: <auth_email>
213
+ ```
214
+
215
+ @method authorize
216
+ @param {jqXHR} jqXHR The XHR request to authorize (see http://api.jquery.com/jQuery.ajax/#jqXHR)
217
+ @param {Object} requestOptions The options as provided to the `$.ajax` method (see http://api.jquery.com/jQuery.ajaxPrefilter/)
218
+ */
219
+
220
+ authorize: function(jqXHR, requestOptions) {
221
+ var authToken = this.get('session.auth_token');
222
+ var authEmail = this.get('session.auth_email');
223
+ if (this.get('session.isAuthenticated') && !Ember.isEmpty(authToken) && !Ember.isEmpty(authEmail)) {
224
+ if (!Ember.SimpleAuth.Utils.isSecureUrl(requestOptions.url)) {
225
+ Ember.Logger.warn('Credentials are transmitted via an insecure connection - use HTTPS to keep them secure.');
226
+ }
227
+ jqXHR.setRequestHeader('auth-token', authToken);
228
+ jqXHR.setRequestHeader('auth-email', authEmail);
229
+ }
230
+ }
231
+ });
232
+
233
+ __exports__.Devise = Devise;
234
+ });
235
+ var devise = requireModule('ember-simple-auth-devise');
236
+
237
+ global.Ember.SimpleAuth.Authenticators.Devise = devise.Authenticator;
238
+ global.Ember.SimpleAuth.Authorizers.Devise = devise.Authorizer;
239
+
240
+ global.Ember.SimpleAuth.initializeExtension(function(container, application, options) {
241
+ container.register('authorizer:devise', global.Ember.SimpleAuth.Authorizers.Devise);
242
+ container.register('authenticator:devise', global.Ember.SimpleAuth.Authenticators.Devise);
243
+ });
244
+ })((typeof global !== 'undefined') ? global : window);