|
| 1 | +// provide service to handle asana credential authorization |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const APIServerModule = require(process.env.CS_API_TOP + '/lib/api_server/api_server_module.js'); |
| 6 | +const fetch = require('node-fetch'); |
| 7 | +const FS = require('fs'); |
| 8 | + |
| 9 | +class AsanaAuth extends APIServerModule { |
| 10 | + |
| 11 | + services () { |
| 12 | + return async () => { |
| 13 | + return { asanaAuth: this }; |
| 14 | + }; |
| 15 | + } |
| 16 | + |
| 17 | + async handleAuthRedirect (options) { |
| 18 | + const { request, provider, state } = options; |
| 19 | + const { config } = request.api; |
| 20 | + const { publicApiUrl, environment } = config.api; |
| 21 | + const { appClientId } = config.asana; |
| 22 | + const { response } = request; |
| 23 | + const parameters = { |
| 24 | + client_id: appClientId, |
| 25 | + redirect_uri: `${publicApiUrl}/no-auth/provider-token/${provider}/${environment}`, |
| 26 | + response_type: 'code', |
| 27 | + state |
| 28 | + }; |
| 29 | + const query = Object.keys(parameters) |
| 30 | + .map(key => `${key}=${encodeURIComponent(parameters[key])}`) |
| 31 | + .join('&'); |
| 32 | + response.redirect(`https://app.asana.com/-/oauth_authorize?${query}`); |
| 33 | + request.responseHandled = true; |
| 34 | + } |
| 35 | + |
| 36 | + async preProcessTokenCallback (options) { |
| 37 | + // must exchange the provided authorization code for an access token |
| 38 | + const { request, state, provider } = options; |
| 39 | + const { config } = request.api; |
| 40 | + const { publicApiUrl, environment } = config.api; |
| 41 | + const { appClientId, appClientSecret } = config.asana; |
| 42 | + const code = request.request.query.code || ''; |
| 43 | + const parameters = { |
| 44 | + grant_type: 'authorization_code', |
| 45 | + client_id: appClientId, |
| 46 | + client_secret: appClientSecret, |
| 47 | + code, |
| 48 | + redirect_uri: `${publicApiUrl}/no-auth/provider-token/${provider}/${environment}`, |
| 49 | + state |
| 50 | + }; |
| 51 | + const FormData = require('form-data'); |
| 52 | + const form = new FormData(); |
| 53 | + Object.keys(parameters).forEach(key => { |
| 54 | + form.append(key, parameters[key]/*encodeURIComponent(parameters[key])*/); |
| 55 | + }); |
| 56 | + const url = 'https://app.asana.com/-/oauth_token'; |
| 57 | + const response = await fetch( |
| 58 | + url, |
| 59 | + { |
| 60 | + method: 'post', |
| 61 | + body: form |
| 62 | + } |
| 63 | + ); |
| 64 | + const responseData = await response.json(); |
| 65 | + return { |
| 66 | + accessToken: responseData.access_token, |
| 67 | + refreshToken: responseData.refresh_token, |
| 68 | + data: responseData.data |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + getAfterAuthHtml () { |
| 73 | + return this.afterAuthHtml; |
| 74 | + } |
| 75 | + |
| 76 | + initialize () { |
| 77 | + this.afterAuthHtml = FS.readFileSync(this.path + '/afterAuth.html', { encoding: 'utf8' }); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +module.exports = AsanaAuth; |
0 commit comments