Skip to content

Commit c9dfbcb

Browse files
Eduard Bosch BertranEduard Bosch Bertran
Eduard Bosch Bertran
authored and
Eduard Bosch Bertran
committed
feat: Add lib folder
1 parent 7180dac commit c9dfbcb

File tree

129 files changed

+22126
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+22126
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ node_modules
4141
.vscode
4242

4343
# Babel.js
44-
lib/
44+
#lib/
4545

4646
# cache folder
4747
.cache

lib/AccountLockout.js

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.AccountLockout = undefined;
7+
8+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); // This class handles the Account Lockout Policy settings.
9+
10+
11+
var _node = require('parse/node');
12+
13+
var _node2 = _interopRequireDefault(_node);
14+
15+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16+
17+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
18+
19+
var AccountLockout = exports.AccountLockout = function () {
20+
function AccountLockout(user, config) {
21+
_classCallCheck(this, AccountLockout);
22+
23+
this._user = user;
24+
this._config = config;
25+
}
26+
27+
/**
28+
* set _failed_login_count to value
29+
*/
30+
31+
32+
_createClass(AccountLockout, [{
33+
key: '_setFailedLoginCount',
34+
value: function _setFailedLoginCount(value) {
35+
var query = {
36+
username: this._user.username
37+
};
38+
39+
var updateFields = {
40+
_failed_login_count: value
41+
};
42+
43+
return this._config.database.update('_User', query, updateFields);
44+
}
45+
46+
/**
47+
* check if the _failed_login_count field has been set
48+
*/
49+
50+
}, {
51+
key: '_isFailedLoginCountSet',
52+
value: function _isFailedLoginCountSet() {
53+
var query = {
54+
username: this._user.username,
55+
_failed_login_count: { $exists: true }
56+
};
57+
58+
return this._config.database.find('_User', query).then(function (users) {
59+
if (Array.isArray(users) && users.length > 0) {
60+
return true;
61+
} else {
62+
return false;
63+
}
64+
});
65+
}
66+
67+
/**
68+
* if _failed_login_count is NOT set then set it to 0
69+
* else do nothing
70+
*/
71+
72+
}, {
73+
key: '_initFailedLoginCount',
74+
value: function _initFailedLoginCount() {
75+
var _this = this;
76+
77+
return this._isFailedLoginCountSet().then(function (failedLoginCountIsSet) {
78+
if (!failedLoginCountIsSet) {
79+
return _this._setFailedLoginCount(0);
80+
}
81+
});
82+
}
83+
84+
/**
85+
* increment _failed_login_count by 1
86+
*/
87+
88+
}, {
89+
key: '_incrementFailedLoginCount',
90+
value: function _incrementFailedLoginCount() {
91+
var query = {
92+
username: this._user.username
93+
};
94+
95+
var updateFields = { _failed_login_count: { __op: 'Increment', amount: 1 } };
96+
97+
return this._config.database.update('_User', query, updateFields);
98+
}
99+
100+
/**
101+
* if the failed login count is greater than the threshold
102+
* then sets lockout expiration to 'currenttime + accountPolicy.duration', i.e., account is locked out for the next 'accountPolicy.duration' minutes
103+
* else do nothing
104+
*/
105+
106+
}, {
107+
key: '_setLockoutExpiration',
108+
value: function _setLockoutExpiration() {
109+
var query = {
110+
username: this._user.username,
111+
_failed_login_count: { $gte: this._config.accountLockout.threshold }
112+
};
113+
114+
var now = new Date();
115+
116+
var updateFields = {
117+
_account_lockout_expires_at: _node2.default._encode(new Date(now.getTime() + this._config.accountLockout.duration * 60 * 1000))
118+
};
119+
120+
return this._config.database.update('_User', query, updateFields).catch(function (err) {
121+
if (err && err.code && err.message && err.code === 101 && err.message === 'Object not found.') {
122+
return; // nothing to update so we are good
123+
} else {
124+
throw err; // unknown error
125+
}
126+
});
127+
}
128+
129+
/**
130+
* if _account_lockout_expires_at > current_time and _failed_login_count > threshold
131+
* reject with account locked error
132+
* else
133+
* resolve
134+
*/
135+
136+
}, {
137+
key: '_notLocked',
138+
value: function _notLocked() {
139+
var _this2 = this;
140+
141+
var query = {
142+
username: this._user.username,
143+
_account_lockout_expires_at: { $gt: _node2.default._encode(new Date()) },
144+
_failed_login_count: { $gte: this._config.accountLockout.threshold }
145+
};
146+
147+
return this._config.database.find('_User', query).then(function (users) {
148+
if (Array.isArray(users) && users.length > 0) {
149+
throw new _node2.default.Error(_node2.default.Error.OBJECT_NOT_FOUND, 'Your account is locked due to multiple failed login attempts. Please try again after ' + _this2._config.accountLockout.duration + ' minute(s)');
150+
}
151+
});
152+
}
153+
154+
/**
155+
* set and/or increment _failed_login_count
156+
* if _failed_login_count > threshold
157+
* set the _account_lockout_expires_at to current_time + accountPolicy.duration
158+
* else
159+
* do nothing
160+
*/
161+
162+
}, {
163+
key: '_handleFailedLoginAttempt',
164+
value: function _handleFailedLoginAttempt() {
165+
var _this3 = this;
166+
167+
return this._initFailedLoginCount().then(function () {
168+
return _this3._incrementFailedLoginCount();
169+
}).then(function () {
170+
return _this3._setLockoutExpiration();
171+
});
172+
}
173+
174+
/**
175+
* handle login attempt if the Account Lockout Policy is enabled
176+
*/
177+
178+
}, {
179+
key: 'handleLoginAttempt',
180+
value: function handleLoginAttempt(loginSuccessful) {
181+
var _this4 = this;
182+
183+
if (!this._config.accountLockout) {
184+
return Promise.resolve();
185+
}
186+
return this._notLocked().then(function () {
187+
if (loginSuccessful) {
188+
return _this4._setFailedLoginCount(0);
189+
} else {
190+
return _this4._handleFailedLoginAttempt();
191+
}
192+
});
193+
}
194+
}]);
195+
196+
return AccountLockout;
197+
}();
198+
199+
exports.default = AccountLockout;

lib/Adapters/AdapterLoader.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.loadAdapter = loadAdapter;
7+
function loadAdapter(adapter, defaultAdapter, options) {
8+
if (!adapter) {
9+
if (!defaultAdapter) {
10+
return options;
11+
}
12+
// Load from the default adapter when no adapter is set
13+
return loadAdapter(defaultAdapter, undefined, options);
14+
} else if (typeof adapter === "function") {
15+
try {
16+
return adapter(options);
17+
} catch (e) {
18+
if (e.name === 'TypeError') {
19+
var Adapter = adapter;
20+
return new Adapter(options);
21+
} else {
22+
throw e;
23+
}
24+
}
25+
} else if (typeof adapter === "string") {
26+
/* eslint-disable */
27+
adapter = require(adapter);
28+
// If it's define as a module, get the default
29+
if (adapter.default) {
30+
adapter = adapter.default;
31+
}
32+
return loadAdapter(adapter, undefined, options);
33+
} else if (adapter.module) {
34+
return loadAdapter(adapter.module, undefined, adapter.options);
35+
} else if (adapter.class) {
36+
return loadAdapter(adapter.class, undefined, adapter.options);
37+
} else if (adapter.adapter) {
38+
return loadAdapter(adapter.adapter, undefined, adapter.options);
39+
}
40+
// return the adapter as provided
41+
return adapter;
42+
}
43+
44+
exports.default = loadAdapter;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8+
9+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
10+
11+
/*eslint no-unused-vars: "off"*/
12+
var AnalyticsAdapter = exports.AnalyticsAdapter = function () {
13+
function AnalyticsAdapter() {
14+
_classCallCheck(this, AnalyticsAdapter);
15+
}
16+
17+
_createClass(AnalyticsAdapter, [{
18+
key: "appOpened",
19+
20+
21+
/*
22+
@param parameters: the analytics request body, analytics info will be in the dimensions property
23+
@param req: the original http request
24+
*/
25+
value: function appOpened(parameters, req) {
26+
return Promise.resolve({});
27+
}
28+
29+
/*
30+
@param eventName: the name of the custom eventName
31+
@param parameters: the analytics request body, analytics info will be in the dimensions property
32+
@param req: the original http request
33+
*/
34+
35+
}, {
36+
key: "trackEvent",
37+
value: function trackEvent(eventName, parameters, req) {
38+
return Promise.resolve({});
39+
}
40+
}]);
41+
42+
return AnalyticsAdapter;
43+
}();
44+
45+
exports.default = AnalyticsAdapter;

lib/Adapters/Auth/AuthAdapter.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8+
9+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
10+
11+
/*eslint no-unused-vars: "off"*/
12+
var AuthAdapter = exports.AuthAdapter = function () {
13+
function AuthAdapter() {
14+
_classCallCheck(this, AuthAdapter);
15+
}
16+
17+
_createClass(AuthAdapter, [{
18+
key: "validateAppId",
19+
20+
21+
/*
22+
@param appIds: the specified app ids in the configuration
23+
@param authData: the client provided authData
24+
@returns a promise that resolves if the applicationId is valid
25+
*/
26+
value: function validateAppId(appIds, authData) {
27+
return Promise.resolve({});
28+
}
29+
30+
/*
31+
@param authData: the client provided authData
32+
@param options: additional options
33+
*/
34+
35+
}, {
36+
key: "validateAuthData",
37+
value: function validateAuthData(authData, options) {
38+
return Promise.resolve({});
39+
}
40+
}]);
41+
42+
return AuthAdapter;
43+
}();
44+
45+
exports.default = AuthAdapter;

0 commit comments

Comments
 (0)