我是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}!`))