UNPKG

1.62 kBJavaScriptView Raw
1const e=require("../utils"),t=require("fs-extra"),r=require("bluebird"),a=require("path"),i=e.messagesHandler,u=["basic","apikey","oauth","custom"];function o(t){return r.coroutine(function*(){if(!t)return r.resolve(!0);var a=yield e.checkCredentials();if(!a)return r.reject(i("UNAUTHORIZED"));const u={method:"GET",headers:{accesstoken:a}};return 200===(yield e.makeApiCall(`/oauth/${t}/validate`,u)).statusCode?r.resolve(!0):r.resolve(!1)})().catch(e=>r.reject(e))}module.exports=function(){return r.coroutine(function*(){const t=i("SELECT_AUTH_TYPE");var c=yield e.createAuthList(t,u);if("oauth"==c.auth){if("create new"===(yield e.createAuthList("Select one of the type",["built-in","create new"])).auth){try{var n=require(a.join(config.APP_DIR,"authentication.js"))}catch(e){}if(n&&""!==n.oauth)if(!(yield o(n.oauth)))return r.reject("Invalid provider");var s=i("OAUTH_TYPE"),h=yield e.getOAuthType(s,["OAuth1","OAuth2"]),l=i("OAUTH_NAME_PROMPT"),d=yield e.getInput(l,!1,/^[\w]+$/);yield e.createOauthTemplate(h.auth_type.trim(),d)}else{var p={headers:{accesstoken:yield e.checkCredentials()}},y=yield e.makeApiCall("/oauths",p),A=(l=i("SELECT_OAUTH_TYPE"),y.body.map(e=>e.name)),m=yield e.createAuthList(l,A);yield e.createAuth(c.auth.trim(),m.auth.trim())}}else yield e.createAuth(c.auth.trim());return e.setHistory("Auth","Auth created of "+c.auth),!0})().catch(e=>r.reject(e))},module.exports.argSpecs=[],module.exports.argOptionsSpecs={},module.exports.help="Setup an authentication type for the connector",module.exports.snippet="flow auth",module.exports.docs="Setup an authentication type for the connector";
\No newline at end of file