Search code examples
google-app-enginegoogle-oauth

GAE: Is there a way to authorize version endpoints with GCP OAuth redirect URLs?


I thought I was smart by using a custom version tag for my applications so I could predetermine the fully qualified version-URL. And my integration tests work wonders when I do this, however, I can't figure out a way around my OAuth rules.

Currently, wildcard Authorized redirect URIs aren't allowed:

Wildcard redirect not allowed

What I want to achieve is basically a fully functional app (consist of three services) that has yet to be promoted. That way our testers can greenlight to deployment before the deployment.

Anyone got any idea is such a thing is possible?


Solution

  • So thanks to @sllopis's comment I ended up creating a login service that took care of the redirecting to multiple versions of the same service.

    It isn't the perfect solution, but it is one we can use. OAuth to our unpublished apps.

    //@ts-check
    const fastify = require("fastify")({ logger: true });
    const {
      google: {
        auth: { OAuth2 }
      }
    } = require("googleapis");
    
    const { googleLoginCallback, googleClientId, googleClientSecret } = process.env;
    
    const defaultScope = [
      "https://www.googleapis.com/auth/plus.me",
      "https://www.googleapis.com/auth/userinfo.email"
    ];
    
    const getGoogleRedirecturl = () =>
      new OAuth2(
        googleClientId,
        googleClientSecret,
        googleLoginCallback
      ).generateAuthUrl({
        access_type: "offline",
        prompt: "consent",
        scope: defaultScope
      });
    
    fastify.get("/login", (req, reply) => {
      const { versionEndpoint } = req.query;
      const encodedEndpoint = encodeURIComponent(versionEndpoint);
      const query = `&state=${encodedEndpoint}`;
    
      reply.redirect(getGoogleRedirecturl() + query);
    });
    
    fastify.get("/login/callback", (req, reply) => {
      const { code, state } = req.query;
    
      const endpoint = decodeURIComponent(state);
    
      reply.redirect(endpoint + "?code=" + code);
    });
    
    fastify
      .listen(3000)
      .then(str => {
        fastify.log.info(`server listening on ${fastify.server.address()}`, str);
      })
      .catch(err => {
        fastify.log.error(err);
        process.exit(1);
      });