GuidAuthClientIdentity.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // -
  2. // <copyright file="GuidAuthClientIdentity.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.Text;
  10. /// <summary>
  11. /// The Hawaii Guid authentication client identity.
  12. /// </summary>
  13. public class GuidAuthClientIdentity : ClientIdentity
  14. {
  15. /// <summary>
  16. /// Initializes a new instance of the GuidAuthClientIdentity class.
  17. /// </summary>
  18. /// <param name="applicationId">The hawaii application Id.</param>
  19. public GuidAuthClientIdentity(string applicationId) :
  20. this(applicationId, string.Empty, string.Empty)
  21. {
  22. }
  23. /// <summary>
  24. /// Initializes a new instance of the GuidAuthClientIdentity class.
  25. /// </summary>
  26. /// <param name="applicationId">The hawaii application Id.</param>
  27. /// <param name="registrationId">The registration Id.</param>
  28. /// <param name="secretKey">The secret key.</param>
  29. public GuidAuthClientIdentity(string applicationId, string registrationId, string secretKey) :
  30. base(registrationId, secretKey)
  31. {
  32. this.ApplicationId = applicationId;
  33. }
  34. /// <summary>
  35. /// Gets the Hawaii application Id.
  36. /// </summary>
  37. public string ApplicationId { get; private set; }
  38. /// <summary>
  39. /// Override the method to copy Guid authentication identity
  40. /// </summary>
  41. /// <returns>Returns the client identity</returns>
  42. public override ClientIdentity Copy()
  43. {
  44. return new GuidAuthClientIdentity(this.ApplicationId, this.RegistrationId, this.SecretKey);
  45. }
  46. /// <summary>
  47. /// Override the method to retrive the access token for hawaii Guid authentication.
  48. /// </summary>
  49. /// <param name="callback">callback from event</param>
  50. public override void RetriveAccessToken(RetriveAccessTokenComplete callback)
  51. {
  52. string token = string.Empty;
  53. if (!string.IsNullOrEmpty(this.RegistrationId) &&
  54. !string.IsNullOrEmpty(this.SecretKey))
  55. {
  56. if (!string.IsNullOrEmpty(this.ApplicationId))
  57. {
  58. // If this app token is not empty, take only id and secret key
  59. token = string.Format("{0}:{1}:{2}", this.ApplicationId, this.RegistrationId, this.SecretKey);
  60. }
  61. else
  62. {
  63. token = string.Format("{0}:{1}", this.RegistrationId, this.SecretKey);
  64. }
  65. }
  66. else if (!string.IsNullOrEmpty(this.ApplicationId))
  67. {
  68. token = this.ApplicationId;
  69. }
  70. if (!string.IsNullOrEmpty(token))
  71. {
  72. this.OnRetriveAccessTokenComplete(string.Format("Basic {0}", Convert.ToBase64String(Encoding.UTF8.GetBytes(token))), null, callback);
  73. }
  74. else
  75. {
  76. this.OnRetriveAccessTokenComplete(string.Empty, new InvalidOperationException("Hawaii Guid authentication token cannot be empty string"), callback);
  77. }
  78. }
  79. }
  80. }