generated from fossapps/Micro.Starter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): add login and refresh mutation
BREAKING CHANGE: Older rest endpoint to login and refresh removed
- Loading branch information
Showing
18 changed files
with
197 additions
and
219 deletions.
There are no files selected for viewing
11 changes: 0 additions & 11 deletions
11
Micro.Auth.Api/Authentication/Exceptions/BadBasicAuthorizationDataException.cs
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
Micro.Auth.Api/Authentication/ViewModels/JwtRefreshViewModels.cs
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
Micro.Auth.Api/GraphQL/Extensions/Exceptions/InvalidTokenTypeException.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,11 @@ | ||
using System; | ||
|
||
namespace Micro.Auth.Api.GraphQL.Extensions.Exceptions | ||
{ | ||
public class InvalidTokenTypeException : Exception | ||
{ | ||
public InvalidTokenTypeException(string expectedType) : base($"{expectedType} token not found") | ||
{ | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Micro.Auth.Api/GraphQL/Extensions/Exceptions/MissingHeaderException.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,11 @@ | ||
using System; | ||
|
||
namespace Micro.Auth.Api.GraphQL.Extensions.Exceptions | ||
{ | ||
public class MissingHeaderException : Exception | ||
{ | ||
public MissingHeaderException(string headerName) : base($"Missing {headerName} header") | ||
{ | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
Micro.Auth.Api/GraphQL/Extensions/LoginRequestExtension.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,22 @@ | ||
using Micro.Auth.Api.Internal.UserData.Extensions; | ||
using Micro.Auth.Business.Sessions; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Micro.Auth.Api.GraphQL.Extensions | ||
{ | ||
public static class LoginRequestExtension | ||
{ | ||
public static LoginRequest GetLoginRequest(this IHttpContextAccessor httpContextAccessor) | ||
{ | ||
var (login, password) = httpContextAccessor.MustGetBasicToken(); | ||
return new LoginRequest | ||
{ | ||
Login = login, | ||
Password = password, | ||
IpAddress = httpContextAccessor.GetIpAddress(), | ||
UserAgent = httpContextAccessor.GetUserAgent(), | ||
Location = httpContextAccessor.GetRoughLocation() | ||
}; | ||
} | ||
} | ||
} |
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,53 @@ | ||
using System; | ||
using System.Text; | ||
using Micro.Auth.Api.GraphQL.Extensions.Exceptions; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Primitives; | ||
|
||
namespace Micro.Auth.Api.GraphQL.Extensions | ||
{ | ||
public static class Tokens | ||
{ | ||
public static string MustGetBearerToken(this IHttpContextAccessor httpContextAccessor) | ||
{ | ||
var headerValue = httpContextAccessor.MustGetAuthHeader(); | ||
if (!headerValue.StartsWith("Bearer ")) | ||
{ | ||
throw new InvalidTokenTypeException("Bearer"); | ||
} | ||
return headerValue.Substring("Bearer ".Length).Trim(); | ||
} | ||
|
||
private static string MustGetAuthHeader(this IHttpContextAccessor httpContextAccessor) | ||
{ | ||
var header = StringValues.Empty; | ||
var exists = httpContextAccessor.HttpContext?.Request.Headers.TryGetValue("Authorization", out header); | ||
if (exists == false || header.ToString() == "") | ||
{ | ||
throw new MissingHeaderException("Authorization"); | ||
} | ||
|
||
return header.ToString(); | ||
} | ||
|
||
public static (string, string) MustGetBasicToken(this IHttpContextAccessor httpContextAccessor) | ||
{ | ||
try | ||
{ | ||
var headerValue = httpContextAccessor.MustGetAuthHeader(); | ||
if (!headerValue.StartsWith("Basic ")) | ||
{ | ||
throw new InvalidTokenTypeException("Basic"); | ||
} | ||
|
||
var token = headerValue.Substring("Basic ".Length).Trim(); | ||
var parts = Encoding.UTF8.GetString(Convert.FromBase64String(token)).Split(":"); | ||
return (parts[0], parts[1]); | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new InvalidTokenTypeException(e.Message); | ||
} | ||
} | ||
} | ||
} |
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,15 @@ | ||
using Micro.Auth.Business.Sessions; | ||
|
||
namespace Micro.Auth.Api.GraphQL.Types | ||
{ | ||
public sealed class TokensType : Micro.GraphQL.Federation.ObjectGraphType<LoginSuccessResponse> | ||
{ | ||
public TokensType() | ||
{ | ||
Name = "Token"; | ||
Field("jwt", x => x.Jwt).Description("JWT"); | ||
Field("refreshToken", x => x.RefreshToken, true) | ||
.Description("Once JWT expires, use this token to create a new session"); | ||
} | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
Micro.Auth.Business/Sessions/Exceptions/InvalidCredentialsException.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,11 @@ | ||
using System; | ||
|
||
namespace Micro.Auth.Business.Sessions.Exceptions | ||
{ | ||
public class InvalidCredentialsException : Exception | ||
{ | ||
public InvalidCredentialsException(string message) : base(message) | ||
{ | ||
} | ||
} | ||
} |
Oops, something went wrong.