Version 19.1 by messines on 2022/08/05 11:17

Show last authors
1 == Requirement ==
2
3 You should read this documentation to [[understand the concept of Authentication and Authorization>>doc:Collabs.the-collaboratory.Documentation IAM.FAQ.OIDC Clients explained.WebHome]] with OIDC and OAuth2 before trying to implement it.
4
5 == Abstract ==
6
7 In order to create an OIDC client, see [[1. Registering an OIDC client>>doc:Collabs.the-collaboratory.Documentation IAM.FAQ.OIDC Clients explained.1\. Registering an OIDC client v2.WebHome]]. After creating the OIDC client, you have a corresponding access token and secret.
8
9 For the example below, we consider the case of someone wanting to provide access to https:~/~/www.getpostman.com as an app for Collaboratory users to access from their collabs. You should replace that URL by the one of your own app.
10
11 The redirect_uri is set with the URL of your application to which your users will be redirected after having been authenticated by their EBRAINS account. For example when you login to this wiki, the redirect URI is [[https:~~/~~/wiki.ebrains.eu/*>>url:https://wiki.ebrains.eu/*]]
12
13 [[image:https://wiki.ebrains.eu/bin/download/Collabs/collaboratory-community-apps/Community%20App%20Developer%20Guide/Authenticating%20with%20your%20OIDC%20client%20and%20fetch%20collab%20user%20info/WebHome/Screenshot%202020-07-15%20at%2017.47.12.png?width=758&height=517&rev=1.1||alt="Screenshot 2020-07-15 at 17.47.12.png" height="517" width="758"]]
14
15 The whole authentication flow presented here is based on the official OAuth2 RFC described in the section 4.1.
16
17 [[https:~~/~~/tools.ietf.org/html/rfc6749#section-4.1>>url:https://tools.ietf.org/html/rfc6749#section-4.1]]
18
19 [[image:https://wiki.ebrains.eu/bin/download/Collabs/collaboratory-community-apps/Community%20App%20Developer%20Guide/Authenticating%20with%20your%20OIDC%20client%20and%20fetch%20collab%20user%20info/WebHome/Screenshot%202020-07-15%20at%2018.32.14.png?width=474&height=410&rev=1.2||alt="Screenshot 2020-07-15 at 18.32.14.png" height="410" width="474"]]
20
21 == Authentication flow ==
22
23 === Authorization Code Request ===
24
25 The first step of the authentication protocol is to fetch an **authorization code **for your client and your user. This is done by directing your users to the URL of the EBRAINS login page (**IAM**) where they can enter their username and password.
26
27 ==== Request ====
28
29 The authorization **code **is fetched by an HTTP request:
30
31 /GET: [[https:~~/~~/iam.ebrains.eu/auth/realms/hbp/protocol/openid-connect/auth>>url:https://iam.ebrains.eu/auth/realms/hbp/protocol/openid-connect/auth]]
32
33 with the following parameters:
34
35 * response_type=code
36 * login=true
37 * client_id=**//community-apps-tutorial//**
38 * redirect_uri=**//https:~/~/www.getpostman.com/oauth2/callback//**
39 * scope=openid**//+group+team//**
40
41 with the italics indicating the fields you customize for your own app. The URL will look like:
42
43 https:~/~/iam.ebrains.eu/auth/realms/hbp/protocol/openid-connect/auth?response_type=code&login=true&client_id=//**community-apps-tutorial**//&redirect_uri=//**https:~/~/www.getpostman.com/oauth2/callback**//&scope=openid//**+group+team**//
44
45 The **scope** parameter can include a combination of several values. Each user will be asked to consent to sharing that scope with your app upon first access.
46
47 * **openid: **This scope is required because we use the OIDC protocol. It will give your app access to the user's basic information such as username, email and full name.
48 * **profile** (optional): More information on user if provided by the user
49 * **email **(optional): The verified email of the user, should be add in addition of openid and/or profile to get the email.
50 * **group **(optional)**: **If you request this scope, the future access token generated will authorize your app to identify which units and groups the user belongs to.
51 * **team **(optional)**: **This scope is like the group scope lets your app identify the permissions of the user, but by identifying what collabs the user has access to and with what roles.
52 * **clb.wiki.read **(optional): access to GET Collab API
53 * **clb.wiki.write** (optional): access to DELETE/PUT/POST Collab API
54 * **collab.drive **(optional): access to GET/POST/PUT/DELETE drive API
55 * **offline_access **(optional)**: **provide refresh token
56 * **quota : **User's quota usage around EBRAINS services
57
58 The group and team scopes are a simple way for your app to grant permissions to its services and resources when you want to grant access to a very few units, groups, or collab teams. For more complex permission management, contact support.
59
60 ==== Response ====
61
62 Once the user has logged in, your app gets an HTTP 301 redirection followed by an HTTP 200 success response with an authorization **code** inside. A typical response might look like:
63
64 https:~/~/www.getpostman.com/oauth2/callback?session_state=a0ff8a68-2654-43ef-977a-6c15ce343546&code=**f3f04f93-hbp-482d-ac3d-demo.turtorial.7122c1d9-3f7e-4d80-9c4f-dcd244bc2ec7**
65
66 The authorization **code** is the part in bold in the response above.
67
68 === Access Token Request ===
69
70 Now that your app has the **authorization** **code** for a user, it can fetch the user ID Token and Access Token
71
72 ==== Request ====
73
74 /POST: [[https:~~/~~/iam.ebrains.eu/auth/realms/hbp/protocol/openid-connect/token>>url:https://iam.ebrains.eu/auth/realms/hbp/protocol/openid-connect/token]]
75
76 with the following parameters:
77
78 * grant_type: authorization_code
79 * code: **//f3f04f93-hbp-482d-ac3d-demo.turtorial.7122c1d9-3f7e-4d80-9c4f-dcd244bc2ec7//**
80 * redirect_uri: **//[[https:~~/~~/www.getpostman.com/oauth2/callback>>url:https://www.getpostman.com/oauth2/callback]]//**
81 * client_id: **//community-apps-tutorial//**
82 * client_secret: **//your client secret obtained during client creation//**
83
84 The image below shows a sample POST request generated from the Postman tool. [The fact that this page is based on getpostman.com as an example is pure coincidence.]
85
86 [[image:https://wiki.ebrains.eu/bin/download/Collabs/collaboratory-community-apps/Community%20App%20Developer%20Guide/Authenticating%20with%20your%20OIDC%20client%20and%20fetch%20collab%20user%20info/WebHome/Screenshot%202020-07-15%20at%2018.20.34.png?rev=1.1||alt="Screenshot 2020-07-15 at 18.20.34.png"]]
87
88 ==== Response ====
89
90 200 OK
91
92 {
93 "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCIgOiAi...pP5vaNwvvsaNGEA",
94 "expires_in": 604773,
95 "refresh_expires_in": 604773,
96 "refresh_token": "eyJh...vC5eIR1rNhRJ4d8",
97 "token_type": "bearer",
98 "id_token": "eyJ...YOwdQ",
99 "not-before-policy": 0,
100 "session_state": "76e553bf-ba2e-45b6-8c6c-c867772b40ec",
101 "scope": "openid"
102 }
103
104 Your app gets a response containing the **access token**, the **refresh token,** the **id token **and other information. The ID Token should be use by developer on their backend to read user informations such as username, first name, last name etc. The ID Token should be use internally, into your app only, the app which triggered the authentication. The access token will be use to reach APIs, the access token can be see as a card to access an ATM. ID Token is for Authentication, Access token is for Authorization. Refresh token is to re-ask a valid access token after expiration.
105
106 == Access user info ==
107
108 Now that your app has the access token of a user, it can fetch the user's info.
109
110 === Request ===
111
112 /GET: [[https:~~/~~/iam.ebrains.eu/auth/realms/hbp/protocol/openid-connect/userinfo>>url:https://iam.ebrains.eu/auth/realms/hbp/protocol/openid-connect/userinfo]]
113
114 with the following parameters:
115
116 * Authorization: the access token preceded by the string "Bearer "
117
118 The image below shows a sample GET request generated from the Postman tool. [The fact that this page is based on getpostman.com as an example is pure coincidence.]
119
120 [[image:https://wiki.ebrains.eu/bin/download/Collabs/collaboratory-community-apps/Community%20App%20Developer%20Guide/Authenticating%20with%20your%20OIDC%20client%20and%20fetch%20collab%20user%20info/WebHome/Screenshot%202020-07-15%20at%2018.28.28.png?width=566&height=161&rev=1.1||alt="Screenshot 2020-07-15 at 18.28.28.png" height="161" width="566"]]
121
122 === Response ===
123
124 As response your app receives a JSON with all the information about the logged user
125
126 {
127 (% style="color:#2980b9" %)"**sub**": "fa2db206-3...0ebaba98e1",
128 (% style="color:#27ae60" %)"**preferred_username**": "demoaccount",(%%)
129 "unit": [
130 "/all/institutions/switzerland/epfl",
131 "/all/projects/hbp/consortium/SGA2/SP05",
132 "/all/projects/hbp/consortium/SGA3/WP6/T6_11"
133 ],
134 "roles": {
135 "jupyterhub": [
136 "feature:authenticate"
137 ],
138 "xwiki": [
139 "feature:authenticate"
140 ],
141 "team": [
142 "**collab**-collaboratory-community-apps-**editor**"
143 ],
144 "group": [
145 "**group**-collaboratory-developers",
146 "**unit**-all-projects-hbp-consortium-sga2-sp05-**administrator**"
147 ]
148 },
149 "mitreid-sub": "30...62"
150 }
151
152 The unit field above lists Collaboratory Units which the user is a member of, with the unit name using slashes instead of the colons you see in the Collaboratory UI.
153
154 jupyterhub and xwiki are OIDC clients with more advanced permission management.
155
156 The team field above lists Collaboratory Teams which the user is a member of, in the form "collab-//collabname//-//role//" where //role //is one of admin, editor, or viewer according to the user's role in collab //collabname//.
157
158 The group field above lists Collaboratory Groups which the user is a member of, in the form "group-//groupname//". It also lists Collaboratory Units which the user is an admin of, in the form "unit-//unitname//-administrator" with //unitname //using dashes instead of the colons you see in the Collaboratory UI.
159
160 ===
161 Which unique identifier should you use for your users ===
162
163 We recommand you to use the "(% style="color:#27ae60" %)preferred_username(%%)" field (which is the username) to identify your users for every new users in your system. The "(% style="color:#27ae60" %)preferred_username(%%)*" is used by us as unique identifier in The Collaboratory, it's really friendly to use it and interact easily with our IDM Api. We are guaranting its unicity.
164
165 You can also use the "(% style="color:#2980b9" %)sub(%%)" provided by keycloak which is the OIDC/OAuth2 standard unique identifier for a user and might be consider as the good choice. If in addition you store the username associate to the sub you will still be able to use easily our IDM Api, if not you will maybe need to perform one more request to get the User object from your sub in the [[IDM Api>>doc:Collabs.the-collaboratory.Documentation Wiki.API.WebHome]]
166
167 You can found additional information about userinfo fields and difference with previous Collaboratory 1 [[here>>doc:Collabs.the-collaboratory.Documentation IAM.FAQ.Migrating your OIDC client from Collaboratory 1 to Collaboratory 2.WebHome]].
168
169 == OIDC Plugin configuration exemple ==
170
171 You will probably not reinvent the wheel and use an existing OIDC Library to establish the authentication. Often theses libary do all the above flow for you, you just have to fill a properties file.
172
173 === OIDC Plugin Properties : Usefull link ===
174
175 All the link you need to know to fill the property file of your OIDC plugin are visibles here
176
177 [[https:~~/~~/iam.ebrains.eu/auth/realms/hbp/.well-known/openid-configuration>>https://iam.ebrains.eu/auth/realms/hbp/.well-known/openid-configuration]]
178
179 === OIDC Plugin for Python : mozilla-django-oidc ===
180
181 We are using this plugin with our Django based project, a full documentation is available here
182 [[https:~~/~~/mozilla-django-oidc.readthedocs.io/en/stable/installation.html>>https://mozilla-django-oidc.readthedocs.io/en/stable/installation.html]]
183
184
185 ==== Github demo project using mozilla-django-oidc
186 [[https:~~/~~/github.com/HumanBrainProject/mozilla-django-oidc-demo>>https://github.com/HumanBrainProject/mozilla-django-oidc-demo]] ====
187
188 ==== ====
189
190 You can found a very basic empty Django project using mozilla-django-oidc on this github. Clone it and follow the README instruction. In few minutes if you already have a clientId and clientSecret available you should be able to login in this demo application. Once you validated that your client works well, you can compare with your own code and debug your own code to made the login works.
191
192
193 In the future, I will add some branch in this project with advanced usage exemple as overriding default mozilla-django-oidc class and fetch team and groups/units of a user.
194 \\For now, you can found more advanced usage in the CodeJam#12 presentation
195 \\[[https:~~/~~/drive.ebrains.eu/lib/cf5b0375-4e07-4e62-847d-e2997eac3f30/file/CodeJam%20%2312%20(2021)/Presentations/Oidc_integration_for_python.pptx>>https://drive.ebrains.eu/lib/cf5b0375-4e07-4e62-847d-e2997eac3f30/file/CodeJam%20%2312%20(2021)/Presentations/Oidc_integration_for_python.pptx]]
196
197 You can see every function of the original mozilla-django-oidc on their github, so you can better understand what you can override and how
198 \\[[https:~~/~~/github.com/mozilla/mozilla-django-oidc/blob/master/mozilla_django_oidc/auth.py>>https://github.com/mozilla/mozilla-django-oidc/blob/master/mozilla_django_oidc/auth.py]]
199
200
201 ==== mozilla-django-oidc sample config ====
202
203 {{code}}
204 # Mozilla django oidc settings
205 OIDC_OP_AUTHORIZATION_ENDPOINT = "https://iam.ebrains.eu/auth/realms/hbp/protocol/openid-connect/auth"
206 OIDC_OP_TOKEN_ENDPOINT = "https://iam.ebrains.eu/auth/realms/hbp/protocol/openid-connect/token"
207 OIDC_OP_USER_ENDPOINT = "https://iam.ebrains.eu/auth/realms/hbp/protocol/openid-connect/userinfo"
208 OIDC_RP_SIGN_ALGO ="RS256"
209 OIDC_OP_JWKS_ENDPOINT="https://iam.ebrains.eu/auth/realms/hbp/protocol/openid-connect/certs"
210 LOGIN_REDIRECT_URL = '/'
211 OIDC_STORE_ACCESS_TOKEN = True
212
213 # Store secret somewhere on your system
214 OIDC_RP_SCOPES="openid profile email"
215 OIDC_RP_CLIENT_ID = os.environ['OIDC_RP_CLIENT_ID']
216 OIDC_RP_CLIENT_SECRET = os.environ['OIDC_RP_CLIENT_SECRET']
217
218 # Theses one are situational, you problably don't need them, it's to use in the case you want to override an action in a python class, for exemple execute some code after the callback
219
220 OIDC_CALLBACK_CLASS = 'clb_auth.views.OIDCAuthenticationCallbackView'
221 OIDC_DRF_AUTH_BACKEND = 'clb_auth.auth.OIDCBearerAuthenticationBackend'
222 OIDC_OP_LOGOUT_URL_METHOD = 'clb_auth.auth.logout_url'
223
224 {{/code}}
225
226
227 === OIDC Plugin for Python : flask-oidc ===
228
229 [[https:~~/~~/flask-oidc.readthedocs.io/en/latest/>>https://flask-oidc.readthedocs.io/en/latest/]]
230
231 You will need at the root of your project a "client_secrets.json" file.
232
233 ==== ====
234
235 client_secrets.json
236
237 {{code language="json"}}
238 {
239 "web": {
240 "client_id": "yourClientId",
241 "client_secret": "yourClientSecret",
242 "redirect_uris": [
243 "http://localhost:8081/",
244 "https://yourappurl.url/"
245 ],
246 "auth_uri": "https://iam.ebrains.eu/auth/realms/hbp/protocol/openid-connect/auth",
247 "token_uri": "https://iam.ebrains.eu/auth/realms/hbp/protocol/openid-connect/token",
248 "userinfo_uri": "https://iam.ebrains.eu/auth/realms/hbp/protocol/openid-connect/userinfo",
249 "issuer": "https://iam.ebrains.eu/auth/realms/hbp"
250 }
251 }
252
253 {{/code}}