示例#1
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "delete", Route = "users/me")] HttpRequest req)
        {
            using var _ = _log.FuncMeScope();
            _log.FuncMeStarted();

            var principal = await _securityValidator.GetClaimsPrincipalAsync(req);

            if (principal == null)
            {
                return(new UnauthorizedResult());
            }

            if (!principal.HasPermission(PermissionConstants.UserReadWrite))
            {
                _log.FuncMeUserDoesNotHavePermission(principal.Identity.Name, PermissionConstants.UserReadWrite);
                return(new UnauthorizedResult());
            }

            var authenticatedUser = principal.ToAuthenticatedUser();

            _log.FuncMeProcessingMethod(req.Method);
            return(req.Method switch
            {
                "GET" => await Get(authenticatedUser),
                "POST" => PostAsync(authenticatedUser, req),
                "DELETE" => Delete(authenticatedUser),
                _ => new StatusCodeResult((int)HttpStatusCode.NotImplemented)
            });
示例#2
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "games/{id}/moves")] HttpRequest req,
            [SignalR(HubName = "GameHub")] IAsyncCollector <SignalRMessage> signalRMessages,
            string id,
            ILogger log)
        {
            using var scope = log.BeginScope("GamesMove");
            log.LogInformation(LoggingEvents.FuncGamesMoveStarted, "GamesMove function processing request.");

            var principal = await _securityValidator.GetClaimsPrincipalAsync(req);

            if (principal == null)
            {
                return(new UnauthorizedResult());
            }

            if (!principal.HasPermission(PermissionConstants.GamesReadWrite))
            {
                log.LogWarning(LoggingEvents.FuncGamesMoveUserDoesNotHavePermission,
                               "User {user} does not have permission {permission}", principal.Identity.Name, PermissionConstants.GamesReadWrite);
                return(new UnauthorizedResult());
            }

            var authenticatedUser = principal.ToAuthenticatedUser();

            log.LogInformation(LoggingEvents.FuncGamesMoveProcessingMethod,
                               "Processing {method} request", req.Method);

            var moveToAdd = await JsonSerializer.DeserializeAsync <MyChessGameMove>(req.Body);

            var error = await _gamesHandler.AddMoveAsync(authenticatedUser, id, moveToAdd);

            if (error == null)
            {
                var payload = JsonSerializer.Serialize(moveToAdd);
                await signalRMessages.AddAsync(new SignalRMessage()
                {
                    GroupName = id,
                    Target    = "MoveUpdate",
                    Arguments = new[] { id, payload }
                });

                return(new OkResult());
            }
            else
            {
                var problemDetail = new ProblemDetails
                {
                    Detail   = error.Detail,
                    Instance = error.Instance,
                    Status   = error.Status,
                    Title    = error.Title
                };

                return(new ObjectResult(problemDetail)
                {
                    StatusCode = problemDetail.Status
                });
            }
        }
示例#3
0
        public async Task <IActionResult> Negotiate([HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequest req)
        {
            var principal = await _securityValidator.GetClaimsPrincipalAsync(req);

            if (principal == null)
            {
                return(new UnauthorizedResult());
            }

            var authenticatedUser = principal.ToAuthenticatedUser();

            return(new OkObjectResult(Negotiate(authenticatedUser.UserIdentifier)));
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("Me function processing request.");

            var principal = await _securityValidator.GetClaimsPrincipalAsync(req, log);

            if (principal == null)
            {
                return(new UnauthorizedResult());
            }

            return(new OkObjectResult(principal
                                      .Claims
                                      .Select(c => c.Value)
                                      .ToList()));
        }
示例#5
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "delete", Route = "games/{id?}")] HttpRequest req,
            [SignalR(HubName = "GameHub")] IAsyncCollector <SignalRGroupAction> signalRGroupActions,
            string id)
        {
            using var _ = _log.FuncGamesScope();
            _log.FuncGamesStarted();

            var principal = await _securityValidator.GetClaimsPrincipalAsync(req);

            if (principal == null)
            {
                return(new UnauthorizedResult());
            }

            if (!principal.HasPermission(PermissionConstants.GamesReadWrite))
            {
                _log.FuncGamesUserDoesNotHavePermission(principal.Identity.Name, PermissionConstants.GamesReadWrite);
                return(new UnauthorizedResult());
            }

            var authenticatedUser = principal.ToAuthenticatedUser();


            var state = "";

            if (req.Query.ContainsKey("state"))
            {
                state = req.Query["state"];
            }

            _log.FuncGamesProcessingMethod(req.Method);
            return(req.Method switch
            {
                "GET" => await GetAsync(authenticatedUser, id, state, signalRGroupActions),
                "POST" => await PostAsync(authenticatedUser, req, id),
                "DELETE" => await DeleteAsync(authenticatedUser, id),
                _ => new StatusCodeResult((int)HttpStatusCode.NotImplemented)
            });
示例#6
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "delete", Route = "sales/{id?}")] HttpRequest req,
            string id,
            ILogger log)
        {
            log.LogInformation("Sales function processing request.");

            var principal = await _securityValidator.GetClaimsPrincipalAsync(req, log);

            if (principal == null)
            {
                return(new UnauthorizedResult());
            }

            log.LogInformation("Processing {method} request", req.Method);
            return(req.Method switch
            {
                "GET" => Get(log, principal, id),
                "POST" => Post(log, principal, req, id),
                "DELETE" => Delete(log, principal, id),
                _ => new StatusCodeResult((int)HttpStatusCode.NotImplemented)
            });