0

Looking at this question I can see there is a way to generate jwt token signed by service account using google auth library

def generate_jwt():
    payload = {"iat": iat, "exp": exp, "iss": iss, "aud":  aud, "sub": iss, "email": iss, "company": company}

    signer = google.auth.crypt.RSASigner.from_service_account_file(sa_keyfile)
    jwt = google.auth.jwt.encode(signer, payload)

    return jwt
  1. How do I achieve this with nodejs?
  2. In the Security section of .yaml file what is the value I need to use for x-google-issuer and x-google-jwks_uri, and please explain in detail what these values are for?
 x-google-issuer: "mygserviceaccount"
 x-google-jwks_uri: "mygserviceaccount.com"

if for example I generated jwt using service account email: resource-access@xyz.iam.gserviceaccount.com then x-google-issuer:"resource-access@xyz.iam.gserviceaccount.com" and x-google-jwks_uri:"https://www.googleapis.com/service_accounts/v1/metadata/x509/resource-access@xyz.iam.gserviceaccount.com" is this accurate?

tars
  • 1

1 Answers1

0

If someone is still looking for the answer, I tried this, It worked for me

Code to generate JWT signed by service account.

const path = require("path");                                         
     let jwt = require("jsonwebtoken");                                                           
     let servAcc = require(path.join(__dirname,pathToServAcc));                                         let token = jwt.sign(                                                  {
        iss: servAcc.client_email,
        sub: req.query.User_ID || "",
        aud: process.env.GATEWAY_MANAGED_SERVICE_URL,
        iat: Math.floor(Date.now() / 1000),
        exp: Math.floor(Date.now() / 1000) + 1000000,
      },
      servAcc.private_key,
      { algorithm: "RS256" }
    );

Security section of yaml file

securityDefinitions:   api_key:
    type: "apiKey"
    name: "x-api-key"
    in: "header"   jwt_auth:
    authorizationUrl: ""
    flow: "implicit"
    type: "oauth2"
    x-google-issuer: "service acc email"
    x-google-jwks_uri: "https://www.googleapis.com/robot/v1/metadata/x509/service acc email"
tom redfern
  • 30,562
  • 14
  • 91
  • 126
tars
  • 1