-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4312 from signalco-io/feat/cloud-pat
feat(cloud): Added PAT for signalco endpoints
- Loading branch information
Showing
24 changed files
with
359 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Signal.Api.Common.Auth; | ||
|
||
[Serializable] | ||
public sealed class PatDto(string userId, string patEnd, string? alias, DateTime? expire) | ||
{ | ||
[JsonPropertyName("userId")] | ||
public string UserId { get; } = userId; | ||
|
||
[JsonPropertyName("patEnd")] | ||
public string PatEnd { get; } = patEnd; | ||
|
||
[JsonPropertyName("alias")] | ||
public string? Alias { get; } = alias; | ||
|
||
[JsonPropertyName("expire")] | ||
public DateTime? Expire { get; } = expire; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,4 @@ public UserAuth(string userId) | |
} | ||
|
||
public string UserId { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
|
||
namespace Signal.Core.Auth; | ||
|
||
public class Pat : IPat | ||
{ | ||
public required string UserId { get; set; } | ||
public required string PatEnd { get; set; } | ||
public required string PatHash { get; set; } | ||
public string? Alias { get; set; } | ||
public DateTime? Expire { get; set; } | ||
} | ||
|
||
public interface IPat | ||
{ | ||
string UserId { get; set; } | ||
|
||
string PatEnd { get; set; } | ||
|
||
string PatHash { get; set; } | ||
|
||
string? Alias { get; set; } | ||
|
||
DateTime? Expire { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
|
||
namespace Signal.Core.Auth; | ||
|
||
public interface IPatCreate | ||
{ | ||
string UserId { get; } | ||
|
||
string? Alias { get; } | ||
|
||
DateTime? Expire { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Signal.Core.Auth; | ||
|
||
public interface IPatService | ||
{ | ||
Task VerifyAsync(string userId, string pat, CancellationToken cancellationToken = default); | ||
|
||
Task<IEnumerable<IPat>> GetAllAsync(string userId, CancellationToken cancellationToken = default); | ||
|
||
Task<string> CreateAsync(IPatCreate patCreate, CancellationToken cancellationToken = default); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
|
||
public interface IUserAuth | ||
{ | ||
public string UserId { get; } | ||
string UserId { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
using System; | ||
|
||
namespace Signal.Core.Auth; | ||
|
||
public record PatCreate(string UserId, string? Alias, DateTime? Expire) : IPatCreate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Net; | ||
using System.Security.Claims; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.IdentityModel.Tokens; | ||
using Signal.Core.Exceptions; | ||
using Signal.Core.Secrets; | ||
using Signal.Core.Storage; | ||
|
||
namespace Signal.Core.Auth; | ||
|
||
public class PatService( | ||
IAzureStorage storage, | ||
IAzureStorageDao dao, | ||
ISecretsProvider secretsProvider) : IPatService | ||
{ | ||
public async Task VerifyAsync(string userId, string pat, CancellationToken cancellationToken = default) | ||
{ | ||
if (!await dao.PatExistsAsync(userId, PatHashSha256(userId, pat), cancellationToken)) | ||
throw new ExpectedHttpException(HttpStatusCode.Unauthorized); | ||
} | ||
|
||
public Task<IEnumerable<IPat>> GetAllAsync(string userId, CancellationToken cancellationToken = default) => | ||
dao.PatsAsync(userId, cancellationToken); | ||
|
||
public async Task<string> CreateAsync(IPatCreate patCreate, CancellationToken cancellationToken = default) | ||
{ | ||
var token = await this.JwtTokenAsync(patCreate.UserId, patCreate.Expire, cancellationToken); | ||
var hash = PatHashSha256(patCreate.UserId, token); | ||
await storage.PatCreateAsync( | ||
patCreate.UserId, | ||
token[^4..], hash, | ||
patCreate.Alias, | ||
patCreate.Expire, cancellationToken); | ||
return token; | ||
} | ||
|
||
private async Task<string> JwtTokenAsync(string userId, DateTime? expire, CancellationToken cancellationToken) | ||
{ | ||
var signingToken = await secretsProvider.GetSecretAsync(SecretKeys.PatSigningToken, cancellationToken); | ||
var signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(signingToken)); | ||
var signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha512Signature); | ||
var claims = new List<Claim> | ||
{ | ||
new(ClaimTypes.NameIdentifier, userId), | ||
}; | ||
var tokenDescriptor = new SecurityTokenDescriptor | ||
{ | ||
Issuer = "signalcopat", | ||
Subject = new ClaimsIdentity(claims), | ||
Expires = expire, | ||
SigningCredentials = signingCredentials | ||
}; | ||
var tokenHandler = new JwtSecurityTokenHandler(); | ||
var token = tokenHandler.CreateToken(tokenDescriptor); | ||
return tokenHandler.WriteToken(token); | ||
} | ||
|
||
private static string PatHashSha256(string key, string pat) | ||
{ | ||
var hash = new StringBuilder(); | ||
var crypto = HMACSHA512.HashData(Encoding.UTF8.GetBytes(key), Encoding.UTF8.GetBytes(pat)); | ||
foreach (var theByte in crypto) | ||
hash.Append(theByte.ToString("x2")); | ||
return hash.ToString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
cloud/src/Signal.Infrastructure.AzureStorage.Tables/AzureAuthPat.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using Signal.Core.Auth; | ||
|
||
namespace Signal.Infrastructure.AzureStorage.Tables; | ||
|
||
[Serializable] | ||
internal class AzureAuthPat : AzureTableEntityBase | ||
{ | ||
public string PatEnd { get; } | ||
public string? Alias { get; } | ||
public DateTime? Expire { get; } | ||
|
||
public AzureAuthPat(string userId, string patHash, string patEnd, string? alias, DateTime? expire) | ||
: this(userId, patHash) | ||
{ | ||
this.PatEnd = patEnd; | ||
this.Alias = alias; | ||
this.Expire = expire; | ||
} | ||
|
||
private AzureAuthPat(string partitionKey, string rowKey) : base(partitionKey, rowKey) | ||
{ | ||
} | ||
|
||
public static IPat ToPat(AzureAuthPat pat) => new Pat | ||
{ | ||
UserId = pat.PartitionKey, | ||
PatHash = pat.RowKey, | ||
PatEnd = pat.PatEnd, | ||
Alias = pat.Alias, | ||
Expire = pat.Expire, | ||
}; | ||
} |
Oops, something went wrong.