【问题标题】:Auth0 NodeJS JWT authentication in API for mobile appAuth0 NodeJS JWT 身份验证在移动应用程序的 API 中
【发布时间】:2019-08-11 05:14:40
【问题描述】:

我是 Auth0 的初学者,几天前按照教程制作了使用 Auth0 登录的 iPhone 应用程序。
它成功了,所以我可以成功获得accessTokenidToken。 之后,我尝试使用 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)

如果我发送idTokenmalformed 异常就消失了,但这次我又收到了一个错误。

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

【问题讨论】:

    标签: node.js jwt auth0


    【解决方案1】:

    研究了几天,终于找到问题的原因了。
    有两个应用程序 - 一个是 Default Native 应用程序,一个是 MTM API 应用程序。我在 iPhone 应用程序中使用旧的 Native 应用程序的 audience,这就是为什么两个 accessTokens 格式不同的原因。
    audience 是每个 Auth0 应用程序的标识符,所以我应该注意这一点。

    【讨论】:

      猜你喜欢
      • 2018-12-06
      • 2015-05-12
      • 2017-02-02
      • 1970-01-01
      • 2017-01-14
      • 2016-10-22
      • 2021-12-26
      • 2016-09-25
      • 2020-07-25
      相关资源
      最近更新 更多