AdmAuthClientIdentity.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // -
  2. // <copyright file="AdmAuthClientIdentity.cs" company="Microsoft Corporation">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // -
  6. namespace Microsoft.Hawaii
  7. {
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Security;
  11. /// <summary>
  12. /// The adm authentication client identity.
  13. /// </summary>
  14. public class AdmAuthClientIdentity : ClientIdentity
  15. {
  16. /// <summary>
  17. /// Specifies the adm OAuth service endpoint.
  18. /// </summary>
  19. private const string AdmOAuthEndpoint = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";
  20. /// <summary>
  21. /// The name of the config file that indicates what is the service scope. Used only as a test hook.
  22. /// All the hawaii services share one single service scope.
  23. /// </summary>
  24. private static readonly string StagingServiceScopeConfigFileName = @"C:\AzureStagingDeploymentConfig\HawaiiServiceScopeConfig.ini";
  25. /// <summary>
  26. /// A dictionary mapping clientids to tokenservice instances
  27. /// </summary>
  28. private static Dictionary<string, AdmTokenService> tokenServices = new Dictionary<string, AdmTokenService>();
  29. /// <summary>
  30. /// The adm token service instance.
  31. /// </summary>
  32. private AdmTokenService tokenService;
  33. /// <summary>
  34. /// The adm client Id.
  35. /// </summary>
  36. private string clientId;
  37. /// <summary>
  38. /// The adm client secret.
  39. /// </summary>
  40. private string clientSecret;
  41. /// <summary>
  42. /// The adm service scope.
  43. /// </summary>
  44. private string scope;
  45. /// <summary>
  46. /// Initializes a new instance of the AdmAuthClientIdentity class.
  47. /// </summary>
  48. /// <param name="clientId">The adm client Id.</param>
  49. /// <param name="clientSecret">The adm client secret.</param>
  50. /// <param name="scope">The scope Uri.</param>
  51. public AdmAuthClientIdentity(string clientId, string clientSecret, string scope) :
  52. this(clientId, clientSecret, scope, string.Empty, string.Empty)
  53. {
  54. }
  55. /// <summary>
  56. /// Initializes a new instance of the AdmAuthClientIdentity class.
  57. /// </summary>
  58. /// <param name="clientId">The adm client Id.</param>
  59. /// <param name="clientSecret">The adm client secret.</param>
  60. /// <param name="scope">The scope Uri.</param>
  61. /// <param name="registrationId">The registration Id.</param>
  62. /// <param name="secretKey">The secret key.</param>
  63. public AdmAuthClientIdentity(string clientId, string clientSecret, string scope, string registrationId, string secretKey) :
  64. base(registrationId, secretKey)
  65. {
  66. this.clientId = clientId;
  67. this.clientSecret = clientSecret;
  68. this.scope = scope;
  69. lock (tokenServices)
  70. {
  71. if (!tokenServices.TryGetValue(clientId, out this.tokenService))
  72. {
  73. this.tokenService = new AdmTokenService(
  74. clientId,
  75. clientSecret,
  76. AdmOAuthEndpoint,
  77. scope);
  78. tokenServices.Add(clientId, this.tokenService);
  79. }
  80. }
  81. }
  82. /// <summary>
  83. /// Gets the Adm client Id.
  84. /// </summary>
  85. public string ClientId
  86. {
  87. get
  88. {
  89. return this.clientId;
  90. }
  91. }
  92. /// <summary>
  93. /// Returns the service scope to be used when accessing the adm OAuth service. This will generally
  94. /// be the value generated by the DefaultServiceScope, but it can conditionally be set with
  95. /// the presence of a config file on first access.
  96. /// </summary>
  97. /// <param name="serviceBaseUri">The target service base Uri.</param>
  98. /// <returns>Returns the service scope</returns>
  99. [SecuritySafeCritical]
  100. public static string GetServiceScope(string serviceBaseUri)
  101. {
  102. UriBuilder uriBuilder = new UriBuilder(serviceBaseUri);
  103. string defaultServiceScope = string.Format("{0}://{1}", uriBuilder.Scheme, uriBuilder.Host);
  104. return ClientLibraryUtils.LookupServiceScopeFromConfig(StagingServiceScopeConfigFileName, defaultServiceScope);
  105. }
  106. /// <summary>
  107. /// Override the method to copy Adm authentication identity
  108. /// </summary>
  109. /// <returns>Returns the client identity</returns>
  110. public override ClientIdentity Copy()
  111. {
  112. return new AdmAuthClientIdentity(this.clientId, this.clientSecret, this.scope, this.RegistrationId, this.SecretKey);
  113. }
  114. /// <summary>
  115. /// Override the method to retrive the access token for Adm authentication.
  116. /// </summary>
  117. /// <param name="callback">event callback</param>
  118. public override void RetriveAccessToken(RetriveAccessTokenComplete callback)
  119. {
  120. this.tokenService.GetAccessToken(new AdmTokenService.RetriveAdmAccessTokenComplete((accessToken, ex) =>
  121. {
  122. TokenService_GetAdmAccessTokenCompleteEvent(accessToken, ex, callback);
  123. }));
  124. }
  125. /// <summary>
  126. /// The callback handler of GetAdmAccessToken event of AdmTokenService.
  127. /// </summary>
  128. /// <param name="accessToken">The token instance.</param>
  129. /// <param name="ex">Coressponding exception if failed to get the access token.</param>
  130. /// <param name="callback">callback from event</param>
  131. private void TokenService_GetAdmAccessTokenCompleteEvent(AdmAccessToken accessToken, Exception ex, RetriveAccessTokenComplete callback)
  132. {
  133. if (ex == null)
  134. {
  135. string token = string.Empty;
  136. if (!string.IsNullOrEmpty(this.RegistrationId) &&
  137. !string.IsNullOrEmpty(this.SecretKey))
  138. {
  139. token = string.Format("BEARER {0} {1} {2}", accessToken.AccessToken, this.SecretKey, this.RegistrationId);
  140. }
  141. else
  142. {
  143. token = string.Format("BEARER {0}", accessToken.AccessToken);
  144. }
  145. this.OnRetriveAccessTokenComplete(token, ex, callback);
  146. }
  147. else
  148. {
  149. this.OnRetriveAccessTokenComplete(string.Empty, ex, callback);
  150. }
  151. }
  152. }
  153. }