OraOpenSource / node4ords

Simple node app to act as web listener for Oracle APEX pages
http://www.oraopensource.com/node4ords/
MIT License
23 stars 5 forks source link

How to use apex_oauth with node4ords ? #33

Open tiagosteil opened 7 years ago

tiagosteil commented 7 years ago

Hi

I'm using node4ords as weblistener on my server with the OXAR project: Https://github.com/OraOpenSource/OXAR

And now I want to use this project below to be able to perform the Login with Gmail: Https://github.com/smart4solutions/apex_oauth

However I have a problem to redirect the user to the Google login page, which I researched is due to CORS. In the apex_oauth project Is cited to create a reverse proxy.

captura de tela 2017-04-15 as 12 04 11

How do I do this reverse proxy on node4ords?

Thank you for your attention.

martindsouza commented 7 years ago

I'm not 100% sure how to resolve this issue. You could try to modify /opt/node4ords/app.js and set res.setHeader('Access-Control-Allow-Origin', '*'); in the middleware.

@krisrice any ideas?

dmcghan commented 7 years ago

If you're redirecting then why would you need CORS? Maybe @rhjmartens could help.

tiagosteil commented 7 years ago

Hi,

The above error occurs when the application tries to make the code below:

t_url := 'https://accounts.google.com/o/oauth2/auth?client_id=' || g_provider.client_id 
                        || '&redirect_uri='  || g_provider.redirect_uri 
                        || '&scope='         || apex_util.url_encode(g_provider.scope)
                        || '&state='         || v('APP_SESSION') || ':' || v('WORKSPACE_ID') || ':' || v('APP_ID') || ':' || p_gotopage
                        || '&response_type=' || 'code'  -- mandatory for google
                        || g_provider.extras
                        || case g_provider.force_approval
                             when 'Y' then '&approval_prompt=force'
                             else ''
                           end;

owa_util.redirect_url ( t_url );

So I changed the /opt/node4ords/app.js file and created the following proxy rule below:

app.use('/acgoogle', proxy(
  {
    target: 'https://accounts.google.com/',
    changeOrigin: true,
    https: true,
    onProxyRes: function(proxyRes, req, res){

        proxyRes.headers['Access-Control-Allow-Origin'] = '*';

        if (req.headers['access-control-request-method']) {
          res.setHeader('access-control-allow-methods', req.headers['access-control-request-method']);
        }

        if (req.headers['access-control-request-headers']) {
          res.setHeader('access-control-allow-headers', req.headers['access-control-request-headers']);
        }

    },
    pathRewrite: {
      '^/acgoogle' : '/'
    }
  }
)); 

After that, I changed the redirect link to the link below:

t_url := 'http://myhost/acgoogle/o/oauth2/auth?client_id=' || g_provider.client_id 
                        || '&redirect_uri='  || g_provider.redirect_uri 
                        || '&scope='         || apex_util.url_encode(g_provider.scope)
                        || '&state='         || v('APP_SESSION') || ':' || v('WORKSPACE_ID') || ':' || v('APP_ID') || ':' || p_gotopage
                        || '&response_type=' || 'code'  -- mandatory for google
                        || g_provider.extras
                        || case g_provider.force_approval
                             when 'Y' then '&approval_prompt=force'
                             else ''
                           end
                        ;   

owa_util.redirect_url ( t_url ); 

And now the error that is being displayed is the error below:

captura de tela 2017-04-17 as 15 55 12

When I click on the link generated by pck, the google page opens correctly ... however redirect does not work.

I believe there is something wrong or missing from my proxy rule.

Thanks for listening.

rmartenss4s commented 7 years ago

Hi,

Yes I think your proxy rule is not in order.

Try to use the URL in your log in a web-browser. If you do not see Google page, proxy is not working.

I do not know how to set up proxy on node.js

Met vriendelijke groet, Kind regards, Vale

Richard Martens

Bosscheweg 44 a

5056 KC Berkel-Enschot

The Netherlands

t: +31 6 579 46 631

e: r.martens@smart4solutions.nl

Chamber of Commerce: 67.49.69.46

The information contained in this communication is intended solely for the use of the individual or entity to whom it is addressed. If you are not the intended recipient of this communication please return the communication to the sender and delete and destroy all copies. In that case you must not use, disclose, distribute, copy, print or rely on this communication.

SMART4Solutions B.V. is neither liable for the proper and complete transmission of the information contained in this communication nor for any delay in its receipt. SMART4Solutions B.V. does not guarantee that the integrity of this communication has been maintained nor that the communication is free of viruses, interceptions or interference.

Van: tiagosteil notifications@github.com Beantwoorden - Aan: OraOpenSource/node4ords reply@reply.github.com Datum: maandag 17 april 2017 21:27 Aan: OraOpenSource/node4ords node4ords@noreply.github.com CC: Richard Martens r.martens@smart4apex.nl, Mention mention@noreply.github.com Onderwerp: Re: [OraOpenSource/node4ords] How to use apex_oauth with node4ords ? (#33)

Hi,

The above error occurs when the application tries to make the code below: t_url := 'https://accounts.google.com/o/oauth2/auth?client_id=' || g_provider.client_id                         || '&redirect_uri='  || g_provider.redirect_uri                         || '&scope='         || apex_util.url_encode(g_provider.scope)                         || '&state='         || v('APP_SESSION') || ':' || v('WORKSPACE_ID') || ':' || v('APP_ID') || ':' || p_gotopage                         || '&response_type=' || 'code'  -- mandatory for google                         || g_provider.extras                         || case g_provider.force_approval                              when 'Y' then '&approval_prompt=force'                              else ''                            end;

owa_util.redirect_url ( t_url ); So I changed the /opt/node4ords/app.js file and created the following proxy rule below: app.use('/acgoogle', proxy(   {     target: 'https://accounts.google.com/',     changeOrigin: true,     https: true,     onProxyRes: function(proxyRes, req, res){

        proxyRes.headers['Access-Control-Allow-Origin'] = '*';

        if (req.headers['access-control-request-method']) {           res.setHeader('access-control-allow-methods', req.headers['access-control-request-method']);         }

       if (req.headers['access-control-request-headers']) {           res.setHeader('access-control-allow-headers', req.headers['access-control-request-headers']);         }

    },     pathRewrite: {       '^/acgoogle' : '/'     }   } )); After that, I changed the redirect link to the link below: t_url := 'http://myhost/acgoogle/o/oauth2/auth?client_id=' || g_provider.client_id                         || '&redirect_uri='  || g_provider.redirect_uri                         || '&scope='         || apex_util.url_encode(g_provider.scope)                         || '&state='         || v('APP_SESSION') || ':' || v('WORKSPACE_ID') || ':' || v('APP_ID') || ':' || p_gotopage                         || '&response_type=' || 'code'  -- mandatory for google                         || g_provider.extras                         || case g_provider.force_approval                              when 'Y' then '&approval_prompt=force'                              else ''                            end                         ;  

owa_util.redirect_url ( t_url ); And now the error that is being displayed is the error below:

When I click on the link generated by pck, the google page opens correctly ... however redirect does not work.

I believe there is something wrong or missing from my proxy rule.

Thanks for listening.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

krisrice commented 7 years ago

The origin is getting lost somehow. I've never used this but did notice this in the wiki 💡 Tip: Set the option changeOrigin to true for name-based virtual hosted sites.

tiagosteil commented 7 years ago

The origin is getting lost somehow. I've never used this but did notice this in the wiki 💡 Tip: Set the option changeOrigin to true for name-based virtual hosted sites.

I put this option to true, but I do not know if it's in the correct place.

app.use('/acgoogle', proxy(
  {
    target: 'https://accounts.google.com/',
    changeOrigin: true,
    https: true,
    onProxyRes: function(proxyRes, req, res){

        proxyRes.headers['Access-Control-Allow-Origin'] = '*';

        if (req.headers['access-control-request-method']) {
          res.setHeader('access-control-allow-methods', req.headers['access-control-request-method']);
        }

        if (req.headers['access-control-request-headers']) {
          res.setHeader('access-control-allow-headers', req.headers['access-control-request-headers']);
        }

    },
    pathRewrite: {
      '^/acgoogle' : '/'
    }
  }
)); 
tiagosteil commented 7 years ago

Try to use the URL in your log in a web-browser. If you do not see Google page, proxy is not working.

When I access the proxy URL I see the following page...

captura de tela 2017-04-18 as 09 04 08

When I access the link that is in the chrome console error, I see the following page

captura de tela 2017-04-18 as 10 54 36