aboutsummaryrefslogtreecommitdiff
path: root/plugins/VNLib.Plugins.Essentials.Accounts.AppData/src/Endpoints/WebEndpoint.cs
blob: 41b8b308ac8698267278b95e1fdddaad2502f372 (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Copyright (c) 2024 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Essentials.Accounts.AppData
* File: WebEndpoint.cs 
*
* WebEndpoint.cs is part of VNLib.Plugins.Essentials.Accounts.AppData which 
* is part of the larger VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Essentials.Accounts is free software: you can redistribute it and/or modify 
* it under the terms of the GNU Affero General Public License as 
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* VNLib.Plugins.Essentials.Accounts is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see https://www.gnu.org/licenses/.
*/

using System;
using System.Net;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;

using VNLib.Net.Http;
using VNLib.Hashing.Checksums;
using VNLib.Plugins.Essentials.Endpoints;
using VNLib.Plugins.Essentials.Extensions;
using VNLib.Plugins.Extensions.Loading;
using VNLib.Plugins.Extensions.Validation;
using VNLib.Plugins.Extensions.Loading.Routing;

using VNLib.Plugins.Essentials.Accounts.AppData.Model;
using VNLib.Plugins.Essentials.Accounts.AppData.Stores;
using VNLib.Plugins.Extensions.Loading.Routing.Mvc;
using static VNLib.Plugins.Essentials.Endpoints.ResourceEndpointBase;
using static VNLib.Plugins.Essentials.Accounts.AppData.Model.HttpExtensions;

namespace VNLib.Plugins.Essentials.Accounts.AppData.Endpoints
{
    
    [EndpointLogName("Endpoint")]
    [ConfigurationName("web_endpoint")]
    internal sealed class WebEndpoint(PluginBase plugin, IConfigScope config) : IHttpController
    {
        const int DefaultMaxDataSize = 8 * 1024;

        private readonly StorageManager _store = plugin.GetOrCreateSingleton<StorageManager>();
        private readonly int MaxDataSize = config.GetValueOrDefault("max_data_size", DefaultMaxDataSize);
        private readonly string[] AllowedScopes = config.GetRequiredProperty<string[]>("allowed_scopes");

        ///<inheritdoc/>
        public ProtectionSettings GetProtectionSettings() => default;

        [HttpStaticRoute("{{path}}", HttpMethod.GET)]
        [HttpRouteProtection(AuthorzationCheckLevel.Critical)]
        public async ValueTask<VfReturnType> GetDataAsync(HttpEntity entity)
        {
            WebMessage webm = new();

            string? scopeId = GetScopeId(entity);
            bool noCache = NoCacheQuery(entity);

            if (webm.Assert(scopeId != null, "Missing scope"))
            {
                return VirtualClose(entity, webm, HttpStatusCode.BadRequest);
            }

            if (webm.Assert(IsScopeAllowed(scopeId), "Invalid scope"))
            {
                return VirtualClose(entity, webm, HttpStatusCode.BadRequest);
            }

            //If the connection has the no-cache header set, also bypass the cache
            noCache |= entity.Server.NoCache();

            UserRecordData? record = await _store.GetRecordAsync(
                entity.Session.UserID, 
                recordKey: scopeId, 
                flags: noCache ? RecordOpFlags.NoCache : RecordOpFlags.None,   //optionally bypass cache if the user requests it
                entity.EventCancellation
            );

            //return the raw data with the checksum header

            return record is null 
                ? VirtualClose(entity, webm, HttpStatusCode.NotFound) 
                : CloseWithRecord(entity, record, HttpStatusCode.OK);
        }


        [HttpStaticRoute("{{path}}", HttpMethod.PUT)]
        [HttpRouteProtection(AuthorzationCheckLevel.Critical)]
        public async ValueTask<VfReturnType> UpdateDataAsync(HttpEntity entity)
        {
            WebMessage webm = new();
            string? scopeId = GetScopeId(entity);
            bool flush = NoCacheQuery(entity);

            if (webm.Assert(entity.Files.Count == 1, "Invalid file count"))
            {
                return VirtualClose(entity, webm, HttpStatusCode.BadRequest);
            }

            if (webm.Assert(scopeId != null, "Missing scope"))
            {
                return VirtualClose(entity, webm, HttpStatusCode.BadRequest);
            }

            if (webm.Assert(IsScopeAllowed(scopeId), "Invalid scope"))
            {
                return VirtualClose(entity, webm, HttpStatusCode.BadRequest);
            }

            FileUpload data = entity.Files[0];

            if (webm.Assert(data.Length <= MaxDataSize, "Data too large"))
            {
                return VirtualClose(entity, webm, HttpStatusCode.RequestEntityTooLarge);
            }

            byte[] recordData = new byte[data.Length];
            int read = await data.FileData.ReadAsync(recordData, entity.EventCancellation);

            if (webm.Assert(read == recordData.Length, "Failed to read data"))
            {
                return VirtualClose(entity, webm, HttpStatusCode.InternalServerError);
            }

            //Compute checksum on sent data and compare to the header if it exists
            ulong checksum = FNV1a.Compute64(recordData);
            ulong? userChecksum = GetUserDataChecksum(entity.Server);

            if (userChecksum.HasValue)
            {
                //compare the checksums
                if (webm.Assert(checksum == userChecksum.Value, "Checksum mismatch"))
                {
                    return VirtualClose(entity, webm, HttpStatusCode.UnprocessableEntity);
                }
            }

            /*
             * If the user specifies the flush flag, the call will wait until the entire record
             * is published to the persistent store before returning. Typically if a caching layer is 
             * used, the record will be written to the cache and the call will return immediately.
             */
            RecordOpFlags flags = flush ? RecordOpFlags.WriteThrough : RecordOpFlags.None;

            //Write the record to the store
            await _store.SetRecordAsync(
                userId: entity.Session.UserID, 
                recordKey: scopeId, 
                recordData, 
                checksum, 
                flags, 
                entity.EventCancellation
            );
            
            return VirtualClose(entity, HttpStatusCode.Accepted);
        }

        [HttpStaticRoute("{{path}}", HttpMethod.DELETE)]
        [HttpRouteProtection(AuthorzationCheckLevel.Critical)]
        public async ValueTask<VfReturnType> DeleteDataAsync(HttpEntity entity)
        {
            WebMessage webm = new();
            string? scopeId = GetScopeId(entity);

            if (webm.Assert(scopeId != null, "Missing scope"))
            {
                return VirtualClose(entity, webm, HttpStatusCode.BadRequest);
            }

            if (webm.Assert(IsScopeAllowed(scopeId), "Invalid scope"))
            {
                return VirtualClose(entity, webm, HttpStatusCode.BadRequest);
            }

            //Write the record to the store
            await _store.DeleteRecordAsync(
                userId: entity.Session.UserID, 
                recordKey: scopeId, 
                entity.EventCancellation
            );
            
            return VirtualClose(entity, HttpStatusCode.Accepted);
        }  

        private bool IsScopeAllowed(string scopeId)
        {
            return AllowedScopes.Contains(scopeId, StringComparer.OrdinalIgnoreCase);
        }

        private static string? GetScopeId(HttpEntity entity) 
            => entity.QueryArgs.GetValueOrDefault("scope");

        private static bool NoCacheQuery(HttpEntity entity) 
            => entity.QueryArgs.ContainsKey("no_cache");
    }
}