Chromium Code Reviews| 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..09dc6a6e3407fa09965b2078b90c4ed9dffcc695 |
| --- /dev/null |
| +++ b/net/android/http_auth_negotiate_android.cc |
| @@ -0,0 +1,112 @@ |
| +// 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 "net/android/http_auth_negotiate_android.h" |
| + |
| +#include "base/android/jni_android.h" |
| +#include "base/android/jni_string.h" |
| +#include "base/android/scoped_java_ref.h" |
| +#include "base/base64.h" |
| +#include "base/location.h" |
| +#include "base/logging.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/strings/string_util.h" |
| +#include "jni/HttpNegotiateAuthenticator_jni.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/http/http_auth.h" |
| +#include "net/http/http_auth_challenge_tokenizer.h" |
| + |
| +using base::android::AttachCurrentThread; |
| +using base::android::ConvertUTF8ToJavaString; |
| +using base::android::ConvertJavaStringToUTF8; |
| +using base::android::ScopedJavaLocalRef; |
| + |
| +namespace net { |
| +namespace android { |
| + |
| +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()); |
| + JNIEnv* env = AttachCurrentThread(); |
| + java_authenticator_.Reset(Java_HttpNegotiateAuthenticator_create( |
| + env, reinterpret_cast<intptr_t>(this), |
| + ConvertUTF8ToJavaString(env, account_type).obj())); |
| +} |
| + |
| +HttpAuthNegotiateAndroid::~HttpAuthNegotiateAndroid() { |
| +} |
| + |
| +bool HttpAuthNegotiateAndroid::Init() { |
| + return true; |
| +} |
| + |
| +bool HttpAuthNegotiateAndroid::NeedsIdentity() const { |
| + return false; |
| +} |
| + |
| +bool HttpAuthNegotiateAndroid::AllowsExplicitCredentials() const { |
| + return false; |
| +} |
| + |
| +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; |
|
Ryan Sleevi
2015/06/16 01:07:46
nit: a newline between 64 & 65 can help readabilit
aberent
2015/06/19 15:06:24
Done.
|
| + completion_callback_ = callback; |
| + callback_task_runner_ = base::MessageLoop::current()->task_runner(); |
|
Ryan Sleevi
2015/06/16 01:07:46
BUG: You should be using base::ThreadTaskRunnerHan
aberent
2015/06/19 15:06:24
Done.
|
| + thread_safe_callback_ = base::Bind( |
| + &HttpAuthNegotiateAndroid::SetResultInternal, weak_factory_.GetWeakPtr()); |
| + JNIEnv* env = AttachCurrentThread(); |
| + ScopedJavaLocalRef<jstring> java_server_auth_token = |
| + ConvertUTF8ToJavaString(env, server_auth_token_); |
| + ScopedJavaLocalRef<jstring> java_spn = ConvertUTF8ToJavaString(env, spn); |
| + ScopedJavaLocalRef<jstring> java_account_type = |
| + ConvertUTF8ToJavaString(env, account_type_); |
| + bool result = Java_HttpNegotiateAuthenticator_getNextAuthToken( |
| + env, java_authenticator_.obj(), java_spn.obj(), |
| + java_server_auth_token.obj(), can_delegate_); |
| + return result ? ERR_IO_PENDING : ERR_INVALID_AUTH_CREDENTIALS; |
|
Ryan Sleevi
2015/06/16 01:07:46
Is this the right error code? If it synchronously
aberent
2015/06/19 15:06:24
Actually the only way it could fail synchronously
|
| +} |
| + |
| +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(); |
| +} |
| + |
| +void HttpAuthNegotiateAndroid::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); |
| + callback_task_runner_->PostTask( |
|
Ryan Sleevi
2015/06/16 01:07:46
DANGEROUS PATTERN: This is an object that lives on
aberent
2015/06/19 15:06:24
Fixed with a helper class, similar to the ::Core m
|
| + FROM_HERE, base::Bind(thread_safe_callback_, result, raw_token)); |
| +} |
| + |
| +bool HttpAuthNegotiateAndroid::Register(JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| +} |
| + |
| +} // namespace android |
| +} // namespace net |