Demo OpenID Connect service provider in NodeJS (tutorial mode)
NodeJS >= 8.16
Clone the sources:
git clone https://github.com/kaliop/oidc-sample-client.git && cd oidc-sample-client
Fetch the start
tag to get the boostrap:
git checkout start
Install main depencencies:
npm install
Start the application:
npm start
Go to http://localhost:3000
checkout step-01
No difficulties here. Note: To avoid having to install an external database for the tutorial, we use a CSV file to store fake useres, and node-csv-query library to request it.
checkout step-02
Just detroy the session.
checkout step-03
Now, let's really start this tutorial. We will implement a client connexion using the Authorization Code Flow. The tutorial code uses a sample identity provider hosted at http://sample-oidc-provider.dev-franceconnect.fr , or you can override environment variables (see config file) if you want to use your own Idp.
The first step is to redirect the user to the identity provider's authorize
enpoint, with the required request parameters :
-
response_type
: specifies the used authorization flow. ie. "code" here. -
scope
: specifies which user data the service requires.
Space delimited list of keywords ('openid', 'profile', 'email', 'address', 'phone'). At least "openid" is required. The other are optional. -
client_id
: The OAuth 2.0 Client Identifier knonw by the identity provider. -
redirect_uri
: the URL to which the end user will be redirected by the identity provider once authenticated. (ie. theloginCallback
that will be implemented in next steps). This uri must have been registered at the identity provider size.
checkout step-04
Once the end user has been authenticated by the identity provider, he is redirected to the redrect_uri
specified above.
The loginCallback
endpoint is as follow : <service-fqdn>/login-callback?code=<code>
.
We need to call the Token Endpoint as a POST HTTP request with following parameters:
grant_type
: must be "authorisation_code"code
: the same code value that has just been sent within the loginCallback request. Used to validate the token request.redirect_uri
: the current request URI.
The reponse must be a JSON containing a access_token
and a id_token
attributes.
We need also to check if the ID Token is valid (see next step)
checkout step-05
Check if the ID Token is a valid JWT and if it is compliant to OpenID ID Token Validation rules.
checkout step-06
Call User Info Endpoint to get all the needed user data, and store them in session. Now, the end user is fully authenticated into our service.
checkout step-07
Claims full scopes to get every userInfo. We also need to reformat the date received from userInfo, to match them to our local format.
checkout step-08
These parameters are not required, but fully recommanded to secure the authentication flow.
Both are random values that are included into the userAuthorize request and check later within the flow:
state
, if present, must be added by the identity provider as additionnal parameter tologinCallback
url.nonce
, if present, must be added by the identity provider within the ID Token.
checkout step-09
Implement the logout propagation (see RP-Initiated Logout):