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.
Added GraphQL and decided to remove most of the REST API. Login and Refresh session does happen with REST. BREAKING CHANGE: Most of the old endpoints are now removed and need to be accessed through GraphQL, logic however remains unchanged with exception of return types.
- Loading branch information
Showing
84 changed files
with
1,179 additions
and
1,001 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,17 @@ | ||
using System; | ||
using GraphQL.Types; | ||
using Micro.Auth.Api.GraphQL.Directives; | ||
|
||
namespace Micro.Auth.Api.GraphQL | ||
{ | ||
public class AuthSchema : Schema | ||
{ | ||
public AuthSchema(IServiceProvider services, Query query, Mutation mutation) : base(services) | ||
{ | ||
Query = query; | ||
Mutation = mutation; | ||
Directives.Register(new AuthorizeDirective()); | ||
RegisterVisitor(typeof(AuthorizeDirectiveVisitor)); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Micro.Auth.Api/GraphQL/DataLoaders/SessionByUserDataLoader.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,29 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using GraphQL.DataLoader; | ||
using Micro.Auth.Storage; | ||
|
||
namespace Micro.Auth.Api.GraphQL.DataLoaders | ||
{ | ||
public class SessionByUserDataLoader : DataLoaderBase<string, IEnumerable<RefreshToken>> | ||
{ | ||
private readonly IRefreshTokenRepository _refreshTokenRepository; | ||
|
||
public SessionByUserDataLoader(IRefreshTokenRepository refreshTokenRepository) | ||
{ | ||
_refreshTokenRepository = refreshTokenRepository; | ||
} | ||
|
||
protected override async Task FetchAsync(IEnumerable<DataLoaderPair<string, IEnumerable<RefreshToken>>> list, CancellationToken cancellationToken) | ||
{ | ||
var userIds = list.Select(x => x.Key); | ||
var sessions = await _refreshTokenRepository.FindByUserIds(userIds); | ||
foreach (var entry in list) | ||
{ | ||
entry.SetResult(sessions[entry.Key]); | ||
} | ||
} | ||
} | ||
} |
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,31 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using GraphQL.DataLoader; | ||
using Micro.Auth.Storage; | ||
using User = Micro.Auth.Business.Users.User; | ||
|
||
namespace Micro.Auth.Api.GraphQL.DataLoaders | ||
{ | ||
public class UserByIdDataLoader : DataLoaderBase<string, User> | ||
{ | ||
private readonly IUserRepository _userRepository; | ||
|
||
public UserByIdDataLoader(IUserRepository userRepository) | ||
{ | ||
_userRepository = userRepository; | ||
} | ||
|
||
protected override async Task FetchAsync(IEnumerable<DataLoaderPair<string, User>> list, CancellationToken cancellationToken) | ||
{ | ||
var ids = list.Select(x => x.Key).ToList(); | ||
var users = await _userRepository.FindByIds(ids.ToArray()); | ||
foreach (var entry in list) | ||
{ | ||
var exists = users.TryGetValue(entry.Key, out var user); | ||
entry.SetResult(exists ? User.FromDbUser(user) : null); | ||
} | ||
} | ||
} | ||
} |
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,47 @@ | ||
using GraphQL; | ||
using GraphQL.Types; | ||
using GraphQL.Utilities; | ||
using Micro.Auth.Api.GraphQL.Directives.Exceptions; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Micro.Auth.Api.GraphQL.Directives | ||
{ | ||
public class AuthorizeDirective : DirectiveGraphType | ||
{ | ||
public const string DirectiveName = "authorize"; | ||
public AuthorizeDirective() : base( | ||
DirectiveName, | ||
DirectiveLocation.Field, | ||
DirectiveLocation.Mutation, | ||
DirectiveLocation.Query, | ||
DirectiveLocation.FieldDefinition) | ||
{ | ||
} | ||
} | ||
|
||
public class AuthorizeDirectiveVisitor : BaseSchemaNodeVisitor | ||
{ | ||
private readonly IHttpContextAccessor _contextAccessor; | ||
public AuthorizeDirectiveVisitor(IHttpContextAccessor contextAccessor) | ||
{ | ||
_contextAccessor = contextAccessor; | ||
} | ||
|
||
public override void VisitObjectFieldDefinition(FieldType field, IObjectGraphType type, ISchema schema) | ||
{ | ||
var applied = field.FindAppliedDirective(AuthorizeDirective.DirectiveName); | ||
if (applied == null) | ||
{ | ||
return; | ||
} | ||
|
||
var isAuthenticated = _contextAccessor.HttpContext?.User.Identity?.IsAuthenticated; | ||
if (isAuthenticated == true) | ||
{ | ||
return; | ||
} | ||
|
||
throw new NotAuthorizedException(); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Micro.Auth.Api/GraphQL/Directives/Exceptions/NotAuthorizedException.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.Directives.Exceptions | ||
{ | ||
public class NotAuthorizedException : Exception | ||
{ | ||
public NotAuthorizedException() : base("This operation requires logging in") | ||
{ | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Micro.Auth.Api/GraphQL/Directives/Extensions/Directives.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,18 @@ | ||
using GraphQL; | ||
using GraphQL.Builders; | ||
using GraphQL.Types; | ||
|
||
namespace Micro.Auth.Api.GraphQL.Directives.Extensions | ||
{ | ||
public static class Directives | ||
{ | ||
public static FieldType Authorize(this FieldType type) | ||
{ | ||
return type.ApplyDirective(AuthorizeDirective.DirectiveName); | ||
} | ||
public static FieldBuilder<TSourceType, TReturnType> Authorize<TSourceType, TReturnType>(this FieldBuilder<TSourceType, TReturnType> type) | ||
{ | ||
return type.Directive(AuthorizeDirective.DirectiveName); | ||
} | ||
} | ||
} |
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,19 @@ | ||
using GraphQL.Types; | ||
|
||
namespace Micro.Auth.Api.GraphQL.Inputs | ||
{ | ||
public class ChangePasswordInput : InputObjectGraphType | ||
{ | ||
public static QueryArgument BuildArgument() | ||
{ | ||
return new QueryArgument<NonNullGraphType<ChangePasswordInput>> {Name = "input"}; | ||
} | ||
|
||
public ChangePasswordInput() | ||
{ | ||
Name = "ChangePasswordInput"; | ||
Field<NonNullGraphType<StringGraphType>>("old_password"); | ||
Field<NonNullGraphType<StringGraphType>>("new_password"); | ||
} | ||
} | ||
} |
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 GraphQL.Types; | ||
|
||
namespace Micro.Auth.Api.GraphQL.Inputs | ||
{ | ||
public class RegisterInputType : InputObjectGraphType | ||
{ | ||
public static QueryArgument BuildArgument() | ||
{ | ||
return new QueryArgument<NonNullGraphType<RegisterInputType>> {Name = "input"}; | ||
} | ||
|
||
public RegisterInputType() | ||
{ | ||
Name = "RegisterInput"; | ||
Field<NonNullGraphType<StringGraphType>>("username"); | ||
Field<NonNullGraphType<StringGraphType>>("email"); | ||
Field<NonNullGraphType<StringGraphType>>("password"); | ||
} | ||
} | ||
} |
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 GraphQL.Types; | ||
|
||
namespace Micro.Auth.Api.GraphQL.Inputs | ||
{ | ||
public class ResetPasswordInput : InputObjectGraphType | ||
{ | ||
public static QueryArgument BuildArgument() | ||
{ | ||
return new QueryArgument<NonNullGraphType<ResetPasswordInput>> {Name = "input"}; | ||
} | ||
|
||
public ResetPasswordInput() | ||
{ | ||
Name = "ResetPasswordInput"; | ||
Field<NonNullGraphType<StringGraphType>>("login"); | ||
Field<NonNullGraphType<StringGraphType>>("token"); | ||
Field<NonNullGraphType<StringGraphType>>("new_password"); | ||
} | ||
} | ||
} |
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,19 @@ | ||
using GraphQL.Types; | ||
|
||
namespace Micro.Auth.Api.GraphQL.Inputs | ||
{ | ||
public class VerifyEmailInputType : InputObjectGraphType | ||
{ | ||
public static QueryArgument BuildArgument() | ||
{ | ||
return new QueryArgument<NonNullGraphType<VerifyEmailInputType>> {Name = "input"}; | ||
} | ||
|
||
public VerifyEmailInputType() | ||
{ | ||
Name = "VerifyEmailInput"; | ||
Field<NonNullGraphType<StringGraphType>>("login"); | ||
Field<NonNullGraphType<StringGraphType>>("token"); | ||
} | ||
} | ||
} |
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,44 @@ | ||
using GraphQL; | ||
using GraphQL.Types; | ||
using Micro.Auth.Api.GraphQL.Directives.Extensions; | ||
using Micro.Auth.Api.GraphQL.Inputs; | ||
using Micro.Auth.Api.GraphQL.Types; | ||
using Micro.Auth.Api.Internal.UserData.Extensions; | ||
using Micro.Auth.Business.Common; | ||
using Micro.Auth.Business.EmailVerification; | ||
using Micro.Auth.Business.PasswordManager; | ||
using Micro.Auth.Business.Users; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Micro.Auth.Api.GraphQL | ||
{ | ||
public class Mutation : ObjectGraphType | ||
{ | ||
public Mutation(IUserService userService, IPasswordManager passwordManager, IEmailVerificationService verification, IHttpContextAccessor contextAccessor) | ||
{ | ||
FieldAsync<NonNullGraphType<UserType>, User>("register", | ||
arguments: new QueryArguments(RegisterInputType.BuildArgument()), | ||
resolve: x => userService.Create(x.GetArgument<RegisterInput>("input"))); | ||
|
||
FieldAsync<NonNullGraphType<UserType>, User>("verifyEmail", | ||
arguments: new QueryArguments(VerifyEmailInputType.BuildArgument()), | ||
resolve: x => verification.ConfirmEmail(x.GetArgument<VerifyEmailInput>("input"))); | ||
|
||
FieldAsync<NonNullGraphType<ResultType>, Result>("sendActivationEmail", | ||
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<StringGraphType>> {Name = "login"}), | ||
resolve: x => verification.SendActivationEmail(x.GetArgument<string>("login"))); | ||
|
||
FieldAsync<NonNullGraphType<ResultType>, Result>("requestPasswordReset", | ||
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<StringGraphType>> {Name = "login"}), | ||
resolve: x => passwordManager.RequestPasswordReset(x.GetArgument<string>("login"))); | ||
|
||
FieldAsync<NonNullGraphType<UserType>, User>("changePassword", | ||
arguments: new QueryArguments(ChangePasswordInput.BuildArgument()), | ||
resolve: x => passwordManager.ChangePassword(contextAccessor.GetUserId(), x.GetArgument<ChangePasswordRequest>("input"))).Authorize(); | ||
|
||
FieldAsync<NonNullGraphType<UserType>, User>("resetPassword", | ||
arguments: new QueryArguments(ResetPasswordInput.BuildArgument()), | ||
resolve: x => passwordManager.ResetPassword(x.GetArgument<ResetPasswordRequest>("input"))); | ||
} | ||
} | ||
} |
Oops, something went wrong.