use of org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException in project che-server by eclipse-che.
the class HttpBitbucketServerApiClient method getUser.
@Override
public BitbucketUser getUser(Subject cheUser) throws ScmUnauthorizedException, ScmCommunicationException, ScmItemNotFoundException {
try {
// Since Bitbucket server API doesn't provide a way to get an account profile currently
// authenticated user we will try to find it and by iterating over the list available to the
// current user Bitbucket users and attempting to get their personal access tokens. To speed
// up this process first of all we will search among users that contain(somewhere in Bitbucket
// user
// entity) Che's user username. At the second step, we will search against all visible(to the
// current Che's user) bitbucket users that are not included in the first list.
Set<String> usersByName = getUsers(cheUser.getUserName()).stream().map(BitbucketUser::getSlug).collect(Collectors.toSet());
Optional<BitbucketUser> currentUser = findCurrentUser(usersByName);
if (currentUser.isPresent()) {
return currentUser.get();
}
Set<String> usersAllExceptByName = getUsers().stream().map(BitbucketUser::getSlug).filter(s -> !usersByName.contains(s)).collect(Collectors.toSet());
currentUser = findCurrentUser(usersAllExceptByName);
if (currentUser.isPresent()) {
return currentUser.get();
}
} catch (ScmBadRequestException | ScmItemNotFoundException scmException) {
throw new ScmCommunicationException(scmException.getMessage(), scmException);
}
throw new ScmItemNotFoundException("Current user not found. That is possible only if user are not authorized against " + serverUri);
}
use of org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException in project devspaces-images by redhat-developer.
the class AuthorizingFileContentProvider method fetchContent.
@Override
public String fetchContent(String fileURL) throws IOException, DevfileException {
final String requestURL = formatUrl(fileURL);
try {
Optional<PersonalAccessToken> token = personalAccessTokenManager.get(EnvironmentContext.getCurrent().getSubject(), remoteFactoryUrl.getHostName());
if (token.isPresent()) {
PersonalAccessToken personalAccessToken = token.get();
String content = urlFetcher.fetch(requestURL, formatAuthorization(personalAccessToken.getToken()));
gitCredentialManager.createOrReplace(personalAccessToken);
return content;
} else {
try {
return urlFetcher.fetch(requestURL);
} catch (IOException exception) {
if (exception instanceof SSLException) {
ScmCommunicationException cause = new ScmCommunicationException(String.format("Failed to fetch a content from URL %s due to TLS key misconfiguration. Please refer to the docs about how to correctly import it. ", requestURL));
throw new DevfileException(exception.getMessage(), cause);
} else if (exception instanceof FileNotFoundException) {
if (isPublicRepository(remoteFactoryUrl)) {
// for public repo-s return 404 as-is
throw exception;
}
}
// unable to determine exact cause, so let's just try to authorize...
try {
PersonalAccessToken personalAccessToken = personalAccessTokenManager.fetchAndSave(EnvironmentContext.getCurrent().getSubject(), remoteFactoryUrl.getHostName());
String content = urlFetcher.fetch(requestURL, formatAuthorization(personalAccessToken.getToken()));
gitCredentialManager.createOrReplace(personalAccessToken);
return content;
} catch (ScmUnauthorizedException | UnknownScmProviderException e) {
throw new DevfileException(e.getMessage(), e);
} catch (ScmCommunicationException e) {
throw new IOException(String.format("Failed to fetch a content from URL %s. Make sure the URL" + " is correct. For private repository, make sure authentication is configured." + " Additionally, if you're using " + " relative form, make sure the referenced file are actually stored" + " relative to the devfile on the same host," + " or try to specify URL in absolute form. The current attempt to authenticate" + " request, failed with the following error message: %s", fileURL, e.getMessage()), e);
}
}
}
} catch (ScmConfigurationPersistenceException | UnsatisfiedScmPreconditionException | ScmUnauthorizedException | ScmCommunicationException e) {
throw new DevfileException(e.getMessage(), e);
}
}
Aggregations