|
|
define([
|
|
|
"dojo/_base/declare", "dojo/node!crypto"], function (declare, crypto) {
|
|
|
let SecData = declare(null, {
|
|
|
_state: null,
|
|
|
_token: null,
|
|
|
_authType: null,
|
|
|
|
|
|
constructor: function (authType) {
|
|
|
this._authType = authType;
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* Проводит раунд аутентификации, изменяет текущее состояние.
|
|
|
* @param challenge {*} - данные для аутентификации, зависит от реализации, например, пароль.
|
|
|
* @retuns authResult {Object} - результат аутентификации { challenge : 'response data', code : AUTH_* }.
|
|
|
*/
|
|
|
doAuth: function (challenge) {
|
|
|
let password = challenge;
|
|
|
|
|
|
if (this.validateHash(password)) {
|
|
|
this._state = SecData.AUTH_SUCCESS;
|
|
|
} else {
|
|
|
this._state = SecData.AUTH_FAIL;
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
code: this._state
|
|
|
};
|
|
|
|
|
|
},
|
|
|
getAuthState: function () {
|
|
|
return this._state;
|
|
|
},
|
|
|
getAuthType: function () {
|
|
|
return this._authType;
|
|
|
},
|
|
|
|
|
|
generateHash: function (password) {
|
|
|
return md5hex(password);
|
|
|
},
|
|
|
parse: function (token) {
|
|
|
this._token = token;
|
|
|
},
|
|
|
|
|
|
validateHash: function (password) {
|
|
|
return this.generateHash(password) == this._token;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
|
|
|
SecData.AUTH_SUCCESS = 0;
|
|
|
SecData.AUTH_INCOMPLETE = 1;
|
|
|
SecData.AUTH_FAIL = 2;
|
|
|
|
|
|
|
|
|
function md5hex() {
|
|
|
let md5 = crypto.createHash('md5');
|
|
|
|
|
|
for (let i = 0; i < arguments.length; i++)
|
|
|
md5.update(String(arguments[i]));
|
|
|
|
|
|
return md5.digest('hex');
|
|
|
}
|
|
|
|
|
|
let i = 0;
|
|
|
|
|
|
SecData.md5hex = md5hex;
|
|
|
SecData.newSSID = function () {
|
|
|
return md5hex(new Date().getTime(), Math.random(), i++);
|
|
|
};
|
|
|
|
|
|
return SecData;
|
|
|
});
|