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