| 1 |
// BearerBasic auth plugin |
| 2 |
// |
| 3 |
// Plugin for use where e2g is sent auth details in 'bearer' format, but sent in via a Basic header |
| 4 |
// |
| 5 |
// Token is sent as username and signature as password |
| 6 |
|
| 7 |
// For all support, instructions and copyright go to: |
| 8 |
// http://e2guardian.org/ |
| 9 |
// Released under the GPL v2, with the OpenSSL exception described in the README file. |
| 10 |
|
| 11 |
// INCLUDES |
| 12 |
#ifdef HAVE_CONFIG_H |
| 13 |
#include "e2config.h" |
| 14 |
#endif |
| 15 |
|
| 16 |
#include "../Auth.hpp" |
| 17 |
#include "../OptionContainer.hpp" |
| 18 |
#include "../Logger.hpp" |
| 19 |
|
| 20 |
#include <syslog.h> |
| 21 |
|
| 22 |
extern bool is_daemonised; |
| 23 |
extern OptionContainer o; |
| 24 |
extern thread_local std::string thread_id; |
| 25 |
|
| 26 |
// DECLARATIONS |
| 27 |
|
| 28 |
class JWT_token { |
| 29 |
public: |
| 30 |
// These are all required |
| 31 |
String sub; // username |
| 32 |
String fgr; // filter group name |
| 33 |
String aud; // target e2g server/tenant - may be used for checking, by super-proxy for forwarding destination, or by tenanted e2g to determine tenant. |
| 34 |
long exp = 0; // expiration timestamp |
| 35 |
// These are optional |
| 36 |
String cip; // client IP (similar to x-forwarded - may be used by super-proxy) |
| 37 |
String clid; // client id |
| 38 |
String jti; // Unique id of token |
| 39 |
|
| 40 |
bool load(String token,NaughtyFilter &cm) { // returns true if valid |
| 41 |
String temp = token.after("{"); |
| 42 |
int man_cnt = 0; |
| 43 |
while ( !temp.empty() ) { |
| 44 |
String line; |
| 45 |
if (temp.contains(",")) { |
| 46 |
line = temp.before(","); |
| 47 |
temp = temp.after(","); |
| 48 |
} else { |
| 49 |
line = temp.before("}"); |
| 50 |
temp = ""; |
| 51 |
} |
| 52 |
|
| 53 |
String key = line.before(":"); |
| 54 |
String data = line.after(":"); |
| 55 |
key.removeWhiteSpace(); |
| 56 |
key.removeChar('"'); |
| 57 |
data.removeWhiteSpace(); |
| 58 |
data.removeChar('"'); |
| 59 |
if (key == "sub") { |
| 60 |
sub = data; |
| 61 |
man_cnt++; |
| 62 |
} else if (key == "fgr") { |
| 63 |
fgr = data; |
| 64 |
man_cnt++; |
| 65 |
} else if (key == "aud") { |
| 66 |
aud = data; |
| 67 |
man_cnt++; |
| 68 |
} else if (key == "exp") { |
| 69 |
man_cnt++; |
| 70 |
exp = data.toLong(); |
| 71 |
} else if (key == "cip") { |
| 72 |
cip = data; |
| 73 |
} else if (key == "clid") { |
| 74 |
clid = data; |
| 75 |
} else if (key == "jti") { |
| 76 |
jti = data; |
| 77 |
} |
| 78 |
} |
| 79 |
if (man_cnt < 4) { |
| 80 |
DEBUG_auth("Mandatory token field missing"); |
| 81 |
return false; |
| 82 |
} |
| 83 |
if (cm.thestart.tv_sec > exp) { |
| 84 |
DEBUG_auth("Token has expired on:", exp); |
| 85 |
return false; |
| 86 |
} |
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
}; |
| 91 |
|
| 92 |
// class name is relevant! |
| 93 |
class bearer_basic_instance : public AuthPlugin |
| 94 |
{ |
| 95 |
public: |
| 96 |
bearer_basic_instance(ConfigVar &definition) |
| 97 |
: AuthPlugin(definition) |
| 98 |
{ |
| 99 |
client_ip_based = false; |
| 100 |
}; |
| 101 |
int identify(Socket &peercon, Socket &proxycon, HTTPHeader &h, std::string &string, bool &is_real_user, auth_rec &authrec,NaughtyFilter &cm); |
| 102 |
int init(void *args); |
| 103 |
}; |
| 104 |
|
| 105 |
// IMPLEMENTATION |
| 106 |
|
| 107 |
// class factory code *MUST* be included in every plugin |
| 108 |
|
| 109 |
AuthPlugin *bearer_basic_create(ConfigVar &definition) |
| 110 |
{ |
| 111 |
return new bearer_basic_instance(definition); |
| 112 |
} |
| 113 |
|
| 114 |
// end of Class factory |
| 115 |
|
| 116 |
// proxy auth header username extraction |
| 117 |
int bearer_basic_instance::identify(Socket &peercon, Socket &proxycon, HTTPHeader &h, std::string &username, |
| 118 |
bool &is_real_user, auth_rec &authrec,NaughtyFilter &cm) { |
| 119 |
// don't match for non-basic auth types |
| 120 |
String t(h.getAuthType()); |
| 121 |
t.toLower(); |
| 122 |
if (t == "basic") { |
| 123 |
// extract token |
| 124 |
String string; |
| 125 |
string = h.getAuthData(); |
| 126 |
//string = h.decodeb64(string); |
| 127 |
DEBUG_auth("decoded auth string:", string); |
| 128 |
DEBUG_auth("decoded auth string length:", string.length()); |
| 129 |
if (string.length() > 0) { |
| 130 |
String token, sig; |
| 131 |
token = string; |
| 132 |
sig = token.after(":"); |
| 133 |
//sig = h.decodeb64(sig); |
| 134 |
token = token.before(":"); |
| 135 |
//token = h.decodeb64(token); |
| 136 |
String tocheck = token; |
| 137 |
tocheck.append(bearer_secret); |
| 138 |
DEBUG_auth("tocheck:", tocheck); |
| 139 |
String gen_md5 = tocheck.md5(); |
| 140 |
gen_md5.toLower(); |
| 141 |
DEBUG_auth("tocheck.md5:", gen_md5); |
| 142 |
DEBUG_auth("sig sent", sig); |
| 143 |
if (gen_md5 == sig) { |
| 144 |
token += "="; |
| 145 |
token = h.decodeb64(token); |
| 146 |
DEBUG_auth("plain token:", token); |
| 147 |
JWT_token token_struct; |
| 148 |
if(token_struct.load(token,cm)) { |
| 149 |
|
| 150 |
authrec.user_name = token_struct.sub; |
| 151 |
username = token_struct.sub; |
| 152 |
authrec.fg_name = token_struct.fgr; |
| 153 |
authrec.user_source = "bearer_b"; |
| 154 |
authrec.group_source = "bearer_b"; |
| 155 |
is_real_user = true; |
| 156 |
return E2AUTH_OK_GOT_GROUP_NAME; |
| 157 |
} |
| 158 |
|
| 159 |
} else { |
| 160 |
DEBUG_auth("signature not valid"); |
| 161 |
} |
| 162 |
} else { |
| 163 |
DEBUG_auth("empty authdata"); |
| 164 |
} |
| 165 |
} else { |
| 166 |
DEBUG_auth("auth is not Basic or absent"); |
| 167 |
} |
| 168 |
|
| 169 |
// need to add logic to send 407 (and close??) |
| 170 |
String outmess = "HTTP/1.1 407 Proxy Authentication Required\r\nProxy-Authenticate: Basic realm=\""; |
| 171 |
outmess += bearer_realm; |
| 172 |
outmess += "\"\r\n\r\n"; |
| 173 |
peercon.writeString(outmess); |
| 174 |
return E2AUTH_407_SENT; |
| 175 |
} |
| 176 |
|
| 177 |
int bearer_basic_instance::init(void *args) |
| 178 |
{ |
| 179 |
AuthPlugin::init(args); |
| 180 |
bearer_secret = cv["bearersecret"]; |
| 181 |
if (bearer_secret.empty()) { |
| 182 |
E2LOGGER_error("No bearersecret supplied in authplugin/BearerBasic.conf"); |
| 183 |
return -1; |
| 184 |
} |
| 185 |
bearer_realm= cv["realm"]; |
| 186 |
if (bearer_realm.empty()) { |
| 187 |
E2LOGGER_error("No realm supplied in authplugin/BearerBasic.conf"); |
| 188 |
return -1; |
| 189 |
} |
| 190 |
return 0; |
| 191 |
} |
| 192 |
|