summaryrefslogtreecommitdiff
path: root/src/auth.cc
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@yahoo.com>2015-06-25 02:14:18 +0200
committerJoel Klinghed <the_jk@yahoo.com>2015-06-25 02:14:18 +0200
commit85adf26a787b33d69d07df0dc786516fed800e21 (patch)
tree12de4b0cae3999f3da7bbf3d21c631eecf6bf4a3 /src/auth.cc
parent0c55606145b6c5d9a303b19b3dba4996ec3ed3a9 (diff)
Add basic auth support
Diffstat (limited to 'src/auth.cc')
-rw-r--r--src/auth.cc46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/auth.cc b/src/auth.cc
new file mode 100644
index 0000000..015b2fc
--- /dev/null
+++ b/src/auth.cc
@@ -0,0 +1,46 @@
+#include "common.hh"
+
+#include "auth.hh"
+#include "base64.hh"
+#include "cgi.hh"
+#include "http.hh"
+#include "strutils.hh"
+
+namespace stuff {
+
+bool Auth::auth(CGI* cgi, const std::string& realm, const std::string& passwd,
+ std::string* user) {
+ auto auth = cgi->http_auth();
+ auto pos = auth.find(' ');
+ if (pos != std::string::npos) {
+ if (ascii_tolower(auth.substr(0, pos)) == "basic") {
+ std::string tmp;
+ if (Base64::decode(auth.substr(pos + 1), &tmp)) {
+ pos = tmp.find(':');
+ if (pos != std::string::npos) {
+ if (tmp.substr(pos + 1) == passwd) {
+ if (user) user->assign(tmp.substr(0, pos));
+ return true;
+ }
+ }
+ }
+ }
+ }
+
+ std::map<std::string, std::string> headers;
+ std::string tmp = realm;
+ for (auto it = tmp.begin(); it != tmp.end(); ++it) {
+ if (!((*it >= 'a' && *it <= 'z') ||
+ (*it >= 'A' && *it <= 'Z') ||
+ (*it >= '0' && *it <= '9') ||
+ *it == '-' || *it == '_' || *it == '.' || *it == ' ')) {
+ *it = '.';
+ }
+ }
+ headers.insert(std::make_pair("WWW-Authenticate",
+ "Basic realm=\"" + tmp + "\""));
+ Http::response(401, headers, "Authentication needed");
+ return false;
+}
+
+} // namespace stuff