【发布时间】:2019-08-11 05:14:40
【问题描述】:
我是 Auth0 的初学者,几天前按照教程制作了使用 Auth0 登录的 iPhone 应用程序。
它成功了,所以我可以成功获得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}!`))
【问题讨论】: