summaryrefslogtreecommitdiff
path: root/lib/authorization.rb
blob: 0cba96a8c710ce67deea298eb7be531a34d47efe (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
module OpenTox

  #Module for Authorization and Authentication
  #@example Authentication
  #  require "opentox-ruby-api-wrapper" 
  #  OpenTox::Authorization::AA_SERVER = "https://opensso.in-silico.ch" #if not set in .opentox/conf/[environment].yaml 
  #  token = OpenTox::Authorization.authenticate("benutzer", "passwort") 
  #@see http://www.opentox.org/dev/apis/api-1.2/AA OpenTox A&A API 1.2 specification
   
  module Authorization

    #Helper Class AA to create and send default policies out of xml templates
    #@example Creating a default policy to a URI 
    #  aa=OpenTox::Authorization::AA.new(tok)  
    #  xml=aa.get_xml('http://uri....')
    #  OpenTox::Authorization.create_policy(xml,tok)   
    
    class AA
      attr_accessor :user, :token_id, :policy  
      
      #Generates AA object - requires token_id
      # @param [String] token_id  
      def initialize(token_id)
        @user = Authorization.get_user(token_id)
        @token_id = token_id
        @policy = Policies.new()
      end
      
      #Cleans AA Policies and loads default xml file into policy attribute
      #set uri and user, returns Policyfile(XML) for open-sso 
      # @param [String] URI to create a policy for
      def get_xml(uri)
        @policy.drop_policies
        @policy.load_default_policy(@user, uri)
        return @policy.to_xml
      end   
      
      #Loads and sends Policyfile(XML) to open-sso server
      # @param [String] URI to create a policy for      
      def send(uri)    
        xml = get_xml(uri)
        ret = false
        ret = Authorization.create_policy(xml, @token_id) 
        LOGGER.debug "Policy send with token_id: #{@token_id}"
        LOGGER.warn "Not created Policy is: #{xml}" if !ret
        ret  
      end
      
    end
    
    #Returns the open-sso server set in the config file .opentox/config/[environment].yaml
    # @return [String, nil] the openSSO server URI or nil
    def self.server
      return AA_SERVER
    end

    #Authentication against OpenSSO. Returns token. Requires Username and Password.
    # @param [String, String]Username,Password 
    # @return [String, nil] gives token_id or nil
    def self.authenticate(user, pw)
      return true if !AA_SERVER
      begin 
        resource = RestClient::Resource.new("#{AA_SERVER}/auth/authenticate")
        out = resource.post(:username=>user, :password => pw).sub("token.id=","").sub("\n","")
        return out
      rescue
        return nil
      end
    end
    
    #Logout on opensso. Make token invalid. Requires token
    # @param [String]token_id the token_id 
    # @return [Boolean] true if logout is OK
    def self.logout(token_id)
      begin
        resource = RestClient::Resource.new("#{AA_SERVER}/auth/logout")
        resource.post(:subjectid => token_id)
        return true 
      rescue
        return false
      end
    end    
    
    #Authorization against OpenSSO for a URI with request-method (action) [GET/POST/PUT/DELETE]
    # @param [String,String,String]uri,action,token_id
    # @return [Boolean, nil]  returns true, false or nil (if authorization-request fails).
    def self.authorize(uri, action, token_id)
      return true if !AA_SERVER
      begin
        resource = RestClient::Resource.new("#{AA_SERVER}/auth/authorize")
        return true if resource.post(:uri => uri, :action => action, :subjectid => token_id) == "boolean=true\n"
      rescue
        return nil
      end    
    end

    #Checks if a token is a valid token 
    # @param [String]token_id token_id from openSSO session 
    # @return [Boolean] token_id is valid or not. 
    def self.is_token_valid(token_id)
      return true if !AA_SERVER
      begin
        resource = RestClient::Resource.new("#{AA_SERVER}/auth/isTokenValid")
        return true if resource.post(:tokenid => token_id) == "boolean=true\n"
      rescue
        return false
      end
    end
 
    #Returns array with all policies of the token owner
    # @param [String]token_id requires token_id
    # @return [Array, nil] returns an Array of policy names or nil if request fails
    def self.list_policies(token_id)
      begin
        resource = RestClient::Resource.new("#{AA_SERVER}/pol")
        out = resource.get(:subjectid => token_id)
        return out.split("\n") 
      rescue
        return nil
      end
    end

    #Returns a policy in xml-format
    # @param [String, String]policy,token_id 
    # @return [String] XML of the policy 
    def self.list_policy(policy, token_id)
      begin
        resource = RestClient::Resource.new("#{AA_SERVER}/pol")
        return resource.get(:subjectid => token_id,:id => policy)
      rescue
        return nil
      end
    end
    
    #Returns the owner (who created the first policy) of an URI
    # @param [String, String]uri,token_id
    # return [String, nil]owner,nil returns owner of the URI
    def self.get_uri_owner(uri, token_id)
      begin
        resource = RestClient::Resource.new("#{AA_SERVER}/pol")
        return resource.get(:uri => uri, :subjectid => token_id).sub("\n","")
      rescue
        return nil
      end      
    end    
    
    #Checks if a policy exists to a URI. Requires URI and token.
    # @param [String, String]uri,token_id
    # return [Boolean] 
    def self.uri_has_policy(uri, token_id)
      owner = get_uri_owner(uri, token_id)
      return true if owner and owner != "null"
      false
    end
    
    #List all policynames for a URI. Requires URI and token.
    # @param [String, String]uri,token_id
    # return [Array, nil] returns an Array of policy names or nil if request fails   
    def self.list_uri_policies(uri, token_id)
      begin
        resource = RestClient::Resource.new("#{AA_SERVER}/pol")
        out = resource.get(:uri => uri, :polnames => true, :subjectid => token_id)        
        policies = []; notfirstline = false
        out.split("\n").each do |line|
          policies << line if notfirstline
          notfirstline = true    
        end
        return policies 
      rescue
        return nil
      end      
    end    

    #Sends a policy in xml-format to opensso server. Requires policy-xml and token.
    # @param [String, String]policyxml,token_id
    # return [Boolean] returns true if policy is created   
    def self.create_policy(policy, token_id)
      begin
#        resource = RestClient::Resource.new("#{AA_SERVER}/Pol/opensso-pol")
        LOGGER.debug "OpenTox::Authorization.create_policy policy: #{policy[168,43]} with token:" + token_id.to_s + " length: " + token_id.length.to_s 
#        return true if resource.post(policy, :subjectid => token_id, :content_type =>  "application/xml")
        return true if RestClientWrapper.post("#{AA_SERVER}/pol", {:subjectid => token_id, :content_type =>  "application/xml"}, policy)        
      rescue
        return false
      end
    end
    
    #Deletes a policy
    # @param [String, String]policyname,token_id
    # @return [Boolean,nil]
    def self.delete_policy(policy, token_id)
      begin
        resource = RestClient::Resource.new("#{AA_SERVER}/pol")
        LOGGER.debug "OpenTox::Authorization.delete_policy policy: #{policy} with token: #{token_id}"
        return true if resource.delete(:subjectid => token_id, :id => policy)        
      rescue
        return nil
      end
    end

    #Returns array of all possible LDAP-Groups
    # @param [String]token_id
    # @return [Array]    
    def self.list_groups(token_id)
      begin
        resource = RestClient::Resource.new("#{AA_SERVER}/opensso/identity/search")
        grps = resource.post(:admin => token_id, :attributes_names => "objecttype", :attributes_values_objecttype => "group")
        grps.split("\n").collect{|x|  x.sub("string=","")}
      rescue
        []
      end
    end    
    
    #Returns array of the LDAP-Groups of an user
    # @param [String]token_id
    # @return [Array] gives array of LDAP groups of a user
    def self.list_user_groups(user, token_id)
      begin
        resource = RestClient::Resource.new("#{AA_SERVER}/opensso/identity/read")
        out = resource.post(:name => user, :admin => token_id, :attributes_names => "group")
        grps = []
        out.split("\n").each do |line|
          grps << line.sub("identitydetails.group=","") if line.include?("identitydetails.group=")    
        end
        return grps
      rescue
        []
      end
    end    
    
    #Returns the owner (user id) of a token
    # @param [String]token_id
    # @return [String]user 
    def self.get_user(token_id)
      begin
        resource = RestClient::Resource.new("#{AA_SERVER}/opensso/identity/attributes")
        out = resource.post(:subjectid => token_id, :attributes_names => "uid")
        user = ""; check = false
        out.split("\n").each do |line|
          if check
            user = line.sub("userdetails.attribute.value=","") if line.include?("userdetails.attribute.value=")
            check = false
          end
          check = true if line.include?("userdetails.attribute.name=uid") 
        end
        return user
      rescue
        nil
      end
    end
    
    #Send default policy with Authorization::AA class
    # @param [String, String]URI,token_id
    def self.send_policy(uri, token_id)
      return true if !AA_SERVER
      aa  = Authorization::AA.new(token_id)
      ret = aa.send(uri)
      LOGGER.debug "OpenTox::Authorization send policy for URI: #{uri} | token_id: #{token_id} - policy created: #{ret}"
      ret
    end
    
    #Deletes all policies of an URI
    # @param [String, String]URI,token_id
    # @return [Boolean]
    def self.delete_policies_from_uri(uri, token_id)
      policies = list_uri_policies(uri, token_id)
      policies.each do |policy|
        ret = delete_policy(policy, token_id)
        LOGGER.debug "OpenTox::Authorization delete policy: #{policy} - with result: #{ret}"
      end    
      return true
    end

    #Checks (if token_id is valid) if a policy exist and create default policy if not    
    def self.check_policy(uri, token_id)
      token_valid = OpenTox::Authorization.is_token_valid(token_id)      
      LOGGER.debug "OpenTox::Authorization.check_policy with uri: #{uri}, token_id: #{token_id} is valid: #{token_valid}"
      if uri and token_valid
        if !uri_has_policy(uri, token_id)     
          return send_policy(uri, token_id)
        else
          LOGGER.debug "OpenTox::Authorization.check_policy URI: #{uri} has already a Policy."
        end
      end
      true
    end    
    
  end 
end