Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
* limitations under the License.
*/

#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDTokenClaim.h"
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDClaim.h"

NSString * const kAuthTimeClaimName = @"auth_time";

// Private interface to declare the internal initializer
@interface GIDTokenClaim ()
@interface GIDClaim ()

- (instancetype)initWithName:(NSString *)name
essential:(BOOL)essential NS_DESIGNATED_INITIALIZER;

@end

@implementation GIDTokenClaim
@implementation GIDClaim

// Private designated initializer
- (instancetype)initWithName:(NSString *)name essential:(BOOL)essential {
Expand Down Expand Up @@ -57,12 +57,12 @@ - (BOOL)isEqual:(id)object {
}

// 2. Check if the other object is not a GIDTokenClaim instance.
if (![object isKindOfClass:[GIDTokenClaim class]]) {
if (![object isKindOfClass:[GIDClaim class]]) {
return NO;
}

// 3. Compare the properties that define equality.
GIDTokenClaim *other = (GIDTokenClaim *)object;
GIDClaim *other = (GIDClaim *)object;
return [self.name isEqualToString:other.name] &&
self.isEssential == other.isEssential;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,38 @@

#import <Foundation/Foundation.h>

@class GIDTokenClaim;
@class GIDClaim;

NS_ASSUME_NONNULL_BEGIN

extern NSString *const kGIDTokenClaimErrorDescription;
extern NSString *const kGIDTokenClaimEssentialPropertyKeyName;
extern NSString *const kGIDTokenClaimKeyName;
extern NSString *const kGIDClaimErrorDescription;
extern NSString *const kGIDClaimEssentialPropertyKeyName;
extern NSString *const kGIDClaimKeyName;

@protocol GIDJSONSerializer;

/**
* An internal utility class for processing and serializing the `NSSet` of `GIDTokenClaim` objects
* An internal utility class for processing and serializing the `NSSet` of `GIDClaim` objects
* into the `JSON` format required for an `OIDAuthorizationRequest`.
*/
@interface GIDTokenClaimsInternalOptions : NSObject
@interface GIDClaimsInternalOptions : NSObject

- (instancetype)init;

- (instancetype)initWithJSONSerializer:
(id<GIDJSONSerializer>)jsonSerializer NS_DESIGNATED_INITIALIZER;

/**
* Processes the `NSSet` of `GIDTokenClaim` objects, handling ambiguous claims,
* Processes the `NSSet` of `GIDClaim` objects, handling ambiguous claims,
* and returns a `JSON` string.
*
* @param claims The `NSSet` of `GIDTokenClaim` objects provided by the developer.
* @param claims The `NSSet` of `GIDClaim` 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
- (nullable NSString *)validatedJSONStringForClaims:(nullable NSSet<GIDClaim *> *)claims
error:(NSError *_Nullable *_Nullable)error;

@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@
* limitations under the License.
*/

#import "GoogleSignIn/Sources/GIDTokenClaimsInternalOptions.h"
#import "GoogleSignIn/Sources/GIDClaimsInternalOptions.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"
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDClaim.h"

NSString * const kGIDTokenClaimErrorDescription =
NSString * const kGIDClaimErrorDescription =
@"The claim was requested as both essential and non-essential. "
@"Please provide only one version.";
NSString * const kGIDTokenClaimEssentialPropertyKey = @"essential";
NSString * const kGIDTokenClaimKeyName = @"id_token";
NSString * const kGIDClaimEssentialPropertyKey = @"essential";
NSString * const kGIDClaimKeyName = @"id_token";

@interface GIDTokenClaimsInternalOptions ()
@interface GIDClaimsInternalOptions ()
@property(nonatomic, readonly) id<GIDJSONSerializer> jsonSerializer;
@end

@implementation GIDTokenClaimsInternalOptions
@implementation GIDClaimsInternalOptions

- (instancetype)init {
return [self initWithJSONSerializer:[[GIDJSONSerializerImpl alloc] init]];
Expand All @@ -44,45 +44,45 @@ - (instancetype)initWithJSONSerializer:(id<GIDJSONSerializer>)jsonSerializer {
return self;
}

- (nullable NSString *)validatedJSONStringForClaims:(nullable NSSet<GIDTokenClaim *> *)claims
- (nullable NSString *)validatedJSONStringForClaims:(nullable NSSet<GIDClaim *> *)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<NSString *, GIDClaim *> *validClaims =
[[NSMutableDictionary alloc] init];

for (GIDTokenClaim *currentClaim in claims) {
GIDTokenClaim *existingClaim = validTokenClaims[currentClaim.name];
for (GIDClaim *currentClaim in claims) {
GIDClaim *existingClaim = validClaims[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
NSLocalizedDescriptionKey:kGIDClaimErrorDescription
}];
}
return nil;
}
validTokenClaims[currentClaim.name] = currentClaim;
validClaims[currentClaim.name] = currentClaim;
}

// === Step 2: Build the dictionary structure required for OIDC JSON ===
NSMutableDictionary<NSString *, NSDictionary *> *tokenClaimsDictionary =
NSMutableDictionary<NSString *, NSDictionary *> *claimsDictionary =
[[NSMutableDictionary alloc] init];
for (GIDTokenClaim *claim in validTokenClaims.allValues) {
for (GIDClaim *claim in validClaims.allValues) {
if (claim.isEssential) {
tokenClaimsDictionary[claim.name] = @{ kGIDTokenClaimEssentialPropertyKey: @YES };
claimsDictionary[claim.name] = @{ kGIDClaimEssentialPropertyKey: @YES };
} else {
tokenClaimsDictionary[claim.name] = @{ kGIDTokenClaimEssentialPropertyKey: @NO };
claimsDictionary[claim.name] = @{ kGIDClaimEssentialPropertyKey: @NO };
}
}
NSDictionary<NSString *, id> *finalRequestDictionary =
@{ kGIDTokenClaimKeyName: tokenClaimsDictionary };
@{ kGIDClaimKeyName: claimsDictionary };

// === Step 3: Serialize the final dictionary into a JSON string ===
return [_jsonSerializer stringWithJSONObject:finalRequestDictionary error:error];
Expand Down
Loading