-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathWorldPermissionsData.cs
More file actions
157 lines (135 loc) · 4.98 KB
/
Copy pathWorldPermissionsData.cs
File metadata and controls
157 lines (135 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace DCL.PrivateWorlds
{
internal static class WorldPermissionTypeNames
{
public const string Unrestricted = "unrestricted";
public const string AllowList = "allow-list";
public const string SharedSecret = "shared-secret";
}
/// <summary>
/// Represents the type of access control for a world.
/// </summary>
public enum WorldAccessType
{
Unknown,
Unrestricted,
AllowList,
SharedSecret
}
/// <summary>
/// Response from the world permissions API endpoint.
/// GET /world/{world_name}/permissions
/// </summary>
[Serializable]
public class WorldPermissionsResponse
{
[JsonProperty("permissions")]
public WorldPermissions Permissions { get; set; } = new ();
[JsonProperty("owner")]
public string Owner { get; set; } = string.Empty;
}
/// <summary>
/// Contains all permission configurations for a world.
/// </summary>
[Serializable]
public class WorldPermissions
{
[JsonProperty("deployment")]
public PermissionConfig? Deployment { get; set; }
[JsonProperty("access")]
public AccessPermissionConfig? Access { get; set; }
[JsonProperty("streaming")]
public PermissionConfig? Streaming { get; set; }
}
/// <summary>
/// Base permission configuration for allow-list type permissions.
/// </summary>
[Serializable]
public class PermissionConfig
{
[JsonProperty("type")]
public string Type { get; set; } = WorldPermissionTypeNames.Unrestricted;
[JsonProperty("wallets")]
public List<string>? Wallets { get; set; }
[JsonProperty("communities")]
public List<string>? Communities { get; set; }
}
/// <summary>
/// Access permission configuration that can include shared-secret type.
/// </summary>
[Serializable]
public class AccessPermissionConfig
{
[JsonProperty("type")]
public string Type { get; set; } = WorldPermissionTypeNames.Unrestricted;
[JsonProperty("wallets")]
public List<string>? Wallets { get; set; }
[JsonProperty("communities")]
public List<string>? Communities { get; set; }
// Note: secret is write-only in the API - it's not returned in GET responses
// We use this when POSTing a password to validate access
}
/// <summary>
/// Parsed and processed world access information for easier consumption.
/// </summary>
public class WorldAccessInfo
{
private List<string> allowedWallets = new ();
private HashSet<string> allowedWalletsSet = new (StringComparer.OrdinalIgnoreCase);
public WorldAccessType AccessType { get; set; }
public string OwnerAddress { get; set; } = string.Empty;
public List<string> AllowedWallets
{
get => allowedWallets;
set
{
allowedWallets = value ?? new List<string>();
allowedWalletsSet = new HashSet<string>(allowedWallets, StringComparer.OrdinalIgnoreCase);
}
}
public IReadOnlyCollection<string> AllowedWalletsSet => allowedWalletsSet;
public List<string> AllowedCommunities { get; set; } = new ();
/// <summary>
/// Creates a WorldAccessInfo from the raw API response.
/// </summary>
public static WorldAccessInfo FromResponse(WorldPermissionsResponse response)
{
var info = new WorldAccessInfo
{
OwnerAddress = response.Owner
};
var access = response.Permissions.Access;
if (access == null || string.IsNullOrEmpty(access.Type) ||
access.Type.Equals(WorldPermissionTypeNames.Unrestricted, StringComparison.OrdinalIgnoreCase))
{
info.AccessType = WorldAccessType.Unrestricted;
}
else if (access.Type.Equals(WorldPermissionTypeNames.AllowList, StringComparison.OrdinalIgnoreCase))
{
info.AccessType = WorldAccessType.AllowList;
info.AllowedWallets = access.Wallets ?? new List<string>();
info.AllowedCommunities = access.Communities ?? new List<string>();
}
else if (access.Type.Equals(WorldPermissionTypeNames.SharedSecret, StringComparison.OrdinalIgnoreCase))
{
info.AccessType = WorldAccessType.SharedSecret;
}
else
{
info.AccessType = WorldAccessType.Unknown;
}
return info;
}
/// <summary>
/// Checks if a wallet address is in the allow list (case-insensitive).
/// </summary>
public bool IsWalletAllowed(string walletAddress)
{
return !string.IsNullOrEmpty(walletAddress) &&
allowedWalletsSet.Contains(walletAddress);
}
}
}