LCOV - code coverage report
Current view: top level - Cqrs.WebApi - CqrsApiController.cs Hit Total Coverage
Test: doc-coverage.info Lines: 1 10 10.0 %
Date: 2017-07-26

          Line data    Source code
       1             : #region Copyright
       2             : // // -----------------------------------------------------------------------
       3             : // // <copyright company="cdmdotnet Limited">
       4             : // //   Copyright cdmdotnet Limited. All rights reserved.
       5             : // // </copyright>
       6             : // // -----------------------------------------------------------------------
       7             : #endregion
       8             : 
       9             : using System;
      10             : using System.Collections.Generic;
      11             : using System.Linq;
      12             : using System.Net;
      13             : using System.Net.Http;
      14             : using System.Net.Http.Headers;
      15             : using System.Web.Http;
      16             : using cdmdotnet.Logging;
      17             : using Cqrs.Authentication;
      18             : using Cqrs.Services;
      19             : 
      20             : namespace Cqrs.WebApi
      21             : {
      22             :         /// <summary>
      23             :         /// A <see cref="ApiController"/> that expects the <see cref="ISingleSignOnToken.Token"/> to be sent as a <see cref="HttpHeaders"/> with a key of "X-Token", in accordance with OAuth specifications
      24             :         /// </summary>
      25             :         /// <remarks>
      26             :         /// See https://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/creating-api-help-pages for details on adding WebApi Help Pages.
      27             :         /// </remarks>
      28             :         public abstract class CqrsApiController
      29             :                 : ApiController
      30           1 :         {
      31           0 :                 protected CqrsApiController(ILogger logger, ICorrelationIdHelper correlationIdHelper)
      32             :                 {
      33             :                         CorrelationIdHelper = correlationIdHelper;
      34             :                         Logger = logger;
      35             :                 }
      36             : 
      37             :                 protected ICorrelationIdHelper CorrelationIdHelper { get; private set; }
      38             : 
      39             :                 protected ILogger Logger { get; private set; }
      40             : 
      41           0 :                 protected virtual string GetToken()
      42             :                 {
      43             :                         string token = null;
      44             :                         IEnumerable<string> tokenValue;
      45             :                         if (Request.Headers.TryGetValues("X-Token", out tokenValue))
      46             :                                 token = tokenValue.First();
      47             :                         else
      48             :                         {
      49             :                                 CookieHeaderValue cookie = Request.Headers.GetCookies("X-Token").FirstOrDefault();
      50             :                                 if (cookie != null)
      51             :                                         token = cookie["X-Token"].Value;
      52             :                         }
      53             : 
      54             :                         return token;
      55             :                 }
      56             : 
      57           0 :                 protected virtual IServiceRequest<TSingleSignOnToken> CreateRequest<TSingleSignOnToken>()
      58             :                         where TSingleSignOnToken : ISingleSignOnToken, new()
      59             :                 {
      60             :                         return new ServiceRequest<TSingleSignOnToken>
      61             :                         {
      62             :                                 AuthenticationToken = CreateAuthenticationToken<TSingleSignOnToken>(),
      63             :                                 CorrelationId = CorrelationIdHelper.GetCorrelationId()
      64             :                         };
      65             :                 }
      66             : 
      67           0 :                 protected virtual IServiceRequestWithData<TSingleSignOnToken, TParameters> CreateRequestWithData<TSingleSignOnToken, TParameters>(Func<TParameters> createParameterDelegate = null)
      68             :                         where TSingleSignOnToken : ISingleSignOnToken, new()
      69             :                         where TParameters : new()
      70             :                 {
      71             :                         return new ServiceRequestWithData<TSingleSignOnToken, TParameters>
      72             :                         {
      73             :                                 AuthenticationToken = CreateAuthenticationToken<TSingleSignOnToken>(),
      74             :                                 CorrelationId = CorrelationIdHelper.GetCorrelationId(),
      75             :                                 Data = createParameterDelegate == null ? CreateParameter<TParameters>() : createParameterDelegate()
      76             :                         };
      77             :                 }
      78             : 
      79           0 :                 protected virtual TSingleSignOnToken CreateAuthenticationToken<TSingleSignOnToken>()
      80             :                         where TSingleSignOnToken : ISingleSignOnToken, new()
      81             :                 {
      82             :                         return new TSingleSignOnToken
      83             :                         {
      84             :                                 DateIssued = GetDateTokenIssued(),
      85             :                                 Token = GetToken(),
      86             :                                 TimeOfExpiry = GetTokenTimeOfExpiry()
      87             :                         };
      88             :                 }
      89             : 
      90           0 :                 protected virtual TParameters CreateParameter<TParameters>()
      91             :                         where TParameters : new()
      92             :                 {
      93             :                         return new TParameters();
      94             :                 }
      95             : 
      96           0 :                 protected virtual DateTime GetDateTokenIssued()
      97             :                 {
      98             :                         return default(DateTime);
      99             :                 }
     100             : 
     101           0 :                 protected virtual DateTime GetTokenTimeOfExpiry()
     102             :                 {
     103             :                         return default(DateTime);
     104             :                 }
     105             : 
     106           0 :                 protected virtual TServiceResponse CompleteResponse<TServiceResponse>(TServiceResponse serviceResponse)
     107             :                         where TServiceResponse : IServiceResponse
     108             :                 {
     109             :                         serviceResponse.CorrelationId = CorrelationIdHelper.GetCorrelationId();
     110             :                         switch (serviceResponse.State)
     111             :                         {
     112             :                                 case ServiceResponseStateType.Succeeded:
     113             :                                         StatusCode(HttpStatusCode.OK);
     114             :                                         break;
     115             :                                 case ServiceResponseStateType.FailedAuthentication:
     116             :                                         StatusCode(HttpStatusCode.Forbidden);
     117             :                                         break;
     118             :                                 case ServiceResponseStateType.FailedAuthorisation:
     119             :                                         StatusCode(HttpStatusCode.Unauthorized);
     120             :                                         break;
     121             :                                 case ServiceResponseStateType.FailedValidation:
     122             :                                         StatusCode(HttpStatusCode.PreconditionFailed);
     123             :                                         break;
     124             :                                 case ServiceResponseStateType.FailedWithAFatalException:
     125             :                                         StatusCode(HttpStatusCode.InternalServerError);
     126             :                                         break;
     127             :                                 case ServiceResponseStateType.FailedWithAnUnexpectedException:
     128             :                                         StatusCode(HttpStatusCode.InternalServerError);
     129             :                                         break;
     130             :                                 case ServiceResponseStateType.Unknown:
     131             :                                         StatusCode(HttpStatusCode.BadRequest);
     132             :                                         break;
     133             :                         }
     134             :                         return serviceResponse;
     135             :                 }
     136             :         }
     137             : }

Generated by: LCOV version 1.10