Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
OAuthClientCredentialsAuthentication.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
5using Waher.Content;
6using Waher.Events;
9
11{
16 {
17 private readonly IUserSource users;
18
24 : this(false, 0, Users)
25 {
26 }
27
36 {
37 this.users = Users;
38 }
39
43 public IUserSource Users => this.users;
44
48 public override string DisplayName
49 {
50 get
51 {
52 StringBuilder sb = new StringBuilder();
53
54 sb.Append("OAUTH Client Credentials");
56
57 return sb.ToString();
58 }
59 }
60
66 public override string[] GetChallenges(HttpRequest Request)
67 {
68 return Array.Empty<string>();
69 }
70
76 public override async Task<IUser?> IsAuthenticated(HttpRequest Request)
77 {
78 if (!(Request.Header.Authorization is null))
79 return null;
80
81 IUser? User;
82
83 if (Request.Header.TryGetQueryParameter("client_id", out string ClientId) &&
84 Request.Header.TryGetQueryParameter("client_secret", out string ClientSecret))
85 {
86 Log.Warning("Insecure authentication method used. Client credentials " +
87 "should be sent in the Authorization header, not as query parameters.",
88 Request.Header.ResourcePart, Request.RemoteEndPoint);
89
90 User = await BasicAuthentication.IsAuthenticated(ClientId, ClientSecret, null, this.users, Request);
91 }
92 else if (Request.Header.TryGetQueryParameter("username", out ClientId) &&
93 Request.Header.TryGetQueryParameter("password", out ClientSecret))
94 {
95 Log.Warning("Insecure authentication method used. Client credentials " +
96 "should be sent in the Authorization header, not as query parameters.",
97 Request.Header.ResourcePart, Request.RemoteEndPoint);
98
99 User = await BasicAuthentication.IsAuthenticated(ClientId, ClientSecret, null, this.users, Request);
100 }
101 else if (Request.HasData)
102 {
103 ContentResponse Response = await Request.DecodeDataAsync();
104 if (Response.HasError)
105 return null;
106
107 if (Response.Decoded is Dictionary<string, object> Data)
108 {
109 if (Data.TryGetValue("client_id", out object Obj) && Obj is string ClientId2 &&
110 Data.TryGetValue("client_secret", out Obj) && Obj is string ClientSecret2)
111 {
112 User = await BasicAuthentication.IsAuthenticated(ClientId2, ClientSecret2, null, this.users, Request);
113 }
114 else if (Data.TryGetValue("username", out Obj) && Obj is string UserName &&
115 Data.TryGetValue("password", out Obj) && Obj is string Password)
116 {
117 User = await BasicAuthentication.IsAuthenticated(UserName, Password, null, this.users, Request);
118 }
119 else
120 return null;
121 }
122 else if (Response.Decoded is Dictionary<string, string> Form)
123 {
124 if (Form.TryGetValue("client_id", out ClientId) &&
125 Form.TryGetValue("client_secret", out ClientSecret))
126 {
127 User = await BasicAuthentication.IsAuthenticated(ClientId, ClientSecret, null, this.users, Request);
128 }
129 else if (Form.TryGetValue("username", out ClientId) &&
130 Form.TryGetValue("password", out ClientSecret))
131 {
132 User = await BasicAuthentication.IsAuthenticated(ClientId, ClientSecret, null, this.users, Request);
133 }
134 else
135 return null;
136 }
137 else
138 return null;
139 }
140 else
141 return null;
142
143 return User;
144 }
145 }
146}
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Warning(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs a warning event.
Definition: Log.cs:576
Basic authentication mechanism, as defined in RFC 2617: https://tools.ietf.org/html/rfc2617
override async Task< IUser > IsAuthenticated(HttpRequest Request)
Checks if the request is authorized.
Base class for all HTTP authentication schemes, as defined in RFC-7235: https://datatracker....
virtual void AppendEncryptionRequirement(StringBuilder DisplayName)
Appends any encryption requirement to the display name.
int MinStrength
Minimum security strength of algorithms used.
bool RequireEncryption
If scheme requires encryption.
HttpFieldAuthorization Authorization
Authorization HTTP Field header. (RFC 2616, §14.8)
bool TryGetQueryParameter(string QueryParameter, out string Value)
Tries to get the value of an individual query parameter, if available.
string ResourcePart
Contains original resource part of request.
Represents an HTTP request.
Definition: HttpRequest.cs:22
HttpRequestHeader Header
Request header.
Definition: HttpRequest.cs:182
string RemoteEndPoint
Remote end-point.
Definition: HttpRequest.cs:243
bool HasData
If the request has data.
Definition: HttpRequest.cs:113
async Task< ContentResponse > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:139
OAUTH Client Credentials authentication scheme, as defined in RFCs 6749.
override async Task< IUser?> IsAuthenticated(HttpRequest Request)
Checks if the request is authorized.
override string[] GetChallenges(HttpRequest Request)
Gets available challenges for the authenticating client to respond to.
OAuthClientCredentialsAuthentication(bool RequireEncryption, int MinStrength, IUserSource Users)
OAUTH Client Credentials authentication scheme, as defined in RFCs 6749.
OAuthClientCredentialsAuthentication(IUserSource Users)
OAUTH Client Credentials authentication scheme, as defined in RFCs 6749.
Basic interface for a user.
Definition: IUser.cs:7
Interface for data sources containing users.
Definition: IUserSource.cs:9