-
Notifications
You must be signed in to change notification settings - Fork 248
GIDTokenClaimsInternalOptions Implementation + Unit Tests #550 #552
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
Merged
AkshatG6
merged 7 commits into
gandhiakshat/sbc-auth_time
from
gandhiakshat/sbc-token_claims-internal-options
Sep 9, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1b43e18
Added GIDTokenClaimsInternalOptions Implementation + Unit Tests
AkshatG6 e9454a6
Added header files to GIDTokenClaimsInternalOptions
AkshatG6 351afc4
Replaced OCMock with Fake impl to support unit testing within GIDToke…
AkshatG6 c7a7fb5
Updated variable names
AkshatG6 3c02be4
Removed nil from error initialization in GIDTokenClaimsInternalOption…
AkshatG6 7c12f3f
Updated GIDTokenClaimsInternalOptionsTest to test correct expected er…
AkshatG6 8bd3bf3
Updated GIDFakeJSONSerializerImpl to accept a seeded error instead of…
AkshatG6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
GoogleSignIn/Sources/GIDJSONSerializer/API/GIDJSONSerializer.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright 2025 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> | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| /** | ||
| * A protocol for serializing an `NSDictionary` into a JSON string. | ||
| */ | ||
| @protocol GIDJSONSerializer <NSObject> | ||
|
|
||
| /** | ||
| * Serializes the given dictionary into a `JSON` string. | ||
| * | ||
| * @param jsonObject The dictionary to be serialized. | ||
| * @param error A pointer to an `NSError` object to be populated upon failure. | ||
| * @return A `JSON` string representation of the dictionary, or `nil` if an error occurs. | ||
| */ | ||
| - (nullable NSString *)stringWithJSONObject:(NSDictionary<NSString *, id> *)jsonObject | ||
| error:(NSError *_Nullable *_Nullable)error; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END |
37 changes: 37 additions & 0 deletions
37
GoogleSignIn/Sources/GIDJSONSerializer/Fake/GIDFakeJSONSerializerImpl.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright 2025 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 "GoogleSignIn/Sources/GIDJSONSerializer/API/GIDJSONSerializer.h" | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| /** A fake implementation of `GIDJSONSerializer` for testing purposes. */ | ||
| @interface GIDFakeJSONSerializerImpl : NSObject <GIDJSONSerializer> | ||
|
|
||
| /** | ||
| * An error to be returned by `stringWithJSONObject:error:`. | ||
| * | ||
| * If this property is set, `stringWithJSONObject:error:` will return `nil` and | ||
| * populate the error parameter with this error. | ||
| */ | ||
| @property(nonatomic, nullable) NSError *serializationError; | ||
|
|
||
| /** The dictionary passed to the serialization method. */ | ||
| @property(nonatomic, readonly, nullable) NSDictionary<NSString *, id> *capturedJSONObject; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END |
46 changes: 46 additions & 0 deletions
46
GoogleSignIn/Sources/GIDJSONSerializer/Fake/GIDFakeJSONSerializerImpl.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Copyright 2025 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 "GoogleSignIn/Sources/GIDJSONSerializer/Fake/GIDFakeJSONSerializerImpl.h" | ||
|
|
||
| #import "GoogleSignIn/Sources/GIDJSONSerializer/Implementation/GIDJSONSerializerImpl.h" | ||
| #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h" | ||
|
|
||
| @implementation GIDFakeJSONSerializerImpl | ||
|
|
||
| - (nullable NSString *)stringWithJSONObject:(NSDictionary<NSString *, id> *)jsonObject | ||
| error:(NSError *_Nullable *_Nullable)error { | ||
| _capturedJSONObject = [jsonObject copy]; | ||
|
|
||
| // Check if a serialization error should be simulated. | ||
| if (self.serializationError) { | ||
| if (error) { | ||
| *error = self.serializationError; | ||
| } | ||
| return nil; | ||
| } | ||
|
|
||
| // If not failing, fall back to the real serialization path. | ||
| NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonObject | ||
| options:0 | ||
| error:error]; | ||
| if (!jsonData) { | ||
| return nil; | ||
| } | ||
| return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; | ||
| } | ||
|
|
||
| @end |
26 changes: 26 additions & 0 deletions
26
GoogleSignIn/Sources/GIDJSONSerializer/Implementation/GIDJSONSerializerImpl.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * Copyright 2025 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 "GoogleSignIn/Sources/GIDJSONSerializer/API/GIDJSONSerializer.h" | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| extern NSString *const kGIDJSONSerializationErrorDescription; | ||
|
|
||
| @interface GIDJSONSerializerImpl : NSObject <GIDJSONSerializer> | ||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END |
46 changes: 46 additions & 0 deletions
46
GoogleSignIn/Sources/GIDJSONSerializer/Implementation/GIDJSONSerializerImpl.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Copyright 2025 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 "GoogleSignIn/Sources/GIDJSONSerializer/Implementation/GIDJSONSerializerImpl.h" | ||
|
|
||
| #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h" | ||
|
|
||
| NSString * const kGIDJSONSerializationErrorDescription = | ||
| @"The provided object could not be serialized to a JSON string."; | ||
|
|
||
| @implementation GIDJSONSerializerImpl | ||
|
|
||
| - (nullable NSString *)stringWithJSONObject:(NSDictionary<NSString *, id> *)jsonObject | ||
| error:(NSError *_Nullable *_Nullable)error { | ||
| NSError *serializationError; | ||
| NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonObject | ||
| options:0 | ||
| error:&serializationError]; | ||
| if (!jsonData) { | ||
| if (error) { | ||
| *error = [NSError errorWithDomain:kGIDSignInErrorDomain | ||
| code:kGIDSignInErrorCodeJSONSerializationFailure | ||
| userInfo:@{ | ||
| NSLocalizedDescriptionKey:kGIDJSONSerializationErrorDescription, | ||
| NSUnderlyingErrorKey:serializationError | ||
| }]; | ||
| } | ||
| return nil; | ||
| } | ||
| return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; | ||
| } | ||
|
|
||
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Copyright 2025 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> | ||
|
|
||
| @class GIDTokenClaim; | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| extern NSString *const kGIDTokenClaimErrorDescription; | ||
| extern NSString *const kGIDTokenClaimEssentialPropertyKeyName; | ||
| extern NSString *const kGIDTokenClaimKeyName; | ||
|
|
||
| @protocol GIDJSONSerializer; | ||
|
|
||
| /** | ||
| * An internal utility class for processing and serializing the `NSSet` of `GIDTokenClaim` objects | ||
| * into the `JSON` format required for an `OIDAuthorizationRequest`. | ||
| */ | ||
| @interface GIDTokenClaimsInternalOptions : NSObject | ||
|
|
||
| - (instancetype)init; | ||
|
|
||
| - (instancetype)initWithJSONSerializer: | ||
| (id<GIDJSONSerializer>)jsonSerializer NS_DESIGNATED_INITIALIZER; | ||
|
|
||
| /** | ||
| * Processes the `NSSet` of `GIDTokenClaim` objects, handling ambiguous claims, | ||
| * and returns a `JSON` string. | ||
| * | ||
| * @param claims The `NSSet` of `GIDTokenClaim` objects provided by the developer. | ||
| * @param error A pointer to an `NSError` object to be populated if an error occurs (e.g., if a | ||
| * claim is requested as both essential and non-essential). | ||
| * @return A `JSON` string representing the claims request, or `nil` if the input is empty or an | ||
| * error occurs. | ||
| */ | ||
| - (nullable NSString *)validatedJSONStringForClaims:(nullable NSSet<GIDTokenClaim *> *)claims | ||
| error:(NSError *_Nullable *_Nullable)error; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * Copyright 2025 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 "GoogleSignIn/Sources/GIDTokenClaimsInternalOptions.h" | ||
|
|
||
| #import "GoogleSignIn/Sources/GIDJSONSerializer/API/GIDJSONSerializer.h" | ||
| #import "GoogleSignIn/Sources/GIDJSONSerializer/Implementation/GIDJSONSerializerImpl.h" | ||
| #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h" | ||
| #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDTokenClaim.h" | ||
|
|
||
| NSString * const kGIDTokenClaimErrorDescription = | ||
| @"The claim was requested as both essential and non-essential. " | ||
| @"Please provide only one version."; | ||
| NSString * const kGIDTokenClaimEssentialPropertyKey = @"essential"; | ||
| NSString * const kGIDTokenClaimKeyName = @"id_token"; | ||
|
|
||
| @interface GIDTokenClaimsInternalOptions () | ||
| @property(nonatomic, readonly) id<GIDJSONSerializer> jsonSerializer; | ||
| @end | ||
|
|
||
| @implementation GIDTokenClaimsInternalOptions | ||
|
|
||
| - (instancetype)init { | ||
| return [self initWithJSONSerializer:[[GIDJSONSerializerImpl alloc] init]]; | ||
| } | ||
|
|
||
| - (instancetype)initWithJSONSerializer:(id<GIDJSONSerializer>)jsonSerializer { | ||
| if (self = [super init]) { | ||
| _jsonSerializer = jsonSerializer; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| - (nullable NSString *)validatedJSONStringForClaims:(nullable NSSet<GIDTokenClaim *> *)claims | ||
| error:(NSError *_Nullable *_Nullable)error { | ||
| if (!claims || claims.count == 0) { | ||
| return nil; | ||
| } | ||
|
|
||
| // === Step 1: Check for claims with ambiguous essential property. === | ||
| NSMutableDictionary<NSString *, GIDTokenClaim *> *validTokenClaims = | ||
| [[NSMutableDictionary alloc] init]; | ||
|
|
||
| for (GIDTokenClaim *currentClaim in claims) { | ||
| GIDTokenClaim *existingClaim = validTokenClaims[currentClaim.name]; | ||
|
|
||
| // Check for a conflict: a claim with the same name but different essentiality. | ||
| if (existingClaim && existingClaim.isEssential != currentClaim.isEssential) { | ||
| if (error) { | ||
| *error = [NSError errorWithDomain:kGIDSignInErrorDomain | ||
| code:kGIDSignInErrorCodeAmbiguousClaims | ||
| userInfo:@{ | ||
| NSLocalizedDescriptionKey:kGIDTokenClaimErrorDescription | ||
| }]; | ||
| } | ||
| return nil; | ||
| } | ||
| validTokenClaims[currentClaim.name] = currentClaim; | ||
| } | ||
|
|
||
| // === Step 2: Build the dictionary structure required for OIDC JSON === | ||
| NSMutableDictionary<NSString *, NSDictionary *> *tokenClaimsDictionary = | ||
| [[NSMutableDictionary alloc] init]; | ||
| for (GIDTokenClaim *claim in validTokenClaims.allValues) { | ||
| if (claim.isEssential) { | ||
| tokenClaimsDictionary[claim.name] = @{ kGIDTokenClaimEssentialPropertyKey: @YES }; | ||
| } else { | ||
| tokenClaimsDictionary[claim.name] = @{ kGIDTokenClaimEssentialPropertyKey: @NO }; | ||
| } | ||
| } | ||
| NSDictionary<NSString *, id> *finalRequestDictionary = | ||
| @{ kGIDTokenClaimKeyName: tokenClaimsDictionary }; | ||
|
|
||
| // === Step 3: Serialize the final dictionary into a JSON string === | ||
| return [_jsonSerializer stringWithJSONObject:finalRequestDictionary error:error]; | ||
| } | ||
|
|
||
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.