Index: net/android/http_android_auth_negotiate.cc |
diff --git a/net/android/http_android_auth_negotiate.cc b/net/android/http_android_auth_negotiate.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f01e31f330430a8a01d9f465b91916ec477a6b5c |
--- /dev/null |
+++ b/net/android/http_android_auth_negotiate.cc |
@@ -0,0 +1,119 @@ |
+// 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_android_auth_negotiate.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/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 { |
+ |
+AndroidAuthNegotiate::AndroidAuthNegotiate(const std::string& account_type, |
+ const std::string& scheme) |
+ : account_type_(account_type), |
+ scheme_(scheme), |
+ can_delegate_(false), |
+ first_challenge_(true) { |
+ JNIEnv* env = AttachCurrentThread(); |
+ java_authenticator_.Reset(Java_HttpNegotiateAuthenticator_create( |
+ env, reinterpret_cast<intptr_t>(this), |
+ ConvertUTF8ToJavaString(env, account_type).obj())); |
+} |
+ |
+AndroidAuthNegotiate::~AndroidAuthNegotiate() { |
+} |
+ |
+bool AndroidAuthNegotiate::Init() { |
+ return !account_type_.empty(); |
+} |
+ |
+bool AndroidAuthNegotiate::NeedsIdentity() const { |
+ return false; |
+} |
+ |
+bool AndroidAuthNegotiate::AllowsExplicitCredentials() const { |
+ return false; |
+} |
+ |
+HttpAuth::AuthorizationResult AndroidAuthNegotiate::ParseChallenge( |
+ net::HttpAuthChallengeTokenizer* tok) { |
+ // Verify the challenge's auth-scheme. |
+ if (!LowerCaseEqualsASCII(tok->scheme(), |
+ base::StringToLowerASCII(scheme_).c_str())) |
+ return HttpAuth::AUTHORIZATION_RESULT_INVALID; |
+ |
+ std::string encoded_auth_token = tok->base64_param(); |
+ if (encoded_auth_token.empty()) { |
+ if (!first_challenge_) |
+ return HttpAuth::AUTHORIZATION_RESULT_REJECT; |
+ return HttpAuth::AUTHORIZATION_RESULT_ACCEPT; |
+ } else { |
+ // If a context has not already been established, additional tokens should |
+ // not be present in the auth challenge. |
+ if (first_challenge_) |
+ return HttpAuth::AUTHORIZATION_RESULT_INVALID; |
+ } |
+ // Make sure the additional token is base64 encoded. |
+ std::string decoded_auth_token; |
+ bool base64_rv = base::Base64Decode(encoded_auth_token, &decoded_auth_token); |
+ if (!base64_rv) |
+ return HttpAuth::AUTHORIZATION_RESULT_INVALID; |
+ server_auth_token_ = encoded_auth_token; |
+ return HttpAuth::AUTHORIZATION_RESULT_ACCEPT; |
+} |
+ |
+int AndroidAuthNegotiate::GenerateAuthToken( |
+ const AuthCredentials* credentials, |
+ const std::string& spn, |
+ std::string* auth_token, |
+ const net::CompletionCallback& callback) { |
+ DCHECK(auth_token); |
+ auth_token_ = auth_token; |
+ completion_callback_ = callback; |
+ 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; |
+} |
+ |
+void AndroidAuthNegotiate::Delegate() { |
+ can_delegate_ = true; |
+} |
+ |
+void AndroidAuthNegotiate::SetResult(JNIEnv* env, |
+ jobject obj, |
+ bool result, |
+ jstring token) { |
+ if (result) |
+ *auth_token_ = scheme_ + " " + ConvertJavaStringToUTF8(env, token); |
+ completion_callback_.Run(result); |
+} |
+ |
+bool AndroidAuthNegotiate::Register(JNIEnv* env) { |
+ return RegisterNativesImpl(env); |
+} |
+ |
+} // namespace android |
+} // namespace net |