summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/authorization.rb24
-rw-r--r--lib/dataset.rb9
-rw-r--r--lib/environment.rb9
-rw-r--r--lib/model.rb4
-rw-r--r--lib/opentox-ruby-api-wrapper.rb2
-rw-r--r--lib/templates/users.yaml5
6 files changed, 46 insertions, 7 deletions
diff --git a/lib/authorization.rb b/lib/authorization.rb
new file mode 100644
index 0000000..372d52b
--- /dev/null
+++ b/lib/authorization.rb
@@ -0,0 +1,24 @@
+helpers do
+
+ def protected!
+ response['WWW-Authenticate'] = %(Basic realm="Testing HTTP Auth") and \
+ throw(:halt, [401, "Not authorized\n"]) and \
+ return unless authorized?
+ end
+
+ def authorized?
+ @auth ||= Rack::Auth::Basic::Request.new(request.env)
+ @auth.provided? && @auth.basic? && @auth.credentials && valid_user?
+ end
+
+ def valid_user?
+ users = @@users[:users]
+ return @auth.credentials == [@auth.username, users.fetch(@auth.username)] if users.has_key?(@auth.username)
+ return false
+ end
+
+end
+
+before do
+ protected! unless env['REQUEST_METHOD'] == "GET"
+end \ No newline at end of file
diff --git a/lib/dataset.rb b/lib/dataset.rb
index 8d1c0c1..f4372be 100644
--- a/lib/dataset.rb
+++ b/lib/dataset.rb
@@ -78,7 +78,7 @@ module OpenTox
end
def self.create(data, content_type = 'application/rdf+xml')
- uri = RestClient.post @@config[:services]["opentox-dataset"], data, :content_type => content_type
+ uri = RestClient::Resource.new(@@config[:services]["opentox-dataset"], :user => request.username, :password => request.password).post data, :content_type => content_type
dataset = Dataset.new
dataset.read uri.to_s
dataset
@@ -168,11 +168,12 @@ module OpenTox
# Delete a dataset
def delete
- RestClient.delete @uri
- end
+ resource = RestClient::Resource.new(@uri, :user => request.username, :password => request.password)
+ resource.delete
+ end
def save
- RestClient.post(@@config[:services]["opentox-dataset"], self.rdf, :content_type => "application/rdf+xml").to_s
+ RestClient::Resource.new(@@config[:services]["opentox-dataset"], :user => request.username, :password => request.password).post(self.rdf, :content_type => "application/rdf+xml").to_s
end
def to_yaml
diff --git a/lib/environment.rb b/lib/environment.rb
index 6100928..f7513c5 100644
--- a/lib/environment.rb
+++ b/lib/environment.rb
@@ -6,6 +6,7 @@ basedir = File.join(ENV['HOME'], ".opentox")
config_dir = File.join(basedir, "config")
@@tmp_dir = File.join(basedir, "tmp")
config_file = File.join(config_dir, "#{ENV['RACK_ENV']}.yaml")
+user_file = File.join(config_dir, "users.yaml")
if File.exist?(config_file)
@@config = YAML.load_file(config_file)
@@ -17,6 +18,14 @@ else
exit
end
+if File.exist?(user_file)
+ @@users = YAML.load_file(user_file)
+else
+ FileUtils.cp(File.join(File.dirname(__FILE__), 'templates/users.yaml'), user_file)
+ puts "Please edit #{user_file} and restart your application."
+ exit
+end
+
# RDF namespaces
RDF = Redland::Namespace.new 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
OWL = Redland::Namespace.new 'http://www.w3.org/2002/07/owl#'
diff --git a/lib/model.rb b/lib/model.rb
index 24f6e52..b86b3fc 100644
--- a/lib/model.rb
+++ b/lib/model.rb
@@ -34,7 +34,7 @@ module OpenTox
# Predict a compound
def predict(compound)
- RestClient.post(@uri, :compound_uri => compound.uri)
+ RestClient::Resource.new(@uri, :user => request.username, :password => request.password).post(:compound_uri => compound.uri)
end
def self.base_uri
@@ -42,7 +42,7 @@ module OpenTox
end
def self.create(data)
- RestClient.post(@@config[:services]["opentox-model"], data, :content_type => "application/x-yaml").to_s
+ RestClient::Resource.new(@@config[:services]["opentox-model"], :user => request.username, :password => request.password).post(data, :content_type => "application/x-yaml").to_s
end
def endpoint
diff --git a/lib/opentox-ruby-api-wrapper.rb b/lib/opentox-ruby-api-wrapper.rb
index ca57fc9..03838e8 100644
--- a/lib/opentox-ruby-api-wrapper.rb
+++ b/lib/opentox-ruby-api-wrapper.rb
@@ -8,6 +8,6 @@ rescue LoadError
puts "Please install Openbabel with 'rake openbabel:install' in the compound component"
end
-['owl', 'compound','dataset','algorithm','model','task','utils'].each do |lib|
+['owl', 'compound','dataset','algorithm','model','task','utils','authorization'].each do |lib|
require lib
end
diff --git a/lib/templates/users.yaml b/lib/templates/users.yaml
new file mode 100644
index 0000000..483fd7b
--- /dev/null
+++ b/lib/templates/users.yaml
@@ -0,0 +1,5 @@
+# please insert users and passwords here.
+# one user and password each line. uncomment the line.
+:users:
+# username: "secretpassword"
+# exampleuser: "ih9aiTog" \ No newline at end of file