|
1 | 1 | # Confidential Client uses CIBA |
2 | 2 |
|
3 | | -TODO |
| 3 | +According to the [specification](https://openid.net/specs/openid-client-initiated-backchannel-authentication-core-1_0.html), Public Client cannot use Client-Initiated Backchannel Authentication(CIBA). |
| 4 | +Only Confidential Client can use it, For example a Console Application, Website or a REST.API. |
| 5 | + |
| 6 | +In this tutorial, we are going to explain how to create a Console Application. It will initiate a CIBA Authentication Request and will get an Access Token by polling the `Token` endpoint. |
| 7 | + |
| 8 | +## Source Code |
| 9 | + |
| 10 | +The source code of this project can be found [here](https://github.com/simpleidserver/SimpleIdServer/tree/master/samples/DeviceUseCIBA). |
| 11 | + |
| 12 | +## Add a client |
| 13 | + |
| 14 | +The first step consists to configure the OPENID client |
| 15 | + |
| 16 | +* Open the IdentityServer website [http://localhost:5002](http://localhost:5002). |
| 17 | +* In the Clients screen, click on `Add client` button. |
| 18 | +* Select `Device` and click on next. |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +* Fill-in the form like this and click on the `Save` button to confirm the creation. |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +* Navigate to the `Certificate Authorities`, click on the `Certificate Authority` that you are interested to create `Client Certificate`. The Certificate Authority MUST be trusted by your machine, don't forget to import it into the appropriate store. |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +* Click on `Client Certificates` tab and click on the `Add Certificate Client` button. |
| 31 | +* Fill-in the form like this and click on `Add` button. |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +* Click on the Settings button next to the Client Certificate and click on `Download`. The certificate will be used later inside the Console Application. |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +## Create a console application |
| 40 | + |
| 41 | +The last step consists to create and configure a Console Application project. |
| 42 | + |
| 43 | +* Open a command prompt, run the following commands to create the directory structure for the solution. |
| 44 | + |
| 45 | +``` |
| 46 | +mkdir DeviceUseCIBA |
| 47 | +cd DeviceUseCIBA |
| 48 | +mkdir src |
| 49 | +dotnet new sln -n DeviceUseCIBA |
| 50 | +``` |
| 51 | + |
| 52 | +* Create a console application named `ConsoleApp` and install the `IdentityModel` Nuget Package |
| 53 | + |
| 54 | +``` |
| 55 | +cd src |
| 56 | +dotnet new console -n ConsoleApp |
| 57 | +cd ConsoleApp |
| 58 | +dotnet add package IdentityModel |
| 59 | +``` |
| 60 | + |
| 61 | +* Add the `ConsoleApp` project into your Visual Studio solution. |
| 62 | + |
| 63 | +``` |
| 64 | +cd .. |
| 65 | +dotnet sln add ./src/ConsoleApp/ConsoleApp.csproj |
| 66 | +``` |
| 67 | + |
| 68 | +* Edit the `Program.cs` file and copy the following code. |
| 69 | + |
| 70 | +``` |
| 71 | +using IdentityModel.Client; |
| 72 | +using System.Security.Authentication; |
| 73 | +using System.Security.Cryptography.X509Certificates; |
| 74 | +
|
| 75 | +var certificate = new X509Certificate2(Path.Combine(Directory.GetCurrentDirectory(), "CN=client.pfx")); |
| 76 | +var req = new BackchannelAuthenticationRequest() |
| 77 | +{ |
| 78 | + Address = "https://localhost:5001/master/mtls/bc-authorize", |
| 79 | + ClientId = "cibaConformance", |
| 80 | + Scope = "openid profile", |
| 81 | + LoginHint = "user", |
| 82 | + BindingMessage = "Message", |
| 83 | + RequestedExpiry = 200 |
| 84 | +}; |
| 85 | +var handler = new HttpClientHandler(); |
| 86 | +handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }; |
| 87 | +handler.CheckCertificateRevocationList = false; |
| 88 | +handler.ClientCertificateOptions = ClientCertificateOption.Manual; |
| 89 | +handler.SslProtocols = SslProtocols.Tls12; |
| 90 | +handler.ClientCertificates.Add(certificate); |
| 91 | +var client = new HttpClient(handler); |
| 92 | +var response = await client.RequestBackchannelAuthenticationAsync(req); |
| 93 | +
|
| 94 | +bool cont = true; |
| 95 | +while(cont) |
| 96 | +{ |
| 97 | + var tokenResponse = await client.RequestBackchannelAuthenticationTokenAsync(new BackchannelAuthenticationTokenRequest |
| 98 | + { |
| 99 | + Address = "https://localhost:5001/master/mtls/token", |
| 100 | + ClientId = "cibaConformance", |
| 101 | + AuthenticationRequestId = response.AuthenticationRequestId |
| 102 | + }); |
| 103 | + if(tokenResponse.IsError) |
| 104 | + Console.WriteLine(tokenResponse.Error); |
| 105 | + else |
| 106 | + { |
| 107 | + Console.WriteLine(tokenResponse.AccessToken); |
| 108 | + cont = false; |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +* Replace the `CN=client.pfx` certificate by the one you have downloaded. |
| 114 | + |
| 115 | +When you run the application, a green message will be displayed in the Identity Server instance. |
| 116 | +Copy the URL in the browser and authenticate with the credentials login : `user`, password: `password`. |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +When the consent is granted then the access token will be displayed by the console application. |
0 commit comments