4using System.Security.Cryptography.X509Certificates;
5using System.Threading.Tasks;
21 private static readonly RandomNumberGenerator rnd = RandomNumberGenerator.Create();
35 private event EventHandlerAsync<CustomizeMarkdownEventArgs>? customizeLoginForm;
36 private event EventHandlerAsync<CustomizeMarkdownEventArgs>? customizeDeviceLoginForm;
37 private event EventHandlerAsync<CustomizeMarkdownEventArgs>? customizeDeviceLoginReceipt;
38 private string? loginMasterFileName;
39 private string? realm;
40 private int minStrength;
41 private bool encrypted;
42 private bool locked =
false;
43 private bool disposed =
false;
136 if (this.authorizeResource is
null)
137 throw new InvalidOperationException(
"No authorize resource has been registered.");
139 return this.authorizeResource;
150 if (this.tokenResource is
null)
151 throw new InvalidOperationException(
"No token resource has been registered.");
153 return this.tokenResource;
164 if (this.registrationResource is
null)
165 throw new InvalidOperationException(
"No registration resource has been registered.");
167 return this.registrationResource;
178 if (this.managementResource is
null)
179 throw new InvalidOperationException(
"No management resource has been registered.");
181 return this.managementResource;
192 if (this.deviceAuthorizationResource is
null)
193 throw new InvalidOperationException(
"No device authorization resource has been registered.");
195 return this.deviceAuthorizationResource;
206 if (this.introspectionResource is
null)
207 throw new InvalidOperationException(
"No introspection resource has been registered.");
209 return this.introspectionResource;
220 if (this.serverMetaDataResource is
null)
221 throw new InvalidOperationException(
"No server meta-data resource has been registered.");
223 return this.serverMetaDataResource;
234 if (this.resourceMetaData is
null)
235 throw new InvalidOperationException(
"No resource meta-data resource has been registered.");
237 return this.resourceMetaData;
248 if (this.userSource is
null)
249 throw new InvalidOperationException(
"No user source has been registered.");
251 return this.userSource;
262 if (this.dynamicUserSource is
null)
263 throw new InvalidOperationException(
"No dynamic user source has been registered.");
265 return this.dynamicUserSource;
276 if (this.thingRegistryUserSource is
null)
277 throw new InvalidOperationException(
"No thing registry user source has been registered.");
279 return this.thingRegistryUserSource;
290 if (this.jwtFactory is
null)
301 return this.jwtFactory;
310 get => this.loginMasterFileName;
313 this.AssertUnlocked();
315 if (!
string.IsNullOrEmpty(value) && !File.Exists(value))
316 throw new FileNotFoundException(
"Login master file not found.", value);
318 this.loginMasterFileName = value;
334 this.CheckDomainParameters();
346 this.CheckDomainParameters();
347 return this.minStrength;
358 this.CheckDomainParameters();
359 return this.encrypted;
363 private void AssertUnlocked()
366 throw new UnauthorizedAccessException(
"OAUTH 2 environment is locked and cannot be modified.");
374 this.AssertUnlocked();
384 this.AssertUnlocked();
394 this.AssertUnlocked();
404 this.AssertUnlocked();
414 this.AssertUnlocked();
424 this.AssertUnlocked();
434 this.AssertUnlocked();
444 this.AssertUnlocked();
454 this.AssertUnlocked();
464 this.AssertUnlocked();
476 this.AssertUnlocked();
488 this.AssertUnlocked();
494 private void CheckDomainParameters()
496 if (this.realm is
null)
515 !(Obj is X509Certificate Certificate))
543 byte[] Bin =
new byte[NrBytes];
560 this.AssertUnlocked();
561 this.customizeLoginForm += value;
565 this.AssertUnlocked();
566 this.customizeLoginForm -= value;
578 if (this.customizeLoginForm is
null)
582 await this.customizeLoginForm.Raise(
this, e);
593 this.AssertUnlocked();
594 this.customizeDeviceLoginForm += value;
598 this.AssertUnlocked();
599 this.customizeDeviceLoginForm -= value;
611 if (this.customizeDeviceLoginForm is
null)
615 await this.customizeDeviceLoginForm.Raise(
this, e);
626 this.AssertUnlocked();
627 this.customizeDeviceLoginReceipt += value;
631 this.AssertUnlocked();
632 this.customizeDeviceLoginReceipt -= value;
644 if (this.customizeDeviceLoginReceipt is
null)
648 await this.customizeDeviceLoginReceipt.Raise(
this, e);
Static class that does BASE64URL encoding (using URL and filename safe alphabet), as defined in RFC46...
static string Encode(byte[] Data)
Converts a binary block of data to a Base64URL-encoded string.
Implements a binary TCP Client, by encapsulating a TcpClient. It also makes the use of TcpClient safe...
static string GetDomainFromSubject(string Subject)
Extracts the domain name from a certificate subject string.
Event arguments for markdown customization events.
string Markdown
Markdown text that can be customized.
Manages the OAuth 2 environment.
bool HasIntrospectionResource
If the environment has a registered introspection resource
EventHandlerAsync< CustomizeMarkdownEventArgs >? CustomizeDeviceLoginReceipt
Event raised when the device login receipt is to be customized.
EventHandlerAsync< CustomizeMarkdownEventArgs >? CustomizeLoginForm
Event raised when the login form is to be customized.
bool HasResourceMetaData
If the environment has a registered resource meta-data resource
EventHandlerAsync< CustomizeMarkdownEventArgs >? CustomizeDeviceLoginForm
Event raised when the device login form is to be customized.
void Register(ProtectedResourceMetaData? ResourceMetaData)
Registers a resource meta-data resource.
OAuthIntrospectionResource IntrospectionResource
Registered introspection resource
OAuthDeviceAuthorizationResource DeviceAuthorizationResource
Registered device authorization resource
bool HasUserSource
If the environment has a registered user source
bool HasAuthorizeResource
If the environment has a registered authorization resource
static void GetDomainParameters(out string? Domain, out int MinStrength, out bool Encrypted)
Gets domain parameters, based on module parameters defined in the system.
async Task< string > RaiseCustomizeDeviceLoginForm(string Markdown)
Raises the CustomizeDeviceLoginForm event to customize a device login form before being returned to t...
OAuthAuthorizeResource AuthorizeResource
Registered authorization resource
bool Encrypted
If TLS-encryption is enabled.
IUserSource UserSource
Registered user source
bool HasManagementResource
If the environment has a registered client management resource
void Register(OAuthIntrospectionResource? IntrospectionResource)
Registers an introspection resource.
void Dispose()
IDisposable.Dispose
OAuthRegistrationResource RegistrationResource
Registered registration resource
bool HasDynamicUserSource
If the environment has a registered dynamic user source
bool Locked
If the environment has been locked.
OAuthTokenResource TokenResource
Registered token resource
void Register(OAuthAuthorizeResource? AuthorizeResource)
Registers an authorization resource.
bool HasRegistrationResource
If the environment has a registered registration resource
void Register(OAuthDeviceAuthorizationResource? DeviceAuthorizationResource)
Registers a device authorization resource.
ProtectedResourceMetaData ResourceMetaData
Registered resource meta-data resource
OAuth2Environment()
Manages the OAuth 2.0 environment.
void Register(AuthorizationServerMetaData? ServerMetaDataResource)
Registers a server meta-data resource.
void Register(OAuthManagementResource? ManagementResource)
Registers a management resource.
AuthorizationServerMetaData ServerMetaDataResource
Registered server meta-data resource
IDynamicUserSource DynamicUserSource
Registered dynamic user source
static string GenerateRandomCode(int NrBytes)
Generates a random unique code.
void Register(OAuthRegistrationResource? RegistrationResource)
Registers a registration resource.
bool HasDeviceAuthorizationResource
If the environment has a registered device authorization resource
bool Disposed
If the object has been disposed.
void Register(string Realm, int MinStrength, bool Encrypted)
Registers domain parameters such as realm, minimum strength and encryption.
string? LoginMasterFileName
File name to master file to use in generated login pages.
JwtFactory JwtFactory
Registered JWT factory
void Register(JwtFactory JwtFactory)
Registers a JWT factory.
bool HasThingRegistryUserSource
If the environment has a registered thing registry user source
bool HasLoginMasterFileName
If a login master file name has been registered
int MinStrength
Minimum strength of ciphers used in encryption.
bool HasServerMetaDataResource
If the environment has a registered server meta-data resource
void Register(OAuthTokenResource? TokenResource)
Registers a token resource.
void Lock()
Locks the OAUTH 2 environment.
async Task< string > RaiseCustomizeDeviceLoginReceipt(string Markdown)
Raises the CustomizeDeviceLoginReceipt event to customize a device login receipt before being returne...
void Register(IUserSource? UserSource)
Registers a user source.
bool HasTokenResource
If the environment has a registered token resource
IThingRegistryUserSource ThingRegistryUserSource
Registered thing registry user source
async Task< string > RaiseCustomizeLoginForm(string Markdown)
Raises the CustomizeLoginForm event to customize a login form before being returned to the client.
string? Realm
Registered realm name.
OAuthManagementResource ManagementResource
Registered registration resource
OAUTH authorize resource, as defined in RFC 6749. https://datatracker.ietf.org/doc/html/rfc6749
OAUTH device authorization resource, as defined in RFC 8628. https://datatracker.ietf....
OAUTH introspection resource, as defined in RFCs 7662. https://datatracker.ietf.org/doc/html/rfc7662
OAUTH client management resource, as defined in RFCs 7591. https://datatracker.ietf....
OAUTH dynamic registration resource, as defined in RFCs 7591 and 7592. https://datatracker....
OAUTH token resource, as defined in RFC 6749. https://datatracker.ietf.org/doc/html/rfc6749
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.
A factory that can create and validate JWT tokens.
bool Disposed
If the factory has been disposed.
static JwtFactory CreateHmacSha256()
Creates a JWT factory that can create and validate JWT tokens using the HMAC-SHA256 algorithm.
A dynamic user source, supporting registering new users.
A Thing Registry user source, supporting management of devices, with information about ownership.
Interface for data sources containing users.