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 "net/android/http_auth_negotiate_android.h" | |
6 | |
7 #include "base/android/jni_android.h" | |
8 #include "base/android/jni_string.h" | |
9 #include "base/android/scoped_java_ref.h" | |
10 #include "base/base64.h" | |
11 #include "base/location.h" | |
12 #include "base/logging.h" | |
13 #include "base/message_loop/message_loop.h" | |
14 #include "base/strings/string_util.h" | |
15 #include "jni/HttpNegotiateAuthenticator_jni.h" | |
16 #include "net/base/net_errors.h" | |
17 #include "net/http/http_auth.h" | |
18 #include "net/http/http_auth_challenge_tokenizer.h" | |
19 | |
20 using base::android::AttachCurrentThread; | |
21 using base::android::ConvertUTF8ToJavaString; | |
22 using base::android::ConvertJavaStringToUTF8; | |
23 using base::android::ScopedJavaLocalRef; | |
24 | |
25 namespace net { | |
26 namespace android { | |
27 | |
28 HttpAuthNegotiateAndroid::HttpAuthNegotiateAndroid( | |
29 const std::string& account_type) | |
30 : account_type_(account_type), | |
31 can_delegate_(false), | |
32 first_challenge_(true), | |
33 auth_token_(nullptr), | |
34 weak_factory_(this) { | |
35 DCHECK(!account_type.empty()); | |
36 JNIEnv* env = AttachCurrentThread(); | |
37 java_authenticator_.Reset(Java_HttpNegotiateAuthenticator_create( | |
38 env, reinterpret_cast<intptr_t>(this), | |
39 ConvertUTF8ToJavaString(env, account_type).obj())); | |
40 } | |
41 | |
42 HttpAuthNegotiateAndroid::~HttpAuthNegotiateAndroid() { | |
43 } | |
44 | |
45 bool HttpAuthNegotiateAndroid::Init() { | |
46 return true; | |
47 } | |
48 | |
49 bool HttpAuthNegotiateAndroid::NeedsIdentity() const { | |
50 return false; | |
51 } | |
52 | |
53 bool HttpAuthNegotiateAndroid::AllowsExplicitCredentials() const { | |
54 return false; | |
55 } | |
56 | |
57 int HttpAuthNegotiateAndroid::GenerateAuthToken( | |
58 const AuthCredentials* credentials, | |
59 const std::string& spn, | |
60 std::string* auth_token, | |
61 const net::CompletionCallback& callback) { | |
62 DCHECK(auth_token); | |
63 DCHECK(completion_callback_.is_null()); | |
64 DCHECK(!callback.is_null()); | |
65 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.
| |
66 completion_callback_ = callback; | |
67 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.
| |
68 thread_safe_callback_ = base::Bind( | |
69 &HttpAuthNegotiateAndroid::SetResultInternal, weak_factory_.GetWeakPtr()); | |
70 JNIEnv* env = AttachCurrentThread(); | |
71 ScopedJavaLocalRef<jstring> java_server_auth_token = | |
72 ConvertUTF8ToJavaString(env, server_auth_token_); | |
73 ScopedJavaLocalRef<jstring> java_spn = ConvertUTF8ToJavaString(env, spn); | |
74 ScopedJavaLocalRef<jstring> java_account_type = | |
75 ConvertUTF8ToJavaString(env, account_type_); | |
76 bool result = Java_HttpNegotiateAuthenticator_getNextAuthToken( | |
77 env, java_authenticator_.obj(), java_spn.obj(), | |
78 java_server_auth_token.obj(), can_delegate_); | |
79 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
| |
80 } | |
81 | |
82 void HttpAuthNegotiateAndroid::Delegate() { | |
83 can_delegate_ = true; | |
84 } | |
85 | |
86 void HttpAuthNegotiateAndroid::SetResultInternal(bool result, | |
87 const std::string& raw_token) { | |
88 DCHECK(auth_token_); | |
89 DCHECK(!completion_callback_.is_null()); | |
90 if (result) | |
91 *auth_token_ = "Negotiate " + raw_token; | |
92 completion_callback_.Run(result ? OK : ERR_INVALID_AUTH_CREDENTIALS); | |
93 completion_callback_.Reset(); | |
94 } | |
95 | |
96 void HttpAuthNegotiateAndroid::SetResult(JNIEnv* env, | |
97 jobject obj, | |
98 bool result, | |
99 jstring token) { | |
100 // This will be called on the UI thread, so we have to post a task back to the | |
101 // correct thread to actually save the result | |
102 std::string raw_token = ConvertJavaStringToUTF8(env, token); | |
103 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
| |
104 FROM_HERE, base::Bind(thread_safe_callback_, result, raw_token)); | |
105 } | |
106 | |
107 bool HttpAuthNegotiateAndroid::Register(JNIEnv* env) { | |
108 return RegisterNativesImpl(env); | |
109 } | |
110 | |
111 } // namespace android | |
112 } // namespace net | |
OLD | NEW |