This file contains hidden or 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
private ClaimsPrincipal GetPrincipalFromToken(string token) | |
{ | |
var tokenHandler = new JwtSecurityTokenHandler(); | |
try | |
{ | |
var principal = tokenHandler.ValidateToken(token, _tokenValidationParameters, out var validatedToken); | |
if (!IsJwtWithValidSecurityAlgorithm(validatedToken)) | |
{ | |
return null; |
This file contains hidden or 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
public class RefreshToken | |
{ | |
[Key] | |
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | |
public string Token { get; set; } | |
public string JwtId { get; set; } | |
public DateTime CreationDate { get; set; } |
This file contains hidden or 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
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 = |
This file contains hidden or 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
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); |
This file contains hidden or 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
version: '3.5' | |
networks: | |
localdev: | |
name: localdev | |
services: | |
main-api: | |
build: Tweetbook/ | |
restart: always |
This file contains hidden or 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
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/ |
This file contains hidden or 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
public class IntegrationTest | |
{ | |
protected readonly HttpClient TestClient; | |
private readonly IServiceProvider _serviceProvider; | |
protected IntegrationTest() | |
{ | |
var appFactory = new WebApplicationFactory<Startup>() | |
.WithWebHostBuilder(builder => |
This file contains hidden or 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
[Fact] | |
public async Task GetAll_WithoutAnyPosts_ReturnsEmptyResponse() | |
{ | |
// Arrange | |
await AuthenticateAsync(); | |
// Act | |
var response = await TestClient.GetAsync(ApiRoutes.Posts.GetAll); | |
// Assert |
This file contains hidden or 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
[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())); |
This file contains hidden or 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
[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())); |