AppCheckRecaptchaEnterpriseProvider.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import Foundation
  2. /*
  3. * Copyright 2021 Google LLC
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import FirebaseCore
  18. import RecaptchaEnterpriseProvider
  19. import AppCheckCore
  20. import FirebaseAppCheckCore
  21. class AppCheckRecaptchaEnterpriseProvider:NSObject, AppCheckProvider{
  22. private let recaptchaEnterpriseProvider: AppCheckCoreRecaptchaEnterpriseProvider
  23. init(recaptchaEnterpriseProvider: AppCheckCoreRecaptchaEnterpriseProvider) {
  24. self.recaptchaEnterpriseProvider = recaptchaEnterpriseProvider
  25. super.init();
  26. }
  27. convenience init(app: FirebaseApp,siteKey:String) {
  28. let missingOptionsFields = AppCheckValidator.tokenExchangeMissingFields(in: app.options)
  29. let recaptchaEnterpriseProvider = AppCheckCoreRecaptchaEnterpriseProvider(
  30. siteKey:siteKey,
  31. resourceName:app.resourceName,
  32. APIKey: app.options.apiKey!,
  33. requestHooks:[app.heartbeatLogger.requestHook()] //TODO: Add HeartBeatLogger
  34. )
  35. self.init(recaptchaEnterpriseProvider:recaptchaEnterpriseProvider)
  36. if !missingOptionsFields.isEmpty {
  37. return
  38. }
  39. }
  40. func getToken(completion handler: @escaping(AppCheckToken?, Error?)->Void){
  41. recaptchaEnterpriseProvider.getToken{
  42. token,
  43. error in
  44. if let error = error {
  45. handler(nil,error)
  46. return
  47. }
  48. if let token=token{
  49. handler(AppCheckToken(token: token.token, expirationDate: token.expirationDate),nil)
  50. }else{
  51. handler(
  52. nil,
  53. NSError(domain:"AppCheckProviderError",code:-1,userInfo:[NSLocalizedDescriptionKey:"Internal token missing without an error"])
  54. )
  55. }
  56. }
  57. }
  58. func getLimitedUseToken(completion handler: @escaping(AppCheckToken?,Error?)->Void){
  59. recaptchaEnterpriseProvider.getLimitedUseToken {token,error in
  60. if let error=error{
  61. handler(nil,error)
  62. return
  63. }
  64. if let token=token{
  65. handler(AppCheckToken(token:token.token,expirationDate:token.expirationDate),nil)
  66. }else{
  67. handler(
  68. nil,
  69. NSError(domain:"AppCheckProviderError",code:-1,userInfo:[NSLocalizedDescriptionKey:"Internal token missing without an error"])
  70. )
  71. }
  72. }
  73. }
  74. }