3using System.Diagnostics.CodeAnalysis;
5using System.Threading.Tasks;
60 public string?
Realm => this.environment.Realm;
70 public bool Encrypted => this.environment.Encrypted;
101 s = Encoding.UTF8.GetString(Convert.FromBase64String(s[6..]));
102 int i = s.IndexOf(
':');
103 if (i == s.Length - 1)
109 return this.authenticationSchemes;
118 if (!(this.Users is
null))
121 this.JwtFactory, this.Users);
124 return !(this.authenticationSchemes is
null);
139 List<HttpAuthenticationScheme> Schemes =
new List<HttpAuthenticationScheme>();
164 if (!(Server is
null))
168 this.MinStrength,
Users));
170 return Schemes.ToArray();
180 return Task.FromResult<
object>(
new Dictionary<string, object>()
182 {
"error",
"invalid_client" },
183 {
"error_description",
"Unauthorized access prohibited." }
196 string ErrorDescription,
int StatusCode,
string StatusMessage)
198 Response.StatusCode = StatusCode;
199 Response.StatusMessage = StatusMessage;
200 Response.
SetHeader(
"Cache-Control",
"max-age=0, no-cache, no-store");
201 Response.
SetHeader(
"Pragma",
"no-cache");
203 return Response.
Return(ErrorResponse(ErrorCode, ErrorDescription));
206 private static Dictionary<string, object> ErrorResponse(
string ErrorCode,
207 string ErrorDescription)
209 Dictionary<string, object> Result =
new Dictionary<string, object>()
211 {
"error", ErrorCode },
212 {
"error_description", ErrorDescription }
225 string ErrorDescription)
227 return ReturnError(Response, ErrorCode, ErrorDescription,
238 string ErrorDescription)
240 return ReturnError(Response, ErrorCode, ErrorDescription,
252 string ErrorDescription,
string[] Challenges)
254 Response.
SetHeader(
"Cache-Control",
"max-age=0, no-cache, no-store");
255 Response.
SetHeader(
"Pragma",
"no-cache");
258 ErrorResponse(ErrorCode, ErrorDescription), Challenges));
268 string ErrorDescription)
270 return ReturnError(Response, ErrorCode, ErrorDescription,
281 string ErrorDescription)
283 return ReturnError(Response, ErrorCode, ErrorDescription,
294 if (
string.IsNullOrEmpty(Scope))
297 string[] Tokens = Scope.Split(
' ');
299 foreach (
string Token
in Tokens)
301 if (Token.Length == 0)
304 foreach (
char ch
in Token)
309 if (ch >= 0x23 && ch <= 0x5B)
312 if (ch >= 0x5D && ch <= 0x7E)
331 [NotNullWhen(
false)] out
string? MissingPrivilege)
334 Scopes.Split(
' ', StringSplitOptions.RemoveEmptyEntries),
335 User, out MissingPrivilege);
347 [NotNullWhen(
false)] out
string? MissingPrivilege)
349 foreach (
string Scope
in Scopes)
354 MissingPrivilege = Privilege;
359 MissingPrivilege =
null;
Basic authentication mechanism, as defined in RFC 2617: https://tools.ietf.org/html/rfc2617
Digest authentication mechanism, as defined in RFC 2617: https://tools.ietf.org/html/rfc2617
mTLS authentication mechanism, where identity is taken from a valid client certificate.
Authentication mechanism that makes sure the user has an established session with the web server.
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
const string StatusMessage
Bad Request
The server understood the request, but is refusing to fulfill it. Authorization will not help and the...
const string StatusMessage
Forbidden
Base class for all HTTP authentication schemes, as defined in RFC-7235: https://datatracker....
Represents an HTTP request.
HttpRequestHeader Header
Request header.
string ResourceName
Name of resource.
Represets a response of an HTTP client request.
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
void SetHeader(string FieldName, string Value)
Sets a custom header field value.
Task Return(Exception ex)
Returns an error to the client.
Implements an HTTP server.
override void Add(ISniffer Sniffer)
ICommunicationLayer.Add
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
The server has not found anything matching the Request-URI. No indication is given of whether the con...
const string StatusMessage
Not Found
Manages the OAuth 2 environment.
OAUTH Client Credentials authentication scheme, as defined in RFCs 6749.
Abstract base class for OAUTH resources.
const string OAuthScopePrivilegePrefix
Privilege prefix for OAUTH scopes.
HttpAuthenticationScheme[] CreateAuthenticationSchemes(JwtFactory JwtFactory, IUserSource Users)
Creates a set of authentication scheme object reference for the resource.
override Task< object > DefaultErrorContent(int StatusCode)
Returns default content for an error, for the resource. If returning null, server will choose default...
override? HttpAuthenticationScheme[] GetAuthenticationSchemes(HttpRequest Request)
Any authentication schemes used to authenticate users before access is granted to the corresponding r...
IUserSource? Users
Data source for users, used to authenticate clients.
OAuth2Environment Environment
OAUTH2 environment, used to access clients, tokens, and other resources.
string? Realm
Realm name, if any, used for authentication. Null if no realm is defined.
override bool HandlesSubPaths
If the resource handles sub-paths.
static Task ServiceUnavailable(HttpResponse Response, string ErrorCode, string ErrorDescription)
Returns a Service Unavailable error back to the client.
static bool IsValidScope(string Scope)
Checks if a scope value is valid, according to the OAUTH2 specification.
static Task ReturnError(HttpResponse Response, string ErrorCode, string ErrorDescription, int StatusCode, string StatusMessage)
Returns an error back to the client.
static bool HasScopePrivileges(string[] Scopes, IUser User, [NotNullWhen(false)] out string? MissingPrivilege)
Checks if a user has the privileges associated with a set of scopes.
bool InitAuthentication()
Initializes authentication schemes, if not already initialized.
int MinStrength
Minimum strength of ciphers used in encryption, if any. 0 if no encryption is used.
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.
override bool UserSessions
If the resource uses user sessions.
static Task NotFound(HttpResponse Response, string ErrorCode, string ErrorDescription)
Returns a Not Found error back to the client.
static Task Unauthorized(HttpResponse Response, string ErrorCode, string ErrorDescription, string[] Challenges)
Returns an Unauthorized error back to the client.
OAuthResource(OAuth2Environment Environment, string ResourceName)
OAUTH authorize resource.
static Task Forbidden(HttpResponse Response, string ErrorCode, string ErrorDescription)
Returns a Forbidden error back to the client.
static bool HasScopePrivileges(string Scopes, IUser User, [NotNullWhen(false)] out string? MissingPrivilege)
Checks if a user has the privileges associated with a set of scopes.
HttpAuthenticationScheme?[] AuthenticationSchemes
Available authentication schemes, if initialized.
The server is currently unable to handle the request due to a temporary overloading or maintenance of...
const string StatusMessage
Service Unavailable
Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or ...
Static class that dynamically manages types and interfaces available in the runtime environment.
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Use JWT tokens for authentication. The Bearer scheme defined in RFC 6750 is used: https://tools....
A factory that can create and validate JWT tokens.
JwtFactory(IJwsAlgorithm Algorithm)
A factory that can create and validate JWT tokens.
bool HasPrivilege(string Privilege)
If the object has a given privilege.
Basic interface for a user.
Interface for data sources containing users.
DigestAlgorithm
Digest algorithm
ClientCertificates
Client Certificate Options