GCP Consume a REST API after OAuth in Node.js
NickName:vdenotaris Ask DateTime:2018-02-25T08:09:40

GCP Consume a REST API after OAuth in Node.js

I am working to implement a Node.js webapp to be deployed on GCP App Engine.

Following the Node.js Bookshelf App sample, I did manage to implement a basic user authentication flow using the passport-google-oauth20 and retrieve basic profile information. I basically just got rid of what was not needed for my purposes

My custom code is available at: gist.github.com/vdenotaris/3a6dcd713e4c3ee3a973aa00cf0a45b0.

However, I would now like to consume a GCP Cloud Storage API to retrieve all the storage objects within a given buckets with the logged identity. This should be possible by:

  • adding a proper scope for the request.
  • authenticating the REST requests using the user session token obtained via OAuth.

About the post-auth handler, the documentation says:

After you obtain credentials, you can store information about the user. Passport.js automatically serializes the user to the session. After the user’s information is in the session, you can make a couple of middleware functions to make it easier to work with authentication.

// Middleware that requires the user to be logged in. If the user is not logged
// in, it will redirect the user to authorize the application and then return
// them to the original URL they requested.
function authRequired (req, res, next) {
  if (!req.user) {
    req.session.oauth2return = req.originalUrl;
    return res.redirect('/auth/login');
  }
  next();
}

// Middleware that exposes the user's profile as well as login/logout URLs to
// any templates. These are available as `profile`, `login`, and `logout`.
function addTemplateVariables (req, res, next) {
  res.locals.profile = req.user;
  res.locals.login = `/auth/login?return=${encodeURIComponent(req.originalUrl)}`;
  res.locals.logout = `/auth/logout?return=${encodeURIComponent(req.originalUrl)}`;
  next();
}

But I do not see where the token is stored, how can I retrieve it and how to use it to consume a web-service (in my case, GCP storage).

I am not at all a node.js expert, so it would be nice having a bit more clarity on that: could someone explain me how to proceed in consuming a REST API using the logged user credentials (thus IAM/ACL privileges)?

Copyright Notice:Content Author:「vdenotaris」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48968939/gcp-consume-a-rest-api-after-oauth-in-node-js

More about “GCP Consume a REST API after OAuth in Node.js” related questions

GCP Consume a REST API after OAuth in Node.js

I am working to implement a Node.js webapp to be deployed on GCP App Engine. Following the Node.js Bookshelf App sample, I did manage to implement a basic user authentication flow using the passport-

Show Detail

How to properly set Authorization to consume Wordpress Rest API (Oauth1) with Nativescript[2.5.4]?

I'm trying to consume the WP Rest API with Nativescript. The WP API and the Authorization with OAUTH1 is already well setup and tested with POSTMAN. Nativescript Login function to consume rest is

Show Detail

NodeJS Consume Oauth Get API Call

I am trying to consume data from OAuth Enabled get API call in NodeJS. I need to pass two Online-Magazine-Subscription-Key and access token. I used 'request' module to consume API call. var access...

Show Detail

How can I consume a rest webservice with oauth 1.0a using spring?

I have a Spring MVC WebApp and a wordpress site with wp api configured alongside oauth 1.0a authentication. I have the following values to authenticate: oauth_consumer_key oauth_consumer_secret

Show Detail

OAuth authentication or JWT authentication using TIBCO for GCP

I want to invoke GCP REST API from TIBCO BW6.X but before that i need to authenticate my request either using OAuth or JWT. In BW5.X there was option to configure OAuth authentication in 'Invoke RE...

Show Detail

Authorizing REST API calls to GCP

I am totally new to GCP products. I am trying to create and manage the resources i.e. Virtual Machines using the REST API's provided in the documentation. One of the endpoints which I am trying is ...

Show Detail

Consume an OAuth-secured REST webservice using Spring oauth2

I want to consume a REST webservice from a server which protects his resources using oauth2. I use Spring boot (JHipster). To do this i have in SecurityConfiguration class this : @Value("${oauth.

Show Detail

Consume Workday REST API with oAuth 2.0 in .NET MVC

I am trying to call the Workday REST API in my .NET MVC application and am having trouble with the oAuth 2.0 aspect of it. Although in my case I am talking about Workday in particular, I think what...

Show Detail

consume Oauth2 (authorization code) rest api with spring rest template

I'm trying to consume a rest web service in spring integration project. This web service is secured with oauth2 (authorization code).Any idea how to achieve this? I tried using OAuth2RestTemplate ...

Show Detail

Consume WP rest api in Java

I am trying consume OAuth 1.0a from wordpress using the plugin "WP REST API - OAuth 1.0a Server" in a Java Swing application. Following the instructions of this link (https://code.tutsplus.com/tut...

Show Detail