blob: 6dfec4bec9e1ab2e1ae88c3f55d4500115c44a48 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
post "/aa/authenticate/?" do
mime_types = ["text/plain"]
bad_request_error "Mime type #{@accept} not supported here. Please request data as #{mime_types.join(', ')}." unless mime_types.include? @accept
bad_request_error "Please send formdata username." unless params[:username]
bad_request_error "Please send formdata password." unless params[:password]
case @accept
when "text/plain"
if OpenTox::Authorization.authenticate(params[:username], params[:password])
return OpenTox::RestClientWrapper.subjectid
else
return nil
end
else
bad_request_error "'#{@accept}' is not a supported content type."
end
end
post "/aa/logout/?" do
mime_types = ["text/plain"]
bad_request_error "Mime type #{@accept} not supported here. Please request data as #{mime_types.join(', ')}." unless mime_types.include? @accept
bad_request_error "Please send formdata subjectid." unless params[:subjectid]
case @accept
when "text/plain"
if OpenTox::Authorization.logout(params[:subjectid])
return "Successfully logged out. \n"
else
return "Logout failed.\n"
end
else
bad_request_error "'#{@accept}' is not a supported content type."
end
end
module OpenTox
AA = "https://opensso.in-silico.ch"
module Authorization
#Authentication against OpenSSO. Returns token. Requires Username and Password.
# @param user [String] Username
# @param pw [String] Password
# @return [Boolean] true if successful
def self.authenticate(user, pw)
begin
res = RestClientWrapper.post("#{AA}/auth/authenticate",{:username=>user, :password => pw},{:subjectid => ""}).sub("token.id=","").sub("\n","")
if is_token_valid(res)
RestClientWrapper.subjectid = res
return true
else
bad_request_error "Authentication failed #{res.inspect}"
end
rescue
bad_request_error "Authentication failed #{res.inspect}"
end
end
#Logout on opensso. Make token invalid. Requires token
# @param [String] subjectid the subjectid
# @return [Boolean] true if logout is OK
def self.logout(subjectid=RestClientWrapper.subjectid)
begin
out = RestClientWrapper.post("#{AA}/auth/logout", :subjectid => subjectid)
return true unless is_token_valid(subjectid)
rescue
return false
end
return false
end
#Checks if a token is a valid token
# @param [String]subjectid subjectid from openSSO session
# @return [Boolean] subjectid is valid or not.
def self.is_token_valid(subjectid=RestClientWrapper.subjectid)
begin
return true if RestClientWrapper.post("#{AA}/auth/isTokenValid",:tokenid => subjectid) == "boolean=true\n"
rescue #do rescue because openSSO throws 401
return false
end
return false
end
end
end
|