简体   繁体   English

Erlang / OTP:RESTful应用程序中的授权/身份验证

[英]Erlang/OTP: authorization/authentication in RESTful applications

I am in the process of designing an Erlang/OTP application which will expose its services (SOA) via a RESTful API. 我正在设计一个Erlang / OTP应用程序,它将通过RESTful API公开其服务(SOA)。

The services, which comprise the backend will be a database service, a price calculation service, etc. 构成后端的服务将是数据库服务,价格计算服务等。

The clients can be of many types: a web client, mobile clients, an Asterisk server client (which needs to look up user records in the database service) and even the clients which I do not plan to have and do not know about yet. 客户端可以是多种类型:Web客户端,移动客户端,Asterisk服务器客户端(需要在数据库服务中查找用户记录),甚至是我不打算拥有和不知道的客户端。 The clients will use the RESTful API differently: some will consume all the services, some will consume just some of the services (the SOA way). 客户端将以不同的方式使用RESTful API:一些将使用所有服务,一些将仅消耗一些服务(SOA方式)。

The main concern that I have is the authentication/authorization. 我所关注的主要问题是身份验证/授权。

RESTful Web应用程序

I can not use the built-in authentication/authorization of Ruby on Rails, because the web-client is just the one client of many possible clients that will use the application via the RESTful API. 我无法使用Ruby on Rails的内置身份验证/授权,因为Web客户端只是许多可能通过RESTful API使用应用程序的客户端的一个客户端。

So, my question is: 所以,我的问题是:

  • what is the general concept of authentication/authorization for a typical RESTful web application which is expected to be used with many different clients? 典型RESTful Web应用程序的身份验证/授权的一般概念是什么,预计将与许多不同的客户端一起使用?
  • what is the most practical software design pattern for authorization/authenication in a RESTful web application? RESTful Web应用程序中最实用的授权/身份验证软件设计模式是什么?
  • what Erlang/OTP open source software libraries could you recommend to implement authentication/authorization for such an application? 您可以推荐哪些Erlang / OTP开源软件库来实现此类应用程序的身份验证/授权?

Just check how others are doing this, for example in this article: Authentication on Facebook . 只需检查其他人如何做到这一点,例如在本文中: Facebook上的身份验证

In general the idea is that there is a separate API call that the client calls in order to authenticate itself to the system. 通常,这个想法是客户端调用一个单独的API调用,以便向系统验证自身。 The system may accept any client or only from a list of registered clients. 系统可以接受任何客户端或仅接受已注册客户端列表。 Once the system verifies the client, it issues a special token that the client is then using in all API calls. 系统验证客户端后,会发出一个特殊令牌,客户端随后会在所有API调用中使用该令牌。 In Facebook documentation it's called Access token. 在Facebook文档中,它被称为访问令牌。 If the client tries to call an API without a valid token the system reports this as an error and in certain conditions may block the client. 如果客户端尝试在没有有效令牌的情况下调用API,则系统会将此报告为错误,并且在某些情况下可能会阻止客户端。

In REST the token may be send simply as another parameter in the URL, in POST or as additional field directly in JSON. 在REST中,令牌可以简单地作为URL中的另一个参数,POST中或直接在JSON中作为附加字段发送。 Sending it as POST or in JSON is probably best as it keeps the URL clean (and won't collide with any caching that may be based on URLs). 将其作为POST或JSON发送可能是最好的,因为它保持URL清洁(并且不会与任何可能基于URL的缓存冲突)。

This is the merit of the idea but there are, as usually, more things to consider. 这是这个想法的优点,但通常需要考虑更多的事情。 For example the token should be difficult to guess so the client isn't able to recreate a valid token without authenticating with the system. 例如,令牌难以猜测,因此客户端无法在未经系统验证的情况下重新创建有效令牌。 Also, the system may need to expire the token if no API is called within a specified period of time. 此外,如果在指定的时间段内未调用API,则系统可能需要使令牌到期。

To answer the last part of your question, some libraries to point out: 为了回答你问题的最后部分,一些图书馆要指出:

  • erlang:phash2 or crypto library can be used to generate unique tokens that aren't easy to guess erlang:phash2crypto库可用于生成不易猜测的唯一令牌
  • Webmachine as an excellent framework, or REST toolkit as they like to call it, to create REST interfaces in Erlang Webmachine是一个优秀的框架,或者他们喜欢称之为REST工具包,用于在Erlang中创建REST接口
  • the logic behind API calls could be implemented in Erlang and served directly from a web server, eg inets or yaws, or it can be implemented using a web framework like Nitrogen or Chicago Boss. API调用背后的逻辑可以在Erlang中实现,并直接从Web服务器提供,例如inets或yaws,或者可以使用像Nitrogen或Chicago Boss这样的Web框架实现。 Check this list of Erlang web frameworks . 检查这个Erlang Web框架列表。

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

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