Skip to content

Commit 7f75975

Browse files
authored
GIDTokenClaim Implementation + Unit Tests (#550)
1 parent f478a1d commit 7f75975

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
NSString * const kAuthTimeClaimName = @"auth_time";
20+
21+
// Private interface to declare the internal initializer
22+
@interface GIDTokenClaim ()
23+
24+
- (instancetype)initWithName:(NSString *)name
25+
essential:(BOOL)essential NS_DESIGNATED_INITIALIZER;
26+
27+
@end
28+
29+
@implementation GIDTokenClaim
30+
31+
// Private designated initializer
32+
- (instancetype)initWithName:(NSString *)name essential:(BOOL)essential {
33+
self = [super init];
34+
if (self) {
35+
_name = [name copy];
36+
_essential = essential;
37+
}
38+
return self;
39+
}
40+
41+
#pragma mark - Factory Methods
42+
43+
+ (instancetype)authTimeClaim {
44+
return [[self alloc] initWithName:kAuthTimeClaimName essential:NO];
45+
}
46+
47+
+ (instancetype)essentialAuthTimeClaim {
48+
return [[self alloc] initWithName:kAuthTimeClaimName essential:YES];
49+
}
50+
51+
#pragma mark - NSObject
52+
53+
- (BOOL)isEqual:(id)object {
54+
// 1. Check if the other object is the same instance in memory.
55+
if (self == object) {
56+
return YES;
57+
}
58+
59+
// 2. Check if the other object is not a GIDTokenClaim instance.
60+
if (![object isKindOfClass:[GIDTokenClaim class]]) {
61+
return NO;
62+
}
63+
64+
// 3. Compare the properties that define equality.
65+
GIDTokenClaim *other = (GIDTokenClaim *)object;
66+
return [self.name isEqualToString:other.name] &&
67+
self.isEssential == other.isEssential;
68+
}
69+
70+
- (NSUInteger)hash {
71+
// The hash value should be based on the same properties used in isEqual:
72+
return self.name.hash ^ @(self.isEssential).hash;
73+
}
74+
75+
@end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
extern NSString *const kAuthTimeClaimName;
22+
23+
/**
24+
* An object representing a single OIDC claim to be requested for an ID token.
25+
*/
26+
@interface GIDTokenClaim : NSObject
27+
28+
/// The name of the claim, e.g., "auth_time".
29+
@property (nonatomic, readonly) NSString *name;
30+
31+
/// Whether the claim is requested as essential.
32+
@property (nonatomic, readonly, getter=isEssential) BOOL essential;
33+
34+
// Making initializers unavailable to force use of factory methods.
35+
- (instancetype)init NS_UNAVAILABLE;
36+
37+
#pragma mark - Factory Methods
38+
39+
/// Creates a *non-essential* (voluntary) "auth_time" claim object.
40+
+ (instancetype)authTimeClaim;
41+
42+
/// Creates an *essential* "auth_time" claim object.
43+
+ (instancetype)essentialAuthTimeClaim;
44+
45+
@end
46+
47+
NS_ASSUME_NONNULL_END
48+

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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
19+
@interface GIDTokenClaimTest : XCTestCase
20+
@end
21+
22+
@implementation GIDTokenClaimTest
23+
24+
- (void)testAuthTimeClaim_PropertiesAreCorrect {
25+
GIDTokenClaim *claim = [GIDTokenClaim authTimeClaim];
26+
XCTAssertEqualObjects(claim.name, kAuthTimeClaimName);
27+
XCTAssertFalse(claim.isEssential);
28+
}
29+
30+
- (void)testEssentialAuthTimeClaim_PropertiesAreCorrect {
31+
GIDTokenClaim *claim = [GIDTokenClaim essentialAuthTimeClaim];
32+
XCTAssertEqualObjects(claim.name, kAuthTimeClaimName);
33+
XCTAssertTrue(claim.isEssential);
34+
}
35+
36+
- (void)testEquality_WithEqualClaims {
37+
GIDTokenClaim *claim1 = [GIDTokenClaim authTimeClaim];
38+
GIDTokenClaim *claim2 = [GIDTokenClaim authTimeClaim];
39+
XCTAssertEqualObjects(claim1, claim2);
40+
XCTAssertEqual(claim1.hash, claim2.hash);
41+
}
42+
43+
- (void)testEquality_WithUnequalClaims {
44+
GIDTokenClaim *claim1 = [GIDTokenClaim authTimeClaim];
45+
GIDTokenClaim *claim2 = [GIDTokenClaim essentialAuthTimeClaim];
46+
XCTAssertNotEqualObjects(claim1, claim2);
47+
}
48+
49+
@end

0 commit comments

Comments
 (0)