API 中用于移动应用的 Auth0 NodeJS JWT 身份验证
Auth0 NodeJS JWT authentication in API for mobile app
我是 Auth0 的初学者,几天前制作了 iPhone 应用程序,它按照教程使用 Auth0 登录。
它成功了,所以我可以成功获得 accessToken
和 idToken
。
在那之后,我尝试使用 Auth0 jwt.
为该应用程序的 API 创建 nodejs 服务器
这次我也遵循了 Auth0 教程,并成功地从 Auth0 API.
获得了带有测试访问令牌的 200 响应
但我的问题是,当我使用令牌在 iPhone 应用程序上请求 API 时,节点服务器抛出异常。
如果我发送 accessToken
它抛出 UnauthorizedError: jwt malformed
,我发现手机 accessToken
的格式与示例 accessToken
.
完全不同
UnauthorizedError: jwt malformed
at /Volumes/Work/Work/NodeJS/GeoServer/GeoServer/node_modules/express-jwt/lib/index.js:102:22
at Object.module.exports [as verify] (/Volumes/Work/Work/NodeJS/GeoServer/GeoServer/node_modules/jsonwebtoken/verify.js:63:12)
at verifyToken (/Volumes/Work/Work/NodeJS/GeoServer/GeoServer/node_modules/express-jwt/lib/index.js:100:13)
at fn (/Volumes/Work/Work/NodeJS/GeoServer/GeoServer/node_modules/async/lib/async.js:746:34)
at /Volumes/Work/Work/NodeJS/GeoServer/GeoServer/node_modules/async/lib/async.js:1213:16
at /Volumes/Work/Work/NodeJS/GeoServer/GeoServer/node_modules/async/lib/async.js:166:37
at /Volumes/Work/Work/NodeJS/GeoServer/GeoServer/node_modules/async/lib/async.js:706:43
at /Volumes/Work/Work/NodeJS/GeoServer/GeoServer/node_modules/async/lib/async.js:167:37
at Immediate.<anonymous> (/Volumes/Work/Work/NodeJS/GeoServer/GeoServer/node_modules/async/lib/async.js:1206:34)
at runCallback (timers.js:705:18)
如果我发送 idToken
,malformed
异常就消失了,但这次我又遇到了另一个错误。
Error: getaddrinfo ENOTFOUND undefined undefined:443
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)
这部分我已经处理了好几天,但还没有找到解决方案。
请给我任何帮助来解决这个问题。
以下是节点服务器代码。
import express from 'express';
import jwt from 'express-jwt';
import jwksRsa from 'jwks-rsa';
import cors from 'cors';
import bodyParser from 'body-parser';
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
const port = 3000
// Create middleware for checking the JWT
const jwtCheck = jwt({
// Dynamically provide a signing key based on the kid in the header and the singing keys provided by the JWKS endpoint.
secret: jwksRsa.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: `https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json`
}),
// Validate the audience and the issuer.
audience: process.env.AUTH0_AUDIENCE,
issuer: `https://${process.env.AUTH0_DOMAIN}`,
algorithms: ['RS256']
});
app.use(jwtCheck);
const locationHistory: any[] = [];
app.get('/', (req, res) => res.send('Hello World!'))
app.post('/api/location', (req, res) => {
locationHistory.push({latitude: req.body.latitude, longitude: req.body.longitude});
res.send(locationHistory);
})
app.listen(port, () => console.log(`API server is listening on port ${port}!`))
研究了几天,终于找到问题的原因了。
有两个应用程序 - 一个是 Default Native
应用程序,一个是 MTM API
应用程序。我在 iPhone 应用程序中使用旧的 Native 应用程序的 audience
,这就是为什么两个 accessTokens
格式不同的原因。
audience
是每个 Auth0 应用程序的标识符,所以我应该注意这一点。
我是 Auth0 的初学者,几天前制作了 iPhone 应用程序,它按照教程使用 Auth0 登录。
它成功了,所以我可以成功获得 accessToken
和 idToken
。
在那之后,我尝试使用 Auth0 jwt.
为该应用程序的 API 创建 nodejs 服务器
这次我也遵循了 Auth0 教程,并成功地从 Auth0 API.
但我的问题是,当我使用令牌在 iPhone 应用程序上请求 API 时,节点服务器抛出异常。
如果我发送 accessToken
它抛出 UnauthorizedError: jwt malformed
,我发现手机 accessToken
的格式与示例 accessToken
.
UnauthorizedError: jwt malformed
at /Volumes/Work/Work/NodeJS/GeoServer/GeoServer/node_modules/express-jwt/lib/index.js:102:22
at Object.module.exports [as verify] (/Volumes/Work/Work/NodeJS/GeoServer/GeoServer/node_modules/jsonwebtoken/verify.js:63:12)
at verifyToken (/Volumes/Work/Work/NodeJS/GeoServer/GeoServer/node_modules/express-jwt/lib/index.js:100:13)
at fn (/Volumes/Work/Work/NodeJS/GeoServer/GeoServer/node_modules/async/lib/async.js:746:34)
at /Volumes/Work/Work/NodeJS/GeoServer/GeoServer/node_modules/async/lib/async.js:1213:16
at /Volumes/Work/Work/NodeJS/GeoServer/GeoServer/node_modules/async/lib/async.js:166:37
at /Volumes/Work/Work/NodeJS/GeoServer/GeoServer/node_modules/async/lib/async.js:706:43
at /Volumes/Work/Work/NodeJS/GeoServer/GeoServer/node_modules/async/lib/async.js:167:37
at Immediate.<anonymous> (/Volumes/Work/Work/NodeJS/GeoServer/GeoServer/node_modules/async/lib/async.js:1206:34)
at runCallback (timers.js:705:18)
如果我发送 idToken
,malformed
异常就消失了,但这次我又遇到了另一个错误。
Error: getaddrinfo ENOTFOUND undefined undefined:443
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)
这部分我已经处理了好几天,但还没有找到解决方案。
请给我任何帮助来解决这个问题。
以下是节点服务器代码。
import express from 'express';
import jwt from 'express-jwt';
import jwksRsa from 'jwks-rsa';
import cors from 'cors';
import bodyParser from 'body-parser';
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
const port = 3000
// Create middleware for checking the JWT
const jwtCheck = jwt({
// Dynamically provide a signing key based on the kid in the header and the singing keys provided by the JWKS endpoint.
secret: jwksRsa.expressJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: `https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json`
}),
// Validate the audience and the issuer.
audience: process.env.AUTH0_AUDIENCE,
issuer: `https://${process.env.AUTH0_DOMAIN}`,
algorithms: ['RS256']
});
app.use(jwtCheck);
const locationHistory: any[] = [];
app.get('/', (req, res) => res.send('Hello World!'))
app.post('/api/location', (req, res) => {
locationHistory.push({latitude: req.body.latitude, longitude: req.body.longitude});
res.send(locationHistory);
})
app.listen(port, () => console.log(`API server is listening on port ${port}!`))
研究了几天,终于找到问题的原因了。
有两个应用程序 - 一个是 Default Native
应用程序,一个是 MTM API
应用程序。我在 iPhone 应用程序中使用旧的 Native 应用程序的 audience
,这就是为什么两个 accessTokens
格式不同的原因。
audience
是每个 Auth0 应用程序的标识符,所以我应该注意这一点。