暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UniWebViewAuthenticationStandardToken.cs 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // UniWebViewAuthenticationStandardToken.cs
  3. // Created by Wang Wei (@onevcat) on 2022-06-25.
  4. //
  5. // This file is a part of UniWebView Project (https://uniwebview.com)
  6. // By purchasing the asset, you are allowed to use this code in as many as projects
  7. // you want, only if you publish the final products under the name of the same account
  8. // used for the purchase.
  9. //
  10. // This asset and all corresponding files (such as source code) are provided on an
  11. // “as is” basis, without warranty of any kind, express of implied, including but not
  12. // limited to the warranties of merchantability, fitness for a particular purpose, and
  13. // noninfringement. In no event shall the authors or copyright holders be liable for any
  14. // claim, damages or other liability, whether in action of contract, tort or otherwise,
  15. // arising from, out of or in connection with the software or the use of other dealing in the software.
  16. //
  17. using System;
  18. using UnityEngine;
  19. /// <summary>
  20. /// Represents the standard token used in the OAuth 2 process.
  21. /// </summary>
  22. [Serializable]
  23. public class UniWebViewAuthenticationStandardToken {
  24. // Unity's JsonUtility.FromJson is quite stupid on this.
  25. // Switch to Newtonsoft.Json when we can support from Unity 2021.
  26. [SerializeField]
  27. private string access_token = default;
  28. /// <summary>
  29. /// The access token retrieved from the service provider.
  30. ///
  31. /// This usually comes from the `access_token` field in the response.
  32. /// Use this token to access the service provider's API.
  33. ///
  34. /// If you do not need the token "offline", just use it and discard. UniWebView will not store this token, if you
  35. /// need to keep it for other purpose, please make sure you do not violate any policies and put it to a secure
  36. /// place yourself.
  37. /// </summary>
  38. public string AccessToken => access_token;
  39. [SerializeField]
  40. private string scope = default;
  41. /// <summary>
  42. /// The granted scopes of the token. This is usually comes from the `scope` field in the response.
  43. ///
  44. /// If there are optional scopes in the initial auth request, the user can choose to not give you some of the
  45. /// permissions. Check this field before you use the access token to perform certain actions to avoid failure
  46. /// before actual attempts.
  47. /// </summary>
  48. public string Scope => scope;
  49. [SerializeField]
  50. private string token_type = default;
  51. /// <summary>
  52. /// The token type. This usually comes from the `token_type` field in the response.
  53. ///
  54. /// For most OAuth 2.0 services, it is fixed to `Bearer`.
  55. /// </summary>
  56. public string TokenType => token_type;
  57. [SerializeField]
  58. private string refresh_token = default;
  59. /// <summary>
  60. /// The refresh token retrieved from the service provider. This usually comes from the `refresh_token` field in the
  61. /// response.
  62. ///
  63. /// If the access token is refreshable, you can use this
  64. /// refresh token to perform a refresh operation and get a new access token without the user's consent again.
  65. ///
  66. /// The refresh policy can be different from the service providers. Read the documentation of the service provider
  67. /// to determine the use of refresh token.
  68. ///
  69. /// If the response does not contain a refresh token, this field will be `null`.
  70. /// </summary>
  71. public string RefreshToken => refresh_token;
  72. [SerializeField]
  73. private long expires_in = default;
  74. /// <summary>
  75. /// How long does this token remain valid. This usually comes from the `expires_in` field in the response.
  76. /// </summary>
  77. public long ExpiresIn => expires_in;
  78. [SerializeField]
  79. private string id_token = default;
  80. /// <summary>
  81. /// The ID token retrieved from the service provider. This usually comes from the `id_token` field in the response.
  82. ///
  83. /// If the service provider does not support ID token or you did not apply for it, this field will be `null`.
  84. /// The ID token is usually a JWT token that contains information about the user.
  85. /// </summary>
  86. public string IdToken => id_token;
  87. /// <summary>
  88. /// The raw value of the response of the exchange token request.
  89. ///
  90. /// If the predefined fields are not enough, you can parse the raw value to get the extra information.
  91. /// </summary>
  92. public string RawValue { get; set; }
  93. }
  94. /// <summary>
  95. /// Util class to generate the standard token from a JSON based exchange token response.
  96. /// </summary>
  97. /// <typeparam name="TToken">The type of target token.</typeparam>
  98. public abstract class UniWebViewAuthenticationTokenFactory<TToken> where TToken : UniWebViewAuthenticationStandardToken {
  99. public static TToken Parse(string result) {
  100. var json = JsonUtility.FromJson<TToken>(result);
  101. json.RawValue = result;
  102. if (json.AccessToken == null) {
  103. throw AuthenticationResponseException.InvalidResponse(result);
  104. }
  105. return json;
  106. }
  107. }