Search in sources :

Example 1 with AuthRequest

use of com.github.games647.craftapi.model.auth.AuthRequest in project CraftAPI by games647.

the class MojangResolver method authenticate.

@Override
public Account authenticate(String email, String password) throws IOException, InvalidCredentialsException {
    HttpURLConnection conn = getConnection(AUTH_URL);
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    try (OutputStream out = conn.getOutputStream();
        OutputStreamWriter outWriter = new OutputStreamWriter(out, StandardCharsets.UTF_8);
        BufferedWriter writer = new BufferedWriter(outWriter)) {
        writer.append(gson.toJson(new AuthRequest(email, password)));
    }
    AuthResponse authResponse = parseRequest(conn, in -> readJson(in, AuthResponse.class));
    return new Account(authResponse.getSelectedProfile(), authResponse.getAccessToken());
}
Also used : AuthRequest(com.github.games647.craftapi.model.auth.AuthRequest) Account(com.github.games647.craftapi.model.auth.Account) HttpURLConnection(java.net.HttpURLConnection) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) AuthResponse(com.github.games647.craftapi.model.auth.AuthResponse)

Example 2 with AuthRequest

use of com.github.games647.craftapi.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

BufferedWriter (java.io.BufferedWriter)2 OutputStreamWriter (java.io.OutputStreamWriter)2 HttpURLConnection (java.net.HttpURLConnection)2 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 Account (com.github.games647.craftapi.model.auth.Account)1 AuthRequest (com.github.games647.craftapi.model.auth.AuthRequest)1 AuthResponse (com.github.games647.craftapi.model.auth.AuthResponse)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1