简体   繁体   English

在Oauth2orize模块中使用资源所有者密码

[英]Using Resource Owner Password in Oauth2orize module

I am developing an app with an mobile client for which I want to deploy Oauth2orize as Oauth server an use authenticate with Resource Owner Password way. 我正在开发一个带有移动客户端的应用程序,我想将Oauth2orize部署为Oauth服务器,并使用资源所有者密码方式进行身份验证。 But I am not able to understand how the flow should be. 但我无法理解流程应该如何。 I searched for lots of examples but could not find one where this use. 我搜索了很多例子,但找不到这个用的地方。

What should the flow be to give a token to the client? 流程应该为客户提供一个令牌?

This came a little late but I think this post can help someone else. 这来得有点晚了,但我认为这篇文章可以帮助别人。 I just spent a week trying to implement this because oauth2orize mix all the oauth flows in one file in the samples so is difficult to figure out which one to use to obtain the desired result. 我只花了一个星期试图实现这个,因为oauth2orize将所有oauth流混合在样本中的一个文件中,因此很难找出用于获得所需结果的那个。

To start answering your question you ask about a resource owner password grant as described here . 要开始回答你的问题,你问一个资源所有者密码授予描述这里 This should give you a head start on the steps defined by oauth2 to exchange a username(or email) and password for a token and optionally a refresh token. 这应该让您先了解oauth2定义的步骤,以便为令牌和可选的刷新令牌交换用户名(或电子邮件)和密码。

Step 1: The client requests a token using username and password to the authorization server 步骤1:客户端使用用户名和密码向授权服务器请求令牌

Step 2: The authorization server issues a token to the client if the client has valid credentials 步骤2:如果客户端具有有效凭据,则授权服务器向客户端发出令牌

So you start sending a request to an authentication resource in application/x-www-form-urlencoded format containing a username, password and grant_type params, optionally you can also use scopes. 因此,您开始以包含用户名,密码和grant_type参数的application / x-www-form-urlencoded格式向身份验证资源发送请求,您也可以选择使用范围。 Oauth2orize provides the server.token() function which generates a middleware to parse this request. Oauth2orize提供server.token()函数,该函数生成一个中间件来解析此请求。

app.post('/token', server.token(), server.errorHandler());

But before this stage you should have the server created and configured. 但在此阶段之前,您应该创建并配置服务器。 I usually use a different file and use module.exports to pass the middleware back to the app. 我通常使用不同的文件并使用module.exports将中间件传递回应用程序。

authorization.js file authorization.js文件

// Create the server
var server = oauth2orize.createServer();

// Setup the server to exchange a password for a token
server.exchange(oauth2orize.exchange.password(function (client, username, password, scope, done) {
    // Find the user in the database with the requested username or email
    db.users.find({ username: username }).then(function (user) {
        // If there is a match and the passwords are equal 
        if (user && cryptolib.compare(password, user.password)) {
            // Generate a token
            var token = util.generatetoken();
            // Save it to whatever persistency you are using
            tokens.save(token, user.id);
            // Return the token
            return done(null,   /* No error*/ 
                        token,  /* The generated token*/
                        null,   /* The generated refresh token, none in this case */
                        null    /* Additional properties to be merged with the token and send in the response */             
            );
        } else {
            // Call `done` callback with false to signal that authentication failed
            return done(null, false);
        }
    }).catch(function (err) {
       // Signal that there was an error processing the request
       return done(err, null);
    })
};

// Middlewares to export
module.exports.token = [
    server.token(),
    server.errorHandler()
];

Later in your app you write something like this 稍后在你的应用程序中你会写这样的东西

var auth = require('./authorization');
app.post('/token', auth.token);

This is a basic example of how you do it. 这是您如何做到这一点的基本示例。 Moreover you should enable some sort of protection on this endpoint. 此外,您应该在此端点上启用某种保护。 You could use client credential validation with the passport-oauth2-client-password module. 您可以使用passport-oauth2-client-password模块进行客户端凭据验证。 This way the client variable in the oauth2orize.exchange.password function will contain information about the client that is trying to access the resource enabling an extra security check for your authorization server. 这样, oauth2orize.exchange.password函数中的client变量将包含有关尝试访问资源的客户端的信息,从而为授权服务器进行额外的安全检查。

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

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