Skip to content

Commit

Permalink
Merge pull request alexeichhorn#1 from alexeichhorn/fix/youtube-consent
Browse files Browse the repository at this point in the history
correctly sets YouTube consent cookie
  • Loading branch information
alexeichhorn authored May 30, 2021
2 parents 3d5e5f1 + 893401b commit f1b2dd8
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 f1b2dd8

Please sign in to comment.