class Faction::Client
See docs.atlassian.com/crowd/current/com/atlassian/crowd/integration/service/soap/server/SecurityServer.html for Crowd SOAP API documentation.
The validation_factors
parameter is a Hash
that can contain the following keys:
-
:user_agent
-ValidationFactor.USER_AGENT
-
:remote_address
-ValidationFactor.REMOTE_ADDRESS
-
:remote_host
-ValidationFactor.REMOTE_HOST
-
:forwarded_for
-ValidationFactor.X_FORWARDED_FOR
-
:random_number
-ValidationFactor.RANDOM_NUMBER
-
:name
-ValidationFactor.NAME
Constants
- CROWD_NAMESPACES
- VALIDATION_FACTOR_MAPPING
Attributes
Application name
Application password
Url of the Crowd SecurityServer
Public Class Methods
Sets the global debugging for Client
# File lib/faction.rb, line 27 def self.debug=(value) @@debug = value end
True if Faction
is in debug mode
# File lib/faction.rb, line 32 def self.debug? @@debug end
Instantiates a new client using a “standard” crowd.properties
file.
# File lib/faction.rb, line 44 def self.from_properties(properties_file) props = Hash[*open(properties_file).readlines.map {|line| line.split(/=/, 2).map(&:strip)}.flatten] self.new(props['crowd.server.url'] + '/SecurityServer', props['application.name'], props['application.password'], :verify_cert => ['yes', 'true'].include?(props['ssl.verify'])) end
Creates a new Crowd client.
Parameters:
-
crowd_url
- The URL to Crowd SecurityServer. -
app_name
- Application name. -
app_password
- Application password. -
options
- A Hash of options (described below).
Options
-
:verify_cert
- Iffalse
the peer SSL certificate is not verified.
Example:
Faction::Client.new('http://localhost:8085/crowd/services/SecurityServer', 'application', 'password')
# File lib/faction.rb, line 67 def initialize(crowd_url, app_name, app_password, options = {}) @crowd_url = crowd_url @crowd = Savon::Client.new(@crowd_url + (Client.debug? ? '?wsdl' : '')) if options[:verify_cert] == false @crowd.request.http.ssl_client_auth(:verify_mode => OpenSSL::SSL::VERIFY_NONE) end @app_name = app_name @app_password = app_password @app_token = nil end
Public Instance Methods
# File lib/faction.rb, line 180 def add_principal(username, password, description, active, attributes) soap_attributes = attributes.map do |name, value| {'p:name' => name, 'p:values' => {'wsdl:string' => value}} end soap_principal = { 'p:active' => active, 'p:name' => username, 'p:description' => description, 'p:attributes' => {'p:SOAPAttribute' => soap_attributes} } credential = { 'auth:credential' => password, 'auth:encryptedCredential' => false } simplify_soap_attributes(authenticated_crowd_call(:add_principal, soap_principal, credential)) end
# File lib/faction.rb, line 172 def add_principal_to_group(principal, group) authenticated_crowd_call(:add_principal_to_group, principal, group) && nil end
See SecurityServerClient.authenticatePrincipal
# File lib/faction.rb, line 79 def authenticate_principal(name, password, validation_factors = nil) authenticated_crowd_call(:authenticate_principal, { 'auth:application' => app_name, 'auth:name' => name, 'auth:credential' => {'auth:credential' => password, 'auth:encryptedCredential' => false}, 'auth:validationFactors' => convert_validation_factors(validation_factors)}) end
# File lib/faction.rb, line 164 def cache_enabled? authenticated_crowd_call(:is_cache_enabled) end
# File lib/faction.rb, line 168 def cache_time authenticated_crowd_call(:get_cache_time) end
See SecurityServerClient.createPrincipalToken
# File lib/faction.rb, line 88 def create_principal_token(name, validation_factors = nil) authenticated_crowd_call(:create_principal_token, { 'auth:application' => app_name, 'auth:name' => name, 'auth:validationFactors' => convert_validation_factors(validation_factors)}) end
# File lib/faction.rb, line 144 def find_group_by_name(name) simplify_soap_group(authenticated_crowd_call(:find_group_by_name, name)) end
# File lib/faction.rb, line 148 def find_group_memberships(name) arrayify(authenticated_crowd_call(:find_group_memberships, name)[:string]) end
# File lib/faction.rb, line 136 def find_principal_by_name(name) simplify_soap_attributes(authenticated_crowd_call(:find_principal_by_name, name)) end
See SecurityServer.findPrincipalByToken
Returns the principal information as a Hash
.
# File lib/faction.rb, line 132 def find_principal_by_token(token) simplify_soap_attributes(authenticated_crowd_call(:find_principal_by_token, token)) end
# File lib/faction.rb, line 140 def find_principal_with_attributes_by_name(name) simplify_soap_attributes(authenticated_crowd_call(:find_principal_with_attributes_by_name, name)) end
# File lib/faction.rb, line 152 def group_names authenticated_crowd_call(:find_all_group_names)[:string] end
See SecurityServerClient.invalidatePrincipalToken
# File lib/faction.rb, line 96 def invalidate_principal_token(token) authenticated_crowd_call(:invalidate_principal_token, token) && nil end
# File lib/faction.rb, line 156 def principal_names authenticated_crowd_call(:find_all_principal_names)[:string] end
# File lib/faction.rb, line 176 def remove_principal(principal) authenticated_crowd_call(:remove_principal, principal) && nil end
See SecurityServer.updatePrincipalCredential
# File lib/faction.rb, line 124 def update_principal_credential(name, new_password) authenticated_crowd_call(:update_principal_credential, name, {'auth:credential' => new_password, 'auth:encryptedCredential' => false}) end
See SecurityServerClient.isValidPrincipalToken
# File lib/faction.rb, line 112 def valid_principal_token?(token, validation_factors = nil) if (validation_factors.nil? || validation_factors.empty?) authenticated_crowd_call(:is_valid_principal_token, token,"") else authenticated_crowd_call(:is_valid_principal_token, token, {'auth:validationFactors' => convert_validation_factors(validation_factors)}) end end
Private Instance Methods
# File lib/faction.rb, line 254 def app_authentication Hash['auth:name' => app_name, 'auth:token' => app_token] end
# File lib/faction.rb, line 263 def app_token @app_token ||= authenticate_application end
# File lib/faction.rb, line 244 def arrayify(soap_object) if soap_object.nil? [] elsif soap_object.is_a? Array soap_object else [soap_object] end end
# File lib/faction.rb, line 267 def authenticate_application response = crowd_call(:authenticate_application) do |soap| soap.body.merge!('wsdl:in0' => { 'auth:name' => app_name, 'auth:credential' => { 'auth:credential' => app_password, 'auth:encryptedCredential' => false, }}) end response[:token] end
# File lib/faction.rb, line 302 def authenticated_crowd_call(name, *args) begin real_authenticated_crowd_call(name, *args) rescue Savon::SOAPFault => f # retry once @app_token = nil begin real_authenticated_crowd_call(name, *args) rescue Savon::SOAPFault => f raise AuthenticationException, f.message end end end
# File lib/faction.rb, line 223 def convert_validation_factors(in_validation_factors) return nil if in_validation_factors.nil? result = in_validation_factors.map do |name, value| raise Faction::Exception, "Invalid validation factor #{name}" if !VALIDATION_FACTOR_MAPPING.include?(name) {'auth:name' => VALIDATION_FACTOR_MAPPING[name], 'auth:value' => value} end {'auth:ValidationFactor' => result} end
# File lib/faction.rb, line 279 def crowd_call(name, &block) method = (Client.debug? ? name : "#{name}!").to_sym response = @crowd.call(method) do |soap| soap.namespaces.merge!(CROWD_NAMESPACES) soap.body = {} yield soap if block_given? end response.to_hash[:"#{name}_response"][:out] end
# File lib/faction.rb, line 259 def ensure_app_token! app_token end
# File lib/faction.rb, line 289 def real_authenticated_crowd_call(name, *args) ensure_app_token! crowd_call(name) do |soap| soap.body = {'wsdl:in0' => app_authentication} order = ['wsdl:in0'] args.each_with_index do |arg, index| soap.body["wsdl:in#{index + 1}"] = arg order << "wsdl:in#{index + 1}" end soap.body[:order!] = order end end
# File lib/faction.rb, line 232 def simplify_soap_attributes(soap_object) attributes = soap_object[:attributes][:soap_attribute].inject({}) do |hash, item| hash[item[:name].to_sym] = item[:values][:string] hash end soap_object.merge(:attributes => attributes) end
# File lib/faction.rb, line 240 def simplify_soap_group(soap_group) soap_group.merge(:members => soap_group[:members][:string]) end