Binding to LDAP service - ruby

I'm using Ruby and trying to bind an LDAP server. The Ruby documentation seems to be very vague here and it's not obvious what I need to do after the following:
>> require 'uri'
=> true
>> newuri = URI::LDAP.build({:host => '10.1.1.1', :dc => 'cjndemo' , :dc => 'com', :user =>'admin', :password => 'Passw0rd'})
=> #<URI::LDAP:0x007fea9d0cef60 URL:ldap://10.1.1.1?>
What do I need to do to bind then query my LDAP service?

URI::LDAP is only for parsing and generating LDAP URIs. If you want to query the LDAP server you need to use a different tool like net-ldap or ruby-ldap.
An example of binding with simple authentication using net-ldap:
require 'net/ldap'
ldap = Net::LDAP.new(:host => '10.1.1.1',
:auth => {
:method => :simple,
:username => 'cn=admin,dc=cjndemo,dc=com',
:password => 'Passw0rd'
})
if ldap.bind
base = 'dc=cjndemo,dc=com'
filter = Net::LDAP::Filter.eq('objectclass', '*')
ldap.search(:base => base, :filter => filter) do |object|
puts "dn: #{object.dn}"
end
else
# authentication error
end

Related

how to create a filter of net-ldap for Ruby

I have a working ldap search query
ldapsearch -H ldaps://ldap.google.com -b dc=pangeare,dc=com '(memberOf=cn=pcore_readonly,ou=Groups,dc=pangeare,dc=com)'
I wonder how should I create the corresponding filter in ruby net-ldap:
treebase="dc=pangeare,dc=com"
filter = Net::LDAP::Filter.XXXXXXXXXX
ldap.search(:base => treebase, :filter=> filter, :return_result => true) do |entry|
entry.each do |attr,values|
puts "DN: #{entry.dn}"
Net::LDAP::Filter#eq should be what your looking for
require 'net/ldap'
host = 'ldap.google.com'
port = 636
auth = {
:method => :simple, # or :simple_tls ? (I don't know Google's LDAP)
:username => 'uid=username,dc=xxx,dc=xxx',
:password => 'password'
}
ldap = Net::LDAP.new( :host => host, :port => port, :auth => auth)
base = 'dc=pangeare,dc=com'
filter = Net::LDAP::Filter.eq('memberOf','cn=pcore_readonly,ou=Groups,dc=pangeare,dc=com')
ldap.search(:base => base, :filter => filter) do |entry|
p entry
end
Or you can just use Net::LDAP::Filter#construct:
filter = Net::LDAP::Filter.construct('(memberOf=cn=pcore_readonly,ou=Groups,dc=pangeare,dc=com)')

Integrating Google+ into Rails app (execute not working)

I am trying to display data from Google+ by adapting this to rails https://github.com/google/google-api-ruby-client I created a rails app, made a few tweaks to get oauth working with the client_secret.json, but when the request executes, nothing displays.
welcome_controller:
class WelcomeController < ApplicationController
def index
require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/installed_app'
$credentials = Google::APIClient::ClientSecrets.load
# Initialize the client.
$authorization = Signet::OAuth2::Client.new(
:authorization_uri => $credentials.authorization_uri,
:token_credential_uri => $credentials.token_credential_uri,
:client_id => $credentials.client_id,
:client_secret => $credentials.client_secret,
:redirect_uri => $credentials.redirect_uris.first,
:scope => 'https://www.googleapis.com/auth/plus.login')
#client = Google::APIClient.new(
:application_name => 'X',
:application_version => 'X'
)
# Initialize Google+ API. Note this will make a request to the
# discovery service every time, so be sure to use serialization
# in your production code. Check the samples for more details.
#plus = #client.discovered_api( "plus", "v1" )
# Make an API call.
#google_plus_info = #client.execute(
:api_method => #plus.activities.list,
:parameters => {'collection' => 'public', 'userId' => 'my_num_here'}
)
#puts #google_plus_info.data #tried this
render plain: "test: #{#google_plus_info.data}" #tried this too, just returns the call name
end
end
Anyone know why I'm getting it in this case, according to the API (i thought) this was a public call? NOTE: I changed this post in response to suggestions to show my current state of work.
Try replacing $authorization.execute with plus.execute
Take a look at https://github.com/google/google-api-ruby-client-samples/tree/master/googleplus
Here's a snippet from my working code:
#client = Google::APIClient.new(
:application_name => 'X',
:application_version => 'X'
)
#client.authorization.update_token!(:access_token => 'X', :refresh_token => 'X')
#plus = #client.discovered_api( "plus", "v1" )
#google_plus_info = #client.execute(
:api_method => #plus.people.get,
:parameters => {'collection' => 'public', 'userId' => 'me'}
)

How do I correctly call Magento SOAP API with Ruby Savon for Category Product Links?

I am trying to call the catalog_product_link.list API method using Savon. However, I keep receiving the error Error cannot find parameter.
Here is what I am using, though I have tried several variations of the call and still cannot get it to go through correctly:
client = Savon.client(wsdl: 'http://localhost/index.php/api/soap/?wsdl')
response = client.call(:login){message(username: 'user', apiKey: 'key')}
session = response.body[:login_response][:login_return]
#These all do not work
client.call(:call){message(:session => session, :method => 'catalog_product_link.list', :type => 'up_sell', :productId => '166')}
client.call(:call){message(:session => session, :method => 'catalog_product_link.list', :type => 'up_sell', :product => '166')}
client.call(:call){message(:sessionId => session, :resourcePath => 'catalog_product_link.list', :args => {:type => 'up_sell', :product => '166'})}
client.call(:call){message(:sessionId => session, :resourcePath => 'catalog_product_link.list', :args => {:type => 'up_sell', :productId => '166'})}
client.call(:call){message(:sessionId => session, :resourcePath => 'catalog_product_link.list', :arguments => {:type => 'up_sell', :product => '166'})}
Is there a different way to format to get this to work?
UPDATE: If I try removing the type parameter, it gives the error Given invalid link type, so it appears it does not like something about multiple parameters.
response = client.call(:call){message(:session => session, :method => 'catalog_product_link.list', :product => '166')}
I was able to get this to work using Builder:
class ServiceRequest
def initialize(session, type, product)
#session = session
#type = type
#product = product
end
def to_s
builder = Builder::XmlMarkup.new()
builder.instruct!(:xml, encoding: "UTF-8")
builder.tag!(
"env:Envelope",
"xmlns:env" => "http://schemas.xmlsoap.org/soap/envelope/",
"xmlns:ns1" => "urn:Magento",
"xmlns:ns2" => "http://xml.apache.org/xml-soap",
"xmlns:xsd" => "http://www.w3.org/2001/XMLSchema",
"xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance"
) do |envelope|
envelope.tag!("env:Body") do |body|
body.tag!("ns1:call") do |call|
builder.sessionId(#session, "xsi:type" => "xsd:string")
builder.resourcePath("catalog_product_link.list", "xsi:type" => "xsd:string")
builder.args("xsi:type" => "ns2:Map") do |args|
args.item do |item|
item.key("type", "xsi:type" => "xsd:string")
item.value(#type, "xsi:type" => "xsd:string")
end
args.item do |item|
item.key("product", "xsi:type" => "xsd:string")
item.value(#product, "xsi:type" => "xsd:string")
end
end
end
end
end
builder.target!
end
end
client.call(:call, xml: ServiceRequest.new(session, 'up_sell', '166').to_s)
Thanks to #rubiii for the direction.

Ruby LDAP search by encrypted password filter

I'm trying to search using an encrypted password filter, but it doesn't match any thing!!
is there a way to get match with it!!!
here is my code:
ldap = Net::LDAP.new :host => "----",
:port => 389,
:auth => {
:method => :simple,
:username => "----",
:password => "----"
}
filter = Net::LDAP::Filter.eq("userPassword", "1212")
filter2 = Net::LDAP::Filter.eq("cn", "general*")
tree_base = "dc=----,dc=---"
ldap.search(:base => tree_base, :filter => filter & filter2) do |entry|
puts "mail: #{entry.mail}"
puts "pw: #{entry.userPassword}"
end
it doesn't return any results but when I replace the filter of pw with another it works!!!
This is not a correct technique in LDAP. You should first search for the user, by username only, and then attempt to bind to LDAP using that username and password. If it succeeds, the password is correct.

Does the twilio-ruby gem take parameters other than 'from', 'to', and 'url' when making calls?

Looking at https://github.com/twilio/twilio-ruby/blob/master/lib/twilio-ruby/rest/calls.rb, it appears that only 'from', 'to', and 'url' are used. How do I pass a value for 'IfMachine'? For example, the following doesn't seem to work.
# set ACCOUNT_SID and AUTH_TOKEN
twilioClient = Twilio::REST::Client.new(ACCOUNT_SID, AUTH_TOKEN)
twilioAccount = twilioClient.account
twilioAccount.calls.create({
:from => 'from_number',
:to => 'to_number',
:url => '/url',
'IfMachine' => 'Hangup'
})
# IfMachine parameter is not passed in the above request
Setting 'IfMachine' => 'hangup' works for me. I think the Twilio API is case-sensitive with respect to parameter values. So 'hangup' would work but 'Hangup' probably won't.
Here's my twilio-ruby session showing that the parameters are passed correctly:
irb(main):002:0> c.account.calls.create :from => '2158377932', :to => '4159334335', :url => 'http://demo.twilio.com/welcome/voice', 'IfMachine' => 'continue'
warning: peer certificate won't be verified in this SSL session
=> <Twilio::REST::Call #uri=/2010-04-01/Accounts/AC8faaf6f7efb9dfd60bc0ff3aa7fa00be/Calls/CA0165c3b35c934ed5a2b7a87f343544ff>
irb(main):003:0> c.last_request
=> #<Net::HTTP::Post POST>
irb(main):004:0> req = c.last_request
=> #<Net::HTTP::Post POST>
irb(main):005:0> req.body
=> "IfMachine=continue&To=4159334335&Url=http%3a%2f%2fdemo.twilio.com%2fwelcome%2fvoice&From=2158377932"
I believe if you set it as a symbol instead it should work:
# set ACCOUNT_SID and AUTH_TOKEN
twilioClient = Twilio::REST::Client.new(ACCOUNT_SID, AUTH_TOKEN)
twilioAccount = twilioClient.account
twilioAccount.calls.create({
:from => 'from_number,
:to => 'to_number',
:url => '/url',
:if_machine => 'Hangup'
})

Resources