Class: Lapis::Yggdrasil::AuthenticationClient

Inherits:
Client
  • Object
show all
Defined in:
lib/lapis/yggdrasil/authentication_client.rb

Overview

Provides interaction with an authentication server.

Constant Summary collapse

OFFICIAL_AUTHENTICATION_URL =

URL of the official Mojang authentication server.

'https://authserver.mojang.com'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ AuthenticationClient

Creates a new interface to an authentication server.

Parameters:

  • url (String)

    Base URL of the authentication server.


19
20
21
22
# File 'lib/lapis/yggdrasil/authentication_client.rb', line 19

def initialize(url)
  super(url)
  @factory = Messaging::ResponseFactory.new
end

Class Method Details

.officialAuthenticationClient

Singleton for accessing the official Mojang authentication server.


26
27
28
# File 'lib/lapis/yggdrasil/authentication_client.rb', line 26

def self.official
  @official ||= new(OFFICIAL_AUTHENTICATION_URL)
end

Instance Method Details

#authenticate(username, password, client_token = nil, agent = Agent::DEFAULT_AGENT) ⇒ Session

Create a new session by authenticating with the server.

Parameters:

  • username (String)

    Minecraft username or Mojang account email address.

  • password (String)

    Password for the user.

  • client_token (Uuid, nil) (defaults to: nil)

    ID for the client instance. Provide nil to have the authentication server provide one.

  • agent (Agent) (defaults to: Agent::DEFAULT_AGENT)

    Game information being requested.

Returns:

  • (Session)

    Newly created session.

Raises:


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lapis/yggdrasil/authentication_client.rb', line 38

def authenticate(username, password, client_token = nil, agent = Agent::DEFAULT_AGENT)
  request  = Messaging::AuthenticationRequest.new(username, password, agent, client_token)
  response = send_request(request)
  message  = @factory.parse_authentication_response(response)
  if message.ok?
    info = SessionInfo.new(message.client_token, message.access_token, message.selected_profile)
    Session.new(info, self)
  else
    fail message.to_error
  end
end

#invalidate(session) ⇒ void

This method returns an undefined value.

Marks a previously used session as not valid.

Parameters:

  • session (SessionInfo)

    Information used in the previous session.

Raises:


96
97
98
99
100
101
# File 'lib/lapis/yggdrasil/authentication_client.rb', line 96

def invalidate(session)
  request  = Messaging::InvalidateRequest.new(session.client_token, session.access_token)
  response = send_request(request)
  message  = @factory.parse_invalidate_response(response)
  fail message.to_error unless message.ok?
end

#refresh(session, profile = nil) ⇒ SessionInfo

Note:

Setting profile to anything other than nil will result in an error. In the current version of Yggdrasil, switching profiles is not supported.

Refresh a session that was previously used.

Parameters:

  • session (SessionInfo)

    Information used in the previous session.

  • profile (Profile, nil) (defaults to: nil)

    Profile to switch to. Set to nil to keep the current profile.

Returns:

Raises:


58
59
60
61
62
63
64
65
66
67
# File 'lib/lapis/yggdrasil/authentication_client.rb', line 58

def refresh(session, profile = nil)
  request  = Messaging::RefreshRequest.new(session.client_token, session.access_token, profile)
  response = send_request(request)
  message  = @factory.parse_refresh_response(response)
  if message.ok?
    SessionInfo.new(message.client_token, message.access_token, message.profile)
  else
    fail message.to_error
  end
end

#signout(username, password) ⇒ void

This method returns an undefined value.

Invalidates a session by using credentials instead of authentication tokens.

Parameters:

  • username (String)

    Minecraft username or Mojang account email address.

  • password (String)

    Password for the user.

Raises:


85
86
87
88
89
90
# File 'lib/lapis/yggdrasil/authentication_client.rb', line 85

def signout(username, password)
  request  = Messaging::SignoutRequest.new(username, password)
  response = send_request(request)
  message  = @factory.parse_signout_response(response)
  fail message.to_error unless message.ok?
end

#validate(session) ⇒ true, false

Checks that a previously used session is still valid.

Parameters:

  • session (SessionInfo)

    Information used in the previous session.

Returns:

  • (true)

    The session is still valid.

  • (false)

    The session is no longer valid.


73
74
75
76
77
78
# File 'lib/lapis/yggdrasil/authentication_client.rb', line 73

def validate(session)
  request  = Messaging::ValidateRequest.new(session.client_token, session.access_token)
  response = send_request(request)
  message  = @factory.parse_validate_response(response)
  message.ok?
end