Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/apps/api/OAuth/Developers/ApplicationRegistry.cs
blob: d28911361d724b59ea2d03ba06a1d0bde5ef1b53 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1

using System.Security.Cryptography;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using OpenIddict.Abstractions;
using Progmasoft.OAuth.Storage;
using static OpenIddict.Abstractions.OpenIddictConstants;

namespace Progmasoft.OAuth.Developers;

/// <summary>An application as its owner sees it in the developer portal.</summary>
/// <param name="ClientId">The public identifier of the client.</param>
/// <param name="DisplayName">The name the consent page shows.</param>
/// <param name="Homepage">The address of the application's site, or empty.</param>
/// <param name="Kind">The type chosen when the token was made.</param>
/// <param name="RedirectUris">The registered redirection addresses.</param>
/// <param name="PostLogoutRedirectUris">The registered end-session addresses.</param>
/// <param name="Scopes">The scopes the application may request.</param>
/// <param name="CreatedAt">When the application was registered.</param>
internal sealed record RegisteredApplication(
    string ClientId,
    string DisplayName,
    string Homepage,
    ApplicationKind Kind,
    IReadOnlyList<string> RedirectUris,
    IReadOnlyList<string> PostLogoutRedirectUris,
    IReadOnlyList<string> Scopes,
    DateTimeOffset CreatedAt)
{
    /// <summary>Whether the client has no secret.</summary>
    public bool IsPublic => Kind.IsPublic();
}

/// <summary>What registering an application produced.</summary>
/// <param name="Application">The application, or <see langword="null"/> when it was refused.</param>
/// <param name="ClientSecret">The secret of a confidential client; shown once and never stored in clear.</param>
/// <param name="Errors">The messages of each refused field.</param>
internal sealed record RegistrationResult(
    RegisteredApplication? Application,
    string? ClientSecret,
    IReadOnlyDictionary<string, string> Errors);

/// <summary>Registers, changes and deletes the applications of an account.</summary>
/// <remarks>
/// Every operation takes the account that acts and touches only applications that account owns. An application of
/// another account is reported as absent, so that client identifiers cannot be probed through the portal.
/// </remarks>
/// <param name="applications">OpenIddict's application manager.</param>
/// <param name="database">The OAuth tables.</param>
/// <param name="options">The settings of Progmasoft OAuth.</param>
/// <param name="timeProvider">Clock of the registration times.</param>
internal sealed class ApplicationRegistry(
    IOpenIddictApplicationManager applications,
    OAuthDbContext database,
    IOptions<OAuthOptions> options,
    TimeProvider timeProvider)
{
    /// <summary>The property of the application that keeps its homepage.</summary>
    private const string HomepageProperty = "homepage";

    /// <summary>The property of the application that keeps its <see cref="ApplicationKind"/>.</summary>
    internal const string KindProperty = "kind";

    /// <summary>Lists the applications of an account, newest first.</summary>
    /// <param name="accountId">The owner.</param>
    /// <param name="cancellationToken">Cancels the operation.</param>
    /// <returns>The applications.</returns>
    public async Task<IReadOnlyList<RegisteredApplication>> ListAsync(Guid accountId, CancellationToken cancellationToken)
    {
        List<ApplicationOwner> owned = await database.ApplicationOwners
            .Where(owner => owner.AccountId == accountId)
            .ToListAsync(cancellationToken);
        List<RegisteredApplication> result = [];
        // At most MaximumApplicationsPerAccount rows: ordered here, as SQLite cannot order by DateTimeOffset.
        foreach (ApplicationOwner owner in owned.OrderByDescending(owner => owner.CreatedAt))
        {
            if (await applications.FindByIdAsync(owner.ApplicationId, cancellationToken) is { } application)
            {
                result.Add(await DescribeAsync(application, owner.CreatedAt, cancellationToken));
            }
        }

        return result;
    }

    /// <summary>Finds an application of an account by its client identifier.</summary>
    /// <param name="accountId">The account that asks.</param>
    /// <param name="clientId">The client identifier.</param>
    /// <param name="cancellationToken">Cancels the operation.</param>
    /// <returns>The application, or <see langword="null"/> when the account owns no such application.</returns>
    public async Task<RegisteredApplication?> FindOwnedAsync(
        Guid accountId,
        string clientId,
        CancellationToken cancellationToken)
    {
        (object application, ApplicationOwner owner)? found = await FindOwnedEntryAsync(accountId, clientId, cancellationToken);
        return found is null ? null : await DescribeAsync(found.Value.application, found.Value.owner.CreatedAt, cancellationToken);
    }

    /// <summary>Finds the account that registered an application.</summary>
    /// <param name="applicationId">The OpenIddict identifier of the application.</param>
    /// <param name="cancellationToken">Cancels the operation.</param>
    /// <returns>The owning account, or <see langword="null"/> for an application without an owner.</returns>
    public async Task<Guid?> FindOwnerAsync(string applicationId, CancellationToken cancellationToken) =>
        (await database.ApplicationOwners.FirstOrDefaultAsync(
            owner => owner.ApplicationId == applicationId, cancellationToken))?.AccountId;

    /// <summary>Registers an application for an account.</summary>
    /// <param name="accountId">The owner.</param>
    /// <param name="input">The submission.</param>
    /// <param name="cancellationToken">Cancels the operation.</param>
    /// <returns>The application and its secret, or the refused fields.</returns>
    public async Task<RegistrationResult> CreateAsync(
        Guid accountId,
        ApplicationInput input,
        CancellationToken cancellationToken)
    {
        Dictionary<string, string> errors = ApplicationPolicy.Validate(input);
        int count = await database.ApplicationOwners.CountAsync(owner => owner.AccountId == accountId, cancellationToken);
        if (count >= options.Value.MaximumApplicationsPerAccount)
        {
            errors["displayName"] =
                $"An account can register {options.Value.MaximumApplicationsPerAccount} applications. Delete one first.";
        }

        if (errors.Count != 0)
        {
            return new RegistrationResult(null, null, errors);
        }

        string clientId = "pmo_" + RandomText(15);
        string? secret = input.Kind.IsPublic() ? null : RandomText(32);
        OpenIddictApplicationDescriptor descriptor = new()
        {
            ClientId = clientId,
            ClientSecret = secret,
        };
        Describe(descriptor, input);
        object application = await applications.CreateAsync(descriptor, cancellationToken);

        DateTimeOffset now = timeProvider.GetUtcNow();
        database.ApplicationOwners.Add(new ApplicationOwner
        {
            ApplicationId = (await applications.GetIdAsync(application, cancellationToken))!,
            AccountId = accountId,
            CreatedAt = now
        });
        await database.SaveChangesAsync(cancellationToken);
        return new RegistrationResult(await DescribeAsync(application, now, cancellationToken), secret,
            new Dictionary<string, string>());
    }

    /// <summary>Changes an application of an account. The client identifier, type and secret stay.</summary>
    /// <param name="accountId">The account that asks.</param>
    /// <param name="clientId">The client identifier.</param>
    /// <param name="input">The new settings; <see cref="ApplicationInput.Kind"/> is ignored.</param>
    /// <param name="cancellationToken">Cancels the operation.</param>
    /// <returns>The refused fields, empty on success, or <see langword="null"/> when the application is absent.</returns>
    public async Task<IReadOnlyDictionary<string, string>?> UpdateAsync(
        Guid accountId,
        string clientId,
        ApplicationInput input,
        CancellationToken cancellationToken)
    {
        if (await FindOwnedEntryAsync(accountId, clientId, cancellationToken) is not { } found)
        {
            return null;
        }

        ApplicationKind kind = await KindOfAsync(found.application, cancellationToken);
        ApplicationInput effective = input with { Kind = kind };
        Dictionary<string, string> errors = ApplicationPolicy.Validate(effective);
        if (errors.Count != 0)
        {
            return errors;
        }

        OpenIddictApplicationDescriptor descriptor = new();
        await applications.PopulateAsync(descriptor, found.application, cancellationToken);
        Describe(descriptor, effective);
        await applications.UpdateAsync(found.application, descriptor, cancellationToken);
        return errors;
    }

    /// <summary>Replaces the secret of a confidential application.</summary>
    /// <param name="accountId">The account that asks.</param>
    /// <param name="clientId">The client identifier.</param>
    /// <param name="cancellationToken">Cancels the operation.</param>
    /// <returns>The new secret, or <see langword="null"/> for an absent or public application.</returns>
    public async Task<string?> RotateSecretAsync(Guid accountId, string clientId, CancellationToken cancellationToken)
    {
        if (await FindOwnedEntryAsync(accountId, clientId, cancellationToken) is not { } found
            || await applications.HasClientTypeAsync(found.application, ClientTypes.Public, cancellationToken))
        {
            return null;
        }

        string secret = RandomText(32);
        OpenIddictApplicationDescriptor descriptor = new();
        await applications.PopulateAsync(descriptor, found.application, cancellationToken);
        descriptor.ClientSecret = secret;
        await applications.UpdateAsync(found.application, descriptor, cancellationToken);
        return secret;
    }

    /// <summary>Deletes an application with its authorizations and tokens.</summary>
    /// <param name="accountId">The account that asks.</param>
    /// <param name="clientId">The client identifier.</param>
    /// <param name="cancellationToken">Cancels the operation.</param>
    /// <returns><see langword="true"/> when the application was deleted.</returns>
    public async Task<bool> DeleteAsync(Guid accountId, string clientId, CancellationToken cancellationToken)
    {
        if (await FindOwnedEntryAsync(accountId, clientId, cancellationToken) is not { } found)
        {
            return false;
        }

        database.ApplicationOwners.Remove(found.owner);
        await database.SaveChangesAsync(cancellationToken);
        await applications.DeleteAsync(found.application, cancellationToken);
        return true;
    }

    /// <summary>Finds an application and its ownership row, when the account owns it.</summary>
    private async Task<(object application, ApplicationOwner owner)?> FindOwnedEntryAsync(
        Guid accountId,
        string clientId,
        CancellationToken cancellationToken)
    {
        if (await applications.FindByClientIdAsync(clientId, cancellationToken) is not { } application)
        {
            return null;
        }

        string? id = await applications.GetIdAsync(application, cancellationToken);
        ApplicationOwner? owner = await database.ApplicationOwners.FirstOrDefaultAsync(
            entry => entry.ApplicationId == id && entry.AccountId == accountId, cancellationToken);
        return owner is null ? null : (application, owner);
    }

    /// <summary>Writes a submission into a descriptor: names, addresses and permissions.</summary>
    /// <param name="descriptor">The descriptor to fill.</param>
    /// <param name="input">The validated submission.</param>
    private static void Describe(OpenIddictApplicationDescriptor descriptor, ApplicationInput input)
    {
        ApplicationKind kind = input.Kind;
        descriptor.DisplayName = input.DisplayName.Trim();
        descriptor.ApplicationType = kind is ApplicationKind.Desktop or ApplicationKind.Mobile
            ? ApplicationTypes.Native
            : ApplicationTypes.Web;
        descriptor.ClientType = kind.IsPublic() ? ClientTypes.Public : ClientTypes.Confidential;
        // Every client of Progmasoft OAuth shows the consent page: none is trusted by default.
        descriptor.ConsentType = ConsentTypes.Explicit;
        descriptor.Properties[HomepageProperty] = JsonSerializer.SerializeToElement(input.Homepage.Trim());
        descriptor.Properties[KindProperty] = JsonSerializer.SerializeToElement(kind.Slug());

        descriptor.RedirectUris.Clear();
        foreach (string uri in input.RedirectUris)
        {
            descriptor.RedirectUris.Add(new Uri(uri));
        }

        descriptor.PostLogoutRedirectUris.Clear();
        foreach (string uri in input.PostLogoutRedirectUris)
        {
            descriptor.PostLogoutRedirectUris.Add(new Uri(uri));
        }

        descriptor.Permissions.Clear();
        descriptor.Requirements.Clear();
        if (!kind.SignsPeopleIn())
        {
            // A service only asks the token endpoint for tokens that name itself; it never reaches the sign-in page.
            descriptor.Permissions.UnionWith(
            [
                Permissions.Endpoints.Token,
                Permissions.Endpoints.Revocation,
                Permissions.Endpoints.Introspection,
                Permissions.GrantTypes.ClientCredentials
            ]);
            return;
        }

        descriptor.Permissions.UnionWith(
        [
            Permissions.Endpoints.Authorization,
            Permissions.Endpoints.Token,
            Permissions.Endpoints.EndSession,
            Permissions.Endpoints.Revocation,
            Permissions.GrantTypes.AuthorizationCode,
            Permissions.ResponseTypes.Code
        ]);
        if (!kind.IsPublic())
        {
            descriptor.Permissions.Add(Permissions.Endpoints.Introspection);
        }

        if (input.Scopes.Contains(Scopes.OfflineAccess))
        {
            descriptor.Permissions.Add(Permissions.GrantTypes.RefreshToken);
        }

        foreach (string scope in input.Scopes)
        {
            descriptor.Permissions.Add(Permissions.Prefixes.Scope + scope);
        }

        descriptor.Requirements.Add(Requirements.Features.ProofKeyForCodeExchange);
    }

    /// <summary>Reads the type of a stored application.</summary>
    /// <param name="application">The application.</param>
    /// <param name="cancellationToken">Cancels the operation.</param>
    /// <returns>The type it was registered with.</returns>
    internal async Task<ApplicationKind> KindOfAsync(object application, CancellationToken cancellationToken)
    {
        var properties = await applications.GetPropertiesAsync(application, cancellationToken);
        if (properties.TryGetValue(KindProperty, out JsonElement value) && value.ValueKind is JsonValueKind.String
            && ApplicationKinds.Parse(value.GetString()) is { } kind)
        {
            return kind;
        }

        // An application stored before types existed: a public client was a native application, a confidential one
        // a web application.
        return await applications.HasClientTypeAsync(application, ClientTypes.Public, cancellationToken)
            ? ApplicationKind.Desktop
            : ApplicationKind.Web;
    }

    /// <summary>Reads an application into the view of the developer portal.</summary>
    private async Task<RegisteredApplication> DescribeAsync(
        object application,
        DateTimeOffset createdAt,
        CancellationToken cancellationToken)
    {
        var permissions = await applications.GetPermissionsAsync(application, cancellationToken);
        var properties = await applications.GetPropertiesAsync(application, cancellationToken);
        string homepage = properties.TryGetValue(HomepageProperty, out JsonElement value)
                          && value.ValueKind is JsonValueKind.String
            ? value.GetString() ?? string.Empty
            : string.Empty;
        return new RegisteredApplication(
            (await applications.GetClientIdAsync(application, cancellationToken))!,
            await applications.GetDisplayNameAsync(application, cancellationToken) ?? string.Empty,
            homepage,
            await KindOfAsync(application, cancellationToken),
            (await applications.GetRedirectUrisAsync(application, cancellationToken)).Order(StringComparer.Ordinal).ToList(),
            (await applications.GetPostLogoutRedirectUrisAsync(application, cancellationToken))
                .Order(StringComparer.Ordinal).ToList(),
            OAuthScopes.All
                .Where(scope => permissions.Contains(Permissions.Prefixes.Scope + scope.Name))
                .Select(scope => scope.Name)
                .ToList(),
            createdAt);
    }

    /// <summary>Random text of the given number of bytes, as unpadded URL-safe base64.</summary>
    /// <param name="bytes">How many random bytes.</param>
    /// <returns>The text.</returns>
    private static string RandomText(int bytes) =>
        Convert.ToBase64String(RandomNumberGenerator.GetBytes(bytes)).TrimEnd('=').Replace('+', '-').Replace('/', '_');
}