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 JNIEnv* env = AttachCurrentThread(); |
| 34 java_authenticator_.Reset(Java_HttpNegotiateAuthenticator_create( |
| 35 env, reinterpret_cast<intptr_t>(this), |
| 36 ConvertUTF8ToJavaString(env, account_type).obj())); |
| 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 return HttpAuth::AUTHORIZATION_RESULT_ACCEPT; |
| 66 } else { |
| 67 // If a context has not already been established, additional tokens should |
| 68 // not be present in the auth challenge. |
| 69 if (first_challenge_) |
| 70 return HttpAuth::AUTHORIZATION_RESULT_INVALID; |
| 71 } |
| 72 // Make sure the additional token is base64 encoded. |
| 73 std::string decoded_auth_token; |
| 74 bool base64_rv = base::Base64Decode(encoded_auth_token, &decoded_auth_token); |
| 75 if (!base64_rv) |
| 76 return HttpAuth::AUTHORIZATION_RESULT_INVALID; |
| 77 server_auth_token_ = encoded_auth_token; |
| 78 return HttpAuth::AUTHORIZATION_RESULT_ACCEPT; |
| 79 } |
| 80 |
| 81 int AndroidAuthNegotiate::GenerateAuthToken( |
| 82 const AuthCredentials* credentials, |
| 83 const std::string& spn, |
| 84 std::string* auth_token, |
| 85 const net::CompletionCallback& callback) { |
| 86 DCHECK(auth_token); |
| 87 auth_token_ = auth_token; |
| 88 completion_callback_ = callback; |
| 89 JNIEnv* env = AttachCurrentThread(); |
| 90 ScopedJavaLocalRef<jstring> java_server_auth_token = |
| 91 ConvertUTF8ToJavaString(env, server_auth_token_); |
| 92 ScopedJavaLocalRef<jstring> java_spn = ConvertUTF8ToJavaString(env, spn); |
| 93 ScopedJavaLocalRef<jstring> java_account_type = |
| 94 ConvertUTF8ToJavaString(env, account_type_); |
| 95 bool result = Java_HttpNegotiateAuthenticator_getNextAuthToken( |
| 96 env, java_authenticator_.obj(), java_spn.obj(), |
| 97 java_server_auth_token.obj(), can_delegate_); |
| 98 return result ? ERR_IO_PENDING : ERR_INVALID_AUTH_CREDENTIALS; |
| 99 } |
| 100 |
| 101 void AndroidAuthNegotiate::Delegate() { |
| 102 can_delegate_ = true; |
| 103 } |
| 104 |
| 105 void AndroidAuthNegotiate::SetResult(JNIEnv* env, |
| 106 jobject obj, |
| 107 bool result, |
| 108 jstring token) { |
| 109 if (result) |
| 110 *auth_token_ = scheme_ + " " + ConvertJavaStringToUTF8(env, token); |
| 111 completion_callback_.Run(result); |
| 112 } |
| 113 |
| 114 bool AndroidAuthNegotiate::Register(JNIEnv* env) { |
| 115 return RegisterNativesImpl(env); |
| 116 } |
| 117 |
| 118 } // namespace android |
| 119 } // namespace net |
OLD | NEW |