Sends NetworkChangeNotifier connection type changes to Blink, to support NetInfo v3.
[chromium-blink-merge.git] / remoting / ios / authorize.mm
blob92f467c648527dd2e6cc493c02c4f19bd6d201ed
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.
5 #if !defined(__has_feature) || !__has_feature(objc_arc)
6 #error "This file requires ARC support."
7 #endif
9 #import "remoting/ios/authorize.h"
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"
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"
23 namespace {
24 static NSString* const kKeychainItemName = @"Google Chromoting iOS";
26 NSString* ClientId() {
27   return
28       [NSString stringWithUTF8String:google_apis::GetOAuth2ClientID(
29                                          google_apis::CLIENT_REMOTING).c_str()];
32 NSString* ClientSecret() {
33   return
34       [NSString stringWithUTF8String:google_apis::GetOAuth2ClientSecret(
35                                          google_apis::CLIENT_REMOTING).c_str()];
38 NSString* Scopes() {
39   return [NSString stringWithUTF8String:remoting::GetOauthScope().c_str()];
42 NSMutableString* HostURL() {
43   return
44       [NSMutableString stringWithUTF8String:remoting::ServiceUrls::GetInstance()
45                                                 ->directory_hosts_url()
46                                                 .c_str()];
49 NSString* APIKey() {
50   return [NSString stringWithUTF8String:google_apis::GetAPIKey().c_str()];
53 }  // namespace
55 @implementation Authorize
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"]);
68   return [GTMOAuth2ViewControllerTouch
69       authForGoogleFromKeychainForName:kKeychainItemName
70                               clientID:ClientId()
71                           clientSecret:ClientSecret()];
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()];
82   NSMutableURLRequest* theRequest =
83       [NSMutableURLRequest requestWithURL:[NSURL URLWithString:hostsUrl]];
85   // Add scopes if needed
86   NSString* scope = authReq.scope;
88   if ([scope rangeOfString:Scopes()].location == NSNotFound) {
89     scope = [GTMOAuth2Authentication scopeWithStrings:scope, Scopes(), nil];
90     authReq.scope = scope;
91   }
93   // Execute request async
94   [authReq authorizeRequest:theRequest delegate:delegate didFinishSelector:sel];
97 + (void)appendCredentials:(NSMutableURLRequest*)request {
98   // Add credentials for service
99   [request addValue:ClientId() forHTTPHeaderField:@"client_id"];
100   [request addValue:ClientSecret() forHTTPHeaderField:@"client_secret"];
103 + (UINavigationController*)createLoginController:(id)delegate
104                                 finishedSelector:(SEL)finishedSelector {
105   [GTMOAuth2ViewControllerTouch
106       removeAuthFromKeychainForName:kKeychainItemName];
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]];
123 @end