Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ProtectedResourceMetaData.cs
1using System;
3using System.Reflection;
4using System.Text;
5using System.Threading.Tasks;
7
9{
15 {
19 public const string WellKnowResourcePath = "/.well-known/oauth-protected-resource";
20
27 {
29 }
30
34 public override bool HandlesSubPaths => true;
35
39 public bool AllowsGET => true;
40
47 public async Task GET(HttpRequest Request, HttpResponse Response)
48 {
49 string ResourceName = Request.SubPath;
50
51 if (string.IsNullOrEmpty(ResourceName))
52 {
53 await BadRequest(Response, "invalid_request", "No resource subpath provided.");
54 return;
55 }
56
57 if (!(Request.Server?.TryGetResource(ref ResourceName, false,
58 out HttpResource Resource, out string SubPath) ?? false) ||
59 !string.IsNullOrEmpty(SubPath))
60 {
61 await NotFound(Response, "invalid_request", "Resource not found: " + ResourceName);
62 return;
63 }
64
65 StringBuilder sb = GenerateServerUrl(Request, out int Port);
66 string ServerUrl = sb.ToString();
67
68 sb.Append(ResourceName);
69 string ResourceUrl = sb.ToString();
70
71 ClientCertificates ClientCertificatesConfig;
72
73 if (Request.Server is null)
74 ClientCertificatesConfig = ClientCertificates.NotUsed;
75 else
76 Request.Server.GetMTlsSettings(Port, out ClientCertificatesConfig, out _);
77
78 Dictionary<string, object> MetaData = new Dictionary<string, object>()
79 {
80 { "resource", ResourceUrl },
81 { "authorization_servers", new string[] { ServerUrl } },
82 { "bearer_methods_supported", new string[] { "header", "body" } },
83 { "tls_client_certificate_bound_access_tokens", ClientCertificatesConfig != ClientCertificates.NotUsed }
84 };
85
86 OAuthMetaDataAttribute[] OAuthMetaDAtaAttributes = Resource.GetType().GetCustomAttributes<OAuthMetaDataAttribute>(true) as OAuthMetaDataAttribute[] ?? Array.Empty<OAuthMetaDataAttribute>();
87
88 foreach (OAuthMetaDataAttribute Attribute in OAuthMetaDAtaAttributes)
89 await Attribute.AddMetaData(Resource, MetaData);
90
91 await Response.Return(MetaData);
92 }
93
100 public static StringBuilder GenerateServerUrl(HttpRequest Request, out int Port)
101 {
102 return GenerateServerUrl(Request.Server, Request.Encrypted, Request.Host, out Port);
103 }
104
112 public StringBuilder GenerateServerUrl(bool Encrypted, string? Host, out int Port)
113 {
114 return GenerateServerUrl(this.FirstServer, Encrypted, Host, out Port);
115 }
116
125 public static StringBuilder GenerateServerUrl(HttpServer Server, bool Encrypted,
126 string? Host, out int Port)
127 {
128 StringBuilder sb = new StringBuilder();
129 bool DefaultPort;
130
131 sb.Append("http");
132 if (Encrypted)
133 {
134 int[]? Ports = Server?.OpenHttpsPorts;
135
136 if ((Ports?.Length ?? 0) > 0)
137 {
138 sb.Append('s');
139 Port = GetPort(Server?.OpenHttpsPorts, HttpServer.DefaultHttpsPort);
140 DefaultPort = Port == HttpServer.DefaultHttpsPort;
141 }
142 else
143 {
144 Port = GetPort(Server?.OpenHttpPorts, HttpServer.DefaultHttpPort);
145 DefaultPort = Port == HttpServer.DefaultHttpPort;
146 }
147 }
148 else
149 {
150 Port = GetPort(Server?.OpenHttpPorts, HttpServer.DefaultHttpPort);
151 DefaultPort = Port == HttpServer.DefaultHttpPort;
152 }
153
154 sb.Append("://");
155 sb.Append(Host ?? "localhost"); // TODO: Public IP, if available, instead of localhost.
156
157 if (!DefaultPort)
158 {
159 sb.Append(':');
160 sb.Append(Port.ToString());
161 }
162
163 return sb;
164 }
165
166 private static int GetPort(int[]? Ports, int DefaultPort)
167 {
168 if (Ports is null || Ports.Length == 0)
169 return DefaultPort;
170 else
171 return Ports[0];
172 }
173
181 public string GetResourceMetaDataUri(bool Encrypted, string? Domain, string ResourceName)
182 {
183 StringBuilder sb = this.GenerateServerUrl(Encrypted, Domain, out _);
184 sb.Append(this.ResourceName);
185 sb.Append(ResourceName);
186 return sb.ToString();
187 }
188 }
189}
Represents an HTTP request.
Definition: HttpRequest.cs:22
bool Encrypted
If the connection is encrypted or not.
Definition: HttpRequest.cs:298
string SubPath
Sub-path. If a resource is found handling the request, this property contains the trailing sub-path o...
Definition: HttpRequest.cs:194
string Host
Host reference. (Value of Host header, without the port number)
Definition: HttpRequest.cs:263
HttpServer Server
HTTP Server receiving the request.
Definition: HttpRequest.cs:118
Base class for all HTTP resources.
Definition: HttpResource.cs:23
HttpServer FirstServer
First server on which the resource has been registered.
string ResourceName
Name of resource.
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
Implements an HTTP server.
Definition: HttpServer.cs:41
bool TryGetResource(HttpRequest Request, out HttpResource Resource, out string SubPath)
Tries to get a resource from the server.
Definition: HttpServer.cs:1796
const int DefaultHttpPort
Default HTTP Port (80).
Definition: HttpServer.cs:45
void GetMTlsSettings(int Port, out ClientCertificates ClientCertificates, out bool TrustClientCertificates)
Gets mTLS settings for a given port number.
Definition: HttpServer.cs:979
int[] OpenHttpsPorts
HTTPS Ports successfully opened.
Definition: HttpServer.cs:773
const int DefaultHttpsPort
Default HTTPS port (443).
Definition: HttpServer.cs:50
Abstract base class for OAUTH meta-data attributes.
abstract Task AddMetaData(HttpResource Resource, Dictionary< string, object > MetaData)
Adds available meta-data to a dictionary of meta-data.
void Register(OAuthAuthorizeResource? AuthorizeResource)
Registers an authorization resource.
Abstract base class for OAUTH resources.
OAuth2Environment Environment
OAUTH2 environment, used to access clients, tokens, and other resources.
bool Encrypted
If TLS-encryption is enabled.
static Task BadRequest(HttpResponse Response, string ErrorCode, string ErrorDescription)
Returns a Bad Request error back to the client.
static Task NotFound(HttpResponse Response, string ErrorCode, string ErrorDescription)
Returns a Not Found error back to the client.
Provides OAUTH resource meta-data, as defined in RFC 9728. https://datatracker.ietf....
override bool HandlesSubPaths
If the resource handles sub-paths.
async Task GET(HttpRequest Request, HttpResponse Response)
Executes the GET method on the resource.
static StringBuilder GenerateServerUrl(HttpServer Server, bool Encrypted, string? Host, out int Port)
Generates a server URL, based on the request.
StringBuilder GenerateServerUrl(bool Encrypted, string? Host, out int Port)
Generates a server URL, based on the request.
string GetResourceMetaDataUri(bool Encrypted, string? Domain, string ResourceName)
Gets the resource meta-data URI for a given resource.
static StringBuilder GenerateServerUrl(HttpRequest Request, out int Port)
Generates a server URL, based on the request.
ProtectedResourceMetaData(OAuth2Environment Environment)
Provides OAUTH resource meta-data, as defined in RFC 9728.
GET Interface for HTTP resources.
Definition: ImplTypes.g.cs:58
ClientCertificates
Client Certificate Options