Skip to content

Commit 8678c00

Browse files
Sro auth examples (#2442)
* v2.9.0 * Add examples for SR OAuth
1 parent 1178272 commit 8678c00

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright 2025 Confluent Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
// Refer to LICENSE for more information.
16+
17+
using Confluent.SchemaRegistry;
18+
using System.Net.Http.Headers;
19+
/// <summary>
20+
/// An example showing schema registry authentication
21+
/// with an OAUTHBEARER token implementation.
22+
/// </summary>
23+
namespace Confluent.Kafka.Examples.SchemaRegistryOAuth
24+
{
25+
26+
public class Program
27+
{
28+
29+
private class ExampleBearerAuthProvider : IAuthenticationBearerHeaderValueProvider
30+
{
31+
private string token;
32+
private string logicalCluster;
33+
private string identityPool;
34+
35+
36+
public ExampleBearerAuthProvider(string token, string logicalCluster, string identityPool)
37+
{
38+
this.token = token;
39+
this.logicalCluster = logicalCluster;
40+
this.identityPool = identityPool;
41+
}
42+
43+
public string GetBearerToken() => token;
44+
public AuthenticationHeaderValue GetAuthenticationHeader() => new AuthenticationHeaderValue("Bearer", token);
45+
public string GetLogicalCluster() => logicalCluster;
46+
public string GetIdentityPool() => identityPool;
47+
public bool NeedsInitOrRefresh() => false;
48+
public Task InitOrRefreshAsync() => Task.CompletedTask;
49+
}
50+
51+
public static async Task Main(string[] args)
52+
{
53+
if (args.Length != 9)
54+
{
55+
Console.WriteLine("Usage: .. schemaRegistryUrl clientId clientSecret scope tokenEndpoint logicalCluster identityPool token");
56+
return;
57+
}
58+
string schemaRegistryUrl = args[1];
59+
string clientId = args[2];
60+
string clientSecret = args[3];
61+
string scope = args[4];
62+
string tokenEndpoint = args[5];
63+
string logicalCluster = args[6];
64+
string identityPool = args[7];
65+
string token = args[8];
66+
67+
//using BearerAuthCredentialsSource.OAuthBearer
68+
var clientCredentialsSchemaRegistryConfig = new SchemaRegistryConfig
69+
{
70+
Url = schemaRegistryUrl,
71+
BearerAuthCredentialsSource = BearerAuthCredentialsSource.OAuthBearer,
72+
BearerAuthClientId = clientId,
73+
BearerAuthClientSecret = clientSecret,
74+
BearerAuthScope = scope,
75+
BearerAuthTokenEndpointUrl = tokenEndpoint,
76+
BearerAuthLogicalCluster = logicalCluster,
77+
BearerAuthIdentityPoolId = identityPool
78+
};
79+
80+
using (var schemaRegistry = new CachedSchemaRegistryClient(clientCredentialsSchemaRegistryConfig))
81+
{
82+
var subjects = await schemaRegistry.GetAllSubjectsAsync();
83+
Console.WriteLine(string.Join(", ", subjects));
84+
}
85+
86+
//using BearerAuthCredentialsSource.StaticToken
87+
var staticSchemaRegistryConfig = new SchemaRegistryConfig
88+
{
89+
Url = schemaRegistryUrl,
90+
BearerAuthCredentialsSource = BearerAuthCredentialsSource.StaticToken,
91+
BearerAuthToken = token,
92+
BearerAuthLogicalCluster = logicalCluster,
93+
BearerAuthIdentityPoolId = identityPool
94+
};
95+
96+
using (var schemaRegistry = new CachedSchemaRegistryClient(staticSchemaRegistryConfig))
97+
{
98+
var subjects = await schemaRegistry.GetAllSubjectsAsync();
99+
Console.WriteLine(string.Join(", ", subjects));
100+
}
101+
102+
//Using BearerAuthCredentialsSource.Custom
103+
var customSchemaRegistryConfig = new SchemaRegistryConfig
104+
{
105+
Url = schemaRegistryUrl,
106+
BearerAuthCredentialsSource = BearerAuthCredentialsSource.Custom
107+
};
108+
109+
var customProvider = new ExampleBearerAuthProvider(token, logicalCluster, identityPool);
110+
using (var schemaRegistry = new CachedSchemaRegistryClient(customSchemaRegistryConfig, customProvider))
111+
{
112+
var subjects = await schemaRegistry.GetAllSubjectsAsync();
113+
Console.WriteLine(string.Join(", ", subjects));
114+
}
115+
}
116+
}
117+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<!-- nuget package reference: <PackageReference Include="Confluent.SchemaRegistry" Version="2.9.0" /> -->
12+
<ProjectReference Include="..\..\src\Confluent.SchemaRegistry\Confluent.SchemaRegistry.csproj" />
13+
</ItemGroup>
14+
15+
</Project>

0 commit comments

Comments
 (0)