Skip to content

Instantly share code, notes, and snippets.

View ntakouris's full-sized avatar
🤖
Building robots

Theodoros Ntakouris ntakouris

🤖
Building robots
View GitHub Profile
private ClaimsPrincipal GetPrincipalFromToken(string token)
{
var tokenHandler = new JwtSecurityTokenHandler();
try
{
var principal = tokenHandler.ValidateToken(token, _tokenValidationParameters, out var validatedToken);
if (!IsJwtWithValidSecurityAlgorithm(validatedToken))
{
return null;
public class RefreshToken
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Token { get; set; }
public string JwtId { get; set; }
public DateTime CreationDate { get; set; }
public async Task<AuthenticationResult> RefreshTokenAsync(string token, string refreshToken)
{
var validatedToken = GetPrincipalFromToken(token);
if (validatedToken == null)
{
return new AuthenticationResult {Errors = new[] {"Invalid Token"}};
}
var expiryDateUnix =
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(claims),
Expires = DateTime.UtcNow.Add(_jwtSettings.TokenLifetime),
SigningCredentials =
new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
var token = tokenHandler.CreateToken(tokenDescriptor);
version: '3.5'
networks:
localdev:
name: localdev
services:
main-api:
build: Tweetbook/
restart: always
FROM microsoft/dotnet:2.2-sdk as build
ARG BUILDCONFIG=RELEASE
ARG VERSION=1.0.0
COPY Tweetbook.csproj /build/
RUN dotnet restore ./build/Tweetbook.csproj
COPY . ./build/
public class IntegrationTest
{
protected readonly HttpClient TestClient;
private readonly IServiceProvider _serviceProvider;
protected IntegrationTest()
{
var appFactory = new WebApplicationFactory<Startup>()
.WithWebHostBuilder(builder =>
[Fact]
public async Task GetAll_WithoutAnyPosts_ReturnsEmptyResponse()
{
// Arrange
await AuthenticateAsync();
// Act
var response = await TestClient.GetAsync(ApiRoutes.Posts.GetAll);
// Assert
[Fact]
public async Task Get_WhenPostExistsInTheDatabase_ReturnsPost()
{
// Arrange
await AuthenticateAsync();
var createdPost = await CreatePostAsync(new CreatePostRequest {Name = "Test post"});
// Act
var response = await TestClient.GetAsync(ApiRoutes.Posts.Get.Replace("{postId}", createdPost.Id.ToString()));
[Fact]
public async Task Get_WhenPostExistsInTheDatabase_ReturnsPost()
{
// Arrange
await AuthenticateAsync();
var createdPost = await CreatePostAsync(new CreatePostRequest {Name = "Test post"});
// Act
var response = await TestClient.GetAsync(ApiRoutes.Posts.Get.Replace("{postId}", createdPost.Id.ToString()));