be/src/service/arrow_flight/call_header_utils.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <sstream> |
21 | | |
22 | | #include "arrow/flight/types.h" |
23 | | #include "arrow/util/base64.h" |
24 | | |
25 | | namespace doris { |
26 | | namespace flight { |
27 | | |
28 | | const char kBearerDefaultToken[] = "bearertoken"; |
29 | | const char kBasicPrefix[] = "Basic "; |
30 | | const char kBearerPrefix[] = "Bearer "; |
31 | | const char kAuthHeader[] = "authorization"; |
32 | | |
33 | | // Function to look in CallHeaders for a key that has a value starting with prefix and |
34 | | // return the rest of the value after the prefix. |
35 | | std::string FindKeyValPrefixInCallHeaders(const arrow::flight::CallHeaders& incoming_headers, |
36 | 0 | const std::string& key, const std::string& prefix) { |
37 | | // Lambda function to compare characters without case sensitivity. |
38 | 0 | auto char_compare = [](const char& char1, const char& char2) { |
39 | 0 | return (::toupper(char1) == ::toupper(char2)); |
40 | 0 | }; |
41 | |
|
42 | 0 | auto iter = incoming_headers.find(key); |
43 | 0 | if (iter == incoming_headers.end()) { |
44 | 0 | return ""; |
45 | 0 | } |
46 | 0 | const std::string val(iter->second); |
47 | 0 | if (val.size() > prefix.length()) { |
48 | 0 | if (std::equal(val.begin(), val.begin() + prefix.length(), prefix.begin(), char_compare)) { |
49 | 0 | return val.substr(prefix.length()); |
50 | 0 | } |
51 | 0 | } |
52 | 0 | return ""; |
53 | 0 | } |
54 | | |
55 | | void ParseBasicHeader(const arrow::flight::CallHeaders& incoming_headers, std::string& username, |
56 | 0 | std::string& password) { |
57 | 0 | std::string encoded_credentials = |
58 | 0 | FindKeyValPrefixInCallHeaders(incoming_headers, kAuthHeader, kBasicPrefix); |
59 | 0 | std::stringstream decoded_stream(arrow::util::base64_decode(encoded_credentials)); |
60 | 0 | std::getline(decoded_stream, username, ':'); |
61 | 0 | std::getline(decoded_stream, password, ':'); |
62 | 0 | } |
63 | | |
64 | | } // namespace flight |
65 | | } // namespace doris |