Class: GameMachine::Handlers::PlayerAuthentication

Inherits:
Object
  • Object
show all
Includes:
Models, Singleton
Defined in:
lib/game_machine/handlers/player_authentication.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlayerAuthentication

Returns a new instance of PlayerAuthentication.



18
19
20
21
22
# File 'lib/game_machine/handlers/player_authentication.rb', line 18

def initialize
  @sessions = {}
  java_import "#{AppConfig.instance.config.handlers.auth}"
  @authclass = AppConfig.instance.config.handlers.auth.split('.').last.constantize
end

Instance Attribute Details

#authclassObject (readonly)

Returns the value of attribute authclass.



17
18
19
# File 'lib/game_machine/handlers/player_authentication.rb', line 17

def authclass
  @authclass
end

Instance Method Details

#authorize(username, password) ⇒ Object

Returns true if authorized, false if not



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/game_machine/handlers/player_authentication.rb', line 37

def authorize(username,password)
  GameMachine.logger.info "authorize: #{username}"
  if player = get_player(username)
    authenticator = authclass.new(player)
    if authenticator.authenticate(password)
      @sessions[username] = authtoken(username,password)
      player.set_authtoken(@sessions[username])
      player.store_set('players')
      return @sessions[username]
    else
      GameMachine.logger.info "player: #{player.id} password does not match"
      false
    end
  else
    GameMachine.logger.info "player: #{username} not found"
    false
  end
  false
end

#authtoken_for(username) ⇒ Object

Returns a session token for a logged in user. This must be a string and should not be too long, as it gets sent with every message.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/game_machine/handlers/player_authentication.rb', line 59

def authtoken_for(username)
  if authtoken = @sessions.fetch(username,nil)
    return authtoken

  # user authenticated on different server, we have to look up their authtoken
  # and save it in the local sessions hash
  elsif player = get_player(username)
    if authtoken = player.authtoken
      @sessions[username] = authtoken
      return authtoken
    else
      GameMachine.logger.info "Authoken for #{username} is nil"
      nil
    end
  else
    GameMachine.logger.info "User #{username} not found"
    nil
  end
end

#get_player(username) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/game_machine/handlers/player_authentication.rb', line 28

def get_player(username)
  if public?
    MessageLib::Player.new.set_id(username)
  else
    MessageLib::Player.store_get('players',username,2000)
  end
end

#public?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/game_machine/handlers/player_authentication.rb', line 24

def public?
  AppConfig.instance.config.handlers.auth == "com.game_machine.authentication.PublicAuthenticator"
end