Search in sources :

Example 1 with Credentials

use of com.haleconnect.api.user.v1.model.Credentials in project hale by halestudio.

the class HaleConnectServiceImpl method login.

/**
 * @see eu.esdihumboldt.hale.io.haleconnect.HaleConnectService#login(java.lang.String,
 *      java.lang.String)
 */
@Override
public boolean login(String username, String password) throws HaleConnectException {
    LoginApi loginApi = UserServiceHelper.getLoginApi(this);
    Credentials credentials = UserServiceHelper.buildCredentials(username, password);
    try {
        Token token = loginApi.login(credentials);
        if (token != null) {
            UsersApi usersApi = UserServiceHelper.getUsersApi(this, token.getToken());
            // First get the current user's profile to obtain the user ID
            // required to fetch the extended profile (including the user's
            // roles/organisations) in the next step
            UserInfo shortProfile = usersApi.getProfileOfCurrentUser();
            session = new HaleConnectSessionImpl(username, token.getToken(), usersApi.getProfile(shortProfile.getId()));
            notifyLoginStateChanged();
        } else {
            clearSession();
        }
    } catch (ApiException e) {
        if (e.getCode() == 401) {
            clearSession();
        } else {
            throw new HaleConnectException(e.getMessage(), e);
        }
    }
    return isLoggedIn();
}
Also used : UsersApi(com.haleconnect.api.user.v1.api.UsersApi) Token(com.haleconnect.api.user.v1.model.Token) UserInfo(com.haleconnect.api.user.v1.model.UserInfo) HaleConnectUserInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectUserInfo) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) LoginApi(com.haleconnect.api.user.v1.api.LoginApi) Credentials(com.haleconnect.api.user.v1.model.Credentials) ApiException(com.haleconnect.api.user.v1.ApiException)

Example 2 with Credentials

use of com.haleconnect.api.user.v1.model.Credentials in project hale by halestudio.

the class UserServiceHelper method buildCredentials.

/**
 * Build a {@link Credentials} object. Any null values passed in will be
 * converted to an empty string.
 *
 * @param username the user name
 * @param password the password
 * @return a Credentials object with the given credentials
 */
public static Credentials buildCredentials(String username, String password) {
    Credentials credentials = new Credentials();
    credentials.setUsername(Optional.ofNullable(username).orElse(""));
    credentials.setPassword(Optional.ofNullable(password).orElse(""));
    return credentials;
}
Also used : Credentials(com.haleconnect.api.user.v1.model.Credentials)

Aggregations

Credentials (com.haleconnect.api.user.v1.model.Credentials)2 ApiException (com.haleconnect.api.user.v1.ApiException)1 LoginApi (com.haleconnect.api.user.v1.api.LoginApi)1 UsersApi (com.haleconnect.api.user.v1.api.UsersApi)1 Token (com.haleconnect.api.user.v1.model.Token)1 UserInfo (com.haleconnect.api.user.v1.model.UserInfo)1 HaleConnectException (eu.esdihumboldt.hale.io.haleconnect.HaleConnectException)1 HaleConnectUserInfo (eu.esdihumboldt.hale.io.haleconnect.HaleConnectUserInfo)1