Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
OAuthScopesSupportedAttribute.cs
1using System;
3using System.Reflection;
4using System.Threading.Tasks;
5using Waher.Events;
8
10{
14 [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
16 {
17 private static readonly SortedDictionary<string, bool> scopesAvailable = new SortedDictionary<string, bool>();
18
24 : this(false, ScopesSupported)
25 {
26 }
27
35 {
36 this.DynamicScopes = DynamicScopes;
37 this.ScopesSupported = ScopesSupported;
38 }
39
44 public bool DynamicScopes { get; }
45
49 public string[] ScopesSupported { get; }
50
56 public async Task<string[]> GetScopes(HttpResource Resource)
57 {
58 object Obj;
59
60 if (this.DynamicScopes)
61 {
63 Type T = Resource.GetType();
64
65 foreach (string Scope in this.ScopesSupported)
66 {
67 PropertyInfo PI = T.GetProperty(Scope, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
68 if (PI is null)
69 {
70 FieldInfo FI = T.GetField(Scope, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
71
72 if (FI is null)
73 {
74 MethodInfo MI = T.GetMethod(Scope, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static, null, new Type[0], null);
75 if (MI is null)
76 continue;
77
78 Obj = await ScriptNode.WaitPossibleTask(MI.Invoke(Resource, Array.Empty<object>()));
79 }
80 else
81 Obj = await ScriptNode.WaitPossibleTask(FI.GetValue(Resource));
82 }
83 else
84 Obj = await ScriptNode.WaitPossibleTask(PI.GetValue(Resource));
85
86 if (Obj is IEnumerable<string> DynamicScopes)
87 ScopesList.AddRange(DynamicScopes);
88 else if (Obj is string DynamicScope)
89 ScopesList.Add(DynamicScope);
90 else
91 Log.Warning("Dynamic scopes returned an object that was not of expected type: " + T.FullName, Resource.ResourceName);
92 }
93
94 return ScopesList.ToArray();
95 }
96 else
97 return this.ScopesSupported;
98 }
99
104 public override async Task RegisterMetaData(HttpResource Resource)
105 {
106 string[] Scopes = await this.GetScopes(Resource);
107
108 lock (scopesAvailable)
109 {
110 foreach (string Scope in Scopes)
111 scopesAvailable[Scope] = true;
112 }
113 }
114
118 public static string[] RegisteredScopes
119 {
120 get
121 {
122 lock (scopesAvailable)
123 {
124 int c = scopesAvailable.Count;
125 string[] Result = new string[c];
126
127 scopesAvailable.Keys.CopyTo(Result, 0);
128
129 return Result;
130 }
131 }
132 }
133
139 public override async Task AddMetaData(HttpResource Resource,
140 Dictionary<string, object> MetaData)
141 {
142 string[] Scopes = await this.GetScopes(Resource);
143
144 if (MetaData.TryGetValue("scopes_supported", out object Obj) &&
145 Obj is string[] Scopes2)
146 {
147 MetaData["scopes_supported"] = Scopes2.Join(Scopes);
148 }
149 else
150 MetaData["scopes_supported"] = Scopes;
151 }
152 }
153}
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
Base class for all HTTP resources.
Definition: HttpResource.cs:23
Abstract base class for OAUTH meta-data attributes.
override async Task AddMetaData(HttpResource Resource, Dictionary< string, object > MetaData)
Adds available meta-data to a dictionary of meta-data.
OAuthScopesSupportedAttribute(params string[] ScopesSupported)
Defines scopes supported by an OAUTH web service.
OAuthScopesSupportedAttribute(bool DynamicScopes, params string[] ScopesSupported)
Defines scopes supported by an OAUTH web service.
bool DynamicScopes
If the scopes point to a field, property or method, that returns the scopes that are supported.
async Task< string[]> GetScopes(HttpResource Resource)
Gets available scopes for a given resource.
override async Task RegisterMetaData(HttpResource Resource)
Registers any meta-data used that requires registration.
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
static async Task< object > WaitPossibleTask(object Result)
Waits for any asynchronous process to terminate.
Definition: ScriptNode.cs:441