node.js - Google Cloud Functions (GCF) works fine locally with Authentication header, but returns 401 when deployed
问题描述
I have function that has some code to check whether user is authenticated or not
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.'
);
}
};
To test it, I generate token using gcloud auth print-identity-token
command, paste it into Authorization header with "Bearer " + token value and invoke it to get successful response.
However, when I deploy function, it does not even reach checkAuthentication
method. It just instantly returns unauthenticated response (401). The workflow where I don't provide Authentication header works fine, I get 400 as expected. I can reproduce this behaviour locally only if I provide some garbage value into auth header.
{
"error": {
"message": "Unauthenticated",
"status": "UNAUTHENTICATED"
}
}
Function uses service account credentials for initialization. In deployed functions permissions its and mine accounts are listed. What could be a reason for deployed function not being able to read id token?
解决方案
Can't find any reference, but looks like only firebase authentication works in both cases (locally and remote). In my case, I should not use google account id token, but a firebase user id token that has linked google account as external provider account.
推荐阅读
- plugins - 如何使用 JetBrains 的插件“Code With Me”共享 localhost?
- javascript - 从带有选项问题的选择标签中的 JSON 数据库中获取汽车品牌名称数据
- javascript - 如何将调试器附加到在 INestApplication.getHttpServer() 返回的服务器上运行的代码
- amazon-web-services - 无服务器部署中的 AWS Appsync 错误
- sql-server - 在对 SQL Server 的 pyodbc 调用中报告 WHICH 字段导致错误
- asp.net-mvc - 我无法为 HomeController Action 索引添加视图
- java - 存在重复依赖项时的 Module-info.java
- matlab - 如何在MATLAB中连接两个表
- php - 是否可以从 viber bot 获取所有消息历史记录
- azure - 更改 azure/windows server 2016 IIS 上的执行超时设置