UNPKG

2.23 kBJavaScriptView Raw
1const e=require("bluebird"),i=require("./files"),t=require("./misc"),n=require("./display"),r=require("./api"),o=require("./credentials"),u=require("./messagehandler").messagesHandler,l=require("path"),a=require("lodash"),c=require("inquirer"),s=require("crypto").randomBytes,d=t.isValidApp,h=module.exports={};function f(e){return e+"_"+j()}function p(t,n){return e.coroutine(function*(){const e=l.join(config.APP_DIR,"oauth.json");var r=yield i.readFile(e);r=r.toString();const o={title:t,redirectURL:`https://flowoauth.built.io/oauth/${n}/cli/return`},u=a.template(r,{interpolate:/{{([\s\S]+?)}}/g})(o);yield i.writeFile(e,u)})().catch(i=>e.reject(i))}function j(){return s(5).toString("hex")}h.createAuth=function(t,n){return console.log("Creating auth ",t),t?"noauth"==t?i.getIndex().then(e=>(e.auth_type=t,i.writeFile(l.join(config.APP_DIR,"index.json"),JSON.stringify(e,null,2)))):e.coroutine(function*(){var e=l.resolve(CLI_PATH,"scaffold","auth",t+".js"),r=yield i.getIndex(),o={service:a.upperFirst(r.name)};o=n?a.extend(o,{oauth:n}):o;var u=a.template(yield i.readFile(e))(o);yield i.writeFile(l.join(config.APP_DIR,"authentication.js"),u),r.auth_type=t,yield i.writeFile(l.join(config.APP_DIR,"index.json"),JSON.stringify(r,null,2))})().catch(i=>e.reject(i)):e.reject("Auth type required received undefined or null")},h.createOauthTemplate=function(t,n){return e.coroutine(function*(){if("OAuth1"===t){const e=l.join(CLI_PATH,"snippets","oauth1.json"),t=l.join(global.config.APP_DIR,"oauth.json");i.copyFile(e,t)}else{const e=l.join(CLI_PATH,"snippets","oauth2.json"),t=l.join(global.config.APP_DIR,"oauth.json");i.copyFile(e,t)}const e=l.resolve(CLI_PATH,"scaffold","auth","oauth.js"),r=yield i.getIndex();var o={service:a.upperFirst(r.name)},c=f(n).toLowerCase(),s=`https://flowoauth.built.io/oauth/${c}/cli/return`;o=a.extend(o,{oauth:c,redirectURL:s});var d=a.template(yield i.readFile(e))(o);yield i.writeFile(l.join(config.APP_DIR,"authentication.js"),d),r.auth_type="oauth",r.oauth_provider=c,yield i.writeFile(l.join(config.APP_DIR,"index.json"),JSON.stringify(r,null,2)),yield p(n,c),Console.info(u("OAUTH_CUSTOM_SUCCESS")),Console.info(u("OAUTH_REDIRECT_URL",{redirect_url:s}))})().catch(i=>e.reject(i))};
\No newline at end of file