【问题标题】:How to verify a access token using adal-node authentication context?如何使用 adal-node 身份验证上下文验证访问令牌?
【发布时间】:2017-06-15 08:24:38
【问题描述】:

我使用 adal-node 身份验证上下文和 angularjs 应用程序创建了一个 azure web api,jwt 令牌(访问令牌)已通过 angularjs 应用程序传递以调用 web API。在允许用户访问 Web API 之前,我需要通过 jwt 令牌验证用户。如何使用 adal-node 身份验证上下文进行此 jwt 验证。

生成访问令牌的示例代码

function getToken(TENANT) {
    var promise = new Promise(function (resolve, reject) {
        try {
            //const authContext = new adal.AuthenticationContext(`https://login.microsoftonline.com/${TENANT}`);
            const authContext = new adal.AuthenticationContext('https://login.microsoftonline.com/'+TENANT);
            authContext.acquireTokenWithClientCredentials(GRAPH_URL,CLIENT_ID,CLIENT_SECRET,function(err,tokenRes)
            {
                if (err)
                {
                    reject(err);
                }
                var accesstoken = tokenRes.accessToken;
                resolve(accesstoken);
            })
        }
        catch (ex) {
            reject(ex);
        };
    });
    return promise;
}

【问题讨论】:

    标签: node.js azure adal


    【解决方案1】:

    其实根据adal-node的文档:

    适用于 node.js 的 ADAL 库使 node.js 应用程序可以轻松地向 AAD 进行身份验证,以便访问受 AAD 保护的 Web 资源。它支持下面快速入门代码中显示的3种身份验证模式。

    因此,总而言之,adal-node 没有将 JWT 验证为 IDP 服务器的功能。

    但是,如果您想阻止没有权限的人访问您的 web api。您可以轻松利用 Azure 应用服务的身份验证和授权功能。您可以使用 AAD 来保护您的 Web API,并且针对此 Web API 的所有请求都需要在其 Authorization 标头中设置来自 AAD 的访问令牌。

    您可以参考Authentication and authorization for API Apps in Azure App Service了解更多信息。

    同时,如果您坚持自己验证 JWT,您可以利用一些 3rd 方模块,例如https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback

    【讨论】:

    • 非常感谢您的宝贵回答。它帮助很大。
    【解决方案2】:

    您可以在 nodejs 中使用 passport 和 passport-azure-ad 验证 ADAL 令牌。它从请求标头中获取 adal 令牌并对其进行验证。 这是示例代码。

    const express = require("express");
    const passport = require("passport");
    const BearerStrategy = require("passport-azure-ad").BearerStrategy;
    
    const options = {
      identityMetadata:
        "https://login.microsoftonline.com/<tenantidguid>/v2.0/.well-known/openid-configuration",
      clientID: "<clientidguid>",
      issuer: "https://sts.windows.net/<tenantidguid>/",
      loggingLevel: "info",
      passReqToCallback: false
    };
    const authenticationStrategy = new BearerStrategy(options, (token, done) => {
      return done(null, {}, token);
    });
    
    const app = express();
    app.use(require("body-parser").urlencoded({ extended: true }));
    app.use(passport.initialize());
    passport.use(authenticationStrategy);
    
    app.use((req, res, next) => {
      res.header("Access-Control-Allow-Origin", "*");
      res.header(
        "Access-Control-Allow-Headers",
        "Authorization, Origin, X-Requested-With, Content-Type, Accept"
      );
      next();
    });
    
    app.get(
      "/secured-api",
      passport.authenticate("oauth-bearer", { session: false }),
      (req, res) => {
        return res.status(200).json({ message: "token is verified" });
      }
    );
    
    const port = process.env.PORT || 3000;
    app.listen(port, function() {
      console.log("Listening on port " + port);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-12
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      相关资源
      最近更新 更多