|
| 1 | +using System.Text; |
| 2 | +using JsonApiDotNetCore.Configuration; |
| 3 | +using JsonApiDotNetCore.Middleware; |
| 4 | +using JsonApiDotNetCore.Resources; |
| 5 | +using JsonApiDotNetCore.Resources.Annotations; |
| 6 | +using Microsoft.AspNetCore.Http; |
| 7 | +using Microsoft.Extensions.Primitives; |
| 8 | + |
| 9 | +namespace JsonApiDotNetCoreTests.IntegrationTests.Authorization.Scopes; |
| 10 | + |
| 11 | +internal sealed class AuthScopeSet |
| 12 | +{ |
| 13 | + private const StringSplitOptions ScopesHeaderSplitOptions = StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries; |
| 14 | + |
| 15 | + public const string ScopesHeaderName = "X-Auth-Scopes"; |
| 16 | + |
| 17 | + private readonly Dictionary<string, Permission> _scopes = new(); |
| 18 | + |
| 19 | + public static AuthScopeSet GetRequestedScopes(IHeaderDictionary requestHeaders) |
| 20 | + { |
| 21 | + var requestedScopes = new AuthScopeSet(); |
| 22 | + |
| 23 | + // In a real application, the scopes would be read from the signed ticket in the Authorization HTTP header. |
| 24 | + // For simplicity, this sample allows the client to send them directly, which is obviously insecure. |
| 25 | + |
| 26 | + if (requestHeaders.TryGetValue(ScopesHeaderName, out StringValues headerValue)) |
| 27 | + { |
| 28 | + foreach (string scopeValue in headerValue.ToString().Split(' ', ScopesHeaderSplitOptions)) |
| 29 | + { |
| 30 | + string[] scopeParts = scopeValue.Split(':', 2, ScopesHeaderSplitOptions); |
| 31 | + |
| 32 | + if (scopeParts.Length == 2 && Enum.TryParse(scopeParts[0], true, out Permission permission) && Enum.IsDefined(permission)) |
| 33 | + { |
| 34 | + requestedScopes.Include(scopeParts[1], permission); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return requestedScopes; |
| 40 | + } |
| 41 | + |
| 42 | + public void IncludeFrom(IJsonApiRequest request, ITargetedFields targetedFields) |
| 43 | + { |
| 44 | + Permission permission = request.IsReadOnly ? Permission.Read : Permission.Write; |
| 45 | + |
| 46 | + if (request.PrimaryResourceType != null) |
| 47 | + { |
| 48 | + Include(request.PrimaryResourceType, permission); |
| 49 | + } |
| 50 | + |
| 51 | + if (request.SecondaryResourceType != null) |
| 52 | + { |
| 53 | + Include(request.SecondaryResourceType, permission); |
| 54 | + } |
| 55 | + |
| 56 | + if (request.Relationship != null) |
| 57 | + { |
| 58 | + Include(request.Relationship, permission); |
| 59 | + } |
| 60 | + |
| 61 | + foreach (RelationshipAttribute relationship in targetedFields.Relationships) |
| 62 | + { |
| 63 | + Include(relationship, permission); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public void Include(ResourceType resourceType, Permission permission) |
| 68 | + { |
| 69 | + Include(resourceType.PublicName, permission); |
| 70 | + } |
| 71 | + |
| 72 | + public void Include(RelationshipAttribute relationship, Permission permission) |
| 73 | + { |
| 74 | + Include(relationship.LeftType, permission); |
| 75 | + Include(relationship.RightType, permission); |
| 76 | + } |
| 77 | + |
| 78 | + private void Include(string name, Permission permission) |
| 79 | + { |
| 80 | + // Unify with existing entries. For example, adding read:movies when write:movies already exists is a no-op. |
| 81 | + |
| 82 | + if (_scopes.TryGetValue(name, out Permission value)) |
| 83 | + { |
| 84 | + if (value >= permission) |
| 85 | + { |
| 86 | + return; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + _scopes[name] = permission; |
| 91 | + } |
| 92 | + |
| 93 | + public bool ContainsAll(AuthScopeSet other) |
| 94 | + { |
| 95 | + foreach (string otherName in other._scopes.Keys) |
| 96 | + { |
| 97 | + if (!_scopes.TryGetValue(otherName, out Permission thisPermission)) |
| 98 | + { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + if (thisPermission < other._scopes[otherName]) |
| 103 | + { |
| 104 | + return false; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return true; |
| 109 | + } |
| 110 | + |
| 111 | + public override string ToString() |
| 112 | + { |
| 113 | + var builder = new StringBuilder(); |
| 114 | + |
| 115 | + foreach ((string name, Permission permission) in _scopes.OrderBy(scope => scope.Key)) |
| 116 | + { |
| 117 | + if (builder.Length > 0) |
| 118 | + { |
| 119 | + builder.Append(' '); |
| 120 | + } |
| 121 | + |
| 122 | + builder.Append($"{permission.ToString().ToLowerInvariant()}:{name}"); |
| 123 | + } |
| 124 | + |
| 125 | + return builder.ToString(); |
| 126 | + } |
| 127 | +} |
0 commit comments