Skip to content

Commit 396805a

Browse files
committed
init1
1 parent 99157c5 commit 396805a

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 "GIDTokenClaimsInternalOptions.h"
18+
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDTokenClaim.h"
19+
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h"
20+
21+
NSString * const kTokenClaimErrorDescription = @"The claim was requested as both essential and non-essential. Please provide only one version.";
22+
23+
NSString * const kTokenClaimEssentialPropertyKey = @"essential";
24+
NSString * const kTokenClaimKeyName = @"id_token";
25+
26+
@implementation GIDTokenClaimsInternalOptions
27+
28+
+ (nullable NSString *)validatedJSONStringForClaims:(nullable NSSet<GIDTokenClaim *> *)claims
29+
error:(NSError **)error {
30+
if (!claims || claims.count == 0) {
31+
return nil;
32+
}
33+
34+
// === Step 1: Check for claims with ambiguous essential property. ===
35+
NSMutableDictionary<NSString *, GIDTokenClaim *> *validTokenClaims =
36+
[[NSMutableDictionary alloc] init];
37+
38+
for (GIDTokenClaim *currentClaim in claims) {
39+
GIDTokenClaim *existingClaim = validTokenClaims[currentClaim.name];
40+
41+
// Check for a conflict: a claim with the same name but different essentiality.
42+
if (existingClaim && existingClaim.isEssential != currentClaim.isEssential) {
43+
if (error) {
44+
*error = [NSError errorWithDomain:kGIDSignInErrorDomain
45+
code:kGIDSignInErrorCodeAmbiguousClaims
46+
userInfo:@{NSLocalizedDescriptionKey: kTokenClaimErrorDescription}];
47+
}
48+
return nil; // Validation failed
49+
}
50+
validTokenClaims[currentClaim.name] = currentClaim;
51+
}
52+
53+
// === Step 2: Build the dictionary structure required for OIDC JSON ===
54+
NSMutableDictionary<NSString *, id> *tokenClaimsDictionary = [[NSMutableDictionary alloc] init];
55+
for (GIDTokenClaim *claim in validTokenClaims.allValues) {
56+
if (claim.isEssential) {
57+
tokenClaimsDictionary[claim.name] = @{ kTokenClaimEssentialPropertyKey: @YES };
58+
} else {
59+
// Per OIDC spec, non-essential claims can be represented by null.
60+
tokenClaimsDictionary[claim.name] = [NSNull null];
61+
}
62+
}
63+
NSDictionary<NSString *, id> *finalRequestDictionary = @{ kTokenClaimKeyName: tokenClaimsDictionary };
64+
65+
// === Step 3: Serialize the final dictionary into a JSON string ===
66+
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:finalRequestDictionary
67+
options:0
68+
error:error];
69+
if (!jsonData) {
70+
return nil;
71+
}
72+
73+
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
74+
}
75+
76+
@end

0 commit comments

Comments
 (0)