13 September 2020
In the previous section, we talked about integrating the doorkeeper gem for the bookmarker
application, and also we documented api/v1/oauth/token
with swagger. Doorkeeper
gem also provides api/v1/oauth/token/info
to get token information. You can check available oauth
routes by running bundle exec rake routes
command. This week we will document api/v1/oauth/token/info
endpoint by using swagger.
api/v1/token/info
endpoint basically gives us the necessary information about the provided token. The client-side developers use this endpoint to check the existing token’s validity and scope. (We don’t have any scope yet!)
Let’s say if we have an existing token that is ffxcdW1ktS59jbqWnRkfZRc6Vr9N16PyCQYKcNq-Su0
we can use it as following request to get the token information.
Basically, it’s a GET
request with token specified in the header. The response should look as demonstrated below.
resource_owner_id
: ID of the resource owner, in our example, it’s a user ID.scope
: We don’t have any scope yet, but we will have in the future to differentiate premium and free users for the bookmarker
application.expires_in
: Tells us after how many seconds this token will expire. By default, doorkeeper
expires token in 2 hours. (You can change this in doorkeeper initializer.)application
: We also don’t have any application but let’s say if we provide an API to external clients, then we could have identified them from their application uid
.created_at
: Timestamp value of the token’s creation.Let’s document the endpoint by editing app/controllers/swagger/controllers/oauth_token_controller.rb
file.
We have defined the 200
response with OauthTokenInfo
and Unauthorized
classes in case of invalid token that we will create now.
We will also define Unauthorized
class inside of the app/controllers/swagger/models
folder. You can test it with a non-existing token to see the structure of the response.
If you enter to the http://localhost:3000/swagger, you should see the documentation of api/v1/oauth/token/info
endpoint in the oauth
section.
We explained how we document api/v1/oauth/token/info
endpoint by using swagger. You can find the source code of bookmarker application on github. You can also find the previous chapter here. From this chapter on, I would like to keep chapters as small as possible to make it easy to consume for readers. (Maybe it’s better to call them sections or modules instead of chapters.)
Thank you for reading! If you want to be notified when I publish a new chapter, you can follow me on twitter.