|
|
|
|
@@ -1,12 +1,24 @@
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using HopFrame.Security.Authentication.OpenID.Models;
|
|
|
|
|
using HopFrame.Security.Authentication.OpenID.Options;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
|
|
|
namespace HopFrame.Security.Authentication.OpenID.Implementation;
|
|
|
|
|
|
|
|
|
|
internal class OpenIdAccessor(IHttpClientFactory clientFactory, IOptions<OpenIdOptions> options) : IOpenIdAccessor {
|
|
|
|
|
internal class OpenIdAccessor(IHttpClientFactory clientFactory, IOptions<OpenIdOptions> options, IHttpContextAccessor accessor, IMemoryCache cache) : IOpenIdAccessor {
|
|
|
|
|
private const string DefaultCallbackEndpoint = "api/v1/openid/callback";
|
|
|
|
|
|
|
|
|
|
private const string ConfigurationCacheKey = "HopFrame:OpenID:Configuration";
|
|
|
|
|
private const string AuthCodeCacheKey = "HopFrame:OpenID:Code:";
|
|
|
|
|
private const string TokenCacheKey = "HopFrame:OpenID:Token:";
|
|
|
|
|
|
|
|
|
|
public async Task<OpenIdConfiguration> LoadConfiguration() {
|
|
|
|
|
if (options.Value.Cache.Enabled && options.Value.Cache.Configuration.Enabled && cache.TryGetValue(ConfigurationCacheKey, out object cachedConfiguration)) {
|
|
|
|
|
return cachedConfiguration as OpenIdConfiguration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var client = clientFactory.CreateClient();
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, Path.Combine(options.Value.Issuer, ".well-known/openid-configuration"));
|
|
|
|
|
var response = await client.SendAsync(request);
|
|
|
|
|
@@ -14,10 +26,22 @@ internal class OpenIdAccessor(IHttpClientFactory clientFactory, IOptions<OpenIdO
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return await JsonSerializer.DeserializeAsync<OpenIdConfiguration>(await response.Content.ReadAsStreamAsync());
|
|
|
|
|
var config = await JsonSerializer.DeserializeAsync<OpenIdConfiguration>(await response.Content.ReadAsStreamAsync());
|
|
|
|
|
|
|
|
|
|
if (options.Value.Cache.Enabled && options.Value.Cache.Configuration.Enabled)
|
|
|
|
|
cache.Set(ConfigurationCacheKey, config, options.Value.Cache.Configuration.TTL.ConstructTimeSpan);
|
|
|
|
|
|
|
|
|
|
return config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<OpenIdToken> RequestToken(string code) {
|
|
|
|
|
if (options.Value.Cache.Enabled && options.Value.Cache.Auth.Enabled && cache.TryGetValue(AuthCodeCacheKey + code, out object cachedToken)) {
|
|
|
|
|
return cachedToken as OpenIdToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var protocol = accessor.HttpContext!.Request.IsHttps ? "https" : "http";
|
|
|
|
|
var callback = options.Value.Callback ?? $"{protocol}://{accessor.HttpContext!.Request.Host.Value}/{DefaultCallbackEndpoint}";
|
|
|
|
|
|
|
|
|
|
var configuration = await LoadConfiguration();
|
|
|
|
|
|
|
|
|
|
var client = clientFactory.CreateClient();
|
|
|
|
|
@@ -25,7 +49,7 @@ internal class OpenIdAccessor(IHttpClientFactory clientFactory, IOptions<OpenIdO
|
|
|
|
|
Content = new FormUrlEncodedContent(new Dictionary<string, string> {
|
|
|
|
|
{ "grant_type", "authorization_code" },
|
|
|
|
|
{ "code", code },
|
|
|
|
|
{ "redirect_uri", options.Value.Callback },
|
|
|
|
|
{ "redirect_uri", callback },
|
|
|
|
|
{ "client_id", options.Value.ClientId },
|
|
|
|
|
{ "client_secret", options.Value.ClientSecret }
|
|
|
|
|
})
|
|
|
|
|
@@ -35,15 +59,27 @@ internal class OpenIdAccessor(IHttpClientFactory clientFactory, IOptions<OpenIdO
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return await JsonSerializer.DeserializeAsync<OpenIdToken>(await response.Content.ReadAsStreamAsync());
|
|
|
|
|
var token = await JsonSerializer.DeserializeAsync<OpenIdToken>(await response.Content.ReadAsStreamAsync());
|
|
|
|
|
|
|
|
|
|
if (options.Value.Cache.Enabled && options.Value.Cache.Auth.Enabled)
|
|
|
|
|
cache.Set(AuthCodeCacheKey + code, token, options.Value.Cache.Auth.TTL.ConstructTimeSpan);
|
|
|
|
|
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> ConstructAuthUri(string state = null) {
|
|
|
|
|
var protocol = accessor.HttpContext!.Request.IsHttps ? "https" : "http";
|
|
|
|
|
var callback = options.Value.Callback ?? $"{protocol}://{accessor.HttpContext!.Request.Host.Value}/{DefaultCallbackEndpoint}";
|
|
|
|
|
|
|
|
|
|
var configuration = await LoadConfiguration();
|
|
|
|
|
return $"{configuration.AuthorizationEndpoint}?response_type=code&client_id={options.Value.ClientId}&redirect_uri={options.Value.Callback}&scope=openid%20profile%20email&state={state}";
|
|
|
|
|
return $"{configuration.AuthorizationEndpoint}?response_type=code&client_id={options.Value.ClientId}&redirect_uri={callback}&scope=openid%20profile%20email%20offline_access&state={state}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<OpenIdIntrospection> InspectToken(string token) {
|
|
|
|
|
if (options.Value.Cache.Enabled && options.Value.Cache.Inspection.Enabled && cache.TryGetValue(TokenCacheKey + token, out object cachedToken)) {
|
|
|
|
|
return cachedToken as OpenIdIntrospection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var configuration = await LoadConfiguration();
|
|
|
|
|
|
|
|
|
|
var client = clientFactory.CreateClient();
|
|
|
|
|
@@ -59,6 +95,31 @@ internal class OpenIdAccessor(IHttpClientFactory clientFactory, IOptions<OpenIdO
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return await JsonSerializer.DeserializeAsync<OpenIdIntrospection>(await response.Content.ReadAsStreamAsync());
|
|
|
|
|
var introspection = await JsonSerializer.DeserializeAsync<OpenIdIntrospection>(await response.Content.ReadAsStreamAsync());
|
|
|
|
|
|
|
|
|
|
if (options.Value.Cache.Enabled && options.Value.Cache.Inspection.Enabled)
|
|
|
|
|
cache.Set(TokenCacheKey + token, introspection, options.Value.Cache.Inspection.TTL.ConstructTimeSpan);
|
|
|
|
|
|
|
|
|
|
return introspection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<OpenIdToken> RefreshAccessToken(string refreshToken) {
|
|
|
|
|
var configuration = await LoadConfiguration();
|
|
|
|
|
|
|
|
|
|
var client = clientFactory.CreateClient();
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Post, configuration.TokenEndpoint) {
|
|
|
|
|
Content = new FormUrlEncodedContent(new Dictionary<string, string> {
|
|
|
|
|
{ "grant_type", "refresh_token" },
|
|
|
|
|
{ "refresh_token", refreshToken },
|
|
|
|
|
{ "client_id", options.Value.ClientId },
|
|
|
|
|
{ "client_secret", options.Value.ClientSecret }
|
|
|
|
|
})
|
|
|
|
|
};
|
|
|
|
|
var response = await client.SendAsync(request);
|
|
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return await JsonSerializer.DeserializeAsync<OpenIdToken>(await response.Content.ReadAsStreamAsync());
|
|
|
|
|
}
|
|
|
|
|
}
|