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

io.github.eliux.mega.auth.MegaAuthSessionID 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 io.github.eliux.mega.auth;

import io.github.eliux.mega.Mega;
import io.github.eliux.mega.MegaSession;
import io.github.eliux.mega.error.MegaException;
import io.github.eliux.mega.error.MegaLoginException;

import java.util.Optional;

/**
 * Logs into MEGA using a session ID
 */
public class MegaAuthSessionID extends MegaAuth {

    private final String sessionID;

    public MegaAuthSessionID(String sessionID) {
        this.sessionID = sessionID;
    }

    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);
    }

    public String getSessionID() {
        return sessionID;
    }

    @Override
    public MegaSession login() throws MegaLoginException {
        try{
            return new MegaSession(this);
        }catch(Throwable err){
            throw new MegaLoginException("There is no started session", err);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy