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_android_auth_negotiate.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/strings/string_util.h" |
| 12 |
| 13 #include "jni/HttpNegotiateAuthenticator_jni.h" |
| 14 |
| 15 #include "net/base/net_errors.h" |
| 16 #include "net/http/http_auth.h" |
| 17 #include "net/http/http_auth_challenge_tokenizer.h" |
| 18 |
| 19 using base::android::AttachCurrentThread; |
| 20 using base::android::ConvertUTF8ToJavaString; |
| 21 using base::android::ConvertJavaStringToUTF8; |
| 22 using base::android::ScopedJavaLocalRef; |
| 23 |
| 24 namespace net { |
| 25 namespace android { |
| 26 |
| 27 AndroidAuthNegotiate::AndroidAuthNegotiate(const std::string& account_type, |
| 28 const std::string& scheme) |
| 29 : account_type_(account_type), |
| 30 scheme_(scheme), |
| 31 can_delegate_(false), |
| 32 first_challenge_(true), |
| 33 server_auth_token_(""), |
| 34 java_authenticator_(Java_HttpNegotiateAuthenticator_create( |
| 35 AttachCurrentThread(), |
| 36 reinterpret_cast<intptr_t>(this))) { |
| 37 } |
| 38 |
| 39 AndroidAuthNegotiate::~AndroidAuthNegotiate() { |
| 40 } |
| 41 |
| 42 bool AndroidAuthNegotiate::Init() { |
| 43 return !account_type_.empty(); |
| 44 } |
| 45 |
| 46 bool AndroidAuthNegotiate::NeedsIdentity() const { |
| 47 return false; |
| 48 } |
| 49 |
| 50 bool AndroidAuthNegotiate::AllowsExplicitCredentials() const { |
| 51 return false; |
| 52 } |
| 53 |
| 54 HttpAuth::AuthorizationResult AndroidAuthNegotiate::ParseChallenge( |
| 55 net::HttpAuthChallengeTokenizer* tok) { |
| 56 // Verify the challenge's auth-scheme. |
| 57 if (!LowerCaseEqualsASCII(tok->scheme(), |
| 58 base::StringToLowerASCII(scheme_).c_str())) |
| 59 return HttpAuth::AUTHORIZATION_RESULT_INVALID; |
| 60 |
| 61 std::string encoded_auth_token = tok->base64_param(); |
| 62 if (encoded_auth_token.empty()) { |
| 63 if (!first_challenge_) { |
| 64 return HttpAuth::AUTHORIZATION_RESULT_REJECT; |
| 65 } |
| 66 return HttpAuth::AUTHORIZATION_RESULT_ACCEPT; |
| 67 } else { |
| 68 // If a context has not already been established, additional tokens should |
| 69 // not be present in the auth challenge. |
| 70 if (first_challenge_) |
| 71 return HttpAuth::AUTHORIZATION_RESULT_INVALID; |
| 72 } |
| 73 // Make sure the additional token is base64 encoded. |
| 74 std::string decoded_auth_token; |
| 75 bool base64_rv = base::Base64Decode(encoded_auth_token, &decoded_auth_token); |
| 76 if (!base64_rv) |
| 77 return HttpAuth::AUTHORIZATION_RESULT_INVALID; |
| 78 server_auth_token_ = encoded_auth_token; |
| 79 return HttpAuth::AUTHORIZATION_RESULT_ACCEPT; |
| 80 } |
| 81 |
| 82 int AndroidAuthNegotiate::GenerateAuthToken( |
| 83 const AuthCredentials* credentials, |
| 84 const std::string& spn, |
| 85 std::string* auth_token, |
| 86 const net::CompletionCallback& callback) { |
| 87 DCHECK(auth_token); |
| 88 auth_token_ = auth_token; |
| 89 completion_callback_ = callback; |
| 90 JNIEnv* env = AttachCurrentThread(); |
| 91 ScopedJavaLocalRef<jstring> java_server_auth_token = |
| 92 ConvertUTF8ToJavaString(env, server_auth_token_); |
| 93 ScopedJavaLocalRef<jstring> java_spn = ConvertUTF8ToJavaString(env, spn); |
| 94 ScopedJavaLocalRef<jstring> java_account_type = |
| 95 ConvertUTF8ToJavaString(env, account_type_); |
| 96 bool result = Java_HttpNegotiateAuthenticator_getNextAuthToken( |
| 97 env, java_authenticator_.obj(), java_account_type.obj(), java_spn.obj(), |
| 98 java_server_auth_token.obj()); |
| 99 return result ? ERR_IO_PENDING : ERR_INVALID_AUTH_CREDENTIALS; |
| 100 } |
| 101 |
| 102 void AndroidAuthNegotiate::Delegate() { |
| 103 can_delegate_ = true; |
| 104 } |
| 105 |
| 106 void AndroidAuthNegotiate::SetResult(JNIEnv* env, |
| 107 jobject obj, |
| 108 bool result, |
| 109 jstring token) { |
| 110 if (result) |
| 111 *auth_token_ = ConvertJavaStringToUTF8(env, token); |
| 112 completion_callback_.Run(result); |
| 113 } |
| 114 |
| 115 bool RegisterHttpNegotiateAuthenticator(JNIEnv* env) { |
| 116 return RegisterNativesImpl(env); |
| 117 } |
| 118 |
| 119 } // namespace android |
| 120 } // namespace net |
OLD | NEW |