代码之家  ›  专栏  ›  技术社区  ›  Yehor Androsov

GCP函数在本地使用Authentication头可以正常工作,但在部署时返回401

  •  0
  • Yehor Androsov  · 技术社区  · 5 年前

    我有一个函数,它有一些代码来检查用户是否经过身份验证

    export const addAffiliate = functions.region('us-central1').https.onCall(
      async (inputData: any, context: functions.https.CallableContext) => {
        checkAuthentication(context);
        ...
    
    export const checkAuthentication = (
      context: functions.https.CallableContext
    ) => {
      // Checking that the user is authenticated.
      if (!context.auth) {
        // Throwing an HttpsError so that the client gets the error details.
        throw new functions.https.HttpsError(
          'failed-precondition',
          'The function must be called ' + 'while authenticated.'
        );
      }
    };
    

    gcloud auth print-identity-token 命令,将其粘贴到带有“bearner”+token值的授权头中,并调用它以获得成功的响应。 local test

    checkAuthentication 方法。它只是立即返回未经验证的响应(401)。我不提供身份验证头的工作流工作正常,我得到了400个。只有在auth头中提供一些垃圾值时,才能在本地重现这种行为。

    {
        "error": {
            "message": "Unauthenticated",
            "status": "UNAUTHENTICATED"
        }
    }
    

    enter image description here

    0 回复  |  直到 5 年前
        1
  •  1
  •   Yehor Androsov    5 年前