55const APIServerModule = require ( process . env . CS_API_TOP + '/lib/api_server/api_server_module.js' ) ;
66const fetch = require ( 'node-fetch' ) ;
77const FS = require ( 'fs' ) ;
8+ const FormData = require ( 'form-data' ) ;
89
910class AsanaAuth extends APIServerModule {
1011
@@ -14,44 +15,36 @@ class AsanaAuth extends APIServerModule {
1415 } ;
1516 }
1617
17- async handleAuthRedirect ( options ) {
18- const { request, provider, state } = options ;
19- const { config } = request . api ;
20- const { authOrigin } = config . api ;
21- const { appClientId } = config . asana ;
22- const { response } = request ;
18+ // get redirect parameters and url to use in the redirect response
19+ getRedirectData ( options ) {
20+ const { request, redirectUri, state } = options ;
21+ const { appClientId } = request . api . config . asana ;
2322 const parameters = {
2423 client_id : appClientId ,
25- redirect_uri : ` ${ authOrigin } /provider-token/ ${ provider } ` ,
24+ redirect_uri : redirectUri ,
2625 response_type : 'code' ,
2726 state
2827 } ;
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 ;
28+ const url = 'https://app.asana.com/-/oauth_authorize' ;
29+ return { url, parameters } ;
3430 }
3531
36- async preProcessTokenCallback ( options ) {
32+ // given an auth code, exchange it for an access token
33+ async exchangeAuthCodeForToken ( options ) {
3734 // must exchange the provided authorization code for an access token
38- const { request, state, provider } = options ;
39- const { config } = request . api ;
40- const { authOrigin } = config . api ;
41- const { appClientId, appClientSecret } = config . asana ;
42- const code = request . request . query . code || '' ;
35+ const { request, state, code, redirectUri } = options ;
36+ const { appClientId, appClientSecret } = request . api . config . asana ;
4337 const parameters = {
4438 grant_type : 'authorization_code' ,
4539 client_id : appClientId ,
4640 client_secret : appClientSecret ,
4741 code,
48- redirect_uri : ` ${ authOrigin } /provider-token/ ${ provider } ` ,
42+ redirect_uri : redirectUri ,
4943 state
5044 } ;
51- const FormData = require ( 'form-data' ) ;
5245 const form = new FormData ( ) ;
5346 Object . keys ( parameters ) . forEach ( key => {
54- form . append ( key , parameters [ key ] /*encodeURIComponent(parameters[key])*/ ) ;
47+ form . append ( key , parameters [ key ] ) ;
5548 } ) ;
5649 const url = 'https://app.asana.com/-/oauth_token' ;
5750 const response = await fetch (
@@ -65,15 +58,19 @@ class AsanaAuth extends APIServerModule {
6558 return {
6659 accessToken : responseData . access_token ,
6760 refreshToken : responseData . refresh_token ,
61+ expiresAt : Date . now ( ) + ( 59 * 60 * 1000 + 55 * 1000 ) , // token good for one hour, we'll give a 5-second margin
6862 data : responseData . data
6963 } ;
7064 }
7165
66+ // get html to display once auth is complete
7267 getAfterAuthHtml ( ) {
7368 return this . afterAuthHtml ;
7469 }
7570
71+ // initialize the module
7672 initialize ( ) {
73+ // read in the after-auth html to display once auth is complete
7774 this . afterAuthHtml = FS . readFileSync ( this . path + '/afterAuth.html' , { encoding : 'utf8' } ) ;
7875 }
7976}
0 commit comments