-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(appcheck): implement RecaptchaEnterpriseProvider and tests #16150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4be9ef3
6b4f4c4
cbc9333
455549f
9d5f030
2df1dd1
fe303d8
39c4d64
e3f36aa
e8c8250
7834a05
868fa0b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #import <Foundation/Foundation.h> | ||
| #import "FIRAppCheckProviderFactory.h" | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| NS_SWIFT_NAME(AppCheckRecaptchaEnterpriseProviderFactory) | ||
| @interface FIRAppCheckRecaptchaEnterpriseProviderFactory : NSObject <FIRAppCheckProviderFactory> | ||
|
|
||
| - (instancetype)init NS_UNAVAILABLE; | ||
| - (instancetype)initWithSiteKey:(NSString *)siteKey; | ||
|
|
||
| @end | ||
| NS_ASSUME_NONNULL_END |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #import <Foundation/Foundation.h> | ||
| #import "FIRAppCheckAvailability.h" | ||
| #import "FIRAppCheckProvider.h" | ||
|
|
||
| @class FIRApp; | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| NS_SWIFT_NAME(RecaptchaEnterpriseProvider) | ||
| @interface FIRRecaptchaEnterpriseProvider : NSObject <FIRAppCheckProvider> | ||
|
|
||
| - (instancetype)init NS_UNAVAILABLE; | ||
| - (nullable instancetype)initWithApp:(FIRApp *)app siteKey:(NSString *)siteKey; | ||
|
|
||
| @end | ||
| NS_ASSUME_NONNULL_END |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckRecaptchaEnterpriseProviderFactory.h" | ||
|
|
||
| #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheck.h" | ||
| #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRRecaptchaEnterpriseProvider.h" | ||
|
|
||
| @interface FIRAppCheckRecaptchaEnterpriseProviderFactory () | ||
|
|
||
| @property(nonatomic, readonly) NSString *siteKey; | ||
|
|
||
| @end | ||
|
|
||
| @implementation FIRAppCheckRecaptchaEnterpriseProviderFactory | ||
|
|
||
| - (instancetype)initWithSiteKey:(NSString *)siteKey { | ||
| NSParameterAssert(siteKey.length > 0); | ||
| self = [super init]; | ||
|
|
||
| if (self) { | ||
| _siteKey = [siteKey copy]; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| - (nullable id<FIRAppCheckProvider>)createProviderWithApp:(nonnull FIRApp *)app { | ||
| return [[FIRRecaptchaEnterpriseProvider alloc] initWithApp:app siteKey:self.siteKey]; | ||
| } | ||
|
|
||
| @end | ||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,106 @@ | ||||||||||||
| /* | ||||||||||||
| * Copyright 2026 Google LLC | ||||||||||||
| * | ||||||||||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||
| * you may not use this file except in compliance with the License. | ||||||||||||
| * You may obtain a copy of the License at | ||||||||||||
| * | ||||||||||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||
| * | ||||||||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||
| * See the License for the specific language governing permissions and | ||||||||||||
| * limitations under the License. | ||||||||||||
| */ | ||||||||||||
|
|
||||||||||||
| #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRRecaptchaEnterpriseProvider.h" | ||||||||||||
| #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckAvailability.h" | ||||||||||||
|
|
||||||||||||
| #import <AppCheckCore/AppCheckCore.h> | ||||||||||||
|
|
||||||||||||
| @import RecaptchaEnterpriseProvider; | ||||||||||||
|
|
||||||||||||
| #import "FirebaseAppCheck/Sources/Core/FIRApp+AppCheck.h" | ||||||||||||
|
|
||||||||||||
| #import "FirebaseAppCheck/Sources/Core/FIRAppCheckLogger.h" | ||||||||||||
| #import "FirebaseAppCheck/Sources/Core/FIRAppCheckToken+Internal.h" | ||||||||||||
| #import "FirebaseAppCheck/Sources/Core/FIRAppCheckValidator.h" | ||||||||||||
| #import "FirebaseAppCheck/Sources/Core/FIRHeartbeatLogger+AppCheck.h" | ||||||||||||
|
|
||||||||||||
| #import "FirebaseCore/Extension/FirebaseCoreInternal.h" | ||||||||||||
|
|
||||||||||||
| @interface FIRRecaptchaEnterpriseProvider () | ||||||||||||
|
|
||||||||||||
| @property(nonatomic, readonly) GACRecaptchaEnterpriseProvider *recaptchaEnterpriseProvider; | ||||||||||||
|
|
||||||||||||
| @end | ||||||||||||
|
|
||||||||||||
| @implementation FIRRecaptchaEnterpriseProvider | ||||||||||||
|
|
||||||||||||
| - (instancetype)initWithRecaptchaEnterpriseProvider: | ||||||||||||
| (GACRecaptchaEnterpriseProvider *)recaptchaEnterpriseProvider { | ||||||||||||
| self = [super init]; | ||||||||||||
| if (self) { | ||||||||||||
| _recaptchaEnterpriseProvider = recaptchaEnterpriseProvider; | ||||||||||||
| } | ||||||||||||
| return self; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| - (nullable instancetype)initWithApp:(FIRApp *)app siteKey:(NSString *)siteKey { | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is recommended to validate that - (nullable instancetype)initWithApp:(FIRApp *)app siteKey:(NSString *)siteKey {
if (siteKey.length == 0) {
return nil;
} |
||||||||||||
| if (siteKey.length == 0) { | ||||||||||||
| return nil; | ||||||||||||
| } | ||||||||||||
| NSArray<NSString *> *missingOptionsFields = | ||||||||||||
| [FIRAppCheckValidator tokenExchangeMissingFieldsInOptions:app.options]; | ||||||||||||
|
Comment on lines
+54
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is an unnecessary blank line and inconsistent indentation here. It's better to keep the assignment clean and follow standard continuation indentation.
Suggested change
|
||||||||||||
| if (missingOptionsFields.count > 0) { | ||||||||||||
| FIRLogError(kFIRLoggerAppCheck, | ||||||||||||
| kFIRLoggerAppCheckMessageRecaptchaEnterpriseProviderIncompleteFIROptions, | ||||||||||||
| @"Cannot instantiate `FIRRecaptchaEnterpriseProvider` for app: %@. The following " | ||||||||||||
| @"`FirebaseOptions` fields are missing: %@", | ||||||||||||
| app.name, [missingOptionsFields componentsJoinedByString:@", "]); | ||||||||||||
| return nil; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| id heartbeatHook = [app.heartbeatLogger requestHook]; | ||||||||||||
| GACRecaptchaEnterpriseProvider *recaptchaEnterpriseProvider = | ||||||||||||
| [[GACRecaptchaEnterpriseProvider alloc] | ||||||||||||
| initWithSiteKey:siteKey | ||||||||||||
| resourceName:app.resourceName | ||||||||||||
| APIKey:app.options.APIKey | ||||||||||||
| requestHooks:heartbeatHook ? @[ heartbeatHook ] : @[]]; | ||||||||||||
|
|
||||||||||||
| return [self initWithRecaptchaEnterpriseProvider:recaptchaEnterpriseProvider]; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #pragma mark - FIRAppCheckProvider | ||||||||||||
|
|
||||||||||||
| - (void)getTokenWithCompletion:(void (^)(FIRAppCheckToken *_Nullable token, | ||||||||||||
| NSError *_Nullable error))handler { | ||||||||||||
| [self.recaptchaEnterpriseProvider | ||||||||||||
| getTokenWithCompletion:^(GACAppCheckToken *_Nullable internalToken, | ||||||||||||
| NSError *_Nullable error) { | ||||||||||||
| if (error) { | ||||||||||||
| handler(nil, error); | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| handler([[FIRAppCheckToken alloc] initWithInternalToken:internalToken], nil); | ||||||||||||
|
ncooke3 marked this conversation as resolved.
|
||||||||||||
| }]; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| - (void)getLimitedUseTokenWithCompletion:(void (^)(FIRAppCheckToken *_Nullable, | ||||||||||||
| NSError *_Nullable))handler { | ||||||||||||
| [self.recaptchaEnterpriseProvider | ||||||||||||
| getLimitedUseTokenWithCompletion:^(GACAppCheckToken *_Nullable internalToken, | ||||||||||||
| NSError *_Nullable error) { | ||||||||||||
| if (error) { | ||||||||||||
| handler(nil, error); | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| handler([[FIRAppCheckToken alloc] initWithInternalToken:internalToken], nil); | ||||||||||||
| }]; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| @end | ||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #import <XCTest/XCTest.h> | ||
|
|
||
| #import <FirebaseAppCheck/FIRAppCheckRecaptchaEnterpriseProviderFactory.h> | ||
| #import <FirebaseAppCheck/FIRRecaptchaEnterpriseProvider.h> | ||
|
|
||
| #import "FirebaseCore/Extension/FirebaseCoreInternal.h" | ||
|
|
||
| @interface FIRAppCheckRecaptchaEnterpriseProviderFactoryTests : XCTestCase | ||
| @end | ||
|
|
||
| @implementation FIRAppCheckRecaptchaEnterpriseProviderFactoryTests | ||
|
|
||
| - (void)testCreateProviderWithApp { | ||
| FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"app_id" GCMSenderID:@"sender_id"]; | ||
| options.APIKey = @"api_key"; | ||
| options.projectID = @"project_id"; | ||
| FIRApp *app = [[FIRApp alloc] initInstanceWithName:@"testCreateProviderWithApp" options:options]; | ||
| app.dataCollectionDefaultEnabled = NO; | ||
|
|
||
| NSString *siteKey = @"test_site_key"; | ||
| FIRAppCheckRecaptchaEnterpriseProviderFactory *factory = | ||
| [[FIRAppCheckRecaptchaEnterpriseProviderFactory alloc] initWithSiteKey:siteKey]; | ||
|
|
||
| FIRRecaptchaEnterpriseProvider *createdProvider = [factory createProviderWithApp:app]; | ||
|
|
||
| XCTAssert([createdProvider isKindOfClass:[FIRRecaptchaEnterpriseProvider class]]); | ||
| } | ||
|
|
||
| @end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to the provider initialization, it is good practice to ensure the
siteKeyprovided to the factory is not empty.