aboutsummaryrefslogtreecommitdiff
path: root/back-end/src/Model/Widget/WidgetAuthManager.cs
blob: 31e0037d9d9b857ff4963ec061eadfa933313cbf (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
// Copyright (C) 2024 Vaughn Nugent
//
// This program 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.
//
// This program 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.Text.Json;
using System.Threading.Tasks;
using System.Collections.Generic;

using VNLib.Hashing;
using VNLib.Hashing.IdentityUtility;
using VNLib.Utils;
using VNLib.Utils.Extensions;
using VNLib.Plugins;
using VNLib.Plugins.Essentials;
using VNLib.Plugins.Essentials.Sessions;
using VNLib.Plugins.Essentials.Users;
using VNLib.Plugins.Extensions.Loading;
using VNLib.Plugins.Extensions.Loading.Users;
using VNLib.Plugins.Essentials.Extensions;

namespace SimpleBookmark.Model.Widget
{
    [ConfigurationName("widgets")]
    internal sealed class WidgetAuthManager(PluginBase plugin, IConfigScope config)
    {
        /*
         * This auth manager attempts to cache auth data by storing user-related data in 
         * the session, instead of hitting the database on every request. This is done to
         * reduce the number of database queries and improve performance.
         */

        const string AuthTokenQueryArgName = "t";
        const string TimestampKey = "sb.w.ts";
        const string SignatureKey = "sb.w.sig";
        const string UserSigningKeyKey = "sb.w.usk";

        private static readonly TimeSpan _authCacheValidFor = TimeSpan.FromMinutes(25);

        private readonly IUserManager users = plugin.GetOrCreateSingleton<UserManager>();

        private readonly int SigningKeySize = config.GetValueOrDefault("key_size", static p => p.GetInt32(), 16);

        public async ValueTask<bool> IsTokenValidAsync(HttpEntity entity)
        {
            string? token = GetTokenString(entity);

            if (string.IsNullOrWhiteSpace(token))
            {
                //No token provided
                return false;
            }

            if (HasExistingAuth(entity, token))
            {
                return true;
            }

            return await AuthorizeSession(entity, token);
        }

        private static bool HasExistingAuth(HttpEntity entity, string token)
        {
            //New sessions cannot be trusted yet
            if (entity.Session.IsNew)
            {
                return false;
            }

            /*
             * See if existing auth data exists in the session
             */
            DateTimeOffset expires = GetTimestamp(in entity.Session);

            if (expires < entity.RequestedTimeUtc)
            {
                return false;
            }

            string cachedToken = GetCachedToken(in entity.Session);

            return string.Equals(cachedToken, token, StringComparison.Ordinal);
        }

        private async Task<bool> AuthorizeSession(HttpEntity entity, string token)
        {
            try
            {
                using JsonWebToken jwt = JsonWebToken.Parse(token);
                using JsonDocument jwtData = jwt.GetPayload();

                string? userId = jwtData.RootElement.GetPropString("sub");

                if (string.IsNullOrWhiteSpace(userId))
                {
                    return false;
                }

                using IUser? user = await users.GetUserFromIDAsync(userId, entity.EventCancellation);

                return false;
            }
            catch (FormatException)
            {
                return false;
            }
        }

        public void GenerateWidgetKey(IUser user)
        {
            /*
             * A base32 number ensures easy serialization by the user-system
             * instead of base64 which can cause json overhead
             */
            user[UserSigningKeyKey] = RandomHash.GetRandomBase32(SigningKeySize);
        }

        /// <summary>
        /// Gets a value that indicates if the user has a widget signing key enabled
        /// </summary>
        /// <param name="user">The user instance to verify</param>
        /// <returns>True if the user has a widget signing key enabled</returns>
        public static bool HasSigningKey(IUser user) => !string.IsNullOrEmpty(user[UserSigningKeyKey]);

        private static DateTimeOffset GetTimestamp(ref readonly SessionInfo session)
        {
            long timestamp = VnEncoding.FromBase32String<long>(session[TimestampKey]);
            return DateTimeOffset.FromUnixTimeSeconds(timestamp);
        }

        private static void SetTimestamp(ref readonly SessionInfo session, DateTimeOffset timestamp)
            => session[TimestampKey] = VnEncoding.ToBase32String(timestamp.ToUnixTimeSeconds());

        private static string GetCachedToken(ref readonly SessionInfo session) => session[SignatureKey];

        private static void SetSignature(ref readonly SessionInfo session, string signature) => session[SignatureKey] = signature;

        private static string? GetTokenString(HttpEntity entity) => entity.QueryArgs.GetValueOrDefault(AuthTokenQueryArgName);

    }
}