@@ -84,6 +84,31 @@ + (BOOL)isExpectedCallbackURL:(nullable NSURL *)URL
84
84
return NO ;
85
85
}
86
86
87
+ + (NSString *)extractDomain : (NSString *)urlString {
88
+ // Remove trailing slashes
89
+ urlString = [urlString
90
+ stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @" /" ]];
91
+
92
+ // Check for the presence of a scheme (e.g., http:// or https://)
93
+ NSRange range = [urlString rangeOfString: @" http://" options: NSCaseInsensitiveSearch];
94
+ if (range.location != NSNotFound ) {
95
+ urlString = [urlString stringByReplacingCharactersInRange: range withString: @" " ];
96
+ } else {
97
+ range = [urlString rangeOfString: @" https://" options: NSCaseInsensitiveSearch];
98
+ if (range.location != NSNotFound ) {
99
+ urlString = [urlString stringByReplacingCharactersInRange: range withString: @" " ];
100
+ }
101
+ }
102
+
103
+ // Split the URL by "/"
104
+ NSArray *urlComponents = [urlString componentsSeparatedByString: @" /" ];
105
+
106
+ // The domain is the first component after removing the scheme
107
+ NSString *domain = urlComponents[0 ];
108
+
109
+ return domain;
110
+ }
111
+
87
112
+ (void )fetchAuthDomainWithRequestConfiguration : (FIRAuthRequestConfiguration *)requestConfiguration
88
113
completion : (FIRFetchAuthDomainCallback)completion {
89
114
if (requestConfiguration.emulatorHostAndPort ) {
@@ -104,22 +129,42 @@ + (void)fetchAuthDomainWithRequestConfiguration:(FIRAuthRequestConfiguration *)r
104
129
return ;
105
130
}
106
131
// Look up an authorized domain ends with one of the supportedAuthDomains.
107
- // The sequence of supportedAuthDomains matters. ("firebaseapp.com", "web.app")
108
- // The searching ends once the first valid suportedAuthDomain is found.
132
+ // The searching ends once the first valid supportedAuthDomain is found.
109
133
NSString *authDomain;
110
- for ( NSString *domain in response. authorizedDomains ) {
111
- for ( NSString *suportedAuthDomain in [ self supportedAuthDomains ] ) {
112
- NSInteger index = domain. length - suportedAuthDomain. length ;
113
- if (index >= 2 ) {
114
- if ([ domain hasSuffix: suportedAuthDomain] &&
115
- domain. length >= suportedAuthDomain. length + 2 ) {
116
- authDomain = domain ;
117
- break ;
118
- }
134
+ NSString *customAuthDomain = requestConfiguration. auth . customAuthDomain ;
135
+ if (customAuthDomain ) {
136
+ customAuthDomain = [FIRAuthWebUtils extractDomain: customAuthDomain] ;
137
+ BOOL isCustomAuthDomainAuthorized = NO ;
138
+ for ( NSString * domain in response. authorizedDomains ) {
139
+ if ([customAuthDomain isEqualToString: domain] ) {
140
+ authDomain = customAuthDomain ;
141
+ isCustomAuthDomainAuthorized = YES ;
142
+ break ;
119
143
}
120
144
}
121
- if (authDomain != nil ) {
122
- break ;
145
+ if (!isCustomAuthDomainAuthorized) {
146
+ NSError *customDomainError =
147
+ [FIRAuthErrorUtils unauthorizedDomainErrorWithMessage:
148
+ @" Error while validating application identity: The "
149
+ @" configured custom domain is not allowlisted." ];
150
+ completion (nil , customDomainError);
151
+ return ;
152
+ }
153
+ } else {
154
+ for (NSString *domain in response.authorizedDomains ) {
155
+ for (NSString *supportedAuthDomain in [self supportedAuthDomains ]) {
156
+ NSInteger index = domain.length - supportedAuthDomain.length ;
157
+ if (index >= 2 ) {
158
+ if ([domain hasSuffix: supportedAuthDomain] &&
159
+ domain.length >= supportedAuthDomain.length + 2 ) {
160
+ authDomain = domain;
161
+ break ;
162
+ }
163
+ }
164
+ }
165
+ if (authDomain != nil ) {
166
+ break ;
167
+ }
123
168
}
124
169
}
125
170
if (!authDomain.length ) {
0 commit comments