Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: remoting/ios/facade/remoting_oauth_authentication.mm

Issue 2949713002: [CRD iOS] Refactor an interface for RemotingAuthorization (Closed)
Patch Set: Resolve feedback Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #if !defined(__has_feature) || !__has_feature(objc_arc) 5 #if !defined(__has_feature) || !__has_feature(objc_arc)
6 #error "This file requires ARC support." 6 #error "This file requires ARC support."
7 #endif 7 #endif
8 8
9 #import "remoting/ios/facade/remoting_authentication.h" 9 #import "remoting/ios/facade/remoting_oauth_authentication.h"
10 10
11 #import <Foundation/Foundation.h> 11 #import <Foundation/Foundation.h>
12 #import <Security/Security.h> 12 #import <Security/Security.h>
13 13
14 #import "base/mac/bind_objc_block.h" 14 #import "base/mac/bind_objc_block.h"
15 #import "remoting/ios/facade/host_info.h" 15 #import "remoting/ios/facade/host_info.h"
16 #import "remoting/ios/facade/host_list_fetcher.h" 16 #import "remoting/ios/facade/host_list_fetcher.h"
17 #import "remoting/ios/facade/ios_client_runtime_delegate.h" 17 #import "remoting/ios/facade/ios_client_runtime_delegate.h"
18 #import "remoting/ios/facade/remoting_service.h" 18 #import "remoting/ios/facade/remoting_service.h"
19 #import "remoting/ios/keychain_wrapper.h" 19 #import "remoting/ios/keychain_wrapper.h"
(...skipping 18 matching lines...) Expand all
38 on_credentials_update) { 38 on_credentials_update) {
39 std::unique_ptr<remoting::OAuthTokenGetter::OAuthIntermediateCredentials> 39 std::unique_ptr<remoting::OAuthTokenGetter::OAuthIntermediateCredentials>
40 oauth_credentials( 40 oauth_credentials(
41 new remoting::OAuthTokenGetter::OAuthIntermediateCredentials( 41 new remoting::OAuthTokenGetter::OAuthIntermediateCredentials(
42 auth_code, /*is_service_account=*/false)); 42 auth_code, /*is_service_account=*/false));
43 oauth_credentials->oauth_redirect_uri = kOauthRedirectUrl; 43 oauth_credentials->oauth_redirect_uri = kOauthRedirectUrl;
44 44
45 std::unique_ptr<remoting::OAuthTokenGetter> oauth_tokenGetter( 45 std::unique_ptr<remoting::OAuthTokenGetter> oauth_tokenGetter(
46 new remoting::OAuthTokenGetterImpl( 46 new remoting::OAuthTokenGetterImpl(
47 std::move(oauth_credentials), on_credentials_update, 47 std::move(oauth_credentials), on_credentials_update,
48 [RemotingService SharedInstance].runtime->url_requester(), 48 RemotingService.instance.runtime->url_requester(),
49 /*auto_refresh=*/true)); 49 /*auto_refresh=*/true));
50 return oauth_tokenGetter; 50 return oauth_tokenGetter;
51 } 51 }
52 52
53 std::unique_ptr<remoting::OAuthTokenGetter> CreateOAuthTokenWithRefreshToken( 53 std::unique_ptr<remoting::OAuthTokenGetter> CreateOAuthTokenWithRefreshToken(
54 const std::string& refresh_token, 54 const std::string& refresh_token,
55 const std::string& email) { 55 const std::string& email) {
56 std::unique_ptr<remoting::OAuthTokenGetter::OAuthAuthorizationCredentials> 56 std::unique_ptr<remoting::OAuthTokenGetter::OAuthAuthorizationCredentials>
57 oauth_credentials( 57 oauth_credentials(
58 new remoting::OAuthTokenGetter::OAuthAuthorizationCredentials( 58 new remoting::OAuthTokenGetter::OAuthAuthorizationCredentials(
59 email, refresh_token, /*is_service_account=*/false)); 59 email, refresh_token, /*is_service_account=*/false));
60 60
61 std::unique_ptr<remoting::OAuthTokenGetter> oauth_tokenGetter( 61 std::unique_ptr<remoting::OAuthTokenGetter> oauth_tokenGetter(
62 new remoting::OAuthTokenGetterImpl( 62 new remoting::OAuthTokenGetterImpl(
63 std::move(oauth_credentials), 63 std::move(oauth_credentials),
64 [RemotingService SharedInstance].runtime->url_requester(), 64 RemotingService.instance.runtime->url_requester(),
65 /*auto_refresh=*/true)); 65 /*auto_refresh=*/true));
66 return oauth_tokenGetter; 66 return oauth_tokenGetter;
67 } 67 }
68 68
69 @interface RemotingAuthentication () { 69 RemotingAuthenticationStatus oauthStatusToRemotingAuthenticationStatus(
70 remoting::OAuthTokenGetter::Status status) {
71 switch (status) {
72 case remoting::OAuthTokenGetter::Status::AUTH_ERROR:
73 return RemotingAuthenticationStatusAuthError;
74 case remoting::OAuthTokenGetter::Status::NETWORK_ERROR:
75 return RemotingAuthenticationStatusNetworkError;
76 case remoting::OAuthTokenGetter::Status::SUCCESS:
77 return RemotingAuthenticationStatusSuccess;
78 }
79 }
80
81 @interface RemotingOAuthAuthentication () {
70 std::unique_ptr<remoting::OAuthTokenGetter> _tokenGetter; 82 std::unique_ptr<remoting::OAuthTokenGetter> _tokenGetter;
71 KeychainWrapper* _keychainWrapper; 83 KeychainWrapper* _keychainWrapper;
72 BOOL _firstLoadUserAttempt; 84 BOOL _firstLoadUserAttempt;
73 } 85 }
74 @end 86 @end
75 87
76 @implementation RemotingAuthentication 88 @implementation RemotingOAuthAuthentication
77 89
78 @synthesize user = _user; 90 @synthesize user = _user;
79 @synthesize delegate = _delegate; 91 @synthesize delegate = _delegate;
80 92
81 - (instancetype)init { 93 - (instancetype)init {
82 self = [super init]; 94 self = [super init];
83 if (self) { 95 if (self) {
84 _keychainWrapper = [[KeychainWrapper alloc] init]; 96 _keychainWrapper = [[KeychainWrapper alloc] init];
85 _user = nil; 97 _user = nil;
86 _firstLoadUserAttempt = YES; 98 _firstLoadUserAttempt = YES;
(...skipping 13 matching lines...) Expand all
100 112
101 - (void)setUser:(UserInfo*)user { 113 - (void)setUser:(UserInfo*)user {
102 _user = user; 114 _user = user;
103 [self storeUserInfo:_user]; 115 [self storeUserInfo:_user];
104 [_delegate userDidUpdate:_user]; 116 [_delegate userDidUpdate:_user];
105 } 117 }
106 118
107 #pragma mark - Class Implementation 119 #pragma mark - Class Implementation
108 120
109 - (void)authenticateWithAuthorizationCode:(NSString*)authorizationCode { 121 - (void)authenticateWithAuthorizationCode:(NSString*)authorizationCode {
110 __weak RemotingAuthentication* weakSelf = self; 122 __weak RemotingOAuthAuthentication* weakSelf = self;
111 _tokenGetter = CreateOAuthTokenGetterWithAuthorizationCode( 123 _tokenGetter = CreateOAuthTokenGetterWithAuthorizationCode(
112 std::string(base::SysNSStringToUTF8(authorizationCode)), 124 std::string(base::SysNSStringToUTF8(authorizationCode)),
113 base::BindBlockArc( 125 base::BindBlockArc(
114 ^(const std::string& user_email, const std::string& refresh_token) { 126 ^(const std::string& user_email, const std::string& refresh_token) {
115 // TODO(nicholss): Do something with these new creds. 127 // TODO(nicholss): Do something with these new creds.
116 VLOG(1) << "New Creds: " << user_email << " " << refresh_token; 128 VLOG(1) << "New Creds: " << user_email << " " << refresh_token;
117 UserInfo* user = [[UserInfo alloc] init]; 129 UserInfo* user = [[UserInfo alloc] init];
118 user.userEmail = base::SysUTF8ToNSString(user_email); 130 user.userEmail = base::SysUTF8ToNSString(user_email);
119 user.refreshToken = base::SysUTF8ToNSString(refresh_token); 131 user.refreshToken = base::SysUTF8ToNSString(refresh_token);
120 [weakSelf setUser:user]; 132 [weakSelf setUser:user];
121 })); 133 }));
122 // Stimulate the oAuth Token Getter to fetch and access token, this forces it 134 // Stimulate the oAuth Token Getter to fetch and access token, this forces it
123 // to convert the authorization code into a refresh token, and saving the 135 // to convert the authorization code into a refresh token, and saving the
124 // refresh token will happen automaticly in the above block. 136 // refresh token will happen automaticly in the above block.
125 [self callbackWithAccessToken:base::BindBlockArc(^( 137 [self callbackWithAccessToken:^(RemotingAuthenticationStatus status,
126 remoting::OAuthTokenGetter::Status status, 138 NSString* user_email,
127 const std::string& user_email, 139 NSString* access_token) {
128 const std::string& access_token) { 140 if (status == RemotingAuthenticationStatusSuccess) {
129 if (status == remoting::OAuthTokenGetter::Status::SUCCESS) { 141 VLOG(1) << "Success fetching access token from authorization code.";
130 VLOG(1) << "Success fetching access token from authorization code."; 142 } else {
131 } else { 143 LOG(ERROR) << "Failed to fetch access token from authorization code. ("
132 LOG(ERROR) 144 << status << ")";
133 << "Failed to fetch access token from authorization code. (" 145 // TODO(nicholss): Deal with the sad path for a bad auth token.
134 << status << ")"; 146 }
135 // TODO(nicholss): Deal with the sad path for a bad auth token. 147 }];
136 }
137 })];
138 } 148 }
139 149
140 #pragma mark - Private 150 #pragma mark - Private
141 151
142 // Provide the |refreshToken| and |email| to authenticate a user as a returning 152 // Provide the |refreshToken| and |email| to authenticate a user as a returning
143 // user of the application. 153 // user of the application.
144 - (void)authenticateWithRefreshToken:(NSString*)refreshToken 154 - (void)authenticateWithRefreshToken:(NSString*)refreshToken
145 email:(NSString*)email { 155 email:(NSString*)email {
146 _tokenGetter = CreateOAuthTokenWithRefreshToken( 156 _tokenGetter = CreateOAuthTokenWithRefreshToken(
147 std::string(base::SysNSStringToUTF8(refreshToken)), 157 std::string(base::SysNSStringToUTF8(refreshToken)),
148 base::SysNSStringToUTF8(email)); 158 base::SysNSStringToUTF8(email));
149 } 159 }
150 160
151 - (void)callbackWithAccessToken: 161 - (void)callbackWithAccessToken:(AccessTokenCallback)onAccessToken {
152 (const remoting::OAuthTokenGetter::TokenCallback&)onAccessToken {
153 // TODO(nicholss): Be careful here since a failure to reset onAccessToken 162 // TODO(nicholss): Be careful here since a failure to reset onAccessToken
154 // will end up with retain cycle and memory leakage. 163 // will end up with retain cycle and memory leakage.
155 if (_tokenGetter) { 164 if (_tokenGetter) {
156 _tokenGetter->CallWithToken(onAccessToken); 165 _tokenGetter->CallWithToken(base::BindBlockArc(
166 ^(remoting::OAuthTokenGetter::Status status,
167 const std::string& user_email, const std::string& access_token) {
168 onAccessToken(oauthStatusToRemotingAuthenticationStatus(status),
169 base::SysUTF8ToNSString(user_email),
170 base::SysUTF8ToNSString(access_token));
171 }));
157 } 172 }
158 } 173 }
159 174
160 - (void)logout { 175 - (void)logout {
161 [self storeUserInfo:nil]; 176 [self storeUserInfo:nil];
162 [self setUser:nil]; 177 [self setUser:nil];
163 } 178 }
164 179
165 #pragma mark - Persistence 180 #pragma mark - Persistence
166 181
(...skipping 19 matching lines...) Expand all
186 201
187 if (!user || ![user isAuthenticated]) { 202 if (!user || ![user isAuthenticated]) {
188 user = nil; 203 user = nil;
189 } else { 204 } else {
190 [self authenticateWithRefreshToken:user.refreshToken email:user.userEmail]; 205 [self authenticateWithRefreshToken:user.refreshToken email:user.userEmail];
191 } 206 }
192 return user; 207 return user;
193 } 208 }
194 209
195 @end 210 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698