mojio-js-sdk
Version:
Mojio javascript REST client.
316 lines (284 loc) • 10.3 kB
JavaScript
// Generated by CoffeeScript 1.10.0
(function() {
var MojioAuthSDK, MojioModelSDK,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
MojioModelSDK = require('./MojioModelSDK');
module.exports = MojioAuthSDK = (function(superClass) {
var authorizeParameters, defaults, ref, ref1, ref2, setup, styleParameters, token, tokenParameters, unauthorizeParameters;
extend(MojioAuthSDK, superClass);
defaults = {
accountsURL: 'accounts.moj.io'
};
styleParameters = ['callback', 'promise', 'sync', 'subscribe', 'observable', 'async'];
token = null;
function MojioAuthSDK(options) {
if (options == null) {
options = {};
}
MojioAuthSDK.__super__.constructor.call(this);
this.configure(options, defaults);
this.currentUser = null;
this.stateMachine.client = this.client_id;
this.stateMachine.secret = this.client_secret;
this.stateMachine.site = this.site;
this.stateMachine.tokenPath = this.tokenPath;
this.stateMachine.authorizationPath = this.authorizationPath;
}
setup = function(parameters, match, name) {
var property, results, value;
if (typeof parameters === 'object') {
results = [];
for (property in parameters) {
value = parameters[property];
if ((indexOf.call(match, property) >= 0)) {
results.push(eval("this." + property)(value));
} else {
throw new Error("Parameter not used in " + name + " flow: " + property);
}
}
return results;
}
};
(ref = (authorizeParameters = ['username', 'password', 'credentials', 'scope', 'email'])).push.apply(ref, styleParameters);
MojioAuthSDK.prototype.authorize = function(redirect_url, implicit) {
if (implicit == null) {
implicit = null;
}
if (implicit == null) {
implicit = typeof document !== "undefined" && document !== null;
}
setup(redirect_url, authorizeParameters, 'authorize');
this.stateMachine.setMethod("POST");
this.stateMachine.setEndpoint("accounts");
this.stateMachine.setResource("oauth2");
this.stateMachine.setAction("authorize");
if (this.sdk_env === 'browser' || implicit) {
this.stateMachine.setBody({
response_type: 'token',
redirect_uri: redirect_url,
client_id: this.client_id
});
} else if (this.sdk_env === 'nodejs') {
this.stateMachine.setBody({
response_type: 'code',
redirect_uri: redirect_url,
client_id: this.client_id
});
}
return this;
};
(ref1 = (unauthorizeParameters = ['login', 'consent', 'loginAndConsent', 'prompt', 'parse', 'code'])).push.apply(ref1, styleParameters);
MojioAuthSDK.prototype.unauthorize = function(redirect_url, implicit) {
if (implicit == null) {
implicit = null;
}
if ((typeof document !== "undefined" && document !== null) && (typeof implict === "undefined" || implict === null)) {
implicit = true;
} else {
implicit = false;
}
setup(redirect_url, unauthorizeParameters, 'unauthorize');
this.stateMachine.setMethod("POST");
this.stateMachine.setEndpoint("accounts");
this.stateMachine.setResource("oauth2");
this.stateMachine.setAction("authorize");
if (this.sdk_env === 'browser' || implicit) {
this.stateMachine.setBody({
response_type: 'token',
redirect_uri: redirect_url,
client_id: this.client_id
});
} else if (this.sdk_env === 'nodejs') {
this.stateMachine.setBody({
response_type: 'code',
redirect_uri: redirect_url,
client_id: this.client_id
});
}
return this;
};
(ref2 = (tokenParameters = [])).push.apply(ref2, authorizeParameters);
MojioAuthSDK.prototype.token = function(redirect_url) {
var redirect_uri;
if (redirect_url == null) {
redirect_url = null;
}
if ((redirect_url != null)) {
if (!setup(redirect_url, tokenParameters, 'token')) {
redirect_uri = redirect_url;
} else {
redirect_uri = this.stateMachine.getBody().redirect_uri;
}
}
this.stateMachine.setMethod("POST");
this.stateMachine.setEndpoint("accounts");
this.stateMachine.setResource("oauth2");
this.stateMachine.setAction("token");
this.stateMachine.setBody({
client_id: this.client_id,
client_secret: this.client_secret
});
if (redirect_uri != null) {
this.stateMachine.setBody({
redirect_uri: redirect_uri
});
}
return this;
};
MojioAuthSDK.prototype.parse = function(return_url) {
var code, i, len, obj, ref3, t;
if ((return_url != null) && (return_url.query != null) && (return_url.query.code != null)) {
code = return_url.query.code;
this.stateMachine.setBody({
code: code,
grant_type: 'authorization_code'
});
this.stateMachine.setCallback((function(_this) {
return function(error, result) {
if (error) {
return console.log('Access Token Error', JSON.stringify(error.content) + " message:" + error.statusMessage);
} else {
return _this.stateMachine.setToken(result);
}
};
})(this));
} else if (return_url.location != null) {
if (return_url.location.hash === "") {
this.stateMachine.setAnswer("");
} else if ((return_url.location.hash != null)) {
obj = {};
ref3 = return_url.location.hash.split("#")[1].split("&");
for (i = 0, len = ref3.length; i < len; i++) {
t = ref3[i];
obj[t.split("=")[0]] = t.split("=")[1];
}
this.stateMachine.setToken(obj);
this.stateMachine.setAnswer(obj);
}
} else if (typeof return_url === 'object' && (return_url.access_token != null)) {
this.stateMachine.setToken(return_url);
this.stateMachine.setAnswer(return_url);
} else if (typeof return_url === 'string') {
return_url = {
access_token: return_url,
expires_in: "unknown",
referesh_token: "unknown",
token_type: "bearer"
};
this.stateMachine.setToken(return_url);
this.stateMachine.setAnswer(return_url);
}
return this;
};
MojioAuthSDK.prototype.refresh = function(refresh_token) {
this.stateMachine.setBody({
refresh_token: refresh_token,
grant_type: 'refresh_token'
});
return this;
};
MojioAuthSDK.prototype.login = function() {
this.prompt({
prompt: 'login'
});
return this;
};
MojioAuthSDK.prototype.consent = function() {
this.prompt({
prompt: 'consent'
});
return this;
};
MojioAuthSDK.prototype.loginAndConsent = function() {
this.prompt({
prompt: 'consent,login'
});
return this;
};
MojioAuthSDK.prototype.prompt = function(prompt) {
if ((this.stateMachine.getBody().prompt != null) && ((this.stateMachine.getBody().prompt === 'login' && prompt.prompt === 'consent' || prompt === 'consent') || (this.stateMachine.getBody().prompt === 'consent' && prompt.prompt === 'login' || prompt === 'login'))) {
this.stateMachine.setBody({
prompt: 'consent,login'
});
} else if (typeof prompt === 'string') {
this.stateMachine.setBody({
prompt: prompt
});
} else if (prompt instanceof Array) {
this.stateMachine.setBody({
prompt: prompt.join()
});
} else {
this.stateMachine.setBody(prompt);
}
return this;
};
MojioAuthSDK.prototype.scope = function(scopes) {
var param;
if (typeof scopes === 'string') {
param = scopes.replace(/,/g, ' ');
this.stateMachine.setBody({
scope: param
});
} else {
param = '';
scopes.map(function(scope) {
return param += scope + ' ';
});
this.stateMachine.setBody({
scope: param.slice(0, -1)
});
}
return this;
};
MojioAuthSDK.prototype.username = function(username) {
this.stateMachine.setBody({
username: username
});
return this;
};
MojioAuthSDK.prototype.email = function(email) {
this.stateMachine.setBody({
username: email
});
return this;
};
MojioAuthSDK.prototype.password = function(password) {
this.stateMachine.setBody({
password: password
});
return this;
};
MojioAuthSDK.prototype.credentials = function(usernameOrEmail_or_credentials, password) {
var credentials;
if (password == null) {
password = null;
}
if (typeof usernameOrEmail_or_credentials === 'object') {
credentials = usernameOrEmail_or_credentials;
} else {
credentials = {
username: usernameOrEmail_or_credentials,
password: password
};
}
this.stateMachine.validator.credentials(credentials);
credentials['grant_type'] = 'password';
this.stateMachine.setBody(credentials);
return this;
};
MojioAuthSDK.prototype["with"] = function(usernameOrEmail_or_credentials, password) {
if (password == null) {
password = null;
}
return this.credentials(usernameOrEmail_or_credentials, password);
};
MojioAuthSDK.prototype.getToken = function() {
return this.stateMachine.getToken();
};
return MojioAuthSDK;
})(MojioModelSDK);
}).call(this);
//# sourceMappingURL=MojioAuthSDK.js.map