| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 #include "net/android/http_auth_negotiate_android.h" | 5 #include "net/android/http_auth_negotiate_android.h" |
| 6 | 6 |
| 7 #include "base/android/jni_string.h" | 7 #include "base/android/jni_string.h" |
| 8 #include "base/android/scoped_java_ref.h" | 8 #include "base/android/scoped_java_ref.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "jni/HttpNegotiateAuthenticator_jni.h" | 13 #include "jni/HttpNegotiateAuthenticator_jni.h" |
| 14 #include "net/base/auth.h" | 14 #include "net/base/auth.h" |
| 15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 16 #include "net/http/http_auth_challenge_tokenizer.h" | 16 #include "net/http/http_auth_challenge_tokenizer.h" |
| 17 #include "net/http/http_auth_multi_round_parse.h" | 17 #include "net/http/http_auth_multi_round_parse.h" |
| 18 #include "net/http/http_auth_preferences.h" |
| 18 | 19 |
| 19 using base::android::AttachCurrentThread; | 20 using base::android::AttachCurrentThread; |
| 20 using base::android::ConvertUTF8ToJavaString; | 21 using base::android::ConvertUTF8ToJavaString; |
| 21 using base::android::ConvertJavaStringToUTF8; | 22 using base::android::ConvertJavaStringToUTF8; |
| 22 using base::android::ScopedJavaLocalRef; | 23 using base::android::ScopedJavaLocalRef; |
| 23 | 24 |
| 24 namespace net { | 25 namespace net { |
| 25 namespace android { | 26 namespace android { |
| 26 | 27 |
| 27 JavaNegotiateResultWrapper::JavaNegotiateResultWrapper( | 28 JavaNegotiateResultWrapper::JavaNegotiateResultWrapper( |
| (...skipping 19 matching lines...) Expand all Loading... |
| 47 // the original thread in an obscure error case. | 48 // the original thread in an obscure error case. |
| 48 callback_task_runner_->PostTask( | 49 callback_task_runner_->PostTask( |
| 49 FROM_HERE, base::Bind(thread_safe_callback_, result, raw_token)); | 50 FROM_HERE, base::Bind(thread_safe_callback_, result, raw_token)); |
| 50 // We will always get precisely one call to set result for each call to | 51 // We will always get precisely one call to set result for each call to |
| 51 // getNextAuthToken, so we can now delete the callback object, and must | 52 // getNextAuthToken, so we can now delete the callback object, and must |
| 52 // do so to avoid a memory leak. | 53 // do so to avoid a memory leak. |
| 53 delete this; | 54 delete this; |
| 54 } | 55 } |
| 55 | 56 |
| 56 HttpAuthNegotiateAndroid::HttpAuthNegotiateAndroid( | 57 HttpAuthNegotiateAndroid::HttpAuthNegotiateAndroid( |
| 57 const std::string& account_type) | 58 const HttpAuthPreferences* prefs) |
| 58 : account_type_(account_type), | 59 : prefs_(prefs), |
| 59 can_delegate_(false), | 60 can_delegate_(false), |
| 60 first_challenge_(true), | 61 first_challenge_(true), |
| 61 auth_token_(nullptr), | 62 auth_token_(nullptr), |
| 62 weak_factory_(this) { | 63 weak_factory_(this) { |
| 63 DCHECK(!account_type.empty()); | |
| 64 JNIEnv* env = AttachCurrentThread(); | 64 JNIEnv* env = AttachCurrentThread(); |
| 65 java_authenticator_.Reset(Java_HttpNegotiateAuthenticator_create( | 65 java_authenticator_.Reset(Java_HttpNegotiateAuthenticator_create( |
| 66 env, ConvertUTF8ToJavaString(env, account_type).obj())); | 66 env, |
| 67 ConvertUTF8ToJavaString(env, prefs->AuthAndroidNegotiateAccountType()) |
| 68 .obj())); |
| 67 } | 69 } |
| 68 | 70 |
| 69 HttpAuthNegotiateAndroid::~HttpAuthNegotiateAndroid() { | 71 HttpAuthNegotiateAndroid::~HttpAuthNegotiateAndroid() { |
| 70 } | 72 } |
| 71 | 73 |
| 72 bool HttpAuthNegotiateAndroid::Register(JNIEnv* env) { | 74 bool HttpAuthNegotiateAndroid::Register(JNIEnv* env) { |
| 73 return RegisterNativesImpl(env); | 75 return RegisterNativesImpl(env); |
| 74 } | 76 } |
| 75 | 77 |
| 76 bool HttpAuthNegotiateAndroid::Init() { | 78 bool HttpAuthNegotiateAndroid::Init() { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 94 std::string decoded_auth_token; | 96 std::string decoded_auth_token; |
| 95 return net::ParseLaterRoundChallenge("negotiate", tok, &server_auth_token_, | 97 return net::ParseLaterRoundChallenge("negotiate", tok, &server_auth_token_, |
| 96 &decoded_auth_token); | 98 &decoded_auth_token); |
| 97 } | 99 } |
| 98 | 100 |
| 99 int HttpAuthNegotiateAndroid::GenerateAuthToken( | 101 int HttpAuthNegotiateAndroid::GenerateAuthToken( |
| 100 const AuthCredentials* credentials, | 102 const AuthCredentials* credentials, |
| 101 const std::string& spn, | 103 const std::string& spn, |
| 102 std::string* auth_token, | 104 std::string* auth_token, |
| 103 const net::CompletionCallback& callback) { | 105 const net::CompletionCallback& callback) { |
| 106 if (prefs_->AuthAndroidNegotiateAccountType().empty()) { |
| 107 // This can happen if there is a policy change, removing the account type, |
| 108 // in the middle of a negotiation. |
| 109 return ERR_UNSUPPORTED_AUTH_SCHEME; |
| 110 } |
| 104 DCHECK(auth_token); | 111 DCHECK(auth_token); |
| 105 DCHECK(completion_callback_.is_null()); | 112 DCHECK(completion_callback_.is_null()); |
| 106 DCHECK(!callback.is_null()); | 113 DCHECK(!callback.is_null()); |
| 107 | 114 |
| 108 auth_token_ = auth_token; | 115 auth_token_ = auth_token; |
| 109 completion_callback_ = callback; | 116 completion_callback_ = callback; |
| 110 scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner = | 117 scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner = |
| 111 base::ThreadTaskRunnerHandle::Get(); | 118 base::ThreadTaskRunnerHandle::Get(); |
| 112 base::Callback<void(int, const std::string&)> thread_safe_callback = | 119 base::Callback<void(int, const std::string&)> thread_safe_callback = |
| 113 base::Bind(&HttpAuthNegotiateAndroid::SetResultInternal, | 120 base::Bind(&HttpAuthNegotiateAndroid::SetResultInternal, |
| 114 weak_factory_.GetWeakPtr()); | 121 weak_factory_.GetWeakPtr()); |
| 115 JNIEnv* env = AttachCurrentThread(); | 122 JNIEnv* env = AttachCurrentThread(); |
| 116 ScopedJavaLocalRef<jstring> java_server_auth_token = | 123 ScopedJavaLocalRef<jstring> java_server_auth_token = |
| 117 ConvertUTF8ToJavaString(env, server_auth_token_); | 124 ConvertUTF8ToJavaString(env, server_auth_token_); |
| 118 ScopedJavaLocalRef<jstring> java_spn = ConvertUTF8ToJavaString(env, spn); | 125 ScopedJavaLocalRef<jstring> java_spn = ConvertUTF8ToJavaString(env, spn); |
| 119 ScopedJavaLocalRef<jstring> java_account_type = | 126 ScopedJavaLocalRef<jstring> java_account_type = |
| 120 ConvertUTF8ToJavaString(env, account_type_); | 127 ConvertUTF8ToJavaString(env, prefs_->AuthAndroidNegotiateAccountType()); |
| 121 | 128 |
| 122 // It is intentional that callback_wrapper is not owned or deleted by the | 129 // It is intentional that callback_wrapper is not owned or deleted by the |
| 123 // HttpAuthNegotiateAndroid object. The Java code will call the callback | 130 // HttpAuthNegotiateAndroid object. The Java code will call the callback |
| 124 // asynchronously on a different thread, and needs an object to call it on. As | 131 // asynchronously on a different thread, and needs an object to call it on. As |
| 125 // such, the callback_wrapper must not be deleted until the callback has been | 132 // such, the callback_wrapper must not be deleted until the callback has been |
| 126 // called, whatever happens to the HttpAuthNegotiateAndroid object. | 133 // called, whatever happens to the HttpAuthNegotiateAndroid object. |
| 127 // | 134 // |
| 128 // Unfortunately we have no automated way of managing C++ objects owned by | 135 // Unfortunately we have no automated way of managing C++ objects owned by |
| 129 // Java, so the Java code must simply be written to guarantee that the | 136 // Java, so the Java code must simply be written to guarantee that the |
| 130 // callback is, in the end, called. | 137 // callback is, in the end, called. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 145 const std::string& raw_token) { | 152 const std::string& raw_token) { |
| 146 DCHECK(auth_token_); | 153 DCHECK(auth_token_); |
| 147 DCHECK(!completion_callback_.is_null()); | 154 DCHECK(!completion_callback_.is_null()); |
| 148 if (result == OK) | 155 if (result == OK) |
| 149 *auth_token_ = "Negotiate " + raw_token; | 156 *auth_token_ = "Negotiate " + raw_token; |
| 150 base::ResetAndReturn(&completion_callback_).Run(result); | 157 base::ResetAndReturn(&completion_callback_).Run(result); |
| 151 } | 158 } |
| 152 | 159 |
| 153 } // namespace android | 160 } // namespace android |
| 154 } // namespace net | 161 } // namespace net |
| OLD | NEW |