簡體   English   中英

如何在 GAE flex(創建反應啟動器)Web 應用程序中訪問 IAP 身份?

[英]How do I access IAP identity within GAE flex (create react starter) web app?

所以我有一個創建 react starter web 應用程序,我用它來了解如何將 web 應用程序部署到 GAE。 我為受限訪問用戶啟用了 IAP。 其中一部分也是以 CI/CD 方式進行的,我已經成功實現了這一點。 我正在努力理解的是我將如何能夠訪問用戶的身份。

我知道標題應該有這些信息。 因此,為了從應用程序中獲取標題,我添加了:

var req = new XMLHttpRequest();
req.open('GET', document.location, false);
req.send(null);
var headers = req.getAllResponseHeaders().toLowerCase();
headers = headers.split(/\n|\r|\r\n/g).reduce(function(a, b) {
    if (b.length) {
        var [ key, value ] = b.split(': ');
        a[key] = value;
    }
    return a;
}, {});
console.log(`${JSON.stringify(headers)}`);

但我似乎無法找到以下字段/值:

X-Goog-IAP-JWT-斷言

然后我假設我可以用來獲取身份,如下所示: https : //cloud.google.com/nodejs/getting-started/authenticate-users#create_the_source_code

僅作為上下文,如果有幫助,我的 app.yaml 是:

runtime: nodejs12
env: flex
instance_class: F1
handlers:
  # Serve all static files with url ending with a file extension
  - url: /(.*\..+)$
    static_files: build/\1
    require_matching_file: false
    upload: build/(.*\..+)$
    http_headers:
      Access-Control-Allow-Origin: "*"
  # Catch all handler to index.html
  - url: /.*
    static_files: build/index.html
    require_matching_file: false
    upload: build/index.html
    http_headers:
      Access-Control-Allow-Origin: "*"
  - url: .*
    script: auto
automatic_scaling:
  min_idle_instances: automatic
  max_idle_instances: automatic
  min_pending_latency: automatic
  max_pending_latency: automatic
network: {}

我看到的其他問題只有 1 個,有人在那里遇到了同樣的問題,但他們並沒有真正詳細說明他們是如何解決的。

您正在混淆 App Engine 標准(例如處理程序和縮放元素)和靈活( 例如資源app.yaml配置。 即使它沒有給您任何錯誤,也只是被忽略了。 仍然建議根據您的應用程序需求修復您的app.yaml

如果您嘗試使用來自 IAP 和 App Engine 的x-goog-iap-jwt-assertion獲取用戶的信息(電子郵件和用戶 ID),以下是完整示例

let expectedAudience = null;
if (projectNumber && projectId) {
  // Expected Audience for App Engine.
  expectedAudience = `/projects/${projectNumber}/apps/${projectId}`;
}

const oAuth2Client = new OAuth2Client();

async function verify() {
  // Verify the id_token, and access the claims.
  const response = await oAuth2Client.getIapPublicKeys();
  const ticket = await oAuth2Client.verifySignedJwtWithCertsAsync(
    iapJwt,
    response.pubkeys,
    expectedAudience,
    ['https://cloud.google.com/iap']
  );
  // Print out the info contained in the IAP ID token
  console.log(ticket);
}

verify().catch(console.error);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM