Skip to content

Commit

Permalink
remove dependency on identityModel. (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
xbotter committed Jul 6, 2023
1 parent c5ba452 commit 40285b9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<!-- Tests -->
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageVersion Include="NSubstitute" Version="5.0.0" />
<PackageVersion Include="System.Text.Json" Version="7.0.3" />
<PackageVersion Include="xunit" Version="2.4.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.5" />
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
<!-- Misc -->
<PackageVersion Include="IdentityModel" Version="6.1.0" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageProjectUrl>https://github.com/custouch/semantic-kernel-ERNIE-Bot</PackageProjectUrl>
<RepositoryUrl>https://github.com/custouch/semantic-kernel-ERNIE-Bot</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>0.3.0-preview</Version>
<Version>0.3.1-preview</Version>
<PackageOutputPath>..\..\nupkgs</PackageOutputPath>
<PackageReadmeFile>readme.md</PackageReadmeFile>

Expand Down
2 changes: 1 addition & 1 deletion src/ERNIE-Bot.SDK/ERNIE-Bot.SDK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@


<ItemGroup>
<PackageReference Include="IdentityModel" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" />
<PackageReference Include="Microsoft.VisualStudio.Validation" />
<PackageReference Include="System.Text.Json" />
</ItemGroup>


Expand Down
27 changes: 8 additions & 19 deletions src/ERNIE-Bot.SDK/ERNIEBotClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using ERNIE_Bot.SDK.Models;
using IdentityModel;
using IdentityModel.Client;
using Microsoft;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -189,29 +187,20 @@ public async Task<string> GetAccessTokenAsync(CancellationToken cancellationToke
return token;
}

var urlBuilder = new RequestUrl(Defaults.AccessTokenEndpoint);
var url = urlBuilder.Create(new Parameters
{
{"client_id",_clientId },
{"client_secret",_clientSecret },
{"grant_type",OidcConstants.GrantTypes.ClientCredentials }
});
var url = $"{Defaults.AccessTokenEndpoint}?client_id={_clientId}&client_secret={_clientSecret}&grant_type=client_credentials";

var requestToken = await _client.RequestTokenAsync(
new TokenRequest()
{
Address = url,
GrantType = OidcConstants.GrantTypes.ClientCredentials,
}) ?? throw new HttpRequestException($"Failed to get access token");
var webRequest = new HttpRequestMessage(HttpMethod.Post, url);
var response = await _client.SendAsync(webRequest, cancellationToken);
var accessToken = await ParseResponseAsync<TokenResponse>(response);

if (requestToken.IsError || requestToken.AccessToken == null)
if (string.IsNullOrWhiteSpace(accessToken.AccessToken))
{
throw new HttpRequestException($"Failed to get access token: {requestToken.Error}");
throw new HttpRequestException($"Failed to get access token");
}

await _tokenStore.SaveTokenAsync(requestToken.AccessToken, TimeSpan.FromSeconds(requestToken.ExpiresIn), cancellationToken);
await _tokenStore.SaveTokenAsync(accessToken.AccessToken, TimeSpan.FromSeconds(accessToken.Expiration), cancellationToken);

return requestToken.AccessToken;
return accessToken.AccessToken;
}

#region ===== private methods =====
Expand Down
31 changes: 31 additions & 0 deletions src/ERNIE-Bot.SDK/Models/TokenResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;

namespace ERNIE_Bot.SDK.Models
{
/// <summary>
/// Access Token Response
/// </summary>
public class TokenResponse
{
[JsonPropertyName("access_token")]
public string AccessToken { get; set; } = string.Empty;

[JsonPropertyName("refresh_token")]
public string RefreshToken { get; set; } = string.Empty;

[JsonPropertyName("expires_in")]
public long Expiration { get; set; }

[JsonPropertyName("session_key")]
public string SessionKey { get; set; } = string.Empty;

[JsonPropertyName("session_secret")]
public string SessionSecret { get; set; } = string.Empty;

[JsonPropertyName("scope")]
public string Scope { get; set; } = string.Empty;
}
}

0 comments on commit 40285b9

Please sign in to comment.