diff --git a/src/main/java/fr/litarvan/openauth/microsoft/MicrosoftAuthenticator.java b/src/main/java/fr/litarvan/openauth/microsoft/MicrosoftAuthenticator.java index f611075..6616c7c 100644 --- a/src/main/java/fr/litarvan/openauth/microsoft/MicrosoftAuthenticator.java +++ b/src/main/java/fr/litarvan/openauth/microsoft/MicrosoftAuthenticator.java @@ -124,6 +124,48 @@ public MicrosoftAuthResult loginWithCredentials(String email, String password) t } } + /** + * Logs in a player using its Microsoft account credentials and retrieves its access and xboxUserHash + * + * @param email Player Microsoft account e-mail + * @param password Player Microsoft account password + * @param retrieveProfile if you want to retrieve the minecraft profile + * @return The player's xbox profile with minecraft access token + * @throws MicrosoftAuthenticationException Thrown if one of the several HTTP requests failed at some point + */ + public MicrosoftAuthResult loginWithCredentials(String email, String password, boolean retrieveProfile) throws MicrosoftAuthenticationException { + CookieHandler currentHandler = CookieHandler.getDefault(); + CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); + + Map params = new HashMap<>(); + params.put("login", email); + params.put("loginfmt", email); + params.put("passwd", password); + + HttpURLConnection result; + + try { + PreAuthData authData = preAuthRequest(); + params.put("PPFT", authData.getPPFT()); + + result = http.followRedirects(http.postForm(authData.getUrlPost(), params)); + } finally { + CookieHandler.setDefault(currentHandler); + } + + try { + return loginWithTokens(extractTokens(result.getURL().toString()),retrieveProfile); + } catch (MicrosoftAuthenticationException e) { + if (match("(identity/confirm)", http.readResponse(result)) != null) { + throw new MicrosoftAuthenticationException( + "User has enabled double-authentication or must allow sign-in on https://account.live.com/activity" + ); + } + + throw e; + } + } + /** * Logs in a player using a webview to display Microsoft login page. * This function blocks the current thread until the process is finished; this can cause your application to @@ -184,6 +226,27 @@ public MicrosoftAuthResult loginWithRefreshToken(String refreshToken) throws Mic return loginWithTokens(new AuthTokens(response.getAccessToken(), response.getRefreshToken()),true); } + /** + * Logs in a player using a Microsoft account refresh token retrieved earlier. + * + * @param refreshToken Player Microsoft account refresh token + * @param retrieveProfile if you want to retrieve the minecraft profile + * @return The player's xbox profile with minecraft access token + * @throws MicrosoftAuthenticationException Thrown if one of the several HTTP requests failed at some point + */ + public MicrosoftAuthResult loginWithRefreshToken(String refreshToken, boolean retrieveProfile) throws MicrosoftAuthenticationException { + Map params = getLoginParams(); + params.put("refresh_token", refreshToken); + params.put("grant_type", "refresh_token"); + + MicrosoftRefreshResponse response = http.postFormGetJson( + MICROSOFT_TOKEN_ENDPOINT, + params, MicrosoftRefreshResponse.class + ); + + return loginWithTokens(new AuthTokens(response.getAccessToken(), response.getRefreshToken()),retrieveProfile); + } + /** * Logs in a player using a Microsoft account tokens retrieved earlier. * If the token was retrieved using Azure AAD/MSAL, it should be prefixed with d=