1 // Copyright 2015 The Chromium Authors
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/functional/bind.h"
10 #include "base/functional/callback_helpers.h"
11 #include "base/location.h"
12 #include "base/task/single_thread_task_runner.h"
13 #include "net/base/auth.h"
14 #include "net/base/net_errors.h"
15 #include "net/http/http_auth.h"
16 #include "net/http/http_auth_challenge_tokenizer.h"
17 #include "net/http/http_auth_multi_round_parse.h"
18 #include "net/http/http_auth_preferences.h"
19 #include "net/log/net_log_with_source.h"
20
21 // Must come after all headers that specialize FromJniType() / ToJniType().
22 #include "net/net_jni_headers/HttpNegotiateAuthenticator_jni.h"
23
24 using base::android::AttachCurrentThread;
25 using base::android::ConvertUTF8ToJavaString;
26 using base::android::ConvertJavaStringToUTF8;
27 using base::android::JavaParamRef;
28 using base::android::ScopedJavaLocalRef;
29
30 namespace net::android {
31
JavaNegotiateResultWrapper(const scoped_refptr<base::TaskRunner> & callback_task_runner,base::OnceCallback<void (int,const std::string &)> thread_safe_callback)32 JavaNegotiateResultWrapper::JavaNegotiateResultWrapper(
33 const scoped_refptr<base::TaskRunner>& callback_task_runner,
34 base::OnceCallback<void(int, const std::string&)> thread_safe_callback)
35 : callback_task_runner_(callback_task_runner),
36 thread_safe_callback_(std::move(thread_safe_callback)) {}
37
38 JavaNegotiateResultWrapper::~JavaNegotiateResultWrapper() = default;
39
SetResult(JNIEnv * env,const JavaParamRef<jobject> & obj,int result,const JavaParamRef<jstring> & token)40 void JavaNegotiateResultWrapper::SetResult(JNIEnv* env,
41 const JavaParamRef<jobject>& obj,
42 int result,
43 const JavaParamRef<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;
47 if (token.obj())
48 raw_token = ConvertJavaStringToUTF8(env, token);
49 // Always post, even if we are on the same thread. This guarantees that the
50 // result will be delayed until after the request has completed, which
51 // simplifies the logic. In practice the result will only ever come back on
52 // the original thread in an obscure error case.
53 callback_task_runner_->PostTask(
54 FROM_HERE,
55 base::BindOnce(std::move(thread_safe_callback_), result, raw_token));
56 // We will always get precisely one call to set result for each call to
57 // getNextAuthToken, so we can now delete the callback object, and must
58 // do so to avoid a memory leak.
59 delete this;
60 }
61
HttpAuthNegotiateAndroid(const HttpAuthPreferences * prefs)62 HttpAuthNegotiateAndroid::HttpAuthNegotiateAndroid(
63 const HttpAuthPreferences* prefs)
64 : prefs_(prefs) {
65 JNIEnv* env = AttachCurrentThread();
66 java_authenticator_.Reset(Java_HttpNegotiateAuthenticator_create(
67 env, ConvertUTF8ToJavaString(env, GetAuthAndroidNegotiateAccountType())));
68 }
69
70 HttpAuthNegotiateAndroid::~HttpAuthNegotiateAndroid() = default;
71
Init(const NetLogWithSource & net_log)72 bool HttpAuthNegotiateAndroid::Init(const NetLogWithSource& net_log) {
73 return true;
74 }
75
NeedsIdentity() const76 bool HttpAuthNegotiateAndroid::NeedsIdentity() const {
77 return false;
78 }
79
AllowsExplicitCredentials() const80 bool HttpAuthNegotiateAndroid::AllowsExplicitCredentials() const {
81 return false;
82 }
83
ParseChallenge(net::HttpAuthChallengeTokenizer * tok)84 HttpAuth::AuthorizationResult HttpAuthNegotiateAndroid::ParseChallenge(
85 net::HttpAuthChallengeTokenizer* tok) {
86 if (first_challenge_) {
87 first_challenge_ = false;
88 return net::ParseFirstRoundChallenge(HttpAuth::AUTH_SCHEME_NEGOTIATE, tok);
89 }
90 std::string decoded_auth_token;
91 return net::ParseLaterRoundChallenge(HttpAuth::AUTH_SCHEME_NEGOTIATE, tok,
92 &server_auth_token_,
93 &decoded_auth_token);
94 }
95
GenerateAuthTokenAndroid(const AuthCredentials * credentials,const std::string & spn,const std::string & channel_bindings,std::string * auth_token,net::CompletionOnceCallback callback)96 int HttpAuthNegotiateAndroid::GenerateAuthTokenAndroid(
97 const AuthCredentials* credentials,
98 const std::string& spn,
99 const std::string& channel_bindings,
100 std::string* auth_token,
101 net::CompletionOnceCallback callback) {
102 return GenerateAuthToken(credentials, spn, channel_bindings, auth_token,
103 NetLogWithSource(), std::move(callback));
104 }
105
GenerateAuthToken(const AuthCredentials * credentials,const std::string & spn,const std::string & channel_bindings,std::string * auth_token,const NetLogWithSource & net_log,net::CompletionOnceCallback callback)106 int HttpAuthNegotiateAndroid::GenerateAuthToken(
107 const AuthCredentials* credentials,
108 const std::string& spn,
109 const std::string& channel_bindings,
110 std::string* auth_token,
111 const NetLogWithSource& net_log,
112 net::CompletionOnceCallback callback) {
113 if (GetAuthAndroidNegotiateAccountType().empty()) {
114 // This can happen if there is a policy change, removing the account type,
115 // in the middle of a negotiation.
116 return ERR_UNSUPPORTED_AUTH_SCHEME;
117 }
118 DCHECK(auth_token);
119 DCHECK(completion_callback_.is_null());
120 DCHECK(!callback.is_null());
121
122 auth_token_ = auth_token;
123 completion_callback_ = std::move(callback);
124 scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner =
125 base::SingleThreadTaskRunner::GetCurrentDefault();
126 base::OnceCallback<void(int, const std::string&)> thread_safe_callback =
127 base::BindOnce(&HttpAuthNegotiateAndroid::SetResultInternal,
128 weak_factory_.GetWeakPtr());
129 JNIEnv* env = AttachCurrentThread();
130 ScopedJavaLocalRef<jstring> java_server_auth_token =
131 ConvertUTF8ToJavaString(env, server_auth_token_);
132 ScopedJavaLocalRef<jstring> java_spn = ConvertUTF8ToJavaString(env, spn);
133
134 // It is intentional that callback_wrapper is not owned or deleted by the
135 // HttpAuthNegotiateAndroid object. The Java code will call the callback
136 // asynchronously on a different thread, and needs an object to call it on. As
137 // such, the callback_wrapper must not be deleted until the callback has been
138 // called, whatever happens to the HttpAuthNegotiateAndroid object.
139 //
140 // Unfortunately we have no automated way of managing C++ objects owned by
141 // Java, so the Java code must simply be written to guarantee that the
142 // callback is, in the end, called.
143 JavaNegotiateResultWrapper* callback_wrapper = new JavaNegotiateResultWrapper(
144 callback_task_runner, std::move(thread_safe_callback));
145 Java_HttpNegotiateAuthenticator_getNextAuthToken(
146 env, java_authenticator_, reinterpret_cast<intptr_t>(callback_wrapper),
147 java_spn, java_server_auth_token, can_delegate());
148 return ERR_IO_PENDING;
149 }
150
SetDelegation(HttpAuth::DelegationType delegation_type)151 void HttpAuthNegotiateAndroid::SetDelegation(
152 HttpAuth::DelegationType delegation_type) {
153 DCHECK_NE(delegation_type, HttpAuth::DelegationType::kByKdcPolicy);
154 can_delegate_ = delegation_type == HttpAuth::DelegationType::kUnconstrained;
155 }
156
GetAuthAndroidNegotiateAccountType() const157 std::string HttpAuthNegotiateAndroid::GetAuthAndroidNegotiateAccountType()
158 const {
159 return prefs_->AuthAndroidNegotiateAccountType();
160 }
161
SetResultInternal(int result,const std::string & raw_token)162 void HttpAuthNegotiateAndroid::SetResultInternal(int result,
163 const std::string& raw_token) {
164 DCHECK(auth_token_);
165 DCHECK(!completion_callback_.is_null());
166 if (result == OK)
167 *auth_token_ = "Negotiate " + raw_token;
168 std::move(completion_callback_).Run(result);
169 }
170
171 } // namespace net::android
172