opentox-server 0.0.1pre → 0.0.2pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,131 @@
1
+ module OpenTox
2
+ # Base class for OpenTox services
3
+ class Application < Service
4
+
5
+ helpers do
6
+
7
+ # Login to get session for browser application(e.G.: ToxCreate)
8
+ #
9
+ # @param [String, String] username,password
10
+ # @return [String] subjectid from session or nil
11
+ def login(username, password)
12
+ logout
13
+ session[:subjectid] = OpenTox::Authorization.authenticate(username, password)
14
+ $logger.debug "ToxCreate login user #{username} with subjectid: " + session[:subjectid].to_s
15
+ if session[:subjectid] != nil
16
+ session[:username] = username
17
+ return session[:subjectid]
18
+ else
19
+ session[:username] = ""
20
+ return nil
21
+ end
22
+ end
23
+
24
+ # Logout to reset session
25
+ #
26
+ # @return [Boolean] true/false
27
+ def logout
28
+ if session[:subjectid] != nil
29
+ session[:subjectid] = nil
30
+ session[:username] = ""
31
+ return true
32
+ end
33
+ return false
34
+ end
35
+
36
+ # Checks session and valid subjectid token.
37
+ # @return [Boolean] true/false
38
+ def logged_in()
39
+ return true if !$aa[:uri]
40
+ if session[:subjectid] != nil
41
+ return OpenTox::Authorization.is_token_valid(session[:subjectid])
42
+ end
43
+ return false
44
+ end
45
+
46
+ # Authorization for a browser/webservice request
47
+ # webapplication: redirects with flash[:notice] if unauthorized
48
+ # webservice: raises error if unauthorized
49
+ # @param [String]subjectid
50
+ def protected!(subjectid)
51
+ if env["session"]
52
+ unless authorized?(subjectid)
53
+ flash[:notice] = "You don't have access to this section: "
54
+ redirect back
55
+ end
56
+ elsif !env["session"] && subjectid
57
+ unless authorized?(subjectid)
58
+ $logger.debug "URI not authorized: clean: " + clean_uri("#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['REQUEST_URI']}").sub("http://","https://").to_s + " full: #{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['REQUEST_URI']} with request: #{request.env['REQUEST_METHOD']}"
59
+ raise OpenTox::NotAuthorizedError.new "Not authorized"
60
+ end
61
+ else
62
+ raise OpenTox::NotAuthorizedError.new "Not authorized" unless authorized?(subjectid)
63
+ end
64
+ end
65
+
66
+ # Check Authorization for URI with method and subjectid.
67
+ # @param [String]subjectid
68
+ def authorized?(subjectid)
69
+ request_method = request.env['REQUEST_METHOD']
70
+ uri = clean_uri("#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['REQUEST_URI']}").sub("http://","https://")
71
+ request_method = "GET" if request_method == "POST" && uri =~ /\/model\/\d+\/?$/
72
+ return OpenTox::Authorization.authorized?(uri, request_method, subjectid)
73
+ end
74
+
75
+ # Cleans URI from querystring and file-extension. Sets port 80 to emptystring
76
+ # @param [String] uri
77
+ def clean_uri(uri)
78
+ uri = uri.sub(" ", "%20") #dirty hacks => to fix
79
+ uri = uri[0,uri.index("InChI=")] if uri.index("InChI=")
80
+ out = URI.parse(uri)
81
+ out.path = out.path[0, out.path.length - (out.path.reverse.rindex(/\/{1}\d+\/{1}/))] if out.path.index(/\/{1}\d+\/{1}/) #cuts after /id/ for a&a
82
+ out.path = out.path.split('.').first #cut extension
83
+ port = (out.scheme=="http" && out.port==80)||(out.scheme=="https" && out.port==443) ? "" : ":#{out.port.to_s}"
84
+ "#{out.scheme}://#{out.host}#{port}#{out.path.chomp("/")}" #"
85
+ end
86
+
87
+ # Unprotected uri for login
88
+ def login_request?
89
+ return env['REQUEST_URI'] =~ /\/login$/
90
+ end
91
+
92
+ # Check if URI returns code 200 //used in model/lazar.rb
93
+ # @param [String]URLString
94
+ # @return [Boolean] true/false
95
+ def uri_available?(urlStr)
96
+ url = URI.parse(urlStr)
97
+ subjectidstr = @subjectid ? "?subjectid=#{CGI.escape @subjectid}" : ""
98
+ http = Net::HTTP.new(url.host, url.port)
99
+ if url.is_a?(URI::HTTPS)
100
+ http.use_ssl = true
101
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
102
+ end
103
+ return http.head("#{url.request_uri}#{subjectidstr}").code == "200"
104
+ end
105
+
106
+ # Get subjectid out of session, params or rack-environment and unescape it if necessary
107
+ # @return [String] subjectid
108
+ def get_subjectid
109
+ begin
110
+ subjectid = nil
111
+ subjectid = session[:subjectid] if session[:subjectid]
112
+ subjectid = params[:subjectid] if params[:subjectid] and !subjectid
113
+ subjectid = request.env['HTTP_SUBJECTID'] if request.env['HTTP_SUBJECTID'] and !subjectid
114
+ # see http://rack.rubyforge.org/doc/SPEC.html
115
+ subjectid = CGI.unescape(subjectid) if subjectid.include?("%23")
116
+ @subjectid = subjectid
117
+ rescue
118
+ @subjectid = nil
119
+ end
120
+ end
121
+
122
+ end
123
+
124
+ before do
125
+ get_subjectid()
126
+ unless !$aa[:uri] or login_request? or $aa[:free_request].include?(env['REQUEST_METHOD'].to_sym)
127
+ protected!(@subjectid)
128
+ end
129
+ end
130
+ end
131
+ end
data/lib/file-store.rb ADDED
@@ -0,0 +1,37 @@
1
+ FileUtils.mkdir_p File.join(File.dirname(__FILE__),"private")
2
+
3
+ module OpenTox
4
+
5
+ # Base class for OpenTox services
6
+ class FileStore < Service
7
+
8
+ helpers do
9
+ def next_id
10
+ id = Dir["./private/*.nt"].collect{|f| File.basename(f,"nt").to_i}.sort.last
11
+ id = 0 if id.nil?
12
+ id + 1
13
+ end
14
+
15
+ def file id
16
+ File.join File.dirname(File.expand_path __FILE__), "private", "#{id.to_s}.nt"
17
+ end
18
+
19
+ def uri_list
20
+ Dir["./private/*.nt"].collect{|f| url_for File.basename(f,".nt")}.join("\n")
21
+ end
22
+ end
23
+
24
+ get '/?' do
25
+ uri_list
26
+ end
27
+
28
+ post '/?' do
29
+ File.open(file(next_id),"w+"){|f| f.puts request.env["rack.input"].read}
30
+ end
31
+
32
+ get '/:id/?' do
33
+ send_file file(params[:id])
34
+ end
35
+ end
36
+
37
+ end
@@ -3,5 +3,8 @@ require 'rack'
3
3
  require 'rack/contrib'
4
4
  require 'sinatra'
5
5
  require 'sinatra/url_for'
6
+ require 'roo'
6
7
  require File.join(File.dirname(__FILE__),"environment.rb")
7
8
  require File.join(File.dirname(__FILE__),"opentox.rb")
9
+ require File.join(File.dirname(__FILE__),"file-store.rb")
10
+ require File.join(File.dirname(__FILE__),"authorization-helper.rb")
data/lib/opentox.rb CHANGED
@@ -2,9 +2,8 @@ require 'sinatra/base'
2
2
  require "sinatra/reloader"
3
3
 
4
4
  module OpenTox
5
+
5
6
  # Base class for OpenTox services
6
- # Errors are formated according to acccept-header
7
- # Non OpenTox::Errors (defined in error.rb) are handled as internal error (500), stacktrace is logged
8
7
  class Service < Sinatra::Base
9
8
 
10
9
  helpers Sinatra::UrlForHelper
@@ -17,39 +16,43 @@ module OpenTox
17
16
  register Sinatra::Reloader
18
17
  end
19
18
 
19
+ helpers do
20
+ def uri
21
+ params[:id] ? url_for("/#{params[:id]}", :full) : "#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}"
22
+ end
23
+ end
24
+
25
+ before do
26
+ @accept = request.env['HTTP_ACCEPT']
27
+ response['Content-Type'] = @accept
28
+ # TODO: A+A
29
+ end
30
+
20
31
  error do
21
- # TODO: set actor, calling OT::Error with uri parameter does not work
32
+ # TODO: convert to OpenTox::Error and set URI
22
33
  error = request.env['sinatra.error']
23
- #error.report.actor = "#{request.env['rack.url_scheme']}://#{request.env['HTTP_HOST']}#{request.env['REQUEST_URI']}"
24
- case request.env['HTTP_ACCEPT']
25
- when 'application/rdf+xml'
26
- content_type 'application/rdf+xml'
27
- when /html/
28
- content_type 'text/html'
29
- when "text/n3"
30
- content_type "text/n3"
31
- else
32
- content_type "text/turtle"
33
- end
34
+ #error.uri = uri
34
35
  if error.respond_to? :report
35
- case request.env['HTTP_ACCEPT']
36
+ # Errors are formated according to acccept-header
37
+ case @accept
36
38
  when 'application/rdf+xml'
37
39
  body = error.report.to_rdfxml
38
40
  when /html/
39
- body = error.report.to_yaml
41
+ # TODO
42
+ # body = error.report.to_html
43
+ body = error.report.to_turtle
40
44
  when "text/n3"
41
45
  body = error.report.to_ntriples
42
46
  else
43
47
  body = error.report.to_turtle
44
48
  end
45
49
  else
46
- content_type "text/plain"
50
+ response['Content-Type'] = "text/plain"
47
51
  body = error.message
48
52
  body += "\n#{error.backtrace}"
49
53
  end
50
- code = error.http_code if error.respond_to? :http_code
51
- code ||= 500
52
- halt code, error.report.to_turtle
54
+ error.respond_to?(:http_code) ? code = error.http_code : code = 500
55
+ halt code, body
53
56
  end
54
57
  end
55
58
  end
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "opentox-server"
6
- s.version = "0.0.1pre"
6
+ s.version = "0.0.2pre"
7
7
  s.authors = ["Christoph Helma, Martin Guetlein, Andreas Maunz, Micha Rautenberg, David Vorgrimmler"]
8
8
  s.email = ["helma@in-silico.ch"]
9
9
  s.homepage = "http://github.com/opentox/opentox-server"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opentox-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1pre
4
+ version: 0.0.2pre
5
5
  prerelease: 5
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-09 00:00:00.000000000 Z
12
+ date: 2012-04-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: opentox-client
16
- requirement: &78527230 !ruby/object:Gem::Requirement
16
+ requirement: &84856440 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *78527230
24
+ version_requirements: *84856440
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rack
27
- requirement: &78526520 !ruby/object:Gem::Requirement
27
+ requirement: &84855790 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *78526520
35
+ version_requirements: *84855790
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rack-contrib
38
- requirement: &78525990 !ruby/object:Gem::Requirement
38
+ requirement: &84855290 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *78525990
46
+ version_requirements: *84855290
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: sinatra
49
- requirement: &78525640 !ruby/object:Gem::Requirement
49
+ requirement: &84854680 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *78525640
57
+ version_requirements: *84854680
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: sinatra-contrib
60
- requirement: &78525220 !ruby/object:Gem::Requirement
60
+ requirement: &84853820 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *78525220
68
+ version_requirements: *84853820
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: emk-sinatra-url-for
71
- requirement: &78508690 !ruby/object:Gem::Requirement
71
+ requirement: &84852960 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *78508690
79
+ version_requirements: *84852960
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: roo
82
- requirement: &78508460 !ruby/object:Gem::Requirement
82
+ requirement: &84851990 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *78508460
90
+ version_requirements: *84851990
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: unicorn
93
- requirement: &78508120 !ruby/object:Gem::Requirement
93
+ requirement: &84851710 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: '0'
99
99
  type: :runtime
100
100
  prerelease: false
101
- version_requirements: *78508120
101
+ version_requirements: *84851710
102
102
  description: Ruby library for opentox services
103
103
  email:
104
104
  - helma@in-silico.ch
@@ -111,7 +111,9 @@ files:
111
111
  - LICENSE
112
112
  - README
113
113
  - Rakefile
114
+ - lib/authorization-helper.rb
114
115
  - lib/environment.rb
116
+ - lib/file-store.rb
115
117
  - lib/opentox-server.rb
116
118
  - lib/opentox.rb
117
119
  - opentox-server.gemspec