Skip to content

Commit 6648e4f

Browse files
committed
GIDTokenClaim Implementation + Unit Tests
1 parent f478a1d commit 6648e4f

4 files changed

Lines changed: 168 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDTokenClaim.h"
18+
19+
// Private interface to declare the internal initializer
20+
@interface GIDTokenClaim ()
21+
22+
- (instancetype)initWithName:(NSString *)name
23+
essential:(BOOL)essential NS_DESIGNATED_INITIALIZER;
24+
25+
@end
26+
27+
@implementation GIDTokenClaim
28+
29+
// Private designated initializer
30+
- (instancetype)initWithName:(NSString *)name essential:(BOOL)essential {
31+
self = [super init];
32+
if (self) {
33+
_name = [name copy];
34+
_essential = essential;
35+
}
36+
return self;
37+
}
38+
39+
#pragma mark - Factory Methods
40+
41+
+ (instancetype)authTimeClaim {
42+
return [[self alloc] initWithName:@"auth_time" essential:NO];
43+
}
44+
45+
+ (instancetype)essentialAuthTimeClaim {
46+
return [[self alloc] initWithName:@"auth_time" essential:YES];
47+
}
48+
49+
#pragma mark - NSObject
50+
51+
- (BOOL)isEqual:(id)object {
52+
// 1. Check if the other object is the same instance in memory.
53+
if (self == object) {
54+
return YES;
55+
}
56+
57+
// 2. Check if the other object is not a GIDTokenClaim instance.
58+
if (![object isKindOfClass:[GIDTokenClaim class]]) {
59+
return NO;
60+
}
61+
62+
// 3. Compare the properties that define equality.
63+
GIDTokenClaim *other = (GIDTokenClaim *)object;
64+
return [self.name isEqualToString:other.name] &&
65+
self.isEssential == other.isEssential;
66+
}
67+
68+
- (NSUInteger)hash {
69+
// The hash value should be based on the same properties used in isEqual:
70+
return self.name.hash ^ @(self.isEssential).hash;
71+
}
72+
73+
@end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
19+
NS_ASSUME_NONNULL_BEGIN
20+
21+
/**
22+
* An object representing a single OIDC claim to be requested for an ID token.
23+
*/
24+
@interface GIDTokenClaim : NSObject
25+
26+
/// The name of the claim, e.g., "auth_time".
27+
@property (nonatomic, readonly) NSString *name;
28+
29+
/// Whether the claim is requested as essential.
30+
@property (nonatomic, readonly, getter=isEssential) BOOL essential;
31+
32+
// Making initializers unavailable to force use of factory methods.
33+
- (instancetype)init NS_UNAVAILABLE;
34+
35+
#pragma mark - Factory Methods
36+
37+
/// Creates a *non-essential* (voluntary) "auth_time" claim object.
38+
+ (instancetype)authTimeClaim;
39+
40+
/// Creates an *essential* "auth_time" claim object.
41+
+ (instancetype)essentialAuthTimeClaim;
42+
43+
@end
44+
45+
NS_ASSUME_NONNULL_END
46+

GoogleSignIn/Sources/Public/GoogleSignIn/GoogleSignIn.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#import "GIDSignIn.h"
2525
#import "GIDToken.h"
2626
#import "GIDSignInResult.h"
27+
#import "GIDTokenClaim.h"
2728
#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
2829
#import "GIDSignInButton.h"
2930
#endif
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
16+
#import <XCTest/XCTest.h>
17+
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDTokenClaim.h"
18+
@interface GIDTokenClaimTest : XCTestCase
19+
@end
20+
21+
@implementation GIDTokenClaimTest
22+
23+
- (void)testAuthTimeClaim_PropertiesAreCorrect {
24+
GIDTokenClaim *claim = [GIDTokenClaim authTimeClaim];
25+
XCTAssertEqualObjects(claim.name, @"auth_time");
26+
XCTAssertFalse(claim.isEssential);
27+
}
28+
29+
- (void)testEssentialAuthTimeClaim_PropertiesAreCorrect {
30+
GIDTokenClaim *claim = [GIDTokenClaim essentialAuthTimeClaim];
31+
XCTAssertEqualObjects(claim.name, @"auth_time");
32+
XCTAssertTrue(claim.isEssential);
33+
}
34+
35+
- (void)testEquality_WithEqualClaims {
36+
GIDTokenClaim *claim1 = [GIDTokenClaim authTimeClaim];
37+
GIDTokenClaim *claim2 = [GIDTokenClaim authTimeClaim];
38+
XCTAssertEqualObjects(claim1, claim2);
39+
XCTAssertEqual(claim1.hash, claim2.hash);
40+
}
41+
42+
- (void)testEquality_WithUnequalClaims {
43+
GIDTokenClaim *claim1 = [GIDTokenClaim authTimeClaim];
44+
GIDTokenClaim *claim2 = [GIDTokenClaim essentialAuthTimeClaim];
45+
XCTAssertNotEqualObjects(claim1, claim2);
46+
}
47+
48+
@end

0 commit comments

Comments
 (0)