Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions GoogleSignIn/Sources/GIDEMMSupport.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ typedef NS_ENUM(NSInteger, ErrorCode) {
ErrorCodeAppVerificationRequired,
};

@interface GIDEMMSupport ()

+ (NSDictionary<NSString *, NSString *> *)
dictionaryWithStringValuesFromDictionary:(NSDictionary *)originalDictionary;

@end

@implementation GIDEMMSupport

- (instancetype)init {
Expand Down Expand Up @@ -115,9 +122,11 @@ + (NSDictionary *)parametersWithParameters:(NSDictionary *)parameters
#pragma mark - GTMAuthSessionDelegate

- (nullable NSDictionary<NSString *,NSString *> *)
additionalTokenRefreshParametersForAuthSession:(GTMAuthSession *)authSession {
return [GIDEMMSupport updatedEMMParametersWithParameters:
authSession.authState.lastTokenResponse.additionalParameters];
additionalTokenRefreshParametersForAuthSession:(GTMAuthSession *)authSession {
NSDictionary *additionalParameters = authSession.authState.lastTokenResponse.additionalParameters;
NSDictionary *updatedAdditionalParameters =
[GIDEMMSupport updatedEMMParametersWithParameters:additionalParameters];
return [GIDEMMSupport dictionaryWithStringValuesFromDictionary:updatedAdditionalParameters];
}

- (void)updateErrorForAuthSession:(GTMAuthSession *)authSession
Expand All @@ -128,6 +137,48 @@ - (void)updateErrorForAuthSession:(GTMAuthSession *)authSession
}];
}

#pragma mark - Private Helpers

+ (NSDictionary<NSString *, NSString *> *)
dictionaryWithStringValuesFromDictionary:(NSDictionary *)originalDictionary {
NSMutableDictionary<NSString *, NSString *> *stringifiedDictionary =
[NSMutableDictionary dictionaryWithCapacity:originalDictionary.count];

[originalDictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
// Case 1: The value is already of `NSString` type.
if ([value isKindOfClass:[NSString class]]) {
stringifiedDictionary[key] = value;
return;
}

// Case 2: The value is of `NSNumber` type (which includes `BOOL` type).
if ([value isKindOfClass:[NSNumber class]]) {
if (CFGetTypeID((__bridge CFTypeRef)value) == CFBooleanGetTypeID()) {
stringifiedDictionary[key] = [value boolValue] ? @"true" : @"false";
} else {
stringifiedDictionary[key] = [value stringValue];
}
return;
}

// Case 3: The value is of NSArray or NSDictionary type.
// To satisfy `GTMAppAuth`'s requirement for [String: String] parameters, the entire
// object is serialized into a single JSON string.
if ([NSJSONSerialization isValidJSONObject:value]) {
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:value options:0 error:&error];

if (jsonData && !error) {
stringifiedDictionary[key] = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
} else {
stringifiedDictionary[key] = [value description];
}
return;
}
}];
return stringifiedDictionary;
}

@end

NS_ASSUME_NONNULL_END
Expand Down
38 changes: 38 additions & 0 deletions GoogleSignIn/Sources/GIDJSONSerializer/API/GIDJSONSerializer.h
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
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
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
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
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
Loading
Loading