コード例 #1
0
ファイル: NegotiateActivity.cs プロジェクト: robece/auth-api
        public HttpResponseMessage Negotiate([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "1.0/negotiate")]
                                             HttpRequestMessage req)
        {
            req.Headers.TryGetValues(HeaderNames.Authorization, out IEnumerable <string> authorizationEnumerable);
            if (authorizationEnumerable == null)
            {
                return(ResponseBuilderHelper.BuildResponse(HttpStatusCode.Unauthorized));
            }

            var authorizationList = authorizationEnumerable.ToList();

            if (authorizationList.Count == 0)
            {
                return(ResponseBuilderHelper.BuildResponse(HttpStatusCode.Unauthorized));
            }

            var(isValidToken, claims) = _jwtProvider.ValidateToken(authorizationList[0], Settings.AuthorizationKey);
            if (!isValidToken)
            {
                return(ResponseBuilderHelper.BuildResponse(HttpStatusCode.Unauthorized));
            }

            claims.TryGetValue("userID", out var userID);
            if (string.IsNullOrEmpty(userID))
            {
                return(ResponseBuilderHelper.BuildResponse(HttpStatusCode.BadRequest, "Missing parameter: userID"));
            }

            return(ResponseBuilderHelper.BuildResponse(HttpStatusCode.OK, new NegotiateActivityResponse()
            {
                UserID = userID
            }));
        }
コード例 #2
0
        public async Task <HttpResponseMessage> Create(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "1.0/create")]
            HttpRequestMessage req, [DurableClient] IDurableOrchestrationClient starter)
        {
            var(isValidToken, claims) = _jwtProvider.ValidateToken(req.Headers.GetValues(HeaderNames.Authorization).FirstOrDefault(), Settings.AuthorizationKey);
            if (!isValidToken)
            {
                return(new HttpResponseMessage(HttpStatusCode.Unauthorized));
            }

            var(response, requestData) = await ValidateRequestAsync(req);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                return(response);
            }

            var parameters = new OrchestrationActivityParameters()
            {
                AccessToken = req.Headers.GetValues(HeaderNames.Authorization).FirstOrDefault(),
                RequestData = (CreateActivityRequest)requestData,
                Claims      = claims
            };

            string instanceID = await starter.StartNewAsync <string>(nameof(OrchestrationActivity.Orchestration), JsonConvert.SerializeObject(parameters));

            _logger.LogInformation($"Instance ID: '{instanceID}'");

            return(new HttpResponseMessage(HttpStatusCode.OK));
        }