mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
2.6 KiB
98 lines
2.6 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System.ComponentModel;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
namespace Squidex.Infrastructure
|
|
{
|
|
[TypeConverter(typeof(RefTokenTypeConverter))]
|
|
public sealed record RefToken
|
|
{
|
|
private static readonly char[] TrimChars = { ' ', ':' };
|
|
|
|
public RefTokenType Type { get; }
|
|
|
|
public string Identifier { get; }
|
|
|
|
public bool IsClient
|
|
{
|
|
get => Type == RefTokenType.Client;
|
|
}
|
|
|
|
public bool IsUser
|
|
{
|
|
get => Type == RefTokenType.Subject;
|
|
}
|
|
|
|
public RefToken(RefTokenType type, string identifier)
|
|
{
|
|
Guard.NotNullOrEmpty(identifier);
|
|
|
|
Type = type;
|
|
|
|
Identifier = identifier;
|
|
}
|
|
|
|
public static RefToken Client(string identifier)
|
|
{
|
|
return new RefToken(RefTokenType.Client, identifier);
|
|
}
|
|
|
|
public static RefToken User(string identifier)
|
|
{
|
|
return new RefToken(RefTokenType.Subject, identifier);
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"{Type.ToString().ToLowerInvariant()}:{Identifier}";
|
|
}
|
|
|
|
public static bool TryParse(string? value, [MaybeNullWhen(false)] out RefToken result)
|
|
{
|
|
value = value?.Trim(TrimChars);
|
|
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
{
|
|
result = null!;
|
|
return false;
|
|
}
|
|
|
|
value = value.Trim();
|
|
|
|
var idx = value.IndexOf(':', StringComparison.Ordinal);
|
|
|
|
if (idx > 0 && idx < value.Length - 1)
|
|
{
|
|
if (!Enum.TryParse<RefTokenType>(value[..idx], true, out var type))
|
|
{
|
|
type = RefTokenType.Subject;
|
|
}
|
|
|
|
result = new RefToken(type, value[(idx + 1)..]);
|
|
}
|
|
else
|
|
{
|
|
result = new RefToken(RefTokenType.Subject, value);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public static RefToken Parse(string value)
|
|
{
|
|
if (!TryParse(value, out var result))
|
|
{
|
|
ThrowHelper.ArgumentException("Ref token cannot be null or empty.", nameof(value));
|
|
return default!;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
|