Class: OpenShift::MongoAuthService

Inherits:
AuthService
  • Object
show all
Defined in:
lib/openshift/mongo_auth_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(auth_info = nil) ⇒ MongoAuthService

Returns a new instance of MongoAuthService.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/openshift/mongo_auth_service.rb', line 8

def initialize(auth_info = nil)
  super

  if @auth_info != nil
    # no-op
  elsif defined? Rails
    @auth_info = Rails.application.config.auth
  else
    raise Exception.new("Mongo DataStore service is not initialized")
  end

  @replica_set  = @auth_info[:mongo_replica_sets]
  @host_port    = @auth_info[:mongo_host_port]
  @user         = @auth_info[:mongo_user]
  @password     = @auth_info[:mongo_password]
  @db           = @auth_info[:mongo_db]
  @collection   = @auth_info[:mongo_collection]
end

Instance Method Details

#authenticate(request, login, password) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/openshift/mongo_auth_service.rb', line 48

def authenticate(request, , password)
  if request.headers['User-Agent'] == "OpenShift"
    # password == iv, login == key
    return validate_broker_key(password, )
  else
    raise OpenShift::AccessDeniedException if .nil? || .empty? || password.nil? || password.empty?
    encoded_password = Digest::MD5.hexdigest(Digest::MD5.hexdigest(password) + @salt)
    hash = db.collection(@collection).find_one({"_id" => })
    if hash && !hash.empty? && (hash["password"] == encoded_password)
      return {:username => , :auth_method => :login}
    else
      raise OpenShift::AccessDeniedException
    end
  end
end

#dbObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/openshift/mongo_auth_service.rb', line 27

def db
  if @replica_set
    con = Mongo::ReplSetConnection.new(*@host_port << {:read => :secondary})
  else
    con = Mongo::Connection.new(@host_port[0], @host_port[1])
  end
  user_db = con.db(@db)
  user_db.authenticate(@user, @password) unless @user.nil?
  user_db
end

#login(request, params, cookies) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/openshift/mongo_auth_service.rb', line 64

def (request, params, cookies)
  if params['broker_auth_key'] && params['broker_auth_iv']
    validate_broker_key(params['broker_auth_iv'], params['broker_auth_key'])
  else
    data = JSON.parse(params['json_data'])
    return authenticate(request, data['rhlogin'], params['password'])
  end
end

#register_user(login, password) ⇒ Object



38
39
40
41
# File 'lib/openshift/mongo_auth_service.rb', line 38

def register_user(,password)
  encoded_password = Digest::MD5.hexdigest(Digest::MD5.hexdigest(password) + @salt)
  db.collection(@collection).insert({"_id" => , "user" => , "password" => encoded_password})
end

#user_exists?(login) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/openshift/mongo_auth_service.rb', line 43

def user_exists?()
  hash = db.collection(@collection).find_one({"_id" => })
  !hash.nil?
end