1// Copyright 2014 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#if !defined(__has_feature) || !__has_feature(objc_arc) 6#error "This file requires ARC support." 7#endif 8 9#import "remoting/ios/authorize.h" 10 11// TODO (aboone) This include is for The Google Toolbox for Mac OAuth 2 12// Controllers https://code.google.com/p/gtm-oauth2/ This may need to be added 13// as a third-party or locate the proper project in Chromium. 14#import "GTMOAuth2ViewControllerTouch.h" 15 16#include "google_apis/google_api_keys.h" 17// TODO (aboone) Pulling in some service values from the host side. The cc's 18// are also compiled as part of this project because the target remoting_host 19// does not build on iOS right now. 20#include "remoting/host/service_urls.h" 21#include "remoting/host/setup/oauth_helper.h" 22 23namespace { 24static NSString* const kKeychainItemName = @"Google Chromoting iOS"; 25 26NSString* ClientId() { 27 return 28 [NSString stringWithUTF8String:google_apis::GetOAuth2ClientID( 29 google_apis::CLIENT_REMOTING).c_str()]; 30} 31 32NSString* ClientSecret() { 33 return 34 [NSString stringWithUTF8String:google_apis::GetOAuth2ClientSecret( 35 google_apis::CLIENT_REMOTING).c_str()]; 36} 37 38NSString* Scopes() { 39 return [NSString stringWithUTF8String:remoting::GetOauthScope().c_str()]; 40} 41 42NSMutableString* HostURL() { 43 return 44 [NSMutableString stringWithUTF8String:remoting::ServiceUrls::GetInstance() 45 ->directory_hosts_url() 46 .c_str()]; 47} 48 49NSString* APIKey() { 50 return [NSString stringWithUTF8String:google_apis::GetAPIKey().c_str()]; 51} 52 53} // namespace 54 55@implementation Authorize 56 57+ (GTMOAuth2Authentication*)getAnyExistingAuthorization { 58 // Ensure the google_apis lib has keys 59 // If this check fails then google_apis was not built right 60 // TODO (aboone) For now we specify the preprocessor macros for 61 // GOOGLE_CLIENT_SECRET_REMOTING and GOOGLE_CLIENT_ID_REMOTING when building 62 // the google_apis target. The values may be developer specific, and should 63 // be well know to the project staff. 64 // See http://www.chromium.org/developers/how-tos/api-keys for more general 65 // information. 66 DCHECK(![ClientId() isEqualToString:@"dummytoken"]); 67 68 return [GTMOAuth2ViewControllerTouch 69 authForGoogleFromKeychainForName:kKeychainItemName 70 clientID:ClientId() 71 clientSecret:ClientSecret()]; 72} 73 74+ (void)beginRequest:(GTMOAuth2Authentication*)authReq 75 delegate:(id)delegate 76 didFinishSelector:(SEL)sel { 77 // Build request URL using API HTTP endpoint, and our api key 78 NSMutableString* hostsUrl = HostURL(); 79 [hostsUrl appendString:@"?key="]; 80 [hostsUrl appendString:APIKey()]; 81 82 NSMutableURLRequest* theRequest = 83 [NSMutableURLRequest requestWithURL:[NSURL URLWithString:hostsUrl]]; 84 85 // Add scopes if needed 86 NSString* scope = authReq.scope; 87 88 if ([scope rangeOfString:Scopes()].location == NSNotFound) { 89 scope = [GTMOAuth2Authentication scopeWithStrings:scope, Scopes(), nil]; 90 authReq.scope = scope; 91 } 92 93 // Execute request async 94 [authReq authorizeRequest:theRequest delegate:delegate didFinishSelector:sel]; 95} 96 97+ (void)appendCredentials:(NSMutableURLRequest*)request { 98 // Add credentials for service 99 [request addValue:ClientId() forHTTPHeaderField:@"client_id"]; 100 [request addValue:ClientSecret() forHTTPHeaderField:@"client_secret"]; 101} 102 103+ (UINavigationController*)createLoginController:(id)delegate 104 finishedSelector:(SEL)finishedSelector { 105 [GTMOAuth2ViewControllerTouch 106 removeAuthFromKeychainForName:kKeychainItemName]; 107 108 // When the sign in is complete a http redirection occurs, and the 109 // user would see the output. We do not want the user to notice this 110 // transition. Wrapping the oAuth2 Controller in a 111 // UINavigationController causes the view to render as a blank/black 112 // page when a http redirection occurs. 113 return [[UINavigationController alloc] 114 initWithRootViewController:[[GTMOAuth2ViewControllerTouch alloc] 115 initWithScope:Scopes() 116 clientID:ClientId() 117 clientSecret:ClientSecret() 118 keychainItemName:kKeychainItemName 119 delegate:delegate 120 finishedSelector:finishedSelector]]; 121} 122 123@end 124