All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.eliux.mega.auth.MegaAuthCredentials Maven / Gradle / Ivy

Go to download

Java client library that works on top of MEGAcmd to provide access to the services of Mega.nz

There is a newer version: 1.6.2
Show newest version
package com.github.eliux.mega.auth;

import com.github.eliux.mega.Mega;
import com.github.eliux.mega.MegaSession;
import com.github.eliux.mega.cmd.MegaCmdLogin;
import com.github.eliux.mega.error.MegaException;

import java.util.Optional;

public class MegaAuthCredentials extends MegaAuth {

    private final String username;

    private final String password;

    public MegaAuthCredentials(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @Override
    public MegaSession login() {
        final MegaCmdLogin megaCmdLogin = new MegaCmdLogin(username, password);
        megaCmdLogin.run();

        return new MegaSession(this);
    }

    public static final MegaAuthCredentials createFromEnvVariables() {
        String username = Optional.ofNullable(System.getenv(Mega.USERNAME_ENV_VAR))
                .orElseThrow(() -> MegaException.nonExistingEnvVariable(
                        Mega.USERNAME_ENV_VAR
                ));

        String password = Optional.ofNullable(System.getenv(Mega.PASSWORD_ENV_VAR))
                .orElseThrow(() -> MegaException.nonExistingEnvVariable(
                        Mega.PASSWORD_ENV_VAR
                ));

        return new MegaAuthCredentials(username, password);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy