mirror of https://github.com/Squidex/squidex.git
19 changed files with 370 additions and 125 deletions
@ -0,0 +1,23 @@ |
|||
// ==========================================================================
|
|||
// WebhookAdded.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Events.Schemas |
|||
{ |
|||
[TypeName("WebhookAddedEvent")] |
|||
public sealed class WebhookAdded : SchemaEvent |
|||
{ |
|||
public Guid Id { get; set; } |
|||
|
|||
public Uri Url { get; set; } |
|||
|
|||
public string SecurityToken { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
// ==========================================================================
|
|||
// WebhookDeleted.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Events.Schemas |
|||
{ |
|||
[TypeName("WebhookDeletedEvent")] |
|||
public sealed class WebhookDeleted : SchemaEvent |
|||
{ |
|||
public Guid Id { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
// ==========================================================================
|
|||
// RandomHash.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Security.Cryptography; |
|||
using System.Text; |
|||
|
|||
namespace Squidex.Infrastructure |
|||
{ |
|||
public static class RandomHash |
|||
{ |
|||
public static string New() |
|||
{ |
|||
using (var sha = SHA256.Create()) |
|||
{ |
|||
var bytes = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()); |
|||
var hash = sha.ComputeHash(bytes); |
|||
|
|||
return Convert.ToBase64String(hash).Replace("+", "x"); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,42 +0,0 @@ |
|||
// ==========================================================================
|
|||
// ClientKeyGenerator.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Security.Cryptography; |
|||
using System.Text; |
|||
|
|||
// ReSharper disable ClassWithVirtualMembersNeverInherited.Global
|
|||
|
|||
namespace Squidex.Write.Apps |
|||
{ |
|||
public class ClientKeyGenerator |
|||
{ |
|||
private readonly Func<HashAlgorithm> algorithmFactory; |
|||
|
|||
public ClientKeyGenerator() |
|||
{ |
|||
algorithmFactory = SHA256.Create; |
|||
} |
|||
|
|||
public virtual string GenerateKey() |
|||
{ |
|||
return Hash(Guid.NewGuid().ToString()); |
|||
} |
|||
|
|||
private string Hash(string input) |
|||
{ |
|||
using (var sha = algorithmFactory()) |
|||
{ |
|||
var bytes = Encoding.UTF8.GetBytes(input); |
|||
var hash = sha.ComputeHash(bytes); |
|||
|
|||
return Convert.ToBase64String(hash).Replace("+", "x"); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,31 @@ |
|||
// ==========================================================================
|
|||
// AddWebhook.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Write.Schemas.Commands |
|||
{ |
|||
public sealed class AddWebhook : SchemaAggregateCommand, IValidatable |
|||
{ |
|||
public Guid Id { get; } = Guid.NewGuid(); |
|||
|
|||
public Uri Url { get; set; } |
|||
|
|||
public string SecurityToken { get; } = RandomHash.New(); |
|||
|
|||
public void Validate(IList<ValidationError> errors) |
|||
{ |
|||
if (Url == null || !Url.IsAbsoluteUri) |
|||
{ |
|||
errors.Add(new ValidationError("Url must be specified and absolute", nameof(Url))); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
// ==========================================================================
|
|||
// DeleteWebhook.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
|
|||
namespace Squidex.Write.Schemas.Commands |
|||
{ |
|||
public class DeleteWebhook : SchemaAggregateCommand |
|||
{ |
|||
public Guid Id { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,32 @@ |
|||
// ==========================================================================
|
|||
// RandomHashTests.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using Xunit; |
|||
|
|||
namespace Squidex.Infrastructure |
|||
{ |
|||
public class RandomHashTests |
|||
{ |
|||
[Fact] |
|||
public void Should_create_long_hash() |
|||
{ |
|||
var hash = RandomHash.New(); |
|||
|
|||
Assert.Equal(44, hash.Length); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_create_new_hashs() |
|||
{ |
|||
var hash1 = RandomHash.New(); |
|||
var hash2 = RandomHash.New(); |
|||
|
|||
Assert.NotEqual(hash1, hash2); |
|||
} |
|||
} |
|||
} |
|||
@ -1,25 +0,0 @@ |
|||
// ==========================================================================
|
|||
// ClientKeyGeneratorTests.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using Xunit; |
|||
|
|||
namespace Squidex.Write.Apps |
|||
{ |
|||
public class ClientKeyGeneratorTests |
|||
{ |
|||
private readonly ClientKeyGenerator sut = new ClientKeyGenerator(); |
|||
|
|||
[Fact] |
|||
public void Should_create_very_long_client_key() |
|||
{ |
|||
var key = sut.GenerateKey(); |
|||
|
|||
Assert.Equal(44, key.Length); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue