Search in sources :

Example 1 with AuthRequest

use of com.github.games647.changeskin.core.model.auth.AuthRequest in project ChangeSkin by games647.

the class MojangAuthApi method authenticate.

public Optional<Account> authenticate(String email, String password) {
    try {
        HttpURLConnection httpConnection = CommonUtil.getConnection(AUTH_URL);
        httpConnection.setRequestMethod("POST");
        httpConnection.setDoOutput(true);
        try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(httpConnection.getOutputStream(), StandardCharsets.UTF_8))) {
            writer.append(gson.toJson(new AuthRequest(email, password)));
        }
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(), StandardCharsets.UTF_8))) {
            AuthResponse authResponse = gson.fromJson(reader, AuthResponse.class);
            return Optional.of(new Account(authResponse.getSelectedProfile(), authResponse.getAccessToken()));
        }
    } catch (IOException ex) {
        logger.error("Failed to authenticate user: {}", email, ex);
    }
    return Optional.empty();
}
Also used : AuthRequest(com.github.games647.changeskin.core.model.auth.AuthRequest) Account(com.github.games647.changeskin.core.model.auth.Account) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) AuthResponse(com.github.games647.changeskin.core.model.auth.AuthResponse)

Aggregations

Account (com.github.games647.changeskin.core.model.auth.Account)1 AuthRequest (com.github.games647.changeskin.core.model.auth.AuthRequest)1 AuthResponse (com.github.games647.changeskin.core.model.auth.AuthResponse)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 HttpURLConnection (java.net.HttpURLConnection)1