├── .github
└── workflows
│ ├── build_nuget.yml
│ └── publish_nuget.yml
├── .gitignore
├── .nuke
├── build.schema.json
└── parameters.json
├── FS.Keycloak.RestApiClient.png
├── FS.Keycloak.RestApiClient
├── FS.Keycloak.RestApiClient.sln.DotSettings
└── src
│ ├── FS.Keycloak.RestApiClient.Test
│ └── Authentication
│ │ └── AuthenticationTests.cs
│ └── FS.Keycloak.RestApiClient
│ ├── Api
│ └── .editorconfig
│ ├── Authentication
│ ├── Client
│ │ ├── AuthenticationHttpClient.cs
│ │ ├── ClientCredentialsGrantHttpClient.cs
│ │ ├── DirectTokenHttpClient.cs
│ │ ├── KeycloakHttpClient.cs
│ │ └── PasswordGrantHttpClient.cs
│ ├── ClientFactory
│ │ └── AuthenticationHttpClientFactory.cs
│ ├── Flow
│ │ ├── AuthenticationFlow.cs
│ │ ├── ClientCredentialsFlow.cs
│ │ ├── DirectTokenFlow.cs
│ │ └── PasswordGrantFlow.cs
│ └── Model
│ │ └── KeycloakApiToken.cs
│ ├── Client
│ └── .editorconfig
│ ├── ClientFactory
│ └── ApiClientFactory.cs
│ └── ContractResolver
│ └── SnakeCaseContractResolver.cs
├── README.md
├── build.cmd
├── build.ps1
├── build.sh
└── build
├── .editorconfig
├── Build.cs
├── Configuration.cs
├── Directory.Build.props
├── Directory.Build.targets
├── Properties
└── launchSettings.json
├── _build.csproj
├── _build.csproj.DotSettings
├── _build.sln
├── openapi-generator
├── openapi-generator.config.json
├── openapitools.json
├── package-lock.json
├── package.json
└── templates
│ ├── api.mustache
│ └── modelGeneric.mustache
├── services
├── BuildPaths.cs
├── ClientGenerator.cs
├── Npm.cs
├── OpenApiSpec.cs
└── Project.cs
└── targets
├── net_standard.props
├── nuget.props
└── version.props
/.github/workflows/build_nuget.yml:
--------------------------------------------------------------------------------
1 | name: Build NuGet Packages
2 |
3 | on: [ push, pull_request ]
4 |
5 | jobs:
6 | build_nuget:
7 | runs-on: ubuntu-latest
8 | steps:
9 | - name: Checkout
10 | uses: actions/checkout@v4
11 | - name: Setup .NET
12 | uses: actions/setup-dotnet@v4
13 | with:
14 | dotnet-version: 8.0.x
15 | - name: Setup Node 20.x
16 | uses: actions/setup-node@v4
17 | with:
18 | node-version: '20.x'
19 | - name: Setup Java
20 | uses: actions/setup-java@v4
21 | with:
22 | distribution: 'microsoft'
23 | java-version: '21'
24 | - name: Build NuGet package
25 | env:
26 | OpenApiJson: https://www.keycloak.org/docs-api/26.2.0/rest-api/openapi.json
27 | PublishFolder: Keycloak.RestApiClient.${{github.ref_name}}
28 | NuGetApiKey: ${{ secrets.NUGET_API_KEY }}
29 | run: ./build.cmd BuildClient
30 |
--------------------------------------------------------------------------------
/.github/workflows/publish_nuget.yml:
--------------------------------------------------------------------------------
1 | name: Publish NuGet Packages
2 |
3 | on:
4 | release:
5 | types: [ published ]
6 |
7 | jobs:
8 | publish_nuget:
9 | runs-on: ubuntu-latest
10 | steps:
11 | - name: Checkout
12 | uses: actions/checkout@v4
13 | - name: Setup .NET
14 | uses: actions/setup-dotnet@v4
15 | with:
16 | dotnet-version: 8.0.x
17 | - name: Setup Node 20.x
18 | uses: actions/setup-node@v4
19 | with:
20 | node-version: '20.x'
21 | - name: Setup Java
22 | uses: actions/setup-java@v4
23 | with:
24 | distribution: 'microsoft'
25 | java-version: '21'
26 | - name: Build NuGet package
27 | env:
28 | OpenApiJson: https://www.keycloak.org/docs-api/26.2.0/rest-api/openapi.json
29 | PublishFolder: Keycloak.RestApiClient.${{github.ref_name}}
30 | NuGetApiKey: ${{ secrets.NUGET_API_KEY }}
31 | run: ./build.cmd PublishClient
32 | - name: Upload NuGet package
33 | uses: actions/upload-artifact@v4
34 | with:
35 | name: Keycloak.RestApiClient.${{github.ref_name}}
36 | path: Keycloak.RestApiClient.${{github.ref_name}}
37 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.suo
8 | *.user
9 | *.userosscache
10 | *.sln.docstates
11 |
12 | # User-specific files (MonoDevelop/Xamarin Studio)
13 | *.userprefs
14 |
15 | # Build results
16 | [Dd]ebug/
17 | [Dd]ebugPublic/
18 | [Rr]elease/
19 | [Rr]eleases/
20 | x64/
21 | x86/
22 | bld/
23 | [Bb]in/
24 | [Oo]bj/
25 | [Ll]og/
26 |
27 | # Visual Studio 2015 cache/options directory
28 | .vs/
29 | # Uncomment if you have tasks that create the project's static files in wwwroot
30 | #wwwroot/
31 |
32 | # MSTest test Results
33 | [Tt]est[Rr]esult*/
34 | [Bb]uild[Ll]og.*
35 |
36 | # NUNIT
37 | *.VisualState.xml
38 | TestResult.xml
39 |
40 | # Build Results of an ATL Project
41 | [Dd]ebugPS/
42 | [Rr]eleasePS/
43 | dlldata.c
44 |
45 | # Benchmark Results
46 | BenchmarkDotNet.Artifacts/
47 |
48 | # .NET Core
49 | project.lock.json
50 | project.fragment.lock.json
51 | artifacts/
52 | **/Properties/launchSettings.json
53 |
54 | *_i.c
55 | *_p.c
56 | *_i.h
57 | *.ilk
58 | *.meta
59 | *.obj
60 | *.pch
61 | *.pdb
62 | *.pgc
63 | *.pgd
64 | *.rsp
65 | *.sbr
66 | *.tlb
67 | *.tli
68 | *.tlh
69 | *.tmp
70 | *.tmp_proj
71 | *.log
72 | *.vspscc
73 | *.vssscc
74 | .builds
75 | *.pidb
76 | *.svclog
77 | *.scc
78 |
79 | # Chutzpah Test files
80 | _Chutzpah*
81 |
82 | # Visual C++ cache files
83 | ipch/
84 | *.aps
85 | *.ncb
86 | *.opendb
87 | *.opensdf
88 | *.sdf
89 | *.cachefile
90 | *.VC.db
91 | *.VC.VC.opendb
92 |
93 | # Visual Studio profiler
94 | *.psess
95 | *.vsp
96 | *.vspx
97 | *.sap
98 |
99 | # TFS 2012 Local Workspace
100 | $tf/
101 |
102 | # Guidance Automation Toolkit
103 | *.gpState
104 |
105 | # ReSharper is a .NET coding add-in
106 | _ReSharper*/
107 | *.[Rr]e[Ss]harper
108 | *.DotSettings.user
109 |
110 | # JustCode is a .NET coding add-in
111 | .JustCode
112 |
113 | # TeamCity is a build add-in
114 | _TeamCity*
115 |
116 | # DotCover is a Code Coverage Tool
117 | *.dotCover
118 |
119 | # Visual Studio code coverage results
120 | *.coverage
121 | *.coveragexml
122 |
123 | # NCrunch
124 | _NCrunch_*
125 | .*crunch*.local.xml
126 | nCrunchTemp_*
127 |
128 | # MightyMoose
129 | *.mm.*
130 | AutoTest.Net/
131 |
132 | # Web workbench (sass)
133 | .sass-cache/
134 |
135 | # Installshield output folder
136 | [Ee]xpress/
137 |
138 | # DocProject is a documentation generator add-in
139 | DocProject/buildhelp/
140 | DocProject/Help/*.HxT
141 | DocProject/Help/*.HxC
142 | DocProject/Help/*.hhc
143 | DocProject/Help/*.hhk
144 | DocProject/Help/*.hhp
145 | DocProject/Help/Html2
146 | DocProject/Help/html
147 |
148 | # Click-Once directory
149 | publish/
150 |
151 | # Publish Web Output
152 | *.[Pp]ublish.xml
153 | *.azurePubxml
154 | # TODO: Comment the next line if you want to checkin your web deploy settings
155 | # but database connection strings (with potential passwords) will be unencrypted
156 | *.pubxml
157 | *.publishproj
158 |
159 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
160 | # checkin your Azure Web App publish settings, but sensitive information contained
161 | # in these scripts will be unencrypted
162 | PublishScripts/
163 |
164 | # NuGet Packages
165 | *.nupkg
166 | # The packages folder can be ignored because of Package Restore
167 | **/packages/*
168 | # except build/, which is used as an MSBuild target.
169 | !**/packages/build/
170 | # Uncomment if necessary however generally it will be regenerated when needed
171 | #!**/packages/repositories.config
172 | # NuGet v3's project.json files produces more ignorable files
173 | *.nuget.props
174 | *.nuget.targets
175 |
176 | # Microsoft Azure Build Output
177 | csx/
178 | *.build.csdef
179 |
180 | # Microsoft Azure Emulator
181 | ecf/
182 | rcf/
183 |
184 | # Windows Store app package directories and files
185 | AppPackages/
186 | BundleArtifacts/
187 | Package.StoreAssociation.xml
188 | _pkginfo.txt
189 | *.appx
190 |
191 | # Visual Studio cache files
192 | # files ending in .cache can be ignored
193 | *.[Cc]ache
194 | # but keep track of directories ending in .cache
195 | !*.[Cc]ache/
196 |
197 | # Others
198 | ClientBin/
199 | ~$*
200 | *~
201 | *.dbmdl
202 | *.dbproj.schemaview
203 | *.jfm
204 | *.pfx
205 | *.publishsettings
206 | orleans.codegen.cs
207 |
208 | # Since there are multiple workflows, uncomment next line to ignore bower_components
209 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
210 | #bower_components/
211 |
212 | # RIA/Silverlight projects
213 | Generated_Code/
214 |
215 | # Backup & report files from converting an old project file
216 | # to a newer Visual Studio version. Backup files are not needed,
217 | # because we have git ;-)
218 | _UpgradeReport_Files/
219 | Backup*/
220 | UpgradeLog*.XML
221 | UpgradeLog*.htm
222 |
223 | # SQL Server files
224 | *.mdf
225 | *.ldf
226 | *.ndf
227 |
228 | # Business Intelligence projects
229 | *.rdl.data
230 | *.bim.layout
231 | *.bim_*.settings
232 |
233 | # Microsoft Fakes
234 | FakesAssemblies/
235 |
236 | # GhostDoc plugin setting file
237 | *.GhostDoc.xml
238 |
239 | # Node.js Tools for Visual Studio
240 | .ntvs_analysis.dat
241 | node_modules/
242 |
243 | # Typescript v1 declaration files
244 | typings/
245 |
246 | # Visual Studio 6 build log
247 | *.plg
248 |
249 | # Visual Studio 6 workspace options file
250 | *.opt
251 |
252 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
253 | *.vbw
254 |
255 | # Visual Studio LightSwitch build output
256 | **/*.HTMLClient/GeneratedArtifacts
257 | **/*.DesktopClient/GeneratedArtifacts
258 | **/*.DesktopClient/ModelManifest.xml
259 | **/*.Server/GeneratedArtifacts
260 | **/*.Server/ModelManifest.xml
261 | _Pvt_Extensions
262 |
263 | # Paket dependency manager
264 | .paket/paket.exe
265 | paket-files/
266 |
267 | # FAKE - F# Make
268 | .fake/
269 |
270 | # JetBrains Rider
271 | .idea/
272 | *.sln.iml
273 |
274 | # CodeRush
275 | .cr/
276 |
277 | # Python Tools for Visual Studio (PTVS)
278 | __pycache__/
279 | *.pyc
280 |
281 | # Cake - Uncomment if you are using it
282 | # tools/**
283 | # !tools/packages.config
284 |
285 | # Tabs Studio
286 | *.tss
287 |
288 | # Telerik's JustMock configuration file
289 | *.jmconfig
290 |
291 | # BizTalk build output
292 | *.btp.cs
293 | *.btm.cs
294 | *.odx.cs
295 | *.xsd.cs
296 |
297 | # User added stuff
298 | .openapi-generator/
299 | /keycloak.openapi.json
300 | /keycloak.openapi.*.json
301 | /FS.Keycloak.RestApiClient/.gitignore
302 | /FS.Keycloak.RestApiClient/.openapi-generator-ignore
303 | /FS.Keycloak.RestApiClient/FS.Keycloak.RestApiClient.sln
304 | /FS.Keycloak.RestApiClient/README.md
305 | /FS.Keycloak.RestApiClient/appveyor.yml
306 | /FS.Keycloak.RestApiClient/docs
307 | /FS.Keycloak.RestApiClient/git_push.sh
308 | /FS.Keycloak.RestApiClient/src/FS.Keycloak.RestApiClient/Api
309 | /FS.Keycloak.RestApiClient/src/FS.Keycloak.RestApiClient/Client
310 | /FS.Keycloak.RestApiClient/src/FS.Keycloak.RestApiClient/Model
311 | /FS.Keycloak.RestApiClient/src/FS.Keycloak.RestApiClient/FS.Keycloak.RestApiClient.csproj
312 | /FS.Keycloak.RestApiClient/src/FS.Keycloak.RestApiClient.Test/Api
313 | /FS.Keycloak.RestApiClient/src/FS.Keycloak.RestApiClient.Test/Model
314 | /FS.Keycloak.RestApiClient/src/FS.Keycloak.RestApiClient.Test/FS.Keycloak.RestApiClient.Test.csproj
315 | /FS.Keycloak.RestApiClient/src/FS.Keycloak.RestApiClient.Test/Authentication/AuthenticationTests.data.json
316 | /FS.Keycloak.RestApiClient/api/openapi.yaml
317 |
--------------------------------------------------------------------------------
/.nuke/build.schema.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json-schema.org/draft-04/schema#",
3 | "definitions": {
4 | "Host": {
5 | "type": "string",
6 | "enum": [
7 | "AppVeyor",
8 | "AzurePipelines",
9 | "Bamboo",
10 | "Bitbucket",
11 | "Bitrise",
12 | "GitHubActions",
13 | "GitLab",
14 | "Jenkins",
15 | "Rider",
16 | "SpaceAutomation",
17 | "TeamCity",
18 | "Terminal",
19 | "TravisCI",
20 | "VisualStudio",
21 | "VSCode"
22 | ]
23 | },
24 | "ExecutableTarget": {
25 | "type": "string",
26 | "enum": [
27 | "BuildClient",
28 | "GenerateClient",
29 | "ProvideOpenApiSpec",
30 | "PublishClient",
31 | "Restore"
32 | ]
33 | },
34 | "Verbosity": {
35 | "type": "string",
36 | "description": "",
37 | "enum": [
38 | "Verbose",
39 | "Normal",
40 | "Minimal",
41 | "Quiet"
42 | ]
43 | },
44 | "NukeBuild": {
45 | "properties": {
46 | "Continue": {
47 | "type": "boolean",
48 | "description": "Indicates to continue a previously failed build attempt"
49 | },
50 | "Help": {
51 | "type": "boolean",
52 | "description": "Shows the help text for this build assembly"
53 | },
54 | "Host": {
55 | "description": "Host for execution. Default is 'automatic'",
56 | "$ref": "#/definitions/Host"
57 | },
58 | "NoLogo": {
59 | "type": "boolean",
60 | "description": "Disables displaying the NUKE logo"
61 | },
62 | "Partition": {
63 | "type": "string",
64 | "description": "Partition to use on CI"
65 | },
66 | "Plan": {
67 | "type": "boolean",
68 | "description": "Shows the execution plan (HTML)"
69 | },
70 | "Profile": {
71 | "type": "array",
72 | "description": "Defines the profiles to load",
73 | "items": {
74 | "type": "string"
75 | }
76 | },
77 | "Root": {
78 | "type": "string",
79 | "description": "Root directory during build execution"
80 | },
81 | "Skip": {
82 | "type": "array",
83 | "description": "List of targets to be skipped. Empty list skips all dependencies",
84 | "items": {
85 | "$ref": "#/definitions/ExecutableTarget"
86 | }
87 | },
88 | "Target": {
89 | "type": "array",
90 | "description": "List of targets to be invoked. Default is '{default_target}'",
91 | "items": {
92 | "$ref": "#/definitions/ExecutableTarget"
93 | }
94 | },
95 | "Verbosity": {
96 | "description": "Logging verbosity during build execution. Default is 'Normal'",
97 | "$ref": "#/definitions/Verbosity"
98 | }
99 | }
100 | }
101 | },
102 | "allOf": [
103 | {
104 | "properties": {
105 | "NuGetApiKey": {
106 | "type": "string",
107 | "default": "Secrets must be entered via 'nuke :secrets [profile]'"
108 | },
109 | "OpenApiJson": {
110 | "type": "string",
111 | "description": "Path or download URL for Open API definition to build client for"
112 | },
113 | "PublishFolder": {
114 | "type": "string"
115 | },
116 | "Version": {
117 | "type": "string"
118 | }
119 | }
120 | },
121 | {
122 | "$ref": "#/definitions/NukeBuild"
123 | }
124 | ]
125 | }
126 |
--------------------------------------------------------------------------------
/.nuke/parameters.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "build.schema.json"
3 | }
4 |
--------------------------------------------------------------------------------
/FS.Keycloak.RestApiClient.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fschick/Keycloak.RestApiClient/07cd2705d15ef8a7bc9c58674c45dd4c9f77c659/FS.Keycloak.RestApiClient.png
--------------------------------------------------------------------------------
/FS.Keycloak.RestApiClient/FS.Keycloak.RestApiClient.sln.DotSettings:
--------------------------------------------------------------------------------
1 |
2 | True
--------------------------------------------------------------------------------
/FS.Keycloak.RestApiClient/src/FS.Keycloak.RestApiClient.Test/Authentication/AuthenticationTests.cs:
--------------------------------------------------------------------------------
1 | using FS.Keycloak.RestApiClient.Authentication.ClientFactory;
2 | using FS.Keycloak.RestApiClient.Authentication.Flow;
3 | using Newtonsoft.Json;
4 | using System.Collections;
5 | using System.Collections.Generic;
6 | using System.IO;
7 | using System.Linq;
8 | using System.Net.Http;
9 | using System.Threading;
10 | using System.Threading.Tasks;
11 | using Xunit;
12 |
13 | namespace FS.Keycloak.RestApiClient.Test.Authentication
14 | {
15 | public class AuthenticationTests
16 | {
17 | [Theory]
18 | [ClassData(typeof(AuthenticationTestDataGenerator))]
19 | public async Task WhenClientAuthenticationIsProvided_UserInfoCanBeRequested(AuthenticationTestData authenticationTestData)
20 | {
21 | if (authenticationTestData == null)
22 | return;
23 |
24 | if (authenticationTestData.ClientCredentialsFlow != null)
25 | await TestClientCredentialsFlow(authenticationTestData);
26 |
27 | if (authenticationTestData.PasswordGrantFlow != null)
28 | await TestPasswordGrantFlow(authenticationTestData);
29 | }
30 |
31 | private static async Task TestClientCredentialsFlow(AuthenticationTestData authenticationTestData)
32 | {
33 | var url = authenticationTestData.KeycloakUrl;
34 | var realm = authenticationTestData.KeycloakRealm;
35 |
36 | var clientCredentials = new ClientCredentialsFlow
37 | {
38 | KeycloakUrl = url,
39 | Realm = realm,
40 | ClientId = authenticationTestData.ClientCredentialsFlow.ClientId,
41 | ClientSecret = authenticationTestData.ClientCredentialsFlow.ClientSecret,
42 | Scope = "openid",
43 | };
44 |
45 | using var httpClient = AuthenticationHttpClientFactory.Create(clientCredentials);
46 | using var request = new HttpRequestMessage(HttpMethod.Get, $"{url}/realms/{realm}/protocol/openid-connect/userinfo");
47 | using var response = await httpClient.SendAsync(request, CancellationToken.None);
48 | Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
49 | }
50 |
51 | private static async Task TestPasswordGrantFlow(AuthenticationTestData authenticationTestData)
52 | {
53 | var url = authenticationTestData.KeycloakUrl;
54 | var realm = authenticationTestData.KeycloakRealm;
55 |
56 | var clientCredentials = new PasswordGrantFlow
57 | {
58 | KeycloakUrl = url,
59 | Realm = realm,
60 | UserName = authenticationTestData.PasswordGrantFlow.Username,
61 | Password = authenticationTestData.PasswordGrantFlow.Password,
62 | Scope = "openid",
63 | };
64 |
65 | using var httpClient = AuthenticationHttpClientFactory.Create(clientCredentials);
66 | using var request = new HttpRequestMessage(HttpMethod.Get, $"{url}/realms/{realm}/protocol/openid-connect/userinfo");
67 | using var response = await httpClient.SendAsync(request, CancellationToken.None);
68 | Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
69 | }
70 | }
71 |
72 | public class AuthenticationTestData
73 | {
74 | public string KeycloakUrl { get; set; }
75 |
76 | public string KeycloakRealm { get; set; }
77 |
78 | public ClientCredentialsTestData ClientCredentialsFlow { get; set; }
79 |
80 | public PasswordGrantTestData PasswordGrantFlow { get; set; }
81 |
82 | public class ClientCredentialsTestData
83 | {
84 | public string ClientId { get; set; }
85 | public string ClientSecret { get; set; }
86 | }
87 |
88 | public class PasswordGrantTestData
89 | {
90 | public string Username { get; set; }
91 | public string Password { get; set; }
92 | }
93 | }
94 |
95 | public class AuthenticationTestDataGenerator : IEnumerable