Index: net/android/http_auth_negotiate_android.cc |
diff --git a/net/android/http_auth_negotiate_android.cc b/net/android/http_auth_negotiate_android.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..96bda794920dbca8644964c7d5ba35fb39c1a2d1 |
--- /dev/null |
+++ b/net/android/http_auth_negotiate_android.cc |
@@ -0,0 +1,143 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/android/jni_string.h" |
+#include "base/android/scoped_java_ref.h" |
+#include "base/bind.h" |
+#include "base/callback_internal.h" |
+#include "base/location.h" |
+#include "base/logging.h" |
+#include "base/memory/weak_ptr.h" |
+#include "base/task_runner.h" |
+#include "base/thread_task_runner_handle.h" |
+#include "jni/HttpNegotiateAuthenticator_jni.h" |
+#include "net/android/http_auth_negotiate_android.h" |
+#include "net/base/auth.h" |
+#include "net/base/net_errors.h" |
+#include "net/http/http_auth_challenge_tokenizer.h" |
+#include "net/http/http_auth_handler_negotiate_parse.h" |
+#include "stdint.h" |
+ |
+using base::android::AttachCurrentThread; |
+using base::android::ConvertUTF8ToJavaString; |
+using base::android::ConvertJavaStringToUTF8; |
+using base::android::ScopedJavaLocalRef; |
+ |
+namespace net { |
+namespace android { |
+ |
+JavaNegotiateResultWrapper::JavaNegotiateResultWrapper( |
+ const scoped_refptr<base::TaskRunner>& callback_task_runner, |
+ const base::Callback<void(bool, const std::string&)>& thread_safe_callback) |
+ : callback_task_runner_(callback_task_runner), |
+ thread_safe_callback_(thread_safe_callback) { |
+} |
+ |
+JavaNegotiateResultWrapper::~JavaNegotiateResultWrapper() { |
+} |
+ |
+void JavaNegotiateResultWrapper::SetResult(JNIEnv* env, |
+ jobject obj, |
+ bool result, |
+ jstring token) { |
+ // This will be called on the UI thread, so we have to post a task back to the |
+ // correct thread to actually save the result |
+ std::string raw_token = ConvertJavaStringToUTF8(env, token); |
+ // Always post, even if we are on the same thread. This guarantees that the |
+ // result will be delayed until after the request has completed, which |
+ // simplifies the logic. In practice the result will only ever come back on |
+ // the original thread in an obscure error case. |
+ callback_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(thread_safe_callback_, result, raw_token)); |
+ delete this; |
+} |
+ |
+HttpAuthNegotiateAndroid::HttpAuthNegotiateAndroid( |
+ const std::string& account_type) |
+ : account_type_(account_type), |
+ can_delegate_(false), |
+ first_challenge_(true), |
+ auth_token_(nullptr), |
+ weak_factory_(this) { |
+ DCHECK(!account_type.empty()); |
+ auto env = AttachCurrentThread(); |
+ java_authenticator_.Reset(Java_HttpNegotiateAuthenticator_create( |
+ env, ConvertUTF8ToJavaString(env, account_type).obj())); |
+} |
+ |
+HttpAuthNegotiateAndroid::~HttpAuthNegotiateAndroid() { |
+} |
+ |
+bool HttpAuthNegotiateAndroid::Init() { |
+ return true; |
+} |
+ |
+bool HttpAuthNegotiateAndroid::NeedsIdentity() const { |
+ return false; |
+} |
+ |
+bool HttpAuthNegotiateAndroid::AllowsExplicitCredentials() const { |
+ return false; |
+} |
+ |
+HttpAuth::AuthorizationResult HttpAuthNegotiateAndroid::ParseChallenge( |
+ net::HttpAuthChallengeTokenizer* tok) { |
+ if (first_challenge_) { |
+ first_challenge_ = false; |
+ return net::ParseFirstNegotiateChallenge("negotiate", tok); |
+ } else { |
+ std::string decoded_auth_token; |
+ return net::ParseAnotherNegotiateChallenge( |
+ "negotiate", tok, &server_auth_token_, &decoded_auth_token); |
+ } |
+} |
+ |
+int HttpAuthNegotiateAndroid::GenerateAuthToken( |
+ const AuthCredentials* credentials, |
+ const std::string& spn, |
+ std::string* auth_token, |
+ const net::CompletionCallback& callback) { |
+ DCHECK(auth_token); |
+ DCHECK(completion_callback_.is_null()); |
+ DCHECK(!callback.is_null()); |
+ |
+ auth_token_ = auth_token; |
+ completion_callback_ = callback; |
+ auto callback_task_runner = base::ThreadTaskRunnerHandle::Get(); |
+ auto thread_safe_callback = base::Bind( |
+ &HttpAuthNegotiateAndroid::SetResultInternal, weak_factory_.GetWeakPtr()); |
+ auto env = AttachCurrentThread(); |
+ auto java_server_auth_token = |
+ ConvertUTF8ToJavaString(env, server_auth_token_); |
+ auto java_spn = ConvertUTF8ToJavaString(env, spn); |
+ auto java_account_type = ConvertUTF8ToJavaString(env, account_type_); |
+ auto callback_wrapper = new JavaNegotiateResultWrapper(callback_task_runner, |
+ thread_safe_callback); |
+ Java_HttpNegotiateAuthenticator_getNextAuthToken( |
+ env, java_authenticator_.obj(), |
+ reinterpret_cast<intptr_t>(callback_wrapper), java_spn.obj(), |
+ java_server_auth_token.obj(), can_delegate_); |
+ return ERR_IO_PENDING; |
+} |
+ |
+void HttpAuthNegotiateAndroid::Delegate() { |
+ can_delegate_ = true; |
+} |
+ |
+void HttpAuthNegotiateAndroid::SetResultInternal(bool result, |
+ const std::string& raw_token) { |
+ DCHECK(auth_token_); |
+ DCHECK(!completion_callback_.is_null()); |
+ if (result) |
+ *auth_token_ = "Negotiate " + raw_token; |
+ completion_callback_.Run(result ? OK : ERR_INVALID_AUTH_CREDENTIALS); |
+ completion_callback_.Reset(); |
+} |
+ |
+bool HttpAuthNegotiateAndroid::Register(JNIEnv* env) { |
+ return RegisterNativesImpl(env); |
+} |
+ |
+} // namespace android |
+} // namespace net |