PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/sipsorcery-core/SIPSorcery.Sys/Auth/ServiceAuthToken.cs

https://github.com/thecc4re/sipsorcery-mono
C# | 110 lines | 59 code | 9 blank | 42 comment | 18 complexity | 6efb766447484a3cece21a8d39d42bdc MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. // ============================================================================
  2. // FileName: ServiceAuthToken.cs
  3. //
  4. // Description:
  5. // Represents a security token that is passed to web or WCF service.
  6. //
  7. // Author(s):
  8. // Aaron Clauson
  9. //
  10. // History:
  11. // 09 Jun 2010 Aaron Clauson Created.
  12. //
  13. // License:
  14. // This software is licensed under the BSD License http://www.opensource.org/licenses/bsd-license.php
  15. //
  16. // Copyright (c) 2010 Aaron Clauson (aaron@sipsorcery.com), SIP Sorcery Ltd, Hobart, Australia (www.sipsorcery.com)
  17. // All rights reserved.
  18. //
  19. // Redistribution and use in source and binary forms, with or without modification, are permitted provided that
  20. // the following conditions are met:
  21. //
  22. // Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  23. // Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
  24. // disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of Blue Face Ltd.
  25. // nor the names of its contributors may be used to endorse or promote products derived from this software without specific
  26. // prior written permission.
  27. //
  28. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  29. // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  30. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  31. // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  32. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. // POSSIBILITY OF SUCH DAMAGE.
  35. // ============================================================================
  36. using System;
  37. using System.Collections.Generic;
  38. using System.Linq;
  39. using System.ServiceModel;
  40. using System.ServiceModel.Channels;
  41. using System.Text;
  42. using System.Text.RegularExpressions;
  43. #if !SILVERLIGHT
  44. using System.Web;
  45. using System.Web.Services;
  46. #endif
  47. namespace SIPSorcery.Sys.Auth
  48. {
  49. public class ServiceAuthToken
  50. {
  51. public const string AUTH_TOKEN_KEY = "authid";
  52. public const string COOKIES_KEY = "Cookie";
  53. #if !SILVERLIGHT
  54. public static string GetAuthId()
  55. {
  56. string authId = null;
  57. if (OperationContext.Current != null)
  58. {
  59. SIPSorcerySecurityHeader securityheader = SIPSorcerySecurityHeader.ParseHeader(OperationContext.Current);
  60. if (securityheader != null)
  61. {
  62. authId = securityheader.AuthID;
  63. }
  64. }
  65. // HTTP Context is available for ?? binding.
  66. if (authId.IsNullOrBlank() && HttpContext.Current != null)
  67. {
  68. // If running in IIS check for a cookie.
  69. HttpCookie authIdCookie = HttpContext.Current.Request.Cookies[AUTH_TOKEN_KEY];
  70. if (authIdCookie != null)
  71. {
  72. //logger.Debug("authid cookie found: " + authIdCookie.Value + ".");
  73. authId = authIdCookie.Value;
  74. }
  75. }
  76. // No HTTP context available so try and get a cookie value from the operation context.
  77. if (authId.IsNullOrBlank() && OperationContext.Current != null && OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name] != null)
  78. {
  79. HttpRequestMessageProperty httpRequest = (HttpRequestMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name];
  80. // Check for the header in a case insensitive way. Allows matches on authid, Authid etc.
  81. if (httpRequest.Headers.AllKeys.Contains(AUTH_TOKEN_KEY, StringComparer.InvariantCultureIgnoreCase))
  82. {
  83. string authIDHeader = httpRequest.Headers.AllKeys.First(h => { return String.Equals(h, AUTH_TOKEN_KEY, StringComparison.InvariantCultureIgnoreCase); });
  84. authId = httpRequest.Headers[authIDHeader];
  85. //logger.Debug("authid HTTP header found: " + authId + ".");
  86. }
  87. else if (httpRequest.Headers.AllKeys.Contains(COOKIES_KEY, StringComparer.InvariantCultureIgnoreCase))
  88. {
  89. Match authIDMatch = Regex.Match(httpRequest.Headers[COOKIES_KEY], @"authid=(?<authid>.+)");
  90. if (authIDMatch.Success)
  91. {
  92. authId = authIDMatch.Result("${authid}");
  93. //logger.Debug("authid HTTP cookie found: " + authId + ".");
  94. }
  95. }
  96. }
  97. return authId;
  98. }
  99. #endif
  100. }
  101. }