OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 #include "net/android/http_auth_negotiate_android.h" | |
6 | |
7 #include "base/android/jni_string.h" | |
8 #include "base/android/scoped_java_ref.h" | |
9 #include "base/bind.h" | |
10 #include "base/callback_internal.h" | |
11 #include "base/location.h" | |
12 #include "base/thread_task_runner_handle.h" | |
13 #include "jni/HttpNegotiateAuthenticator_jni.h" | |
14 #include "net/base/auth.h" | |
15 #include "net/base/net_errors.h" | |
16 #include "net/http/http_auth_challenge_tokenizer.h" | |
17 #include "net/http/http_auth_handler_negotiate_parse.h" | |
18 | |
19 using base::android::AttachCurrentThread; | |
20 using base::android::ConvertUTF8ToJavaString; | |
21 using base::android::ConvertJavaStringToUTF8; | |
22 using base::android::ScopedJavaLocalRef; | |
23 | |
24 namespace net { | |
25 namespace android { | |
26 | |
27 JavaNegotiateResultWrapper::JavaNegotiateResultWrapper( | |
28 const scoped_refptr<base::TaskRunner>& callback_task_runner, | |
29 const base::Callback<void(int, const std::string&)>& thread_safe_callback) | |
30 : callback_task_runner_(callback_task_runner), | |
31 thread_safe_callback_(thread_safe_callback) { | |
32 } | |
33 | |
34 JavaNegotiateResultWrapper::~JavaNegotiateResultWrapper() { | |
35 } | |
36 | |
37 void JavaNegotiateResultWrapper::SetResult(JNIEnv* env, | |
38 jobject obj, | |
39 int result, | |
40 jstring token) { | |
41 // This will be called on the UI thread, so we have to post a task back to the | |
42 // correct thread to actually save the result | |
43 std::string raw_token = ConvertJavaStringToUTF8(env, token); | |
44 // Always post, even if we are on the same thread. This guarantees that the | |
45 // result will be delayed until after the request has completed, which | |
46 // simplifies the logic. In practice the result will only ever come back on | |
47 // the original thread in an obscure error case. | |
48 callback_task_runner_->PostTask( | |
49 FROM_HERE, base::Bind(thread_safe_callback_, result, raw_token)); | |
50 delete this; | |
51 } | |
52 | |
53 HttpAuthNegotiateAndroid::HttpAuthNegotiateAndroid( | |
54 const std::string& account_type) | |
55 : account_type_(account_type), | |
56 can_delegate_(false), | |
57 first_challenge_(true), | |
58 auth_token_(nullptr), | |
59 weak_factory_(this) { | |
60 DCHECK(!account_type.empty()); | |
61 auto env = AttachCurrentThread(); | |
62 java_authenticator_.Reset(Java_HttpNegotiateAuthenticator_create( | |
63 env, ConvertUTF8ToJavaString(env, account_type).obj())); | |
64 } | |
65 | |
66 HttpAuthNegotiateAndroid::~HttpAuthNegotiateAndroid() { | |
67 } | |
68 | |
69 bool HttpAuthNegotiateAndroid::Init() { | |
70 return true; | |
71 } | |
72 | |
73 bool HttpAuthNegotiateAndroid::NeedsIdentity() const { | |
74 return false; | |
75 } | |
76 | |
77 bool HttpAuthNegotiateAndroid::AllowsExplicitCredentials() const { | |
78 return false; | |
79 } | |
80 | |
81 HttpAuth::AuthorizationResult HttpAuthNegotiateAndroid::ParseChallenge( | |
82 net::HttpAuthChallengeTokenizer* tok) { | |
83 if (first_challenge_) { | |
84 first_challenge_ = false; | |
85 return net::ParseFirstNegotiateChallenge("negotiate", tok); | |
86 } else { | |
Ryan Sleevi
2015/06/29 13:56:44
Don't do else after a return
if (first_challenge_
aberent
2015/07/02 21:13:35
Done.
| |
87 std::string decoded_auth_token; | |
88 return net::ParseAnotherNegotiateChallenge( | |
89 "negotiate", tok, &server_auth_token_, &decoded_auth_token); | |
90 } | |
91 } | |
92 | |
93 int HttpAuthNegotiateAndroid::GenerateAuthToken( | |
94 const AuthCredentials* credentials, | |
95 const std::string& spn, | |
96 std::string* auth_token, | |
97 const net::CompletionCallback& callback) { | |
98 DCHECK(auth_token); | |
99 DCHECK(completion_callback_.is_null()); | |
100 DCHECK(!callback.is_null()); | |
101 | |
102 auth_token_ = auth_token; | |
103 completion_callback_ = callback; | |
104 auto callback_task_runner = base::ThreadTaskRunnerHandle::Get(); | |
Ryan Sleevi
2015/06/29 13:56:44
STYLE: Not a valid use of auto
aberent
2015/07/02 21:13:34
Please explain further. The Google style guide sim
Ryan Sleevi
2015/07/02 23:51:47
http://google-styleguide.googlecode.com/svn/trunk/
aberent
2015/07/03 10:43:49
Done.
| |
105 auto thread_safe_callback = base::Bind( | |
Ryan Sleevi
2015/06/29 13:56:44
STYLE: Not a valid use of auto
aberent
2015/07/03 10:43:49
Done.
| |
106 &HttpAuthNegotiateAndroid::SetResultInternal, weak_factory_.GetWeakPtr()); | |
107 auto env = AttachCurrentThread(); | |
Ryan Sleevi
2015/06/29 13:56:44
STYLE: Not a valid use of auto
aberent
2015/07/03 10:43:49
Done.
| |
108 auto java_server_auth_token = | |
109 ConvertUTF8ToJavaString(env, server_auth_token_); | |
Ryan Sleevi
2015/06/29 13:56:45
STYLE: Not a valid use of auto
aberent
2015/07/03 10:43:49
Done.
| |
110 auto java_spn = ConvertUTF8ToJavaString(env, spn); | |
Ryan Sleevi
2015/06/29 13:56:45
STYLE: Not a valid use of auto
aberent
2015/07/03 10:43:49
Done.
| |
111 auto java_account_type = ConvertUTF8ToJavaString(env, account_type_); | |
Ryan Sleevi
2015/06/29 13:56:44
STYLE: Not a valid use of auto
aberent
2015/07/03 10:43:49
Done.
| |
112 auto callback_wrapper = new JavaNegotiateResultWrapper(callback_task_runner, | |
Ryan Sleevi
2015/06/29 13:56:45
STYLE: Not a valid use of auto
aberent
2015/07/03 10:43:49
Done.
| |
113 thread_safe_callback); | |
114 Java_HttpNegotiateAuthenticator_getNextAuthToken( | |
115 env, java_authenticator_.obj(), | |
116 reinterpret_cast<intptr_t>(callback_wrapper), java_spn.obj(), | |
117 java_server_auth_token.obj(), can_delegate_); | |
118 return ERR_IO_PENDING; | |
119 } | |
120 | |
121 void HttpAuthNegotiateAndroid::Delegate() { | |
122 can_delegate_ = true; | |
123 } | |
124 | |
125 void HttpAuthNegotiateAndroid::SetResultInternal(int result, | |
126 const std::string& raw_token) { | |
127 DCHECK(auth_token_); | |
128 DCHECK(!completion_callback_.is_null()); | |
129 if (result == OK) | |
130 *auth_token_ = "Negotiate " + raw_token; | |
131 completion_callback_.Run(result); | |
132 completion_callback_.Reset(); | |
Ryan Sleevi
2015/06/29 13:56:45
use base::ResetAndReturn() here
aberent
2015/07/02 21:13:34
Done.
| |
133 } | |
134 | |
135 bool HttpAuthNegotiateAndroid::Register(JNIEnv* env) { | |
Ryan Sleevi
2015/06/29 13:56:45
The order of methods in the .cc should match the o
aberent
2015/07/02 21:13:35
Done.
| |
136 return RegisterNativesImpl(env); | |
137 } | |
138 | |
139 } // namespace android | |
140 } // namespace net | |
OLD | NEW |