简体   繁体   English

使用Oauth服务器/客户端进行Ember身份验证

[英]Ember authentication with Oauth server/client

I am trying to design the authentication flow of an Ember application with a Rails backend. 我正在尝试使用Rails后端设计Ember应用程序的身份验证流程。 I basically want to authenticate users via Google/Facebook/etc., I do not want to provide an 'independent' authentication service. 我基本上想通过Google / Facebook / etc等对用户进行身份验证,但我不想提供“独立”身份验证服务。 I do want to maintain a list of users of course on the server side, potentially merging different authentications from different sources into the same user. 我确实希望在服务器端维护一个用户列表,有可能将来自不同来源的不同身份验证合并到同一用户中。 I will not interact on behalf of the user on Google/Facebook from the client side, but I will do that on the server side. 我不会代表客户在Google / Facebook上从客户端进行交互,但是我会在服务器端进行交互。

For the above reason I was planning to do the following: 由于上述原因,我计划执行以下操作:

  1. I will use torii to fetch an auth_token on the client side and I will pass that onto the server side, where I will validate it, convert it into an access token. 我将使用torii在客户端获取auth_token,并将其传递到服务器端,在此我将对其进行验证并将其转换为访问令牌。

  2. I will generate a custom token on the server side which I will send back to the client and require all further API calls to be accompanied by that token. 我将在服务器端生成一个自定义令牌,然后将其发送回客户端,并要求所有其他API调用都附带该令牌。 I will not share the access token with the client at all. 我完全不会与客户端共享访问令牌。

Would you say that this is an optimal flow? 您会说这是最佳流程吗?

In terms of implementation, I have been able to get auth_tokens from the different providers using the example here . 在实现方面,我可以使用此处的示例从其他提供程序获取auth_tokens。 I am completely unsure however: 但是我完全不确定:

  • if I need ember-simple-auth or only torii (how do these two complement each other?) 如果我需要ember-simple-auth或只需要torii(这两个如何相互补充?)
  • how do I pass the auth token to the server side? 如何将身份验证令牌传递到服务器端? With the code below I can get the auth token, but is this the proper place to implement the call to the API? 使用下面的代码,我可以获得auth令牌,但这是实现对API的调用的合适位置吗?

     export default Ember.Route.extend({ actions: { googleLogin: function() { var _this = this; this.get('session').authenticate('simple-auth-authenticator:torii', 'google-oauth2').then( function() {console.log(_this.get('session.secure.authorizationCode'));} ); return; }, facebookLogin: function() { this.get('session').authenticate('simple-auth-authenticator:torii', 'facebook-oauth2'); return; } } }); 
  • how do I make all further requests to the API to be accompanied by a specific token? 如何向API发出所有其他进一步请求并带有特定令牌?

  • should I use devise on the server side to make it easier or not? 我应该在服务器端使用devise使其变得更容易或更简单吗?

I have been implemented exactly the same kind of workflow. 我已经实现了完全相同的工作流程。 I used ember-simple-auth with ember-simple-auth-torii and implemented a custom authenticator to achieve this goal. 我将ember-simple-auth与ember-simple-auth-torii结合使用,并实现了自定义身份验证器以实现此目标。

Ember-simple-auth provides an example of a custom authenticator here . Ember-simple-auth 在此处提供了一个自定义身份验证器的示例。

Your custom authenticator implementation will look like the following 您的自定义身份验证器实现将如下所示

  • First get auth_token using torii 首先使用torii获取auth_token
  • Then valid this auth_token against your backend in order to get your custom token 然后针对您的后端对此auth_token进行验证,以获取您的自定义令牌

Your authenticate callback in your custom authenticator will basically look like the following : 自定义身份验证器中的身份验证回调基本上如下所示:

authenticate: function(provider, options) {
    var self = this;

    return new Ember.RSVP.Promise(function(resolve, reject) {
        self.torii.open(provider, options || {}).then(function(data) {

            var endpoint = '/token'; // Your API endpoint used to get your cutom token
            var dataToSend = { // Data sent to your endpoint
                grant_type: 'authorization_code',
                code: data.accessToken,
                access_token: data.accessToken
            };
            $.post(endpoint, dataToSend).done(function(response) {
                response.provider = provider;
                resolve(data);
            }).fail(function(response) {
                response.provider = provider;
                reject(data);
            })
        }, reject)

    })
}

Once you have the custom authenticator initilized you can use it this way on your controllers : 初始化自定义身份验证器后,就可以在控制器上以这种方式使用它:

   this.get('session').authenticate(
    'authenticator:customauthenticator', // Or wathever name you gave
    'facebook-connect' // Any compatible torii provider
).then(function(user) {
    console.log(user); // Will display ajax response from your endpoint
})

Finally, if you want your custom token to be automatically sent with all ajax request, you can use the ember-simple-auth oauth2-bearer authorizer. 最后,如果您希望自定义令牌与所有ajax请求一起自动发送,则可以使用ember-simple-auth oauth2-bearer授权器。

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

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