public override async Task <AuthorizationResponse> LoginWithToken(AuthorizedTokenRequest request, ServerCallContext context)
        {
            if (string.IsNullOrEmpty(request.Token))
            {
                return new AuthorizationResponse()
                       {
                           Status = ResponseFactory.createResponse(ResponseType.INVALID_CREDENTIALS)
                       }
            }
            ;

            if (JwtManager.ValidateToken(request.Token, out var uuid))
            {
                // Validate user exists.
                RedisUserModel user = await RedisUserModel.GetUserFromGuid(uuid);

                if (user != null)
                {
                    context.UserState["user"] = user;
                    return(new AuthorizationResponse()
                    {
                        Status = ResponseFactory.createResponse(ResponseType.SUCCESS),
                        Token = request.Token,
                        User = user.asUser(),
                    });
                }
            }
            return(new AuthorizationResponse()
            {
                Status = ResponseFactory.createResponse(ResponseType.INVALID_CREDENTIALS)
            });
        }
        public void SuperUserAccountHasAdminClaims()
        {
            SuperUser superUser = RedisUserModel.CreateSuperUser().Result;

            // Fetch the user from the database.
            RedisUserModel user = RedisUserModel.GetUserFromGuid(superUser.userModel.UserModel.Id).Result;

            // Ensure the user has admin power
            Assert.IsTrue(user.HasClaim(UserClaim.Admin));
            Assert.IsTrue(user.HasClaim(UserClaim.Dev));
            Assert.IsTrue(user.HasClaim(UserClaim.EmailVerified));
        }
        public override async Task <SendFriendRequestResponse> SendFriendRequest(SendFriendRequestRequest request, ServerCallContext context)
        {
            RedisUserModel user = context.UserState["user"] as RedisUserModel;

            if (user == null)
            {
                return new SendFriendRequestResponse()
                       {
                           Status = ResponseFactory.createResponse(ResponseType.UNAUTHORIZED)
                       }
            }
            ;

            RedisUserModel friend = await RedisUserModel.GetUserFromGuid(request.FriendId);

            if (friend == null)
            {
                return new SendFriendRequestResponse()
                       {
                           Status = ResponseFactory.createResponse(ResponseType.PLAYER_DOES_NOT_EXIST)
                       }
            }
            ;

            if (await friend.HasFriendRequestFrom(user))
            {
                return new SendFriendRequestResponse()
                       {
                           Status = ResponseFactory.createResponse(ResponseType.DUPLICATE)
                       }
            }
            ;

            if (await friend.IsBlocked(user) || await user.IsBlocked(friend))
            {
                return new SendFriendRequestResponse()
                       {
                           Status = ResponseFactory.createResponse(ResponseType.PLAYER_IS_BLOCKED)
                       }
            }
            ;

            // Add request to the other player.
            await friend.AddFriendRequestFrom(user);

            return(new SendFriendRequestResponse()
            {
                Status = ResponseFactory.createResponse(ResponseType.SUCCESS)
            });
        }
        public override async Task <UnblockPlayerResponse> UnblockPlayer(UnblockPlayerRequest request, ServerCallContext context)
        {
            RedisUserModel user = context.UserState["user"] as RedisUserModel;

            if (user == null)
            {
                return new UnblockPlayerResponse()
                       {
                           Status = ResponseFactory.createResponse(ResponseType.UNAUTHORIZED)
                       }
            }
            ;

            // Check if player is valid.
            RedisUserModel friend = await RedisUserModel.GetUserFromGuid(request.UserIdToBlock);

            if (friend == null)
            {
                return new UnblockPlayerResponse()
                       {
                           Status = ResponseFactory.createResponse(ResponseType.PLAYER_DOES_NOT_EXIST)
                       }
            }
            ;

            // Check if player is blocked.
            if (!await user.IsBlocked(friend))
            {
                return new UnblockPlayerResponse()
                       {
                           Status = ResponseFactory.createResponse(ResponseType.INVALID_REQUEST)
                       }
            }
            ;

            await user.UnblockUser(friend);

            return(new UnblockPlayerResponse()
            {
                Status = ResponseFactory.createResponse(ResponseType.SUCCESS)
            });
        }
        public override async Task <TResponse> UnaryServerHandler <TRequest, TResponse>(TRequest request, ServerCallContext context, UnaryServerMethod <TRequest, TResponse> continuation)
        {
            string calledMethod = context.Method.Split('/').Last();

            context.UserState["user"] = null;

            // Do not authorize these methods:
            string[] whitelist =
            {
                "Login",
                "LoginWithToken",
                "RegisterAccount",
                "HealthCheck",
            };

            if (!whitelist.Contains(calledMethod))
            {
                // The endpoint requires authorization before performing.
                // Get JWT header
                Metadata.Entry entry = context.RequestHeaders.Get("authorization");
                if (entry?.Value != null)
                {
                    if (JwtManager.ValidateToken(entry.Value, out var uuid))
                    {
                        // Validate user exists.
                        RedisUserModel user = await RedisUserModel.GetUserFromGuid(uuid);

                        if (user != null)
                        {
                            context.UserState["user"] = user;
                        }
                    }
                }
            }

            // Do regular request and return
            var response = await base.UnaryServerHandler(request, context, continuation);

            return(response);
        }
        public override async Task <BlockPlayerResponse> BlockPlayer(BlockPlayerRequest request, ServerCallContext context)
        {
            RedisUserModel user = context.UserState["user"] as RedisUserModel;

            if (user == null)
            {
                return new BlockPlayerResponse()
                       {
                           Status = ResponseFactory.createResponse(ResponseType.UNAUTHORIZED)
                       }
            }
            ;

            RedisUserModel friend = await RedisUserModel.GetUserFromGuid(request.UserIdToBlock);

            if (friend == null)
            {
                return new BlockPlayerResponse()
                       {
                           Status = ResponseFactory.createResponse(ResponseType.PLAYER_DOES_NOT_EXIST)
                       }
            }
            ;

            if (await user.IsBlocked(friend))
            {
                return new BlockPlayerResponse()
                       {
                           Status = ResponseFactory.createResponse(ResponseType.DUPLICATE)
                       }
            }
            ;

            await user.BlockUser(friend);

            return(new BlockPlayerResponse()
            {
                Status = ResponseFactory.createResponse(ResponseType.SUCCESS)
            });
        }
        public override async Task <DenyFriendRequestResponse> DenyFriendRequest(DenyFriendRequestRequest request, ServerCallContext context)
        {
            RedisUserModel user = context.UserState["user"] as RedisUserModel;

            if (user == null)
            {
                return new DenyFriendRequestResponse()
                       {
                           Status = ResponseFactory.createResponse(ResponseType.UNAUTHORIZED)
                       }
            }
            ;

            RedisUserModel friend = await RedisUserModel.GetUserFromGuid(request.FriendId);

            if (friend == null)
            {
                return new DenyFriendRequestResponse()
                       {
                           Status = ResponseFactory.createResponse(ResponseType.PLAYER_DOES_NOT_EXIST)
                       }
            }
            ;

            if (!await user.HasFriendRequestFrom(friend))
            {
                return new DenyFriendRequestResponse()
                       {
                           Status = ResponseFactory.createResponse(ResponseType.FRIEND_REQUEST_DOES_NOT_EXIST)
                       }
            }
            ;

            return(new DenyFriendRequestResponse()
            {
                Status = await user.RemoveFriendRequestFrom(friend),
            });
        }