Class: OpenNebula::LdapAuth
- Inherits:
-
Object
- Object
- OpenNebula::LdapAuth
- Defined in:
- lib/opennebula/ldap_auth.rb
Instance Method Summary collapse
- #authenticate(user, password) ⇒ Object
- #find_user(name) ⇒ Object
- #generate_mapping ⇒ Object
- #get_groups ⇒ Object
- #in_hash_ignore_case?(hash, key) ⇒ Boolean
-
#initialize(options) ⇒ LdapAuth
constructor
A new instance of LdapAuth.
- #is_in_group?(user, group) ⇒ Boolean
- #load_mapping ⇒ Object
Constructor Details
#initialize(options) ⇒ LdapAuth
Returns a new instance of LdapAuth.
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 |
# File 'lib/opennebula/ldap_auth.rb', line 35 def initialize() @options={ :host => 'localhost', :port => 389, :user => nil, :password => nil, :base => nil, :group_base => nil, :auth_method => :simple, :user_field => 'cn', :user_group_field => 'dn', :group_field => 'member', :mapping_generate => true, :mapping_timeout => 300, :mapping_filename => 'server1.yaml', :mapping_key => 'GROUP_DN', :mapping_default => 1, :attributes => [ "memberOf" ], :rfc2307bis => true }.merge() ops={} if @options[:user] ops[:auth] = { :method => @options[:auth_method], :username => @options[:user], :password => @options[:password] } end if !@options[:rfc2307bis] @options[:attributes] << @options[:user_field] end # fetch the user group field only if we need that if @options[:group] or !@options[:rfc2307bis] @options[:attributes] << @options[:user_group_field] end ops[:host]=@options[:host] if @options[:host] ops[:port]=@options[:port].to_i if @options[:port] ops[:encryption]=@options[:encryption] if @options[:encryption] @options[:mapping_file_path] = VAR_LOCATION + @options[:mapping_filename] generate_mapping if @options[:mapping_generate] load_mapping @ldap=Net::LDAP.new(ops) end |
Instance Method Details
#authenticate(user, password) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/opennebula/ldap_auth.rb', line 172 def authenticate(user, password) ldap=@ldap.clone auth={ :method => @options[:auth_method], :username => user, :password => password } if ldap.bind(auth) true else false end end |
#find_user(name) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/opennebula/ldap_auth.rb', line 134 def find_user(name) filter = Net::LDAP::Filter.equals(@options[:user_field], name) result = @ldap.search( :base => @options[:base], :attributes => @options[:attributes], :filter => filter ) if result && result.first @user = result.first [@user.dn, @user[@options[:user_group_field]]] else result=@ldap.search(:base => name) if result && result.first @user = result.first [name, @user[@options[:user_group_field]]] else [nil, nil] end end end |
#generate_mapping ⇒ Object
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 |
# File 'lib/opennebula/ldap_auth.rb', line 86 def generate_mapping file=@options[:mapping_file_path] generate = false if File.exists?(file) stat = File.stat(file) age = Time.now.to_i - stat.mtime.to_i generate = true if age > @options[:mapping_timeout] else generate = true end return if !generate client = OpenNebula::Client.new group_pool = OpenNebula::GroupPool.new(client) group_pool.info groups = group_pool.to_hash[''] groups=[group_pool.get_hash['GROUP_POOL']['GROUP']].flatten yaml={} groups.each do |group| if group['TEMPLATE'] && group['TEMPLATE'][@options[:mapping_key]] yaml[group['TEMPLATE'][@options[:mapping_key]]] = group['ID'] end end File.open(file, 'w') do |f| f.write(yaml.to_yaml) end end |
#get_groups ⇒ Object
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 |
# File 'lib/opennebula/ldap_auth.rb', line 192 def get_groups groups = [] if @options[:rfc2307bis] [@user['memberOf']].flatten.each do |group| if (g = in_hash_ignore_case?(@mapping, group)) groups << @mapping[g] end end else group_base = @options[:group_base] ? @options[:group_base] : @options[:base] filter = Net::LDAP::Filter.equals(@options[:group_field], @user[@options[:user_group_field]].first) @ldap.search( :base => group_base, :attributes => [ "dn" ], :filter => filter ) do |entry| if (g = in_hash_ignore_case?(@mapping, entry.dn)) groups << @mapping[g] end end end groups.delete(false) groups.compact.uniq end |
#in_hash_ignore_case?(hash, key) ⇒ Boolean
188 189 190 |
# File 'lib/opennebula/ldap_auth.rb', line 188 def in_hash_ignore_case?(hash, key) return hash.keys.find {|k| key.downcase == k.downcase} end |
#is_in_group?(user, group) ⇒ Boolean
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/opennebula/ldap_auth.rb', line 158 def is_in_group?(user, group) username = user.first.force_encoding(Encoding::UTF_8) result=@ldap.search( :base => group, :attributes => [@options[:group_field]], :filter => "(#{@options[:group_field]}=#{username})") if result && result.first true else false end end |
#load_mapping ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/opennebula/ldap_auth.rb', line 120 def load_mapping file=@options[:mapping_file_path] @mapping = {} if File.exists?(file) @mapping = YAML.load(File.read(file)) end if @mapping.class != Hash @mapping = {} end end |