Permalink
Browse files

move playdar_auth table from main db to authdb

  • Loading branch information...
1 parent 0142b13 commit f73e15760caaa9583c12ba3acb3e512496c1f603 @dougma dougma committed Jun 18, 2009
Showing with 603 additions and 455 deletions.
  1. +1 −0 CMakeLists.txt
  2. +19 −0 etc/auth.sql
  3. +3 −2 etc/playdar.conf
  4. +0 −12 etc/schema.sql
  5. +13 −75 includes/playdar/auth.hpp
  6. +168 −0 src/auth.cpp
  7. +29 −0 src/auth_sql.h
  8. +1 −1 src/playdar_request_handler.cpp
  9. +369 −365 win32/playdar/playdar.vcproj
View
@@ -83,6 +83,7 @@ ADD_EXECUTABLE( playdar
${SRC}/playdar_request_handler.cpp
${SRC}/playdar_request.cpp
${SRC}/main.cpp
+ ${SRC}/auth.cpp
# json parser:
${DEPS}/json_spirit_v3.00/json_spirit/json_spirit_reader.cpp
View
@@ -0,0 +1,19 @@
+-- HTTP Authentication
+
+DROP TABLE IF EXISTS playdar_auth;
+CREATE TABLE IF NOT EXISTS playdar_auth (
+ token TEXT NOT NULL PRIMARY KEY,
+ website TEXT NOT NULL,
+ name TEXT NOT NULL,
+ ua TEXT NOT NULL,
+ mtime INTEGER NOT NULL,
+ permissions TEXT NOT NULL
+);
+
+-- Schema version and misc settings
+
+CREATE TABLE IF NOT EXISTS settings (
+ key TEXT NOT NULL PRIMARY KEY,
+ value TEXT NOT NULL DEFAULT ''
+);
+INSERT INTO settings(key ,value) VALUES('schema_version', '2');
View
@@ -1,6 +1,7 @@
{
- "name" : "YOURNAMEHERE",
- "db" : "collection-empty.db",
+ "name" : "YOURNAMEHERE",
+ "db" : "collection-empty.db",
+ "authdb" : "auth.db"
"http_port" : 8888,
"load_plugins" : true,
"load_scripts" : true,
View
@@ -76,18 +76,6 @@ CREATE TABLE IF NOT EXISTS file_join (
);
CREATE INDEX file_join_track ON file_join(track);
--- HTTP Authentication
-
-DROP TABLE IF EXISTS playdar_auth;
-CREATE TABLE IF NOT EXISTS playdar_auth (
- token TEXT NOT NULL PRIMARY KEY,
- website TEXT NOT NULL,
- name TEXT NOT NULL,
- ua TEXT NOT NULL,
- mtime INTEGER NOT NULL,
- permissions TEXT NOT NULL
-);
-
-- Schema version, and misc playdar settings
CREATE TABLE IF NOT EXISTS playdar_system (
View
@@ -19,94 +19,32 @@
#ifndef _PLAYDAR_AUTH_HPP_
#define _PLAYDAR_AUTH_HPP_
-#include "sqlite3pp.h"
#include <map>
#include <string>
#include <vector>
#include <set>
-#include "playdar/utils/uuid.h"
+#include "sqlite3pp.h"
+#include <boost/thread/mutex.hpp>
namespace playdar {
+
// deals with authcodes etc
class auth
{
public:
-
- auth(std::string dbfilepath)
- : m_db(dbfilepath.c_str())
- {
- }
-
- bool is_valid(std::string token, std::string & whom)
- {
- boost::mutex::scoped_lock lock(m_mut);
- sqlite3pp::query qry(m_db, "SELECT name FROM playdar_auth WHERE token = ?" );
- qry.bind(1, token.c_str(), true);
- for(sqlite3pp::query::iterator i = qry.begin(); i!=qry.end(); ++i){
- whom = std::string((*i).get<const char *>(0));
- return true;
- }
- return false;
- }
+ auth(const std::string& dbfilepath);
- std::vector< std::map<std::string,std::string> > get_all_authed()
- {
- boost::mutex::scoped_lock lock(m_mut);
- std::vector< std::map<std::string,std::string> > ret;
- sqlite3pp::query qry(m_db, "SELECT token, website, name, ua FROM playdar_auth ORDER BY mtime DESC");
- for(sqlite3pp::query::iterator i = qry.begin(); i!=qry.end(); ++i){
- std::map<std::string,std::string> m;
- m["token"] = std::string((*i).get<const char *>(0));
- m["website"] = std::string((*i).get<const char *>(1));
- m["name"] = std::string((*i).get<const char *>(2));
- m["ua"] = std::string((*i).get<const char *>(3));
- ret.push_back( m );
- }
- return ret;
- }
-
- void
- deauth(std::string token)
- {
- boost::mutex::scoped_lock lock(m_mut);
- std::string sql = "DELETE FROM playdar_auth WHERE token = ?";
- sqlite3pp::command cmd(m_db, sql.c_str());
- cmd.bind(1, token.c_str(), true);
- cmd.execute();
- }
-
- void create_new(const std::string &token, const std::string &website, const std::string &name, const std::string &ua )
- {
- boost::mutex::scoped_lock lock(m_mut);
- std::string sql = "INSERT INTO playdar_auth "
- "(token, website, name, ua, mtime, permissions) "
- "VALUES(?, ?, ?, ?, ?, ?)";
- sqlite3pp::command cmd(m_db, sql.c_str());
- cmd.bind(1, token.c_str(), true);
- cmd.bind(2, website.c_str(), true);
- cmd.bind(3, name.c_str(), true);
- cmd.bind(4, ua.c_str(), true);
- cmd.bind(5, 0);
- cmd.bind(6, "*", true);
- cmd.execute();
- }
-
- void add_formtoken( const std::string& ft )
- {
- m_formtokens.insert( ft );
- }
-
- bool consume_formtoken(const std::string& ft)
- {
- if(m_formtokens.find(ft) == m_formtokens.end())
- {
- return false;
- }
- m_formtokens.erase(ft);
- return true;
- }
+ bool is_valid(const std::string& token, std::string & whom);
+ std::vector< std::map<std::string,std::string> > get_all_authed();
+ void deauth(const std::string& token);
+ void create_new(const std::string& token, const std::string& website, const std::string& name, const std::string& ua);
+ void add_formtoken(const std::string& ft);
+ bool consume_formtoken(const std::string& ft);
private:
+ void check_db();
+ void create_db_schema();
+
std::set<std::string> m_formtokens;
sqlite3pp::database m_db;
boost::mutex m_mut;
View
@@ -0,0 +1,168 @@
+/*
+ Playdar - music content resolver
+ Copyright (C) 2009 Richard Jones
+ Copyright (C) 2009 Last.fm Ltd.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <boost/algorithm/string.hpp>
+#include <boost/foreach.hpp>
+#include "playdar/auth.hpp"
+#include "auth_sql.h"
+
+namespace playdar {
+
+using namespace std;
+
+auth::auth(const string& dbfilepath)
+ : m_db(dbfilepath.c_str())
+{
+ check_db();
+}
+
+bool
+auth::is_valid(const string& token, string& whom)
+{
+ boost::mutex::scoped_lock lock(m_mut);
+ sqlite3pp::query qry(m_db, "SELECT name FROM playdar_auth WHERE token = ?" );
+ qry.bind(1, token.c_str(), true);
+ for(sqlite3pp::query::iterator i = qry.begin(); i!=qry.end(); ++i){
+ whom = string((*i).get<const char *>(0));
+ return true;
+ }
+ return false;
+}
+
+vector< map<string, string> >
+auth::get_all_authed()
+{
+ boost::mutex::scoped_lock lock(m_mut);
+ vector< map<string, string> > ret;
+ sqlite3pp::query qry(m_db, "SELECT token, website, name, ua FROM playdar_auth ORDER BY mtime DESC");
+ for(sqlite3pp::query::iterator i = qry.begin(); i!=qry.end(); ++i){
+ map<string, string> m;
+ m["token"] = string((*i).get<const char *>(0));
+ m["website"] = string((*i).get<const char *>(1));
+ m["name"] = string((*i).get<const char *>(2));
+ m["ua"] = string((*i).get<const char *>(3));
+ ret.push_back( m );
+ }
+ return ret;
+}
+
+void
+auth::deauth(const string& token)
+{
+ boost::mutex::scoped_lock lock(m_mut);
+ string sql = "DELETE FROM playdar_auth WHERE token = ?";
+ sqlite3pp::command cmd(m_db, sql.c_str());
+ cmd.bind(1, token.c_str(), true);
+ cmd.execute();
+}
+
+void
+auth::create_new(const string& token, const string& website, const string& name, const string& ua )
+{
+ boost::mutex::scoped_lock lock(m_mut);
+ string sql = "INSERT INTO playdar_auth "
+ "(token, website, name, ua, mtime, permissions) "
+ "VALUES(?, ?, ?, ?, ?, ?)";
+ sqlite3pp::command cmd(m_db, sql.c_str());
+ cmd.bind(1, token.c_str(), true);
+ cmd.bind(2, website.c_str(), true);
+ cmd.bind(3, name.c_str(), true);
+ cmd.bind(4, ua.c_str(), true);
+ cmd.bind(5, 0);
+ cmd.bind(6, "*", true);
+ cmd.execute();
+}
+
+void
+auth::add_formtoken(const string& ft)
+{
+ boost::mutex::scoped_lock lock(m_mut);
+ m_formtokens.insert( ft );
+}
+
+bool
+auth::consume_formtoken(const string& ft)
+{
+ boost::mutex::scoped_lock lock(m_mut);
+ if(m_formtokens.find(ft) == m_formtokens.end())
+ {
+ return false;
+ }
+ m_formtokens.erase(ft);
+ return true;
+}
+
+void
+auth::check_db()
+{
+ try
+ {
+ sqlite3pp::query qry(m_db, "SELECT value FROM settings WHERE key = 'schema_version'");
+ sqlite3pp::query::iterator i = qry.begin();
+ if( i == qry.end() )
+ {
+ // unusual - table exists but doesn't contain this row.
+ // could have been created wrongly
+ cerr << "Error, settings table missing schema_version key!" << endl
+ << "Maybe you created the database wrong, or it's corrupt." << endl
+ << "Try deleting the auth database" << endl;
+ throw; // not caught here.
+ }
+ string val = (*i).get<string>(0);
+ cout << "Auth database schema detected as version " << val << endl;
+ // check the schema version is what we expect
+ // TODO auto-upgrade to newest schema version as needed.
+ if( val != "2" )
+ {
+ throw; // not caught here
+ }
+ // OK.
+ }
+ catch(sqlite3pp::database_error err)
+ {
+ // probably doesn't exist yet, try and create it
+ //
+ cout << "database_error: " << err.what() << endl;
+ create_db_schema();
+ }
+}
+
+void
+auth::create_db_schema()
+{
+ cout << "Attempting to create DB schema..." << endl;
+ string sql( playdar::get_auth_sql() );
+ vector<string> statements;
+ boost::split( statements, sql, boost::is_any_of(";") );
+ BOOST_FOREACH( string s, statements )
+ {
+ boost::trim( s );
+ if (!s.empty()) {
+ cout << "Executing: " << s << endl;
+ sqlite3pp::command cmd(m_db, s.c_str());
+ cmd.execute();
+ }
+ }
+ cout << "Schema created." << endl;
+}
+
+
+} //ns
+
+
View
@@ -0,0 +1,29 @@
+/*
+ This file was automatically generated from auth.sql on Thu Jun 18 17:34:23 GMTDT 2009.
+*/
+namespace playdar {
+
+static const char * auth_schema_sql =
+"DROP TABLE IF EXISTS playdar_auth;"
+"CREATE TABLE IF NOT EXISTS playdar_auth ("
+" token TEXT NOT NULL PRIMARY KEY,"
+" website TEXT NOT NULL,"
+" name TEXT NOT NULL,"
+" ua TEXT NOT NULL,"
+" mtime INTEGER NOT NULL,"
+" permissions TEXT NOT NULL"
+");"
+"CREATE TABLE IF NOT EXISTS settings ("
+" key TEXT NOT NULL PRIMARY KEY,"
+" value TEXT NOT NULL DEFAULT ''"
+");"
+"INSERT INTO settings(key ,value) VALUES('schema_version', '2');"
+ ;
+
+const char * get_auth_sql()
+{
+ return auth_schema_sql;
+}
+
+} // namespace
+
@@ -53,7 +53,7 @@ playdar_request_handler::init(MyApplication * app)
{
m_disableAuth = app->conf()->get<bool>( "disableauth", false );
cout << "HTTP handler online." << endl;
- m_pauth = new playdar::auth(app->conf()->get<string>( "db", "" ));
+ m_pauth = new playdar::auth(app->conf()->get<string>( "authdb", "" ));
m_app = app;
// built-in handlers:
m_urlHandlers[ "" ] = boost::bind( &playdar_request_handler::handle_root, this, _1, _2 );
Oops, something went wrong.

0 comments on commit f73e157

Please sign in to comment.