lapis-yggdrasil 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +18 -0
  3. data/.gitignore +151 -0
  4. data/.rspec +3 -0
  5. data/.rubocop.yml +1156 -0
  6. data/.travis.yml +4 -0
  7. data/Gemfile +8 -0
  8. data/LICENSE.md +16 -0
  9. data/README.md +130 -0
  10. data/Rakefile +28 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +7 -0
  13. data/lapis-yggdrasil.gemspec +35 -0
  14. data/lib/lapis/yggdrasil.rb +18 -0
  15. data/lib/lapis/yggdrasil/agent.rb +52 -0
  16. data/lib/lapis/yggdrasil/authentication_client.rb +106 -0
  17. data/lib/lapis/yggdrasil/authentication_error.rb +29 -0
  18. data/lib/lapis/yggdrasil/client.rb +38 -0
  19. data/lib/lapis/yggdrasil/error_codes.rb +43 -0
  20. data/lib/lapis/yggdrasil/messaging.rb +23 -0
  21. data/lib/lapis/yggdrasil/messaging/authentication_request.rb +50 -0
  22. data/lib/lapis/yggdrasil/messaging/authentication_response.rb +36 -0
  23. data/lib/lapis/yggdrasil/messaging/error_response.rb +74 -0
  24. data/lib/lapis/yggdrasil/messaging/invalidate_request.rb +34 -0
  25. data/lib/lapis/yggdrasil/messaging/refresh_request.rb +47 -0
  26. data/lib/lapis/yggdrasil/messaging/refresh_response.rb +29 -0
  27. data/lib/lapis/yggdrasil/messaging/request.rb +25 -0
  28. data/lib/lapis/yggdrasil/messaging/response.rb +33 -0
  29. data/lib/lapis/yggdrasil/messaging/response_factory.rb +82 -0
  30. data/lib/lapis/yggdrasil/messaging/signout_request.rb +38 -0
  31. data/lib/lapis/yggdrasil/messaging/token_request.rb +42 -0
  32. data/lib/lapis/yggdrasil/messaging/token_response.rb +36 -0
  33. data/lib/lapis/yggdrasil/messaging/validate_request.rb +34 -0
  34. data/lib/lapis/yggdrasil/profile.rb +65 -0
  35. data/lib/lapis/yggdrasil/session.rb +60 -0
  36. data/lib/lapis/yggdrasil/session_info.rb +62 -0
  37. data/lib/lapis/yggdrasil/version.rb +5 -0
  38. data/spec/factories/agent_factory.rb +10 -0
  39. data/spec/factories/authentication_error_factory.rb +14 -0
  40. data/spec/factories/message_factory.rb +111 -0
  41. data/spec/factories/profile_factory.rb +20 -0
  42. data/spec/factories/session_info_factory.rb +11 -0
  43. data/spec/factories/uuid_factory.rb +28 -0
  44. data/spec/lapis/yggdrasil/agent_spec.rb +103 -0
  45. data/spec/lapis/yggdrasil/authentication_client_spec.rb +200 -0
  46. data/spec/lapis/yggdrasil/authentication_error_spec.rb +42 -0
  47. data/spec/lapis/yggdrasil/messaging/authentication_request_spec.rb +61 -0
  48. data/spec/lapis/yggdrasil/messaging/authentication_response_spec.rb +63 -0
  49. data/spec/lapis/yggdrasil/messaging/error_response_spec.rb +164 -0
  50. data/spec/lapis/yggdrasil/messaging/invalidate_request_spec.rb +29 -0
  51. data/spec/lapis/yggdrasil/messaging/refresh_request_spec.rb +70 -0
  52. data/spec/lapis/yggdrasil/messaging/refresh_response_spec.rb +50 -0
  53. data/spec/lapis/yggdrasil/messaging/response_factory_spec.rb +130 -0
  54. data/spec/lapis/yggdrasil/messaging/response_spec.rb +36 -0
  55. data/spec/lapis/yggdrasil/messaging/signout_request_spec.rb +29 -0
  56. data/spec/lapis/yggdrasil/messaging/validate_request_spec.rb +29 -0
  57. data/spec/lapis/yggdrasil/profile_spec.rb +108 -0
  58. data/spec/lapis/yggdrasil/session_info_spec.rb +131 -0
  59. data/spec/lapis/yggdrasil/session_spec.rb +158 -0
  60. data/spec/spec_helper.rb +89 -0
  61. metadata +269 -0
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Pull in common gem from source until it has a stable release.
4
+ gem 'lapis-common', :github => 'lapis-mc/common', :branch => 'develop'
5
+
6
+ # Gem dependencies and information defined in lapis-yggdrasil.gemspec
7
+ gemspec
8
+
@@ -0,0 +1,16 @@
1
+ The MIT License (MIT)
2
+ =====================
3
+
4
+ *Copyright (c) 2016 Lapis MC*
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7
+ documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
8
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
9
+ persons to whom the Software is furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,130 @@
1
+ Yggdrasil
2
+ =========
3
+
4
+ [![Travis CI](https://travis-ci.org/lapis-mc/yggdrasil.svg)](https://travis-ci.org/lapis-mc/yggdrasil)
5
+ [![Code Climate](https://codeclimate.com/github/lapis-mc/yggdrasil/badges/gpa.svg)](https://codeclimate.com/github/lapis-mc/yggdrasil)
6
+ [![Test Coverage](https://codeclimate.com/github/lapis-mc/yggdrasil/badges/coverage.svg)](https://codeclimate.com/github/lapis-mc/yggdrasil/coverage)
7
+ [![Issue Count](https://codeclimate.com/github/lapis-mc/yggdrasil/badges/issue_count.svg)](https://codeclimate.com/github/lapis-mc/yggdrasil)
8
+ [![Documentation](https://inch-ci.org/github/lapis-mc/yggdrasil.svg?branch=master)](http://www.rubydoc.info/github/lapis-mc/yggdrasil/master)
9
+
10
+ Provides access to Mojang's authentication system.
11
+ This functionality can be used to authenticate with custom game launchers.
12
+
13
+ Installation
14
+ ------------
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'lapis-yggdrasil'
20
+ ```
21
+
22
+ Or to your gemspec:
23
+
24
+ ```ruby
25
+ spec.add_dependency 'lapis-yggdrasil'
26
+ ```
27
+
28
+ And then execute:
29
+
30
+ $ bundle
31
+
32
+ Or install it yourself as:
33
+
34
+ $ gem install lapis-yggdrasil
35
+
36
+ Usage
37
+ -----
38
+
39
+ ### Script usage
40
+
41
+ Include the gem by adding this to your scripts:
42
+
43
+ ```ruby
44
+ require 'lapis/yggdrasil'
45
+ ```
46
+
47
+ #### Authenticating with Yggdrasil
48
+
49
+ This gem tracks authentication by using sessions.
50
+ To create a session, you must perform an initial authentication.
51
+
52
+ ```ruby
53
+ client = Lapis::Yggdrasil::AuthenticationClient.official
54
+ session = client.authenticate(username, password)
55
+ ```
56
+
57
+ `session` will contain a tracking information for the authentication.
58
+ The session can be reused later without prompting the user for a username and password again.
59
+ An `AuthenticationError` will be raised if the credentials are wrong.
60
+
61
+ *NOTE:* The username will be the user's email address for "Mojang accounts."
62
+
63
+ #### Reusing a session
64
+
65
+ To reuse the session at a later time:
66
+
67
+ ```ruby
68
+ session.refresh
69
+ ```
70
+
71
+ And to check if the current session is still valid:
72
+
73
+ ```ruby
74
+ unless session.valid?
75
+ # Session has expired and needs to be refreshed.
76
+ session.refresh
77
+ end
78
+ # Proceed with valid session.
79
+ ```
80
+
81
+ #### Ending a session
82
+
83
+ To end the session:
84
+
85
+ ```ruby
86
+ session.invalidate
87
+ ```
88
+
89
+ *NOTE:* Due to the way Yggdrasil works, a session may become invalid at any time.
90
+ The exact details aren't known, but authenticating elsewhere and timeouts seem to cause previous sessions to become invalid.
91
+
92
+ #### Using a session
93
+
94
+ The information needed for a Minecraft client is contained in the session information.
95
+ The following information is available after authenticating:
96
+
97
+ ```ruby
98
+ session.info.client_token # Token representing the Minecraft client.
99
+ session.info.access_token # Token provided by Yggdrasil used with the client token to prove authentication.
100
+ session.info.profile # Current profile the user has selected.
101
+ session.info.profile.name # Displayed name in the game.
102
+ session.info.profile.id # UUID of the player.
103
+ ```
104
+
105
+ `client_token`, `access_token`, and `id` are all UUIDs.
106
+ Lapis has its own UUID class.
107
+ Use `#to_s` to generate a more recognizable format.
108
+ Passing false to `#to_s` will remove dashes.
109
+
110
+ ### Command-line usage
111
+
112
+ TODO
113
+
114
+ Development
115
+ -----------
116
+
117
+ After checking out the repo, run `bin/setup` to install dependencies.
118
+ Then, run `bundle exec rake test` to run the tests.
119
+ It's recommended that you run `bundle exec rake inspect` to run inspections.
120
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
121
+ `bundle exec rake doc` will generate documentation.
122
+
123
+ To install this gem onto your local machine, run `bundle exec rake install`.
124
+ To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`.
125
+ This will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
126
+
127
+ Contributing
128
+ ------------
129
+
130
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/lapis-mc/yggdrasil).
@@ -0,0 +1,28 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'reek/rake/task'
3
+ require 'rubocop/rake_task'
4
+ require 'rubocop'
5
+ require 'rspec/core/rake_task'
6
+ require 'yard'
7
+
8
+ task :default => [:build, :test]
9
+ task :inspect => [:reek, :rubocop]
10
+
11
+ RSpec::Core::RakeTask.new(:spec)
12
+ task :test => :spec
13
+
14
+ Reek::Rake::Task.new do |task| # :reek
15
+ task.name = :reek
16
+ task.source_files = 'lib/**/*.rb'
17
+ task.fail_on_error = false
18
+ end
19
+
20
+ RuboCop::RakeTask.new(:rubocop) do |task|
21
+ task.patterns = ['lib/**/*.rb']
22
+ task.fail_on_error = false
23
+ end
24
+
25
+ YARD::Rake::YardocTask.new do |task| # :yard
26
+ task.files = %w(lib/**/*.rb - *.md)
27
+ end
28
+ task :doc => :yard
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'lapis/yggdrasil'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install --path=vendor/bundle
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lapis/yggdrasil/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'lapis-yggdrasil'
8
+ spec.version = Lapis::Yggdrasil::VERSION
9
+ spec.authors = ['Michael Miller']
10
+ spec.email = ['bluepixelmike@gmail.com']
11
+
12
+ spec.summary = %q{Provides access to Mojang's authentication system.}
13
+ spec.description = %q{Small library for authenticating accounts with Mojang's game servers.
14
+ This functionality can be used to authenticate with custom game launchers.}
15
+ spec.homepage = 'https://github.com/lapis-mc/yggdrasil'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.bindir = 'bin'
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.executables = spec.files.grep(%r{^bin/(lapis|minecraft|yggdrasil|mojang)-}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'httpclient', '~> 2.1'
25
+
26
+ spec.add_development_dependency 'bundler'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ spec.add_development_dependency 'factory_girl', '~> 4.0'
30
+ spec.add_development_dependency 'webmock', '~> 2.0'
31
+ spec.add_development_dependency 'reek', '~> 3.0'
32
+ spec.add_development_dependency 'rubocop', '~> 0.38.0'
33
+ spec.add_development_dependency 'codeclimate-test-reporter'
34
+ spec.add_development_dependency 'yard'
35
+ end
@@ -0,0 +1,18 @@
1
+ module Lapis
2
+
3
+ # Authentication protocol for accessing Mojang's games.
4
+ module Yggdrasil
5
+ end
6
+
7
+ end
8
+
9
+ require_relative 'yggdrasil/version'
10
+ require_relative 'yggdrasil/error_codes'
11
+ require_relative 'yggdrasil/authentication_error'
12
+ require_relative 'yggdrasil/agent'
13
+ require_relative 'yggdrasil/profile'
14
+ require_relative 'yggdrasil/session_info'
15
+ require_relative 'yggdrasil/client'
16
+ require_relative 'yggdrasil/authentication_client'
17
+ require_relative 'yggdrasil/session'
18
+ require_relative 'yggdrasil/messaging'
@@ -0,0 +1,52 @@
1
+ module Lapis
2
+ module Yggdrasil
3
+
4
+ # Information about the game to associate profiles with.
5
+ class Agent
6
+
7
+ # Name of the game.
8
+ # @return [String]
9
+ attr_reader :name
10
+
11
+ # Client profile version.
12
+ # @return [Fixnum]
13
+ # @note This is always 1 right now, but may be increased in the future.
14
+ attr_reader :version
15
+
16
+ # Creates an agent.
17
+ # @param name [String] Name of the game.
18
+ # @param version [Fixnum] Client profile version.
19
+ def initialize(name, version)
20
+ @name = name.dup.freeze
21
+ @version = version
22
+ end
23
+
24
+ # Compares the agent to another.
25
+ # @param other [Agent] Agent to compare against.
26
+ # @return [true] The agents are the same.
27
+ # @return [false] The agents are different.
28
+ def ==(other)
29
+ other.name == @name &&
30
+ other.version == @version
31
+ end
32
+
33
+ # Creates an agent from a set of properties in a request.
34
+ # @param properties [Hash<Symbol => String, Fixnum>] Set of properties.
35
+ # @option properties [String] :name Name of the game.
36
+ # @option properties [Fixnum] :version Client profile version.
37
+ # @return [Agent]
38
+ # @note Missing properties will have their values pulled from {DefaultAgent}.
39
+ def self.from_properties(properties)
40
+ name = (properties[:name] || DEFAULT_AGENT.name).dup.freeze
41
+ version = properties[:version] || DEFAULT_AGENT.version
42
+ new(name, version)
43
+ end
44
+
45
+ # Agent used for Minecraft.
46
+ # @return [Agent]
47
+ DEFAULT_AGENT = new('Minecraft', 1)
48
+
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,106 @@
1
+ require_relative 'client'
2
+ require_relative 'agent'
3
+ require_relative 'session_info'
4
+ require_relative 'session'
5
+ require_relative 'authentication_error'
6
+ require_relative 'messaging'
7
+
8
+ module Lapis
9
+ module Yggdrasil
10
+
11
+ # Provides interaction with an authentication server.
12
+ class AuthenticationClient < Client
13
+
14
+ # URL of the official Mojang authentication server.
15
+ OFFICIAL_AUTHENTICATION_URL = 'https://authserver.mojang.com'
16
+
17
+ # Creates a new interface to an authentication server.
18
+ # @param url [String] Base URL of the authentication server.
19
+ def initialize(url)
20
+ super(url)
21
+ @factory = Messaging::ResponseFactory.new
22
+ end
23
+
24
+ # Singleton for accessing the official Mojang authentication server.
25
+ # @return [AuthenticationClient]
26
+ def self.official
27
+ @official ||= new(OFFICIAL_AUTHENTICATION_URL)
28
+ end
29
+
30
+ # Create a new session by authenticating with the server.
31
+ # @param username [String] Minecraft username or Mojang account email address.
32
+ # @param password [String] Password for the user.
33
+ # @param client_token [Uuid, nil] ID for the client instance.
34
+ # Provide +nil+ to have the authentication server provide one.
35
+ # @param agent [Agent] Game information being requested.
36
+ # @return [Session] Newly created session.
37
+ # @raise [AuthenticationError] A problem occurred while authenticating.
38
+ def authenticate(username, password, client_token = nil, agent = Agent::DEFAULT_AGENT)
39
+ request = Messaging::AuthenticationRequest.new(username, password, agent, client_token)
40
+ response = send_request(request)
41
+ message = @factory.parse_authentication_response(response)
42
+ if message.ok?
43
+ info = SessionInfo.new(message.client_token, message.access_token, message.selected_profile)
44
+ Session.new(info, self)
45
+ else
46
+ fail message.to_error
47
+ end
48
+ end
49
+
50
+ # Refresh a session that was previously used.
51
+ # @param session [SessionInfo] Information used in the previous session.
52
+ # @param profile [Profile, nil] Profile to switch to.
53
+ # Set to +nil+ to keep the current profile.
54
+ # @return [SessionInfo] Updated session information.
55
+ # @note Setting +profile+ to anything other than +nil+ will result in an error.
56
+ # In the current version of Yggdrasil, switching profiles is not supported.
57
+ # @raise [AuthenticationError] A problem occurred with authentication.
58
+ def refresh(session, profile = nil)
59
+ request = Messaging::RefreshRequest.new(session.client_token, session.access_token, profile)
60
+ response = send_request(request)
61
+ message = @factory.parse_refresh_response(response)
62
+ if message.ok?
63
+ SessionInfo.new(message.client_token, message.access_token, message.profile)
64
+ else
65
+ fail message.to_error
66
+ end
67
+ end
68
+
69
+ # Checks that a previously used session is still valid.
70
+ # @param session [SessionInfo] Information used in the previous session.
71
+ # @return [true] The session is still valid.
72
+ # @return [false] The session is no longer valid.
73
+ def validate(session)
74
+ request = Messaging::ValidateRequest.new(session.client_token, session.access_token)
75
+ response = send_request(request)
76
+ message = @factory.parse_validate_response(response)
77
+ message.ok?
78
+ end
79
+
80
+ # Invalidates a session by using credentials instead of authentication tokens.
81
+ # @param username [String] Minecraft username or Mojang account email address.
82
+ # @param password [String] Password for the user.
83
+ # @return [void]
84
+ # @raise [AuthenticationError] A problem occurred with authentication.
85
+ def signout(username, password)
86
+ request = Messaging::SignoutRequest.new(username, password)
87
+ response = send_request(request)
88
+ message = @factory.parse_signout_response(response)
89
+ fail message.to_error unless message.ok?
90
+ end
91
+
92
+ # Marks a previously used session as not valid.
93
+ # @param session [SessionInfo] Information used in the previous session.
94
+ # @return [void]
95
+ # @raise [AuthenticationError] A problem occurred with authentication.
96
+ def invalidate(session)
97
+ request = Messaging::InvalidateRequest.new(session.client_token, session.access_token)
98
+ response = send_request(request)
99
+ message = @factory.parse_invalidate_response(response)
100
+ fail message.to_error unless message.ok?
101
+ end
102
+
103
+ end
104
+
105
+ end
106
+ end
@@ -0,0 +1,29 @@
1
+ module Lapis
2
+ module Yggdrasil
3
+
4
+ # Information about an error that occurred while interacting with an authentication server.
5
+ class AuthenticationError < RuntimeError
6
+
7
+ # Key used to distinguish from other error types.
8
+ # @return [Symbol]
9
+ # @see ErrorCodes
10
+ attr_reader :code
11
+
12
+ # Creates a new authentication error.
13
+ # @param code [Symbol] Key used to distinguish from other error types.
14
+ # @param message [String, nil] Information about why the error occurred.
15
+ def initialize(code, message = nil)
16
+ super(message)
17
+ @code = code
18
+ end
19
+
20
+ # Creates a string representation of the error.
21
+ # @return [String]
22
+ def to_s
23
+ "#{code} - #{super}"
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+ end