Project

Profile

Help

Using the Redmine REST API with OAuth2 at Planio » History » Sprint/Milestone 7

Jan Schulz-Hofen, 08/19/2020 02:42 PM

1 1 Jens Krämer
# Using the Redmine REST API with OAuth2 at Planio
2
3
As you might know, Planio comes with a powerful [REST API](https://plan.io/api)
4
which covers almost all aspects of Planio. If you were working with the API
5
before, you know that in order to use it, you had to generate an API key and
6
use that to make authorized API calls.
7
8 7 Jan Schulz-Hofen
{{>toc}}
9
10 1 Jens Krämer
This approach, while relatively easy to work with, has a few drawbacks:
11
12
- Each API key is tied to a single user account, meaning that your application
13
  will always act as this user when interacting with Planio.
14
- There is no way to restrict what an application can do - an API key always
15
  grants it's user the same set of permissions that the user it belongs to has.
16
17
**OAuth 2** introduces a mechanism to restrict applications to a certain
18
*scope*. Further, users need to explicitly grant access to an application
19
before it may act on their behalf. When doing so, they will be informed about
20
the scope, that is, what data the application is going to have access to. In
21
the same way, a user may later decide to revert this decision and revoke access
22
for an application at any time.
23
24 2 Jens Krämer
Starting today, Planio now supports OAuth 2.
25 1 Jens Krämer
26
## Using OAuth 2 with Planio
27
28
Let's look a minimal example for you to try out.
29
30
### Create an OAuth Application in your Planio Account
31
32
In order to use OAuth with Planio, you have to create an **Application** first.
33
This will generate a unique identifier for your API client, as well as a
34
secret. Both will be used to authenticate your application when it's
35
communicating with Planio.
36
37
Go to *Your Avatar* → **Administration** → **Applications** and click **New
38
Application**.
39
40 3 Jan Schulz-Hofen
{{figure(Creating a new OAuth application)
41 1 Jens Krämer
![Creating a new OAuth application](new_application%402x.png)
42 3 Jan Schulz-Hofen
}}
43 1 Jens Krämer
44
You may enter any descriptive **Name** for your application. This will be shown later to users when they are about to authorize your app.
45
46
The **Redirect URI** is the location where Planio will redirect a user's
47
browser to after they granted access to your application. The redirect will
48
carry a `code` query parameter which holds an authorization code that's needed
49
to retrieve the actuall access token later.
50
51
For now, enter `urn:ietf:wg:oauth:2.0:oob` as the **Redirect URI**. This
52
special value tells Planio that this application is not reachable over the
53
web. Instead, the authorization code will be simply displayed to the user for
54
manual transfer to the client application requesting access.
55
56
Below, in the **Scopes** section, you decide what your application will be
57
allowed to do. Don't be too generous here, and restrict the set of granted
58
permissions to the minimum necessary. For now, just select the
59
**Add issues** permission and hit **Save** below.
60
61
You will be redirected to a page that lists the details you just entered, along
62
with the application's **Application Id** and **Secret**.
63
64
### Build the OAuth 2 client
65
66
We'll be using the [Ruby language](https://www.ruby-lang.org/en/) and the [OAuth2 Gem](https://rubygems.org/gems/oauth2) for this.
67
68
Of the various *OAuth Flows* that exist, Planio currently supports the most commonly used *Authorization Code* flow. Please refer to [the OAuth 2 spec](http://tools.ietf.org/html/rfc6749#section-4.1) for more technical details. Any applications you create are considered *confidential* in the sense of the spec, which means that the application secret may not be disclosed. If you require support for a *public* application (for example a mobile app or an application running exclusively in the browser), please contact us.
69
70
**Set up the client**
71
72
~~~ruby
73
require 'oauth2'
74
75
client_id     = '...' # your application id
76
client_secret = '...' # your application's secret
77
redirect_uri  = '...' # your application's redirect uri
78
site          = "https://your-domain.plan.io/" # your planio account's URL
79
80
client = OAuth2::Client.new(client_id, client_secret, site: site)
81
~~~
82
83
84
**Authorize the Application**
85
86
If you were building a real application, you would now send your user to some
87
URL where they are prompted to grant access. Usually you don't have to
88
construct these URLs yourself, but your OAuth 2 client library will do it for
89
you:
90
91
~~~ruby
92
client.auth_code.authorize_url(redirect_uri: redirect_uri, scope: 'add_issues')
93
# => https://your-domain.plan.io/oauth/authorize?response_type=code&client_id=...&redirect_uri=...
94
~~~
95
96
As `scope`, list all permissions you are planning to use. You cannot request
97
any permissions that have not been selected when the application was registered in Planio, but
98
you can choose to select less. Here, we only request the `add_issues` permission in order to be able to add issues.
99
100
Open this URL in your browser and you will be prompted for authorization,
101
listing the permissions you are applying for. 
102
103 4 Jan Schulz-Hofen
{{figure(Authorizing an OAuth 2 Application)
104 1 Jens Krämer
![Authorizing an OAuth 2 Application](authorize_app%402x.png)
105 4 Jan Schulz-Hofen
}}
106 1 Jens Krämer
107
Click **Authorize**, and take
108
note of the **Authorization code**. If you had entered a real **Redirect URI**
109
earlier, you would have been redirected to that URI now, with the authorization
110
code as query parameter.
111
112
113
114
**Retrieve an Access Token**
115
116
With the authorization code you can now request an access token from your
117
Planio account like this:
118
119
~~~ruby
120
code = '...' # the authorization code from above
121
token = client.auth_code.get_token(code, redirect_uri: redirect_uri)
122
# => <#OAuth2::AccessToken ...>
123
~~~
124
125
If at this point you get an error, it is most likely that the code, which is
126
only valid for a short time, already has expired.
127
128
**Use the Access Token for API requests**
129
130
If everything worked out, you may now use the token to do requests against
131
Planio's REST API.
132
133
~~~ruby
134
JSON.parse token.get('/users/current.json').body
135
~~~
136
137
This will give you some basic information about the user you are acting as. Of
138
course at this point you can stop using the OAuth 2 client and use any other
139
HTTP client to query Planio's API. Let's try with
140
[RestClient](https://github.com/rest-client/rest-client):
141
142
~~~ruby
143
# get the actual token string from the oauth lib
144
token_value = token.token
145
# compile the issue data
146
payload = { issue: { subject: "Hello world" } }
147
# specify the token in the Authorization HTTP header
148
headers = { Authorization: "Bearer #{token_value}"}
149
RestClient.post "https://your-domain.plan.io/projects/some-project/issues.json", payload, headers
150
# => <RestClient::Response 201 "{\"issue\":{\"...">
151
~~~
152
153
And that's it! You successfully created an issue with authorization obtained via OAuth 2.
154
155
**A word on security**
156
157
As the developer of an OAuth 2 client application it is your responsibility to keep the application secret as well as any auth and refresh tokens you obtain safe - any unintended disclosure may lead to unauthorized access to your users' data.
158
159
### Manage your Authorized Applications
160
161
Click on *Your Avatar* → **My Account** → **Authorized Applications** in order
162
to see the list of applications that currently have access to your account.
163
164 5 Jan Schulz-Hofen
{{figure(List of authorized apps)
165 6 Jan Schulz-Hofen
![List of authorized apps](authorized_apps%402x.png)
166 5 Jan Schulz-Hofen
}}
167 1 Jens Krämer
168
Clicking **Revoke** will invalidate any access or refresh token that the application might still possess and remove it from the list.