Search in sources :

Example 1 with ExtendedUser

use of ch.cyberduck.core.storegate.io.swagger.client.model.ExtendedUser in project cyberduck by iterate-ch.

the class StoregateSession method login.

@Override
public void login(final Proxy proxy, final LoginCallback controller, final CancelCallback cancel) throws BackgroundException {
    authorizationService.setTokens(authorizationService.authorize(host, controller, cancel, OAuth2AuthorizationService.FlowType.AuthorizationCode));
    try {
        final HttpRequestBase request = new HttpPost(new HostUrlProvider().withUsername(false).withPath(true).get(host.getProtocol().getScheme(), host.getPort(), null, host.getHostname(), "/identity/core/connect/userinfo"));
        request.addHeader(HttpHeaders.CONTENT_TYPE, MEDIA_TYPE);
        final CloseableHttpResponse response = client.getClient().execute(request);
        try {
            switch(response.getStatusLine().getStatusCode()) {
                case HttpStatus.SC_OK:
                    try {
                        final JsonElement element = JsonParser.parseReader(new InputStreamReader(response.getEntity().getContent()));
                        if (element.isJsonObject()) {
                            final JsonObject json = element.getAsJsonObject();
                            final URI url = URI.create(json.getAsJsonPrimitive("web_url_api").getAsString());
                            if (log.isInfoEnabled()) {
                                log.info(String.format("Set base path to %s", url));
                            }
                            client.setBasePath(StringUtils.removeEnd(url.toString(), String.valueOf(Path.DELIMITER)));
                        }
                    } catch (JsonParseException | IllegalArgumentException e) {
                        log.warn(String.format("Ignore failure %s", e));
                    }
                    break;
                case HttpStatus.SC_FORBIDDEN:
                    // Insufficient scope
                    final BackgroundException failure = new StoregateExceptionMappingService(fileid).map(new ApiException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase(), Collections.emptyMap(), EntityUtils.toString(response.getEntity())));
                    throw new LoginFailureException(failure.getDetail(), failure);
                default:
                    throw new StoregateExceptionMappingService(fileid).map(new ApiException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase(), Collections.emptyMap(), EntityUtils.toString(response.getEntity())));
            }
        } finally {
            EntityUtils.consume(response.getEntity());
        }
        final Credentials credentials = host.getCredentials();
        // Get username
        final ExtendedUser me = new UsersApi(client).usersGetMe();
        if (log.isDebugEnabled()) {
            log.debug(String.format("Authenticated for user %s", me));
        }
        credentials.setUsername(me.getUsername());
        credentials.setSaved(true);
        // Get root folders
        roots = new SettingsApi(client).settingsGetRootfolders();
    } catch (ApiException e) {
        throw new StoregateExceptionMappingService(fileid).map(e);
    } catch (IOException e) {
        throw new DefaultIOExceptionMappingService().map(e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HostUrlProvider(ch.cyberduck.core.HostUrlProvider) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) ExtendedUser(ch.cyberduck.core.storegate.io.swagger.client.model.ExtendedUser) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) URI(java.net.URI) CYBERDUCK_REDIRECT_URI(ch.cyberduck.core.oauth.OAuth2AuthorizationService.CYBERDUCK_REDIRECT_URI) LoginFailureException(ch.cyberduck.core.exception.LoginFailureException) JsonElement(com.google.gson.JsonElement) UsersApi(ch.cyberduck.core.storegate.io.swagger.client.api.UsersApi) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) SettingsApi(ch.cyberduck.core.storegate.io.swagger.client.api.SettingsApi) DefaultIOExceptionMappingService(ch.cyberduck.core.DefaultIOExceptionMappingService) BackgroundException(ch.cyberduck.core.exception.BackgroundException) Credentials(ch.cyberduck.core.Credentials) ApiException(ch.cyberduck.core.storegate.io.swagger.client.ApiException)

Aggregations

Credentials (ch.cyberduck.core.Credentials)1 DefaultIOExceptionMappingService (ch.cyberduck.core.DefaultIOExceptionMappingService)1 HostUrlProvider (ch.cyberduck.core.HostUrlProvider)1 BackgroundException (ch.cyberduck.core.exception.BackgroundException)1 LoginFailureException (ch.cyberduck.core.exception.LoginFailureException)1 CYBERDUCK_REDIRECT_URI (ch.cyberduck.core.oauth.OAuth2AuthorizationService.CYBERDUCK_REDIRECT_URI)1 ApiException (ch.cyberduck.core.storegate.io.swagger.client.ApiException)1 SettingsApi (ch.cyberduck.core.storegate.io.swagger.client.api.SettingsApi)1 UsersApi (ch.cyberduck.core.storegate.io.swagger.client.api.UsersApi)1 ExtendedUser (ch.cyberduck.core.storegate.io.swagger.client.model.ExtendedUser)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 URI (java.net.URI)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)1