Няма описание
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.

AppleAuthSerializer.m 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // MIT License
  3. //
  4. // Copyright (c) 2019 Daniel Lupiañez Casares
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in all
  14. // copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. // SOFTWARE.
  23. //
  24. #import "AppleAuthSerializer.h"
  25. @implementation AppleAuthSerializer
  26. + (NSDictionary *) dictionaryForNSError:(NSError *)error
  27. {
  28. if (!error)
  29. return nil;
  30. NSMutableDictionary *result = [NSMutableDictionary dictionary];
  31. [result setValue:@([error code]) forKey:@"_code"];
  32. [result setValue:[error domain] forKey:@"_domain"];
  33. [result setValue:[error localizedDescription] forKey:@"_localizedDescription"];
  34. [result setValue:[error localizedRecoveryOptions] forKey:@"_localizedRecoveryOptions"];
  35. [result setValue:[error localizedRecoverySuggestion] forKey:@"_localizedRecoverySuggestion"];
  36. [result setValue:[error localizedFailureReason] forKey:@"_localizedFailureReason"];
  37. return [result copy];
  38. }
  39. + (NSDictionary *) credentialResponseDictionaryForCredentialState:(NSNumber *)credentialStateNumber
  40. errorDictionary:(NSDictionary *)errorDictionary
  41. {
  42. NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
  43. [result setValue:@(errorDictionary == nil) forKey:@"_success"];
  44. [result setValue:@(credentialStateNumber != nil) forKey:@"_hasCredentialState"];
  45. [result setValue:@(errorDictionary != nil) forKey:@"_hasError"];
  46. [result setValue:credentialStateNumber forKey:@"_credentialState"];
  47. [result setValue:errorDictionary forKey:@"_error"];
  48. return [result copy];
  49. }
  50. + (NSDictionary *) loginResponseDictionaryForAppleIdCredentialDictionary:(NSDictionary *)appleIdCredentialDictionary
  51. passwordCredentialDictionary:(NSDictionary *)passwordCredentialDictionary
  52. errorDictionary:(NSDictionary *)errorDictionary
  53. {
  54. NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
  55. [result setValue:@(errorDictionary == nil) forKey:@"_success"];
  56. [result setValue:@(appleIdCredentialDictionary != nil) forKey:@"_hasAppleIdCredential"];
  57. [result setValue:@(passwordCredentialDictionary != nil) forKey:@"_hasPasswordCredential"];
  58. [result setValue:@(errorDictionary != nil) forKey:@"_hasError"];
  59. [result setValue:appleIdCredentialDictionary forKey:@"_appleIdCredential"];
  60. [result setValue:passwordCredentialDictionary forKey:@"_passwordCredential"];
  61. [result setValue:errorDictionary forKey:@"_error"];
  62. return [result copy];
  63. }
  64. // IOS/TVOS 9.0 | MACOS 10.11
  65. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000 || __TV_OS_VERSION_MAX_ALLOWED >= 90000 || __MAC_OS_X_VERSION_MAX_ALLOWED >= 101100
  66. + (NSDictionary *) dictionaryForNSPersonNameComponents:(NSPersonNameComponents *)nameComponents
  67. {
  68. if (!nameComponents)
  69. return nil;
  70. // Sometimes, when not requesting a name in the ASAuthorizationAppleIDRequest scopes,
  71. // Apple will just send an empty NSPersonNameComponents instance...
  72. // This should be treated as a nil person name components
  73. if ([nameComponents namePrefix] == nil &&
  74. [nameComponents givenName] == nil &&
  75. [nameComponents middleName] == nil &&
  76. [nameComponents familyName] == nil &&
  77. [nameComponents nameSuffix] == nil &&
  78. [nameComponents nickname] == nil &&
  79. [nameComponents phoneticRepresentation] == nil)
  80. return nil;
  81. NSMutableDictionary *result = [NSMutableDictionary dictionary];
  82. [result setValue:[nameComponents namePrefix] forKey:@"_namePrefix"];
  83. [result setValue:[nameComponents givenName] forKey:@"_givenName"];
  84. [result setValue:[nameComponents middleName] forKey:@"_middleName"];
  85. [result setValue:[nameComponents familyName] forKey:@"_familyName"];
  86. [result setValue:[nameComponents nameSuffix] forKey:@"_nameSuffix"];
  87. [result setValue:[nameComponents nickname] forKey:@"_nickname"];
  88. NSDictionary *phoneticRepresentationDictionary = [AppleAuthSerializer dictionaryForNSPersonNameComponents:[nameComponents phoneticRepresentation]];
  89. [result setValue:@(phoneticRepresentationDictionary != nil) forKey:@"_hasPhoneticRepresentation"];
  90. [result setValue:phoneticRepresentationDictionary forKey:@"_phoneticRepresentation"];
  91. return [result copy];
  92. }
  93. #endif
  94. // IOS/TVOS 13.0 | MACOS 10.15
  95. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 || __TV_OS_VERSION_MAX_ALLOWED >= 130000 || __MAC_OS_X_VERSION_MAX_ALLOWED >= 101500
  96. + (NSDictionary *) dictionaryForASAuthorizationAppleIDCredential:(ASAuthorizationAppleIDCredential *)appleIDCredential
  97. {
  98. if (!appleIDCredential)
  99. return nil;
  100. NSMutableDictionary *result = [NSMutableDictionary dictionary];
  101. [result setValue:[[appleIDCredential identityToken] base64EncodedStringWithOptions:0] forKey:@"_base64IdentityToken"];
  102. [result setValue:[[appleIDCredential authorizationCode] base64EncodedStringWithOptions:0] forKey:@"_base64AuthorizationCode"];
  103. [result setValue:[appleIDCredential state] forKey:@"_state"];
  104. [result setValue:[appleIDCredential user] forKey:@"_user"];
  105. [result setValue:[appleIDCredential authorizedScopes] forKey:@"_authorizedScopes"];
  106. [result setValue:[appleIDCredential email] forKey:@"_email"];
  107. [result setValue:@([appleIDCredential realUserStatus]) forKey:@"_realUserStatus"];
  108. NSDictionary *fullNameDictionary = [AppleAuthSerializer dictionaryForNSPersonNameComponents:[appleIDCredential fullName]];
  109. [result setValue:@(fullNameDictionary != nil) forKey:@"_hasFullName"];
  110. [result setValue:fullNameDictionary forKey:@"_fullName"];
  111. return [result copy];
  112. }
  113. + (NSDictionary *) dictionaryForASPasswordCredential:(ASPasswordCredential *)passwordCredential
  114. {
  115. if (!passwordCredential)
  116. return nil;
  117. NSMutableDictionary *result = [NSMutableDictionary dictionary];
  118. [result setValue:[passwordCredential user] forKey:@"_user"];
  119. [result setValue:[passwordCredential password] forKey:@"_password"];
  120. return [result copy];
  121. }
  122. #endif
  123. @end