简体   繁体   English

使用PassportJS进行令牌认证/授权

[英]Token Authentication/Authorization With PassportJS

I'm currently in the process of developing a Node/Express-based API for an application that will be distributed on multiple platforms. 我目前正在为将在多个平台上分发的应用程序开发基于Node / Express的API。 Because of this, I will need to authenticate/authorize users based on a token rather than sessions/cookies. 因此,我需要根据令牌而不是会话/ cookie来验证/授权用户。

After doing some research, I've found that PassportJS is a great plugin for accomplishing something like this. 经过一些研究,我发现PassportJS是一个很棒的插件,可以完成这样的事情。 Unfortunately, after going through its documentation for several hours, there doesn't seem to be any good explanation of raw token-based authentication. 不幸的是,经过几个小时的文档后,对基于令牌的原始身份验证似乎没有任何好的解释。

I don't want to use any plugins such as JWT -- just Passport, Express, and MongoDB. 我不想使用任何插件,如JWT - 只是Passport,Express和MongoDB。

How would I go about implementing a token-based user authorization system with Passport. 我将如何使用Passport实现基于令牌的用户授权系统。 I need explanations on token generation, token passing, and the rest of the process. 我需要有关令牌生成,令牌传递和其余过程的解释。

Please answer this in a clear and concise manner, as if you're explaining to a 5th grader. 请以清晰简洁的方式回答这个问题,就像您向五年级学生解释一样。

Thanks :) 谢谢 :)

I'm going to try and break down your question into a few parts: 我将尝试将您的问题分解为几个部分:

  • Configuring Passport 配置Passport
  • Where to use Passport 在哪里使用Passport
  • Miscellaneous final remarks 其他最后的评论

First, a slight misconception. 首先,有点误解。 Passport doesn't come bundled with a token generation system. Passport没有与令牌生成系统捆绑在一起。 Passport is designed to be configured with strategies that you have to include as dependencies. Passport旨在配置必须包含为依赖项的策略。 This is done so Passport itself can be light and modular. 这样做是为了使Passport本身可以轻巧且模块化。 I might need to authenticate with Twitter when you need a local (username and password) authentication strategy. 当您需要本地(用户名和密码)身份验证策略时,我可能需要通过Twitter进行身份验证。 So, Passport includes neither. 因此,Passport既不包括。 I install what I need and keep the package size light. 我安装了我需要的东西,并保持包装尺寸轻。

TL;DR: There's no way of not installing a plugin for Passport. TL; DR:没有办法不为Passport安装插件。 You need to include a strategy of some kind. 你需要包含某种策略

I'm going to assume you want to use a local strategy. 我假设您想要使用本地策略。 So, you need passport-local . 所以,你需要护照本地 Don't worry, it was written by Jared Hanson, the author of Passport. 别担心,它是由Passport的作者Jared Hanson编写的。

Most everything I use will be from the Passport docs, specifically here . 我使用的大部分内容都来自Passport文档,特别是在这里

Let's look at configuration: 我们来看看配置:

  • Install Passport and the passport-local module. 安装Passport和护照本地模块。
  • Make sure Express, MongoDB, Node, npm, etc. are up to date. 确保Express,MongoDB,Node,npm等是最新的。

The basic configuration file for passport-local looks like this: passport-local的基本配置文件如下所示:

var passport = require('passport')
  , LocalStrategy = require('passport-local').Strategy;

passport.use(new LocalStrategy(
    function(username, password, done) {
        User.findOne({ username: username ), function(err, user) {
            if (err) { return done(err); }
            if (!user) {
                return done(null, false, { message: 'Incorrect username.' });
            }
            if (!user.validPassword(password)) {
                return done(null, false, { message: 'Incorrect password.' });
            }
            return done(null, user);
        });
    }
));

Now you need to place a form on a web page. 现在您需要在网页上放置一个表单。 Here is a very basic example: 这是一个非常基本的例子:

<form action="/login" method="post">
    <div>
        <label>Username:</label>
        <input type="text" name="username" />
    </div>
    <div>
        <label>Password:</label>
        <input type="password" name="password" />
    </div>
    <div>
         <input type="submit" value="Log In" />
    </div>
</form>

Next, you need a route in your Express app. 接下来,您需要在Express应用中使用路线。 See the Express Routing Documentation for more info on how this works. 有关其工作原理的详细信息,请参阅Express Routing Documentation

app.post('/login',
    passport.authenticate('local', { successRedirect: '/',
                                     failureRedirect: '/login' })
);

Note on parameters: By default, LocalStrategy expects the credentials to be in parameters named username and password . 有关参数的注意事项:默认情况下, LocalStrategy希望凭据位于名为usernamepassword的参数中。 There are configuration options to name them otherwise, for instance logging in with email rather than a username. 除此之外,还有一些配置选项可以命名,例如使用电子邮件而不是用户名登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM