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