简体   繁体   English

使用Rails进行移动API身份验证

[英]Mobile API Authentication with Rails

I'm building a mobile API server for an APP. 我正在为APP构建移动API服务器。 My server needs to supports users and authentication. 我的服务器需要支持用户和身份验证。 for the last 2 days i've been searching for a good gem/ best practices of doing such task and i've failed to found. 在过去的两天里,我一直在寻找一个很好的宝石/最佳做法来完成这样的任务,而我却未能找到。

I've seen tweaks around devise or self implementation of authentication tokens but yet to discover a full solution for such a trivial task. 我已经看到围绕设计或自我实现身份验证令牌的调整,但尚未发现这样一个简单任务的完整解决方案。

http://lucatironi.github.io/tutorial/2012/10/15/ruby_rails_android_app_authentication_devise_tutorial_part_one/ http://lucatironi.github.io/tutorial/2012/10/15/ruby_rails_android_app_authentication_devise_tutorial_part_one/

https://github.com/plataformatec/devise/issues/2739 https://github.com/plataformatec/devise/issues/2739

Check out the Arcadex gem https://github.com/cleor41/ArcadeX . 查看Arcadex gem https://github.com/cleor41/ArcadeX

It's a rails engine that creates a token model. 它是一个创建令牌模型的rails引擎。 You put the gem's function inside your application controller and it grabs the token from the request headers or parameters and returns the user associated with the token or nil. 您将gem的功能放在应用程序控制器中,并从请求标头或参数中获取令牌,并返回与令牌或nil相关联的用户。 Nil means the user could not be found/the token is nonexistent. Nil表示无法找到用户/令牌不存在。 The only setup is to create a token when a user is created and you want to return this token after registering or signing up and destroy it after signing out. 唯一的设置是在创建用户时创建令牌,并且您希望在注册或注册后返回此令牌并在注销后将其销毁。 An example use case can be found on that github. 可以在该github上找到示例用例。 All of the functions you need for this are completed. 您完成此操作所需的所有功能。 The github will tell you where to place them. github会告诉你把它们放在哪里。

I hope it helps out! 我希望它有所帮助!

As far as I know there is no such thing as an out of the box solution for a given problem. 据我所知,对于给定的问题,没有开箱即用的解决方案。 On a few projects that i was working on we always used our own authentication implementation. 在我正在开发的一些项目中,我们总是使用自己的身份验证实现。 Usually its kind of a "2 tier token auth". 通常是一种“2层令牌身份验证”。

The idea is to have 1 token to authorize the app, with that token app gets certain privileges, and its allowed to do selection of the public calls. 我们的想法是拥有1个令牌来授权应用程序,该令牌应用程序获得某些特权,并允许选择公共呼叫。 If users decides to log in trough the app, app exchanges user credentials with the API, and as a result API returns second token to the app. 如果用户决定通过应用程序登录,则应用程序会使用API​​交换用户凭据,因此API会将第二个令牌返回给应用程序。 This second token is user specific token and is assigned to certain role, so that API knows what kind of the actions is the user with that token allowed to do, and for which user or groups of users that particular token can make changes to. 第二个令牌是用户特定的令牌,并被分配给某个角色,因此API知道允许该令牌执行的用户的操作类型,以及特定令牌可以对哪些用户或用户组进行更改。

Have you considered Grape ? 你考虑过葡萄吗? It's an API Microframework which sits quite nicely alongside your rails app in a Rack-based environment, or can be hosted inside the rails app itself. 它是一个API Microframework,与基于Rack的环境中的rails应用程序非常相似,或者可以在rails应用程序本身内部托管。 And it supports HTTP Basic and Digest Authentication out of the box. 它支持开箱即用的HTTP基本和摘要式身份验证

If you are not too far into developing the API, it may be worth looking into. 如果您在开发API方面不是太过分,那么可能值得研究。

As a disclaimer, I have not personally used Grape, but maintaining an API myself now, I wish I had (I discovered it after I was well on my way to developing the API in question). 作为一个免责声明,我没有亲自使用过Grape,但我现在自己维护一个API,我希望我(我在开发有问题的API之后很好地发现了它)。

For API you can use the individual hashes wich can generated, regenerated by yours user cabinet form. 对于API,您可以使用可以生成的单个哈希值,由您的用户机柜表单重新生成。 With this hashes, which stores on user device, user application can be authorised throw each http get request with params, which contains pair {api_key: this_hash} 有了这个存储在用户设备上的哈希,用户应用程序可以被授权抛出每个带有params的http get请求,其中包含对{api_key:this_hash}

Look on http://guides.spreecommerce.com/api/summary.html - this will be a nice worked example for you. 请访问http://guides.spreecommerce.com/api/summary.html - 这将是一个很好的工作示例。

I did the same as you, looked around and didn't find anything satisfactory. 我和你一样,环顾四周,没有找到任何令人满意的东西。 I was using devise prior to needing API authentication, but had found it less than friction free. 我在需要API身份验证之前使用了devise,但发现它不是无摩擦的。 In the end moving away from Devise was a very good move as I now have control over user and credential policy plus I can nicely solve API authentication and authorization. 最后,离开Devise是一个非常好的举动,因为我现在可以控制用户和凭证策略,而且我可以很好地解决API身份验证和授权问题。 The part I did keep was the Warden rack middleware that devise uses. 我保留的部分是设计用途的Warden机架中间件。

I have provided a comprehensive answer on setting up token authentication here: https://stackoverflow.com/a/21409810/2238268 我在此处提供了有关设置令牌身份验证的全面答案: https//stackoverflow.com/a/21409810/2238268

Be aware that any gem you use will always face the issue of integrating at both the model, controller and view layers and will force some paradigm that often just doesn't align with the way you need to manage users or credentials. 请注意,您使用的任何gem都将始终面临在模型,控制器和视图层进行集成的问题,并将强制一些通常与您管理用户或凭据所需的方式不一致的范例。

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

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