百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rest-oauth2-server

> 编程语言
开源

Rest OAuth2 Server 是一款 Rails 3 应用,可让您打开 API,管理最终用户身份验证和客户端应用授权,实现 OAuth 协议。

275 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Rest OAuth2 Server 是一款 Rails 3 应用,可让您打开 API,管理最终用户身份验证和客户端应用授权,实现 OAuth 协议。

= Rest OAuth 2.0 Server is dead

We are sorry to annunce that due to a lack of time rest oauth2 server is now without a mantainer. For this reason we call the project dead and we suggest you to use more solid solution such as {Doorkeeper}[https://github.com/applicake/doorkeeper] or {oPRO}[https://github.com/opro/opro]. Anyway, we still think this project can be helpful to understand how a OAuth2 server works so if interested in that checkout the code and the whole documentation. It was really nice to have such good people collaborating on this project.

P.S. If you are interested about becoming the new mantainer of this project write us.

{The Lelylan Team}[http://lelylan.com]

= Desription

Rest OAuth 2.0 Server is a project that easily allows the generation of an OAuth 2.0 Server following the {draft 13}[http://tools.ietf.org/html/draft-ietf-oauth-v2-13] of the OAuth 2.0 protocol with {bearer tokens}[http://tools.ietf.org/html/draft-ietf-oauth-v2-bearer-02]. The spec is close to settling down, and we intend to update our code to match the final OAuth 2.0 and bearer token standards. OAuth has often been described as a "valet key for the web." It lets applications ask users for access to just the data they need and no more, giving them the ability to enable and disable the accesses whenever they want, most of the time without sharing their secret credentials.

= Installation

For the Rest OAuth 2.0 Server to work you need to have

  • {Ruby 1.9.2}[www.ruby-lang.org/en/] (use rvm[http://screencasts.org/episodes/how-to-use-rvm?utm_source=rubyweekly&utm_medium=email] to manage versions).
  • {MongoDB}[http://www.mongodb.org/].

To install the project run the following commands (remember to run $ mongod before)

$ git clone [email protected]:lelylan/rest-oauth2-server.git $ cd rest-oauth2-server $ bundle install $ rake spec $ rails s

If everything works fine, you have your OAuth 2.0 Server up and running! We are also working at a gem and a generator to easily integrate the Rest OAuth 2.0 server into your project, so stay tuned.

== Admin user definition

When accessing the application for the first time, click to sign up. A message will ask you to create the first administrator user as no one have been found.

http://github.com/Lelylan/rest-oauth2-server/raw/development/public/images/screenshots/first-user-creation.png

Register, log in and access the admin dashboard where you will find the following sections.

  • Users: list with all registered users.
  • Scopes: authorization scopes administration.
  • Accesses: clients that access the user's data.
  • Clients: registered clients (third party application)

While the Users and Scopes sections are visible only to the admin, Accesses and Clients are available to every registered user, also the ones that will grant access for their resources. To better understand what you can do explore the Dashboard and read the following sections.

http://github.com/Lelylan/rest-oauth2-server/raw/development/public/images/screenshots/admin-dashboard.png

== Scopes explained

In a short way, scopes tell you what can and can't be accessed. The Rest OAuth 2.0 Server ships with a flexible and powerful scope system which can be dynamically built.

http://github.com/Lelylan/rest-oauth2-server/raw/development/public/images/screenshots/scopes.png

To create a new scope click Create a new scope and you will get a simple form with two fields

  • Name: unique alphanumeric key that identify a scope.
  • Values: list of space separated alphanumeric strings, each of one refers to an action (built following the convention {controller name}/{action name}) or to an existing scope name.

Going a bit deeper you can define the accessible actions in two ways.

=== Action specific values

You can specify any action present in your rails app. For example if you want to allow the access to the action create in the controller pizzas you just add the string "pizzas/create". Here you can see an example on defining the access to all RESTful actions in a sample pizzas controller.

http://github.com/Lelylan/rest-oauth2-server/raw/development/public/images/screenshots/pizzas-scope.png

=== Scope name values

You can specify any group of actions adding a name scope. For example if the scope pizzas allows the access to all actions in the pizzas controller and the scope pastas allow the access to all actions in pastas controller, then the "all" "cope could have as values the list "pizzas pastas"

http://github.com/Lelylan/rest-oauth2-server/raw/development/public/images/screenshots/all-scope.png

== Protect your resources

After scopes are defined there is one more step. You need to protect the actions you want to authorize. To do thi add the filter oauth_authorized in any controller you want to protect.

class PizzasController oauth_authorized line in the {ApplicationController}[https://github.com/Lelylan/rest-oauth2-server/blob/master/app/controllers/application_controller.rb].

Last, you can make some actions public by using the exclude option.

before_filter :oauth_authorized, except: %w(index, show)

== Client definition

Every registered user can define a client (third party application). To do this access the dashboard and create your first client filling these fields.

  • Name: client name.
  • Siti URI: client web site URI.
  • Redirect URI: client redirect URI, used as callback after the user grant or deny the access.
  • Scope: one or more scope names, separated by spaces (limit the possible accesses a client can have). By default a scope named "all" is set as default. For this reason follow the convention to call "all" the scope that give all accesses.
  • Info: additional information.

Once the client is create the additional field client uri and secret are generated. You will use these info later on, during the authorization flows.

http://github.com/Lelylan/rest-oauth2-server/raw/development/public/images/screenshots/client-show.png

If you define a scope named all you can use one more functionality. You can click the button Simulate Authorization that you can find in the end of the client detail page, and you will see the authorization page that a user would normally see when granting access to a client.

http://github.com/Lelylan/rest-oauth2-server/raw/development/public/images/screenshots/authorization.png

=== Block clients

The admin can access to all created clients and decide to block any of them, meaning all related access tokens are disabled. This is pretty useful in cases where a client is considered "not safe". When a client is blocked every authorization request will be disabled, until the admin unblock it.

http://github.com/Lelylan/rest-oauth2-server/raw/development/public/images/screenshots/block-clients.png

== Granted Clients (aka accesses)

Once users grant the access to their resources, the accesses list is updated. Here a user can see which clients are accessing their resources, and with which frequency. One important functionality lies on the possibility for a user to block a specific client, whenever it is considered "not safe".

http://github.com/Lelylan/rest-oauth2-server/raw/development/public/images/screenshots/access.png

= OAuth 2.0 flows explained

Today Rest OAuth 2.0 Server supports three flows of OAuth 2.0

  • The server-side flow for web applications with servers that can securely store persistent information ({Authorization Code Flow}[http://tools.ietf.org/html/draft-ietf-oauth-v2-13#section-4.1])
  • The client-side flow for JavaScript applications running in a browser ({Implicit Grant Flow}[http://tools.ietf.org/html/draft-ietf-oauth-v2-13#section-4.2])
  • The native application flow for desktop and mobile applications ({Resource Owner Password Credentials Flow}[http://tools.ietf.org/html/draft-ietf-oauth-v2-13#section-4.3])

== OAuth 2.0 for server-side web applications

This flow is meant for web applications with servers that can keep secrets and maintain state.

The server-side flow has two parts. In the first part, your application asks the user for permission to access their data. If the user approves, instead of sending an access token directly as in the client-side flow, the Rest OAuth 2.0 Server will send to the client an authorization code. In the second part, the client will POST that code along with its client secret to the Rest OAuth 2.0 Server in order to get the access token.

=== Getting an access token

This flow begins by sending the user to the authorization endpoint /oauth/authorization with the following query parameters

  • response_type (REQUIRED): always use "code" as response type
  • client_id (REQUIRED): client identifier (the URI of the client model)
  • redirect_uri (REQUIRED): callback URI to the client application
  • scope (REQUIRED): privileges given to the client
  • state (OPTIONAL): opaque value used by the client to maintain state between the request and callback

Here's an example URL for a hypothetical app called "Example App" running on https://www.example.com

http://localhost:3000/oauth/authorization? response_type=code& client_id=http://localhost:3000/clients/a918F2fs3& redirect_uri=httsp://www.example.com/callback& scope=write& state=2af5D3vds

And this is what you should see.

http://github.com/Lelylan/rest-oauth2-server/raw/development/public/images/screenshots/authorization.png

After the user approves access or chooses not to, we'll redirect to the redirect_uri you pass us. If the user denies access, an error code is appended:

https://example.com/callback?error=access_denied&state=2af5D3vds

If the user approves access will be appended an authorization code in the query string of the URL:

https://example.com/callback?code=g2VDXwrT0S6iZeUeYQBYi2stxRy&state=2af5D3vds

Now, the client reached through the redirect_uri should swap that authorization code for an access token by POSTing it along the following params to the token endpoint /oauth/token using the JSON format.

  • code (REQUIRED): authorization code (from the previous step)
  • grant_type (REQUIRED): always use "authorization_code" as grant type
  • client_id (REQUIRED): client identifier (in our case is the uri field of the client)
  • client_secred (REQUIRED): client secret code

Using curl the request might look like:

curl -i http://localhost:3000/oauth/token
-H "Accept: application/json"
-X POST -d '{ "code": "g2VDXwrT0S6iZeUeYQBYi2stxRy",
"grant_type": "authorization_code",
"client_id": "http://localhost:30000/clients/a918F2fs3",
"client_secret": "a34a7afe4731e745de9d61iZeUeY"
}'

The response is a JSON Object containing the access token:

{ "access_token": "SlAV32hkKG", "expires_in": 1800, "refresh_token": "Da8i1930LSj" }

=== Getting additional access tokens

When your access token expires, Rest OAuth 2.0 Server API endpoints will respond with HTTP 401 Unauthorized. At any time, you can use the token endpoint with your refresh token with the following query parameters

  • grant_type (REQUIRED): always use "refresh_token" as grant type
  • client_id (REQUIRED): client identifier (in our case is the uri field of the client)
  • client_secred (REQUIRED): client secret code
  • refresh_token (REQUIRED): refresh token previusly received

Using curl the request might look like:

curl -i http://localhost:3000/oauth/token
-H "Accept: application/json"
-X POST -d '{ "grant_type": "refresh_token",
"refresh_token": "Da8i1930LSj",
"client_id": "http://localhost:30000/clients/a918F2fs3",
"client_secret": "a34a7afe4731e745de9d61iZeUeY"
}'

The response is a JSON Object containing the new access token.

{ "access_token": "AlYZ892hsKs", "expires_in": 1800, "refresh_token": "Da8i1930LSj" }

=== Going deep

If you are curious and you want to find more check the {acceptance}[https://github.com/Lelylan/rest-oauth2-ser

Issues· 16 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言