GithubHelp home page GithubHelp logo

hycrafthd / minecraft-authenticator Goto Github PK

View Code? Open in Web Editor NEW
43.0 4.0 9.0 472 KB

This is a library to authenticate a mojang and a microsoft account for minecraft services

License: Apache License 2.0

Java 100.00%
minecraft authentication minecraft-authentication minecraft-auth mojang mojang-authentication microsoft microsoft-authentication library

minecraft-authenticator's Introduction

Minecraft Authenticator

If you need mojang authentication or java 8 support please have a look at branch 1.x and 2.x

A minecraft authentication library that allows microsoft (xbox live) accounts to be logged in and returns a minecraft profile with an access token as well as xbox profile settings. This library also allows for storage of the authentication data and therefore sessions can be refreshed without a new login on the users side.

Building

To build just run ./gradlew build. You will find the jars in the build/libs directory. This project requires gson and java 17 as dependency.

Include in your own project

To include this project you can use the maven build of this project which will resolve all required dependencies automatically. The latest version is the latest tag in github.

Gradle

repositories {
	maven {
		url = "https://repo.u-team.info"
	}
}

dependencies {
	implementation "net.hycrafthd:minecraft_authenticator:3.0.5"
}

Maven

<repositories>
  <repository>
    <id>u-team-repo</id>
    <url>https://repo.u-team.info/</url>
  </repository>
</repositories>

<dependencies>
  <dependency>
    <groupId>net.hycrafthd</groupId>
    <artifactId>minecraft_authenticator</artifactId>
    <version>3.0.5</version>
  </dependency>
</dependencies>

Usage

The main public facing api is the Authenticator class. This class is documented and you should have a look here about more information. The following code snippets are just some simple usage demonstrations.

Here is a simple login with microsoft

// Build authenticator
final Authenticator authenticator = Authenticator.ofMicrosoft(authorizationCode).shouldAuthenticate().build();
try {
	// Run authentication
	authenticator.run();
} catch (final AuthenticationException ex) {
	// Always check if result file is present when an exception is thrown
	final AuthenticationFile file = authenticator.getResultFile();
	if (file != null) {
		// Save authentication file
		file.writeCompressed(outputStream);
	}
	
	// Show user error or rethrow
	throw ex;
}

// Save authentication file
final AuthenticationFile file = authenticator.getResultFile();
file.writeCompressed(outputStream);

// Get user
final Optional<User> user = authenticator.getUser();

Here is an login with an existing authentication file to refresh the session

// Build authenticator
final Authenticator authenticator = Authenticator.of(existingAuthFile).shouldAuthenticate().build();
try {
	// Run authentication
	authenticator.run();
} catch (final AuthenticationException ex) {
	// Always check if result file is present when an exception is thrown
	final AuthenticationFile file = authenticator.getResultFile();
	if (file != null) {
		// Save authentication file
		file.writeCompressed(outputStream);
	}
	
	// Show user error or rethrow
	throw ex;
}

// Save authentication file
final AuthenticationFile file = authenticator.getResultFile();
file.writeCompressed(outputStream);

// Get user
final Optional<User> user = authenticator.getUser();

Here is a login with a custom azure application and the retrieval of xbox profile settings

// Build authenticator
final Authenticator authenticator = Authenticator.ofMicrosoft(authorizationCode)
	.customAzureApplication(clientId, redirectUrl)
	.shouldRetrieveXBoxProfile()
	.shouldAuthenticate()
	.build();
try {
	// Run authentication
	authenticator.run();
} catch (final AuthenticationException ex) {
	// Always check if result file is present when an exception is thrown
	final AuthenticationFile file = authenticator.getResultFile();
	if (file != null) {
		// Save authentication file
		file.writeCompressed(outputStream);
	}
	
	// Show user error or rethrow
	throw ex;
}

// Save authentication file
final AuthenticationFile file = authenticator.getResultFile();
file.writeCompressed(outputStream);

// Get user
final Optional<User> user = authenticator.getUser();

// Get XBox profile
final Optional<XBoxProfile> xBoxProfile = authenticator.getXBoxProfile();

License

This project is licensed under apache 2 license. For more information see here.

minecraft-authenticator's People

Contributors

appledash avatar hycrafthd avatar shirosaka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

minecraft-authenticator's Issues

Request: Enable localhost as callback URL (allows external browser usage)

Hello,

I have had great success so far with this library, and look forward to implementingreleasing(edit: because I already have things implemented in my dev branch) it in my custom launcher. There is currently one limitation holding me back, which is that my launcher's GUI is in JavaFX, and the JavaFX WebView has problems with the Microsoft login.

As a solution, I've created a process in my application where it launches the system web browser to the URL, and creates and HTTP listener on localhost for callback. Everything works great when I tested using my own client ID (though of course I cannot use that auth code for the minecraft_authenticator library). When I use the URL from MicrosoftService.oAuthLoginUrl() and replace the redirect URI with MicrosoftService.oAuthLoginUrl().toString().replaceAll( Constants.MICROSOFT_OAUTH_REDIRECT_URL, "http://localhost" ), the following message is shown from Microsoft when the page loads:

We're unable to complete your request
invalid_request: The provided value for the input parameter 'redirect_uri' is not valid. The expected value is a URI which matches a redirect URI registered for this client application.

Can the "http://localhost" redirect URL please be enabled for the library?

image

What is exactly the authorization code?

I dropped in my msaid, but this error popped out:

net.hycrafthd.minecraft_authenticator.microsoft.MicrosoftAuthenticationException: Cannot get oAuth token because: OAuthError [error=invalid_grant, errorDescription=The provided value for the 'code' parameter is not valid., correlationId=884cd3ea-a5cb-413f-b288-b9b241572197]
at net.hycrafthd.minecraft_authenticator.util.AuthenticationUtil.createMicrosoftAuthenticationFile(AuthenticationUtil.java:61)
at net.hycrafthd.minecraft_authenticator.login.Authenticator.lambda$ofMicrosoft$1(Authenticator.java:126)
at net.hycrafthd.minecraft_authenticator.login.Authenticator.(Authenticator.java:259)
at net.hycrafthd.minecraft_authenticator.login.Authenticator$Builder.run(Authenticator.java:242)
at net.minecraft.LoginForm$1.actionPerformed(LoginForm.java:76)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6539)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6304)
at java.awt.Container.processEvent(Container.java:2239)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.awt.Container.dispatchEventImpl(Container.java:2283)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Login failed: Invalid login session.

I've used this library to obtain the access code. However, when I try to join a server using the access code I get "Login failed: Invalid login session". I believe this means that the access code has expired. But when I try re-authenticating with a new authorization code it results in the same access code and thus the same error. Do you have any ideas on how to solve this?

Edit: This happened due to a bug unrelated to this library

Microsoft login with username and password

Would be great for automating things like bots that connect without any user interaction if I could provide a username + password instead of OAuth. (At least when 2FA is off)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.