Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
OAuthIntrospectionResource.cs
2using System.Threading.Tasks;
3using Waher.Content;
6
8{
14 {
18 public const string OAuthIntrospectionPrivilege = "OAUTH.Introspection";
19
23 public const string DefaultResourcePath = "/oauth/introspect";
24
31 {
32 }
33
40 string ResourceName)
42 {
44 }
45
54 {
55 this.InitAuthentication();
56
57 return this.AuthenticationSchemes;
58 }
59
63 public bool AllowsPOST => true;
64
71 public async Task POST(HttpRequest Request, HttpResponse Response)
72 {
73 if (!Request.HasData)
74 {
75 await BadRequest(Response, "invalid_request", "Missing payload.");
76 return;
77 }
78
79 if (!Request.User?.HasPrivilege(OAuthIntrospectionPrivilege) ?? false)
80 {
81 await Response.SendResponse(ForbiddenException.AccessDenied(this.ResourceName,
82 Request.RemoteEndPoint.RemovePortNumber(), OAuthIntrospectionPrivilege));
83 return;
84 }
85
86 ContentResponse Decoded = await Request.DecodeDataAsync();
87 if (Decoded.HasError)
88 {
89 await Response.SendResponse(Decoded.Error);
90 return;
91 }
92
93 if (!(Decoded.Decoded is Dictionary<string, string> Form))
94 {
95 await BadRequest(Response, "invalid_request", "Expected form data.");
96 return;
97 }
98
99 if (!(Request.Header.Authorization is null) &&
100 Form.ContainsKey("client_secret") || Form.ContainsKey("password"))
101 {
102 await BadRequest(Response, "invalid_request", "Multiple client credentials provided.");
103 return;
104 }
105
106 if (!Form.TryGetValue("token", out string Token) || string.IsNullOrEmpty(Token))
107 {
108 await BadRequest(Response, "invalid_request", "Missing token.");
109 return;
110 }
111
112 OAuthTokenType? TokenType;
113 JwtToken? ParsedToken;
114 bool Active;
115
117 {
118 Active = false;
119 ParsedToken = null;
120 TokenType = null;
121 }
122 else
123 {
124 KeyValuePair<OAuthTokenType?, JwtToken?> P = await this.Environment.
125 TokenResource.TryGetTokenType(Token);
126
127 TokenType = P.Key;
128 ParsedToken = P.Value;
129
130 Active = TokenType.HasValue && (
131 TokenType == OAuthTokenType.AccessToken ||
132 TokenType == OAuthTokenType.RefreshToken);
133 }
134
135 Dictionary<string, object> Result = new Dictionary<string, object>()
136 {
137 { "active", Active }
138 };
139
140 if (Active)
141 {
142 if (!(ParsedToken is null))
143 {
144 foreach (KeyValuePair<string, object> P in ParsedToken.Claims)
145 {
146 Result[P.Key] = P.Value;
147
148 if (P.Key == JwtClaims.Subject)
149 {
150 if (P.Value is string UserName &&
151 !(await this.Environment.UserSource.TryGetUser(UserName) is null))
152 {
153 Result["username"] = UserName;
154 }
155 }
156 }
157 }
158
159 if (TokenType == OAuthTokenType.AccessToken ||
160 TokenType == OAuthTokenType.ExpiredAccessToken)
161 {
162 Result["token_type"] = "Bearer";
163 }
164 else
165 Result["token_type"] = "N_A";
166 }
167
168 await Response.Return(Result);
169 }
170 }
171}
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Exception Error
Error response.
The server understood the request, but is refusing to fulfill it. Authorization will not help and the...
static ForbiddenException AccessDenied(string ObjectId, string ActorId)
Returns a ForbiddenException object, and logs a entry in the event log about the event.
Base class for all HTTP authentication schemes, as defined in RFC-7235: https://datatracker....
HttpFieldAuthorization Authorization
Authorization HTTP Field header. (RFC 2616, §14.8)
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
IUser User
Authenticated user, if available, or null if not available.
Definition: HttpRequest.cs:203
async Task< ContentResponse > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:139
string ResourceName
Name of resource.
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
Task Return(Exception ex)
Returns an error to the client.
void Register(OAuthAuthorizeResource? AuthorizeResource)
Registers an authorization resource.
bool HasTokenResource
If the environment has a registered token resource
OAUTH introspection resource, as defined in RFCs 7662. https://datatracker.ietf.org/doc/html/rfc7662
OAuthIntrospectionResource(OAuth2Environment Environment)
OAUTH introspection resource, as defined in RFCs 7662.
override? HttpAuthenticationScheme[] GetAuthenticationSchemes(HttpRequest Request)
Any authentication schemes used to authenticate users before access is granted to the corresponding r...
const string DefaultResourcePath
Default introspection resource path: /oauth/introspect
const string OAuthIntrospectionPrivilege
Privilege for OAUTH introspection.
async Task POST(HttpRequest Request, HttpResponse Response)
Executes the POST method on the resource.
OAuthIntrospectionResource(OAuth2Environment Environment, string ResourceName)
OAUTH introspection resource, as defined in RFCs 7662.
Abstract base class for OAUTH resources.
OAuth2Environment Environment
OAUTH2 environment, used to access clients, tokens, and other resources.
bool InitAuthentication()
Initializes authentication schemes, if not already initialized.
static Task BadRequest(HttpResponse Response, string ErrorCode, string ErrorDescription)
Returns a Bad Request error back to the client.
HttpAuthenticationScheme?[] AuthenticationSchemes
Available authentication schemes, if initialized.
Static class containing predefined JWT claim names.
Definition: JwtClaims.cs:10
const string Subject
Subject of the JWT (the user)
Definition: JwtClaims.cs:19
Contains information about a Java Web Token (JWT). JWT is defined in RFC 7519: https://tools....
Definition: JwtToken.cs:22
IEnumerable< KeyValuePair< string, object > > Claims
Claims provided in token. For a list of public claim names, see: https://www.iana....
Definition: JwtToken.cs:239
POST Interface for HTTP resources.
OAuthTokenType
Type of OAuth token.