1 /*
2   Copyright (c) DataStax, Inc.
3 
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7 
8   http://www.apache.org/licenses/LICENSE-2.0
9 
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 */
16 
17 #include "auth.hpp"
18 #include "external.hpp"
19 #include "string.hpp"
20 
21 #include "cassandra.h"
22 
23 #include <algorithm>
24 
25 using namespace datastax;
26 using namespace datastax::internal::core;
27 
28 extern "C" {
29 
cass_authenticator_address(const CassAuthenticator * auth,CassInet * address)30 void cass_authenticator_address(const CassAuthenticator* auth, CassInet* address) {
31   address->address_length = auth->address().to_inet(address->address);
32 }
33 
cass_authenticator_hostname(const CassAuthenticator * auth,size_t * length)34 const char* cass_authenticator_hostname(const CassAuthenticator* auth, size_t* length) {
35   if (length != NULL) *length = auth->hostname().length();
36   return auth->hostname().c_str();
37 }
38 
cass_authenticator_class_name(const CassAuthenticator * auth,size_t * length)39 const char* cass_authenticator_class_name(const CassAuthenticator* auth, size_t* length) {
40   if (length != NULL) *length = auth->class_name().length();
41   return auth->class_name().c_str();
42 }
43 
cass_authenticator_exchange_data(CassAuthenticator * auth)44 void* cass_authenticator_exchange_data(CassAuthenticator* auth) { return auth->exchange_data(); }
45 
cass_authenticator_set_exchange_data(CassAuthenticator * auth,void * exchange_data)46 void cass_authenticator_set_exchange_data(CassAuthenticator* auth, void* exchange_data) {
47   auth->set_exchange_data(exchange_data);
48 }
49 
cass_authenticator_response(CassAuthenticator * auth,size_t size)50 char* cass_authenticator_response(CassAuthenticator* auth, size_t size) {
51   String* response = auth->response();
52 
53   if (response != NULL) {
54     response->resize(size, 0);
55     return &(*response)[0];
56   }
57 
58   return NULL;
59 }
60 
cass_authenticator_set_response(CassAuthenticator * auth,const char * response,size_t response_size)61 void cass_authenticator_set_response(CassAuthenticator* auth, const char* response,
62                                      size_t response_size) {
63   if (auth->response() != NULL) {
64     auth->response()->assign(response, response_size);
65   }
66 }
67 
cass_authenticator_set_error(CassAuthenticator * auth,const char * message)68 void cass_authenticator_set_error(CassAuthenticator* auth, const char* message) {
69   cass_authenticator_set_error_n(auth, message, SAFE_STRLEN(message));
70 }
71 
cass_authenticator_set_error_n(CassAuthenticator * auth,const char * message,size_t message_length)72 void cass_authenticator_set_error_n(CassAuthenticator* auth, const char* message,
73                                     size_t message_length) {
74   auth->set_error(String(message, message_length));
75 }
76 
77 } // extern "C"
78 
initial_response(String * response)79 bool PlainTextAuthenticator::initial_response(String* response) {
80   response->reserve(username_.size() + password_.size() + 2);
81   response->push_back(0);
82   response->append(username_);
83   response->push_back(0);
84   response->append(password_);
85   return true;
86 }
87 
evaluate_challenge(const String & token,String * response)88 bool PlainTextAuthenticator::evaluate_challenge(const String& token, String* response) {
89   // no-op
90   return true;
91 }
92 
success(const String & token)93 bool PlainTextAuthenticator::success(const String& token) {
94   // no-op
95   return true;
96 }
97 
ExternalAuthenticator(const Address & address,const String & hostname,const String & class_name,const CassAuthenticatorCallbacks * callbacks,void * data)98 ExternalAuthenticator::ExternalAuthenticator(const Address& address, const String& hostname,
99                                              const String& class_name,
100                                              const CassAuthenticatorCallbacks* callbacks,
101                                              void* data)
102     : address_(address)
103     , hostname_(hostname)
104     , class_name_(class_name)
105     , response_(NULL)
106     , callbacks_(callbacks)
107     , data_(data)
108     , exchange_data_(NULL) {}
109 
~ExternalAuthenticator()110 ExternalAuthenticator::~ExternalAuthenticator() {
111   response_ = NULL;
112   if (callbacks_->cleanup_callback != NULL) {
113     callbacks_->cleanup_callback(CassAuthenticator::to(this), data_);
114   }
115 }
116 
initial_response(String * response)117 bool ExternalAuthenticator::initial_response(String* response) {
118   if (callbacks_->initial_callback == NULL) {
119     return true;
120   }
121   response_ = response;
122   error_.clear();
123   callbacks_->initial_callback(CassAuthenticator::to(this), data_);
124   return error_.empty();
125 }
126 
evaluate_challenge(const String & token,String * response)127 bool ExternalAuthenticator::evaluate_challenge(const String& token, String* response) {
128   if (callbacks_->challenge_callback == NULL) {
129     return true;
130   }
131   response_ = response;
132   error_.clear();
133   callbacks_->challenge_callback(CassAuthenticator::to(this), data_, token.data(), token.size());
134   return error_.empty();
135 }
136 
success(const String & token)137 bool ExternalAuthenticator::success(const String& token) {
138   if (callbacks_->success_callback == NULL) {
139     return true;
140   }
141   response_ = NULL;
142   error_.clear();
143   callbacks_->success_callback(CassAuthenticator::to(this), data_, token.data(), token.size());
144   return error_.empty();
145 }
146