Tightening up naming conventions.
Project: http://git-wip-us.apache.org/repos/asf/incubator-milagro-mfa-js-lib/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-milagro-mfa-js-lib/commit/11db8398 Tree: http://git-wip-us.apache.org/repos/asf/incubator-milagro-mfa-js-lib/tree/11db8398 Diff: http://git-wip-us.apache.org/repos/asf/incubator-milagro-mfa-js-lib/diff/11db8398 Branch: refs/heads/add-documentation Commit: 11db83982824506f4c8979300591935983f2344f Parents: f272668 Author: Simeon Aladjem <[email protected]> Authored: Tue Dec 22 14:18:49 2015 +0200 Committer: Simeon Aladjem <[email protected]> Committed: Tue Dec 22 14:18:49 2015 +0200 ---------------------------------------------------------------------- example/exampleJquery.html | 16 ++--- lib/mpin.js | 140 ++++++++++++++++++++-------------------- 2 files changed, 78 insertions(+), 78 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-milagro-mfa-js-lib/blob/11db8398/example/exampleJquery.html ---------------------------------------------------------------------- diff --git a/example/exampleJquery.html b/example/exampleJquery.html index de1b29e..cf86013 100644 --- a/example/exampleJquery.html +++ b/example/exampleJquery.html @@ -51,10 +51,10 @@ and open the template in the editor. for (var i in mpinUsers) { htmlList += "<li>" + mpinUsers[i].userId + " "; htmlList += "<input type='text' placeholder='PIN' size='10'> "; - if (mpinUsers[i].status === "REGISTERED") { - htmlList += "<span class='registered' data-userid='" + mpinUsers[i].userId + "' data-status='" + mpinUsers[i].status + "'>Authenticate</span>"; + if (mpinUsers[i].state === "REGISTERED") { + htmlList += "<span class='registered' data-userid='" + mpinUsers[i].userId + "' data-state='" + mpinUsers[i].state + "'>Authenticate</span>"; } else { - htmlList += "<span class='setup' data-userid='" + mpinUsers[i].userId + "' data-status='" + mpinUsers[i].status + "'>Setup</span>"; + htmlList += "<span class='setup' data-userid='" + mpinUsers[i].userId + "' data-state='" + mpinUsers[i].state + "'>Setup</span>"; } htmlList += "</span></li>"; } @@ -65,12 +65,12 @@ and open the template in the editor. $(document).on('click', ".setup", function (ev) { - var thisElem, userId, userPin, status; + var thisElem, userId, userPin, state; thisElem = $(ev.currentTarget); userId = thisElem.data('userid'); userPin = thisElem.prev("input").val(); - status = thisElem.data('status'); + state = thisElem.data('state'); mpin.startRegistration(userId, function (err3, data3) { mpin.confirmRegistration(userId, function (err, data) { @@ -94,10 +94,10 @@ and open the template in the editor. $(document).on('click', ".registered", function (ev) { - var userId, userPin, status; + var userId, userPin, state; userId = $(ev.currentTarget).data('userid'); userPin = $(ev.currentTarget).prev("input").val(); - status = $(ev.currentTarget).data('status'); + state = $(ev.currentTarget).data('state'); mpin.startAuthentication(userId, function (err, data) { if (err) { @@ -137,7 +137,7 @@ and open the template in the editor. function chkUser (userId) { - var userStatus = mpin.getUser(userId, "status"); + var userState = mpin.getUser(userId, "state"); mpin.confirmRegistration(userId, function (err, data) { //not Active Identity http://git-wip-us.apache.org/repos/asf/incubator-milagro-mfa-js-lib/blob/11db8398/lib/mpin.js ---------------------------------------------------------------------- diff --git a/lib/mpin.js b/lib/mpin.js index 7db5fd7..7ae1e67 100644 --- a/lib/mpin.js +++ b/lib/mpin.js @@ -18,22 +18,22 @@ */ var mpinjs = (function () { - var Mpin, Users = {}, Errors = {}, Status = {}; - - Errors.missingUserId = {code: 0, type: "MISSING_USERID"}; - Errors.invalidUserId = {code: 1, type: "INVALID_USERID"}; - Errors.missingParams = {code: 2, type: "MISSING_PARAMETERS"}; - Errors.identityNotVerified = {code: 3, type: "IDENTITY_NOT_VERIFIED"}; - Errors.identityMissing = {code: 4, type: "IDENTITY_MISSING"}; - Errors.wrongPin = {code: 5, type: "WRONG_PIN"}; - Errors.wrongFlow = {code: 6, type: "WRONG_FLOW"}; - Errors.timeoutFinish = {code: 7, type: "TIMEOUT_FINISH"}; - - Status.invalid = "INVALID"; - Status.start = "STARTED"; - Status.active = "ACTIVATED"; - Status.register = "REGISTERED"; - Status.block = "BLOCKED"; + var Mpin, Users = {}, Error = {}, State = {}; + + Error.missingUserId = {code: 0, type: "MISSING_USERID"}; + Error.invalidUserId = {code: 1, type: "INVALID_USERID"}; + Error.missingParams = {code: 2, type: "MISSING_PARAMETERS"}; + Error.identityNotVerified = {code: 3, type: "IDENTITY_NOT_VERIFIED"}; + Error.identityMissing = {code: 4, type: "IDENTITY_MISSING"}; + Error.wrongPin = {code: 5, type: "WRONG_PIN"}; + Error.wrongFlow = {code: 6, type: "WRONG_FLOW"}; + Error.timeoutFinish = {code: 7, type: "TIMEOUT_FINISH"}; + + State.invalid = "INVALID"; + State.start = "STARTED"; + State.active = "ACTIVATED"; + State.register = "REGISTERED"; + State.block = "BLOCKED"; Mpin = function (options) { if (!options || !options.server) { @@ -72,10 +72,10 @@ var mpinjs = (function () { Mpin.prototype.makeNewUser = function (userId, deviceId) { if (!userId) { - return Errors.missingUserId; + return Error.missingUserId; } - this.addToUser(userId, {userId: userId, deviceId: deviceId, status: Status.invalid}); + this.addToUser(userId, {userId: userId, deviceId: deviceId, state: State.invalid}); return this; }; @@ -83,11 +83,11 @@ var mpinjs = (function () { Mpin.prototype.startRegistration = function (userId, cb) { var _reqData = {}, self = this; if (!userId) { - return cb ? cb(Errors.missingUserId, null) : {error: 1}; + return cb ? cb(Error.missingUserId, null) : {error: 1}; } else if (!this.checkUser(userId)) { - return cb(Errors.invalidUserId, null); + return cb(Error.invalidUserId, null); } else if (!this.opts.registerURL) { - return cb({code: Errors.missingParams.code, type: Errors.missingParams.type, message: "Missing registerURL"}, null); + return cb({code: Error.missingParams.code, type: Error.missingParams.type, message: "Missing registerURL"}, null); } _reqData.url = this.generateUrl("register"); @@ -102,11 +102,11 @@ var mpinjs = (function () { return cb(err, null); } - self.addToUser(userId, {regOTT: data.regOTT, mpinId: data.mpinId, status: Status.start}); + self.addToUser(userId, {regOTT: data.regOTT, mpinId: data.mpinId, state: State.start}); //force activate if (data.active) { - self.addToUser(userId, {status: Status.active}); + self.addToUser(userId, {state: State.active}); } cb && cb(null, true); @@ -115,19 +115,19 @@ var mpinjs = (function () { //request cs1 + cs2 Mpin.prototype.confirmRegistration = function (userId, cb) { - var _cs1Url = "", self = this, _userStatus; + var _cs1Url = "", self = this, _userState; if (!userId) { - return cb ? cb(Errors.missingUserId, null) : Errors.missingUserId; + return cb ? cb(Error.missingUserId, null) : Error.missingUserId; } else if (!this.checkUser(userId)) { - return cb(Errors.invalidUserId, null); + return cb(Error.invalidUserId, null); } else if (!this.opts.signatureURL) { - return cb({code: Errors.missingParams.code, type: Errors.missingParams.type, message: "Missing signatureURL option."}, null); + return cb({code: Error.missingParams.code, type: Error.missingParams.type, message: "Missing signatureURL option."}, null); } //started || activated - _userStatus = this.getUser(userId, "status"); - if (_userStatus !== Status.start && _userStatus !== Status.active) { - return cb(Errors.wrongFlow, null); + _userState = this.getUser(userId, "state"); + if (_userState !== State.start && _userState !== State.active) { + return cb(Error.wrongFlow, null); } //already set. @@ -141,13 +141,13 @@ var mpinjs = (function () { var _cs2Url = ""; if (err) { if (err.status == 401) { - return cb(Errors.identityNotVerified, null); + return cb(Error.identityNotVerified, null); } else if (err.status == 400) { - return cb(Errors.wrongFlow, null); + return cb(Error.wrongFlow, null); } } - self.addToUser(userId, {cs1: cs1Data.clientSecretShare, csParams: cs1Data.params, status: Status.active}); + self.addToUser(userId, {cs1: cs1Data.clientSecretShare, csParams: cs1Data.params, state: State.active}); _cs2Url = self.opts.certivoxURL + "clientSecret?" + cs1Data.params; //req cs2 @@ -170,19 +170,19 @@ var mpinjs = (function () { var _user, token; if (!userId) { - return Errors.missingUserId; + return Error.missingUserId; } _user = this.getUser(userId); - if (_user.status !== Status.active) { - return Errors.wrongFlow; + if (_user.state !== State.active) { + return Error.wrongFlow; } token = MPINAuth.calculateMPinToken(Users[userId].mpinId, pin, Users[userId].csHex); delete Users[userId].csHex; - this.addToUser(userId, {token: token, status: Status.register}); + this.addToUser(userId, {token: token, state: State.register}); return true; }; @@ -196,20 +196,20 @@ var mpinjs = (function () { Mpin.prototype.startAuthentication = function (userId, cb) { - var _tp1Url, self = this, _userStatus; + var _tp1Url, self = this, _userState; if (!userId) { - return cb ? cb(Errors.missingUserId, null) : Errors.missingUserId; + return cb ? cb(Error.missingUserId, null) : Error.missingUserId; } else if (!this.checkUser(userId)) { - return cb(Errors.invalidUserId, null); + return cb(Error.invalidUserId, null); } else if (!this.opts.timePermitsURL || !this.opts.certivoxURL) { - return cb({code: Errors.missingParams.code, type: Errors.missingParams.type, message: "Missing timePermitsURL or/and certivoxURL option."}, null); + return cb({code: Error.missingParams.code, type: Error.missingParams.type, message: "Missing timePermitsURL or/and certivoxURL option."}, null); } //registered - _userStatus = this.getUser(userId, "status"); - if (_userStatus !== Status.register) { - return cb(Errors.wrongFlow, null); + _userState = this.getUser(userId, "state"); + if (_userState !== State.register) { + return cb(Error.wrongFlow, null); } //checkUser @@ -270,28 +270,28 @@ var mpinjs = (function () { Mpin.prototype.finishAuthentication = function (userId, aPin, cb) { - var _userStatus; + var _userState; //registered - _userStatus = this.getUser(userId, "status"); - if (_userStatus !== Status.register) { - return cb(Errors.wrongFlow, null); + _userState = this.getUser(userId, "state"); + if (_userState !== State.register) { + return cb(Error.wrongFlow, null); } else if (!Users[userId].timePermitHex) { - return cb({code: Errors.wrongFlow.code, type: Errors.wrongFlow.type, message: "Need to call startAuthentication method before this."}, null); + return cb({code: Error.wrongFlow.code, type: Error.wrongFlow.type, message: "Need to call startAuthentication method before this."}, null); } this._passRequests({userId: userId, aPin: aPin}, cb); }; Mpin.prototype.finishAuthenticationOtp = function (userId, aPin, cb) { - var _userStatus; + var _userState; //registered - _userStatus = this.getUser(userId, "status"); - if (_userStatus !== Status.register) { - return cb(Errors.wrongFlow, null); + _userState = this.getUser(userId, "state"); + if (_userState !== State.register) { + return cb(Error.wrongFlow, null); } else if (!Users[userId].timePermitHex) { - return cb({code: Errors.wrongFlow.code, type: Errors.wrongFlow.type, message: "Need to call startAuthentication method before this."}, null); + return cb({code: Error.wrongFlow.code, type: Error.wrongFlow.type, message: "Need to call startAuthentication method before this."}, null); } this._passRequests({userId: userId, aPin: aPin, otp: true}, function (err, data) { @@ -361,12 +361,12 @@ var mpinjs = (function () { this.request(_authData, function (authErr, authData) { if (authErr) { if (authErr.status === 401) { - return cb(Errors.wrongPin, null); + return cb(Error.wrongPin, null); } else if (authErr.status === 410) { - opts.userId && self.addToUser(opts.userId, {status: Status.block}); - return cb(Errors.wrongPin, null); + opts.userId && self.addToUser(opts.userId, {state: State.block}); + return cb(Error.wrongPin, null); } else { - return cb(Errors.wrongPin, null); + return cb(Error.wrongPin, null); } } @@ -425,9 +425,9 @@ var mpinjs = (function () { Mpin.prototype.waitForMobileAuth = function (timeoutSeconds, requestSeconds, cb) { var self = this, _reqData = {}; if (!this.webOTT) { - return cb({code: Errors.wrongFlow.code, type: Errors.wrongFlow.type, message: "Need to call getAccessNumber method before this."}, null); + return cb({code: Error.wrongFlow.code, type: Error.wrongFlow.type, message: "Need to call getAccessNumber method before this."}, null); } else if (!timeoutSeconds) { - return cb({code: Errors.missingParams.code, type: Errors.missingParams.type, message: "Missing timeout/expiration period(in seconds)."}, null); + return cb({code: Error.missingParams.code, type: Error.missingParams.type, message: "Missing timeout/expiration period(in seconds)."}, null); } @@ -451,7 +451,7 @@ var mpinjs = (function () { }, _requestPeriod); return; } else if (self.timeoutPeriod <= 0) { - cb && cb(Errors.timeoutFinish, null); + cb && cb(Error.timeoutFinish, null); return; } } @@ -524,7 +524,7 @@ var mpinjs = (function () { listUsers[uKey] = { userId: Users[uKey].userId, deviceId: Users[uKey].deviceId || "", - status: Users[uKey].status || "" + state: Users[uKey].state || "" }; } return listUsers; @@ -537,15 +537,15 @@ var mpinjs = (function () { Mpin.prototype.getUser = function (userId, property) { var _user = {}; if (!userId) { - return Errors.missingUserId; + return Error.missingUserId; } else if (!this.checkUser(userId)) { - return Errors.invalidUserId; + return Error.invalidUserId; } _user = { userId: Users[userId].userId, deviceId: Users[userId].deviceId || "", - status: Users[userId].status + state: Users[userId].state }; if (!property) { @@ -560,9 +560,9 @@ var mpinjs = (function () { var mpinData = this.getData(), delMpinId; if (!userId) { - return Errors.missingUserId; + return Error.missingUserId; } else if (!this.checkUser(userId)) { - return Errors.invalidUserId; + return Error.invalidUserId; } delMpinId = Users[userId].mpinId; @@ -630,9 +630,9 @@ var mpinjs = (function () { mpinData.accounts[mpinId].token = upData.token; } - if (upData.status && Users[userId].mpinId) { + if (upData.state && Users[userId].mpinId) { var mpinId = Users[userId].mpinId; - mpinData.accounts[mpinId].status = upData.status; + mpinData.accounts[mpinId].state = upData.state; } //cache cache @@ -664,7 +664,7 @@ var mpinjs = (function () { mpinData.accounts[mpinId].MPinPermit && (userData.MPinPermit = mpinData.accounts[mpinId].MPinPermit); mpinData.accounts[mpinId].timePermitCache && (userData.timePermitCache = mpinData.accounts[mpinId].timePermitCache); - mpinData.accounts[mpinId].status && (userData.status = mpinData.accounts[mpinId].status); + mpinData.accounts[mpinId].state && (userData.state = mpinData.accounts[mpinId].state); //call add To user & skip Save this.addToUser(userId, userData, true);
