-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAcl.js
284 lines (246 loc) · 7.9 KB
/
Acl.js
1
2
3
4
5
6
7
8
9
10
11
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
42
43
44
45
46
47
48
49
50
51
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
var F = require('RebelFrame/Framework'),
Alloy = require('alloy'),
_ = Alloy._,
Backbone = Alloy.Backbone;
/**
* Access Control List library
*
* @class Acl
*/
var Acl = {
user: null,
/**
* {String} The salt to hash passwords before they are sent to the Oauth endpoint
*/
salt: Alloy.CFG.salt,
/**
* {String} Access token set by loggedin User
*/
cloudAccessToken: '',
/**
* Set and Store a new Collapp API access token
*
* @param {String} access_token Access token to store
*/
setCloudAccessToken: function(access_token) {
Acl.cloudAccessToken = access_token;
Acl.storeCredentials(Ti.App.id, 'cloudAccessToken', access_token);
},
/**
* Authenticate against the Oauth 2 endpoint if necessary
*
* @param {String} username Username of User to authenticate
* @param {String} password Password (or better Hash) of User to authenticate
* @param {String} type User type of User to authenticate
* @param {Object} success Callback object containing function (& scope); will be called upon success with the authentication's result
* @param {Object} failure Callback object containing function (& scope); will be called upon error
*/
authenticateUserWithCloud: function(username, password, type, success, failure) {
var Cloud = require('RebelFrame/Cloud');
return new Cloud({
url: '/oauth/access_token',
method: 'POST',
data: {
username: username,
password: password,
include_entities: true,
user_type: type,
grant_type: 'client_credentials'
},
success: function(response) {
Ti.API.info('Saving Access Token', 'acl');
Ti.API.info(response);
Acl.setCloudAccessToken(response.access_token.access_token);
success(response);
},
error: failure
});
},
/**
* Is there a loggedin User?
*
* @return {Bool} Whether or not there is a logged in User
*/
isLoggedin: function() {
return !!Ti.App.Properties.getString('loggedinUserId');
},
/**
* Set loggedin User
*/
setIsLoggedin: function(user) {
Ti.App.Properties.setString('loggedinUserId', user.id);
},
/**
* Get the loggedin User
* @return {Sc.model.UserLoggedIn} The currently loggedin User
*/
getLoggedinUser: function() {
if (!Acl.user) {
Acl.user = Alloy.createModel('User', {
objectId: Ti.App.Properties.getString('loggedinUserId')
});
Acl.user.set(Ti.App.Properties.getObject('loggedinUser', {}));
Acl.user.on('change', function(model) {
Ti.App.Properties.setObject('loggedinUser', model.attributes);
});
Acl.user.fetch();
}
return Acl.user;
},
/**
* Set the loggedin User
* @param {Sc.model.UserLoggedIn} user User to set as loggedin User
*/
setLoggedinUser: function(user) {
if(Acl.user)
Acl.user.off();
Acl.user = user;
// Every time the Loggedin User changes, save it to properties
Acl.user.on('change', function(model) {
Ti.App.Properties.setObject('loggedinUser', model.attributes);
});
// Save to properties now
Ti.App.Properties.setObject('loggedinUser', Acl.user.attributes);
Acl.setIsLoggedin(user);
},
/**
* Handle Signup of a new User in the Collapp API
*
* @param {Object} userData Data for the new User
* @param {Object} callback Callback to call
* @param {function} callback.successFn Function to call on success
* @param {function} callback.error Function to call on error
* @param {Object} callback.scope Scope to call the callback function in
*/
signup: function(userData, callback) {
userData.password = Ti.Utils.sha1(userData.password + Acl.salt);
// Store credentials
Acl.storeCredentials(Ti.App.id, userData.email, userData.password);
Acl.registerUserWithCloud(userData, callback);
},
/**
* Login an existing User to the Collapp API
*
* @param {String} email Email of existing User
* @param {String} password Password of existing User
* @param {Object} callback Callback to call
* @param {function} callback.successFn Function to call on success
* @param {function} callback.error Function to call on error
* @param {Object} callback.scope Scope to call the callback function in
*/
login: function(email, password, callback) {
var hashedPass = Ti.Utils.sha1(password + Acl.salt);
password = null;
// Login
Acl.authenticateUserWithCloud(email, hashedPass, 'collapp',
function(event) {
Ti.API.info('login', event);
// Store credentials
Acl.storeCredentials(Ti.App.id, email, hashedPass);
// Add email to stored User
event.user.email = email;
Acl.onLoginSuccess(event, callback);
},
callback.error
);
},
/**
* Log the current user out
*
* @param {Boolean} forced True if the logout was forced, false otherwise
*/
logout: function(forced) {
Ti.API.info('Removing logged in user id and cloud access token from properties', 'prop');
Ti.App.Properties.removeProperty('loggedinUserId');
Acl.setCloudAccessToken(null);
// Reset App state to logged out
// Routing will handle the rest
F.setStatus(F.LOGGEDOUT);
},
/**
* Store credentials
*
* @param {String} service
* @param {String} account
* @param {String} password
*/
storeCredentials: function(service, account, password) {
// keychain.setPasswordForService(password, service, account);
Ti.App.Properties.setString(service + '-' + account, password);
},
/**
* Get previously stored credentials
*
* @param {String} service
* @param {String} account
* @return {String} Stored password
*/
getCredentials: function(service, account) {
// return keychain.getPasswordForService(service, account);
return Ti.App.Properties.getString(service + '-' + account);
},
/**
* Register a new User at the Collapp API
*
* @param {Object} userData
* @param {Object} callback Callback to call
* @param {function} callback.successFn Function to call on success
* @param {function} callback.error Function to call on error
* @param {Object} callback.scope Scope to call the callback function in
*/
registerUserWithCloud: function(userData, callback) {
var Cloud = require('RebelFrame/Cloud');
return new Cloud({
url: '/user',
method: 'POST',
data: userData,
contentType: 'multipart/form-data',
success: function(cloudUser) {
Acl.onLoginSuccess(cloudUser, callback);
},
error: callback.error
});
},
/**
* Connect a social media account to the currently loggedin User
*
* @param {Object} userData Data of the social media account
* @param {Object} callback Callback to call after connecting is done
* @param {function} callback.successFn Function to call on success
* @param {function} callback.error Function to call on error
* @param {Object} callback.scope Scope to call the callback function in
*/
connectSocialMediaToUser: function(userData, callback) {
var Cloud = require('RebelFrame/Cloud');
return new Cloud({
url: '/user/' + Acl.getLoggedinUser().id + '/connect',
method: 'POST',
data: userData,
success: callback.success,
error: callback.error
});
},
/**
* Default callback to be called fist after login, signup or connect
*
* @param {Object} cloudResponse Response from the Collapp API
* @param {Object} callback
* @param {function} callback.successFn Function to call on success
* @param {Object} callback.scope Scope to call the callback function in
*/
onLoginSuccess: function(cloudResponse, callback) {
var cloudUser = cloudResponse.user;
if (cloudResponse.access_token) {
Acl.setCloudAccessToken(cloudResponse.access_token.access_token);
}
// Ti.API.info('on login/connect success: ' + cloudUser.name, 'login');
// Set new loggedin user
Acl.setLoggedinUser(Alloy.createModel('User', cloudUser));
// When done, call callback
Ti.API.info('Calling login callback', 'login');
callback.success();
}
};
// Load AccessToken and store it in ACL and Cloud
Acl.cloudAccessToken = Acl.getCredentials(Ti.App.id, 'cloudAccessToken');
module.exports = Acl;