Debian Codegraph @

doc/sources/admin/testopenidconnect.rst

raw
1 Test OpenID Connect with command line tools
2 ===========================================
3
4 We present here how to test the OpenID Connect protocol (authorization code flow) with commande line tools, like `curl`.
5 We use in this example a public OIDC provider based on LL::NG: `<https://oidctest.wsweet.org>`_
6
7 Authentication
8 --------------
9
10 The first step is to obtain a valid SSO session on the portal. The standard solution is to use a web browser and log into the portal, then get the value of the SSO cookie.
11
12 In our case, to be able to use only command lines, we will use portal REST API (which requires to adapt the `requireToken` configuration to get cookie value in JSON response (see :doc:`REST services<restservices>`). This should not be what you want on a production service.
13
14 Example of REST service usage, with credentials `dwho`/`dwho`:
15
16 .. code-block:: shell
17
18 curl -X POST -d user=dwho -d password=dwho -H 'Accept: application/json' 'https://oidctest.wsweet.org/oauth2/'
19
20 The session id is displayed in JSON response:
21
22 .. code-block:: javascript
23
24 {
25 "error" : "0",
26 "id" : "0640f95827111f00ba7ad5863ba819fe46cfbcecdb18ce525836369fb4c8350b",
27 "result" : 1
28 }
29
30 Authorization code
31 ------------------
32
33 In the first step of authorization code flow, we request a temporary code, on the `authorize` end point.
34
35 Required parameters:
36 * SSO session id (will be passed in `lemonldap` cookie, adapt the name if needed)
37 * Client ID: given by your OIDC provider, we use here `private`
38 * Scope: depends on which information you want, we will use here `openid profile email`
39 * Redirect URI: shoud match the value registered in your OIDC provider, we will use here `http://localhost`
40
41 The OIDC provider will return the code in location header, so we just output this reponse header:
42
43 .. code-block:: shell
44
45 curl -s -D - -o /dev/null -b lemonldap=0640f95827111f00ba7ad5863ba819fe46cfbcecdb18ce525836369fb4c8350b 'https://oidctest.wsweet.org/oauth2/authorize?response_type=code&client_id=private&scope=openid+profile+email&redirect_uri=http://localhost' | grep -i '^location'
46
47 Value of the location header is:
48
49 .. code-block:: shell
50
51 location: http://localhost?code=294b0facd91a0fa92762edc48d18369e99c330ba2b8fb05ab2c45999fcef6e17&session_state=BpB8KRMBEDUs%2B7lAjsz4DRk3E0RJImxgUbMsCFFAUa8%3D.N3dVOFg3a2RpNXVJK3ltSldrYXZjUjhtU0tvd29sWkpuWWJJbll5ZGs5NzhZMnh5bmQwd0IxRmJVWUxJSTlkWDBnSWZ2SWFVZmU0UnRaMkVJVjNUY3c9PQ
52
53
54 So we get the code value: `294b0facd91a0fa92762edc48d18369e99c330ba2b8fb05ab2c45999fcef6e17`
55
56 This code has a short lifetime, we will use it to get Access token and ID token in next step
57
58 Tokens
59 ------
60
61 In this step, we exchange the authorization code against tokens:
62 * Access token
63 * ID token
64 * Refresh token (optional)
65
66 Required parameters:
67 * Authorization code: see previous step
68 * Grant type: we use here `authorization_code`
69 * Redirect URI: same value as the one used in the previous step
70 * Client ID and Client Secret: given by your OIDC provider, we use here `private`/`tardis`
71
72 .. code-block:: shell
73
74 curl -s -X POST -d grant_type=authorization_code -d 'redirect_uri=http://localhost' -d code=294b0facd91a0fa92762edc48d18369e99c330ba2b8fb05ab2c45999fcef6e17 -u 'private:tardis' 'https://oidctest.wsweet.org/oauth2/token' | json_pp
75
76 The JSON response looks like this:
77
78 .. code-block:: javascript
79
80 {
81 "access_token" : "a88b8dde538719e55c3cb8fbd14d06ed77853c685a62abf6ecb88d86228a9c64",
82 "expires_in" : 3600,
83 "id_token" : "eyJhbGciOiJSUzI1NiIsImtpZCI6Im9pZGN0ZXN0IiwidHlwIjoiSldUIn0.eyJhdXRoX3RpbWUiOjE2MTQxNjAwMDYsImlhdCI6MTYxNDE2MzIxOCwiaXNzIjoiaHR0cHM6Ly9vaWRjdGVzdC53c3dlZXQub3JnLyIsImF0X2hhc2giOiJIVGswOVNjSjRObEFua3k5SGFFX2VRIiwiYWNyIjoibG9hLTIiLCJleHAiOjE2MTQxNjY4MTgsInN1YiI6ImR3aG8iLCJhenAiOiJwcml2YXRlIiwiYXVkIjpbInByaXZhdGUiXX0.N3TNufjKLzKM3qiIitA7JHUei4L572XjF6AcVl7UAFB6efdGUCiAL7amlUl0FgjZfzW9bzvulBVDidoYSicIaysIdI4KkjmjpVN0Z3gOSu0ecuk5p8fD1KbX6-tmA3txeR18nzfhdckq-S-6Lx7wrWpPNyrzGx-FImbOaUPN2yeVhKPXhdyHJbzI0RqJETxnBkyW-CLEzAJyq3rCUVX-D8kHADvg6a42QQyPdxvBuGrdBfyDDDb_Py13H1qhn40NnuFknR1wSahsY6U97uUooyk-0_U4J3XJAHySjCtivtSeP0fM_5eblMuh6WdVjrfnUF0xnCTbCa2gYRlTS38BkqcsWY26PXoRAOo31a1cmB5sMSZyPtRF9UZcmGiNBIymMMdFgVAJONb6uliiTS5j9-nkmHOqVC-XJ6tuiU3ZSBQ8nCRyNW2LaCzpJ5c3ytP9yYQtyT8HmhN0VnXob3K1uJEA_Xcu4sADjtrm-LbrGiwaVMkfu-C6YIrbuC9riOW6TneV2gAzAjXPOW_UZeXrCrx66GHIJPsJIq29UfbTN5Pxo9SH2yKw6PSfxevkZhBIhEXCOMaIUHrlWz2jDBBzPIWeiSRbK_MRtejQmdRUs8nqdq-McVwnFiUMDt1KZXxqScTtMDF_Lo9oK2RaCijEJ7MSPEscr_YOyp3KIq2FLVg",
84 "refresh_token" : "19434440ed4da2803e8ba9d91cb2eabd5b8bd12af2609429bda03ed487e6ef57",
85 "token_type" : "Bearer"
86 }
87
88 The Access token will be used for the last step, to retrieve information about the user.
89
90 The ID token is a JWT (JSON Web Token) and can be parsed easily, as this is the concatenation of 3 JSON strings encoded in base 64: `base64(header).base64(payload).base64(signature)`.
91
92 Decoding the payload gives:
93
94 .. code-block:: shell
95
96 echo 'eyJhdXRoX3RpbWUiOjE2MTQxNjAwMDYsImlhdCI6MTYxNDE2MzIxOCwiaXNzIjoiaHR0cHM6Ly9vaWRjdGVzdC53c3dlZXQub3JnLyIsImF0X2hhc2giOiJIVGswOVNjSjRObEFua3k5SGFFX2VRIiwiYWNyIjoibG9hLTIiLCJleHAiOjE2MTQxNjY4MTgsInN1YiI6ImR3aG8iLCJhenAiOiJwcml2YXRlIiwiYXVkIjpbInByaXZhdGUiXX0=' | base64 -d | json_pp
97
98 .. code-block:: javascript
99
100 {
101 "acr" : "loa-2",
102 "at_hash" : "HTk09ScJ4NlAnky9HaE_eQ",
103 "aud" : [
104 "private"
105 ],
106 "auth_time" : 1614160006,
107 "azp" : "private",
108 "exp" : 1614166818,
109 "iat" : 1614163218,
110 "iss" : "https://oidctest.wsweet.org/",
111 "sub" : "dwho"
112 }
113
114 User info
115 ---------
116
117 This step is optional and allows to fetch user information linked to scopes requested in the first step.
118
119 Required parameters:
120 * Access token, used as bearer authorization
121
122 .. code-block:: shell
123
124 curl -s -H 'Authorization: Bearer a88b8dde538719e55c3cb8fbd14d06ed77853c685a62abf6ecb88d86228a9c64' 'https://oidctest.wsweet.org/oauth2/userinfo' | json_pp
125
126 JSON response:
127
128 .. code-block:: javascript
129
130 {
131 "email" : "[email protected]",
132 "name" : "Doctor Who",
133 "preferred_username" : "dwho",
134 "sub" : "dwho"
135 }
136
137 Introspection
138 -------------
139
140 You can test Access token validity with the introspection endpoint.
141
142 Required parameters:
143 * Client ID and Client Secret, used as basic authorization
144 * Access token, sent as POST data
145
146 .. code-block:: shell
147
148 curl -s -u private:tardis -X POST -d 'token=a88b8dde538719e55c3cb8fbd14d06ed77853c685a62abf6ecb88d86228a9c64' 'https://oidctest.wsweet.org/oauth2/introspect' | json_pp
149
150 JSON response:
151
152 .. code-block:: javascript
153
154 {
155 "active" : true,
156 "client_id" : "private",
157 "exp" : 1630684115,
158 "iss" : "https://oidctest.wsweet.org/",
159 "scope" : "openid profile email",
160 "sub" : "dwho"
161 }
162
163 Refresh an Access token
164 -----------------------
165
166 If the Access token has expired, you can get a new one with the refresh token.
167
168 Required parameters:
169 * Grant type: we use here `refresh_token`, sent as POST data
170 * Refresh token, sent as POST data
171 * Client ID and Client Secret, used as basic authorization
172
173 .. code-block:: shell
174
175 curl -s -X POST -d grant_type=refresh_token -d refresh_token=19434440ed4da2803e8ba9d91cb2eabd5b8bd12af2609429bda03ed487e6ef57 -u 'private:tardis' 'https://oidctest.wsweet.org/oauth2/token' | json_pp
176
177 JSON response:
178
179 .. code-block:: javascript
180
181 {
182 "access_token" : "78929118546b1a11a2e3b607f607d0ccb73d72bbd95c59d0b03ae69ffa17f41a",
183 "expires_in" : 3600,
184 "id_token" : "eyJhbGciOiJSUzI1NiIsImtpZCI6Im9pZGN0ZXN0IiwidHlwIjoiSldUIn0.eyJhdXRoX3RpbWUiOjE2MTQxNjAwMDYsImlhdCI6MTYxNDE2MzIxOCwiaXNzIjoiaHR0cHM6Ly9vaWRjdGVzdC53c3dlZXQub3JnLyIsImF0X2hhc2giOiJIVGswOVNjSjRObEFua3k5SGFFX2VRIiwiYWNyIjoibG9hLTIiLCJleHAiOjE2MTQxNjY4MTgsInN1YiI6ImR3aG8iLCJhenAiOiJwcml2YXRlIiwiYXVkIjpbInByaXZhdGUiXX0.N3TNufjKLzKM3qiIitA7JHUei4L572XjF6AcVl7UAFB6efdGUCiAL7amlUl0FgjZfzW9bzvulBVDidoYSicIaysIdI4KkjmjpVN0Z3gOSu0ecuk5p8fD1KbX6-tmA3txeR18nzfhdckq-S-6Lx7wrWpPNyrzGx-FImbOaUPN2yeVhKPXhdyHJbzI0RqJETxnBkyW-CLEzAJyq3rCUVX-D8kHADvg6a42QQyPdxvBuGrdBfyDDDb_Py13H1qhn40NnuFknR1wSahsY6U97uUooyk-0_U4J3XJAHySjCtivtSeP0fM_5eblMuh6WdVjrfnUF0xnCTbCa2gYRlTS38BkqcsWY26PXoRAOo31a1cmB5sMSZyPtRF9UZcmGiNBIymMMdFgVAJONb6uliiTS5j9-nkmHOqVC-XJ6tuiU3ZSBQ8nCRyNW2LaCzpJ5c3ytP9yYQtyT8HmhN0VnXob3K1uJEA_Xcu4sADjtrm-LbrGiwaVMkfu-C6YIrbuC9riOW6TneV2gAzAjXPOW_UZeXrCrx66GHIJPsJIq29UfbTN5Pxo9SH2yKw6PSfxevkZhBIhEXCOMaIUHrlWz2jDBBzPIWeiSRbK_MRtejQmdRUs8nqdq-McVwnFiUMDt1KZXxqScTtMDF_Lo9oK2RaCijEJ7MSPEscr_YOyp3KIq2FLVg",
185 "token_type" : "Bearer"
186 }
187
188 Logout
189 ------
190
191 To kill SSO session, call the OIDC logout endpoint. By default a confirmation is requested, but you can bypass it by adding `confirm=1` to URL.
192
193 Required parameters:
194 * SSO session id (will be passed in `lemonldap` cookie)
195
196 .. code-block:: shell
197
198 curl -s -D - -o /dev/null -b lemonldap=0640f95827111f00ba7ad5863ba819fe46cfbcecdb18ce525836369fb4c8350b 'https://oidctest.wsweet.org/oauth2/logout?confirm=1'
199
200 The session is deleted on server side and the cookie is destroyed in the browser. You can use the introspection endpoint to verify that the access token is no longer valid.