Skip to content

Commit

Permalink
correctly sets YouTube consent cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeichhorn committed May 2, 2021
1 parent 23f7659 commit 893401b
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions XCDYouTubeKit/XCDYouTubeVideoOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,49 @@ - (void) handleConnectionError:(NSError *)connectionError requestType:(XCDYouTub

#pragma mark - Response Parsing

- (void) initializeConsentWithResponse:(NSURLResponse *)response {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse && response.URL) {
NSArray <NSHTTPCookie *> *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:httpResponse.allHeaderFields forURL:(NSURL *_Nonnull)response.URL];

for (NSHTTPCookie *cookie in cookies) {
if ([cookie.name isEqualToString:@"__Secure-3PSID"]) return;
}

for (NSHTTPCookie *cookie in cookies) {
if ([cookie.name isEqualToString:@"CONSENT"]) {
if ([cookie.value isEqualToString:@"YES"]) return;

NSString *rawConsentID = [cookie.value stringByReplacingOccurrencesOfString:@"PENDING+" withString:@""];
int consentID = [rawConsentID intValue];

// generate random consent id, if doesn't match expected format
if (consentID < 100 || consentID > 999) {
consentID = 100 + (int)arc4random_uniform((uint32_t)(999 - 100 + 1));
}

NSString *cookieValue = [[NSString alloc] initWithFormat:@"YES+cb.20210328-17-p0.en+FX+%i", consentID];
NSHTTPCookie *consentCookie = [NSHTTPCookie cookieWithProperties:@{
NSHTTPCookiePath: @"/",
NSHTTPCookieName: @"CONSENT",
NSHTTPCookieValue: cookieValue,
NSHTTPCookieDomain:@".youtube.com",
NSHTTPCookieSecure:@"TRUE"
}];
[self.session.configuration.HTTPCookieStorage setCookie:consentCookie];
return;
}
}

}
}

- (void) handleVideoInfoResponseWithInfo:(NSDictionary *)info response:(NSURLResponse *)response
{
XCDYouTubeLogDebug(@"Handling video info response");

[self initializeConsentWithResponse:response];

NSError *error = nil;
XCDYouTubeVideo *video = [[XCDYouTubeVideo alloc] initWithIdentifier:self.videoIdentifier info:info playerScript:self.playerScript response:response error:&error];
if (video)
Expand Down

0 comments on commit 893401b

Please sign in to comment.