use of org.eclipse.che.api.core.UnauthorizedException in project che by eclipse.
the class RemoteOAuthTokenProvider method getToken.
/** {@inheritDoc} */
@Override
public OAuthToken getToken(String oauthProviderName, String userId) throws IOException {
if (userId.isEmpty()) {
return null;
}
try {
UriBuilder ub = UriBuilder.fromUri(apiEndpoint).path(OAuthAuthenticationService.class).path(OAuthAuthenticationService.class, "token").queryParam("oauth_provider", oauthProviderName);
Link getTokenLink = DtoFactory.newDto(Link.class).withHref(ub.build().toString()).withMethod("GET");
return httpJsonRequestFactory.fromLink(getTokenLink).request().asDto(OAuthToken.class);
} catch (NotFoundException ne) {
LOG.warn("Token not found for user {}", userId);
return null;
} catch (ServerException | UnauthorizedException | ForbiddenException | ConflictException | BadRequestException e) {
LOG.warn("Exception on token retrieval, message : {}", e.getLocalizedMessage());
return null;
}
}
use of org.eclipse.che.api.core.UnauthorizedException in project che by eclipse.
the class ProjectManager method doImportProject.
/** Note: Use {@link FileWatcherManager#suspend()} and {@link FileWatcherManager#resume()} while importing source code */
private RegisteredProject doImportProject(String path, SourceStorage sourceStorage, boolean rewrite, LineConsumerFactory lineConsumerFactory) throws ServerException, IOException, ForbiddenException, UnauthorizedException, ConflictException, NotFoundException {
final ProjectImporter importer = importers.getImporter(sourceStorage.getType());
if (importer == null) {
throw new NotFoundException(format("Unable import sources project from '%s'. Sources type '%s' is not supported.", sourceStorage.getLocation(), sourceStorage.getType()));
}
String normalizePath = (path.startsWith("/")) ? path : "/".concat(path);
FolderEntry folder = asFolder(normalizePath);
if (folder != null && !rewrite) {
throw new ConflictException(format("Project %s already exists ", path));
}
if (folder == null) {
folder = getProjectsRoot().createFolder(normalizePath);
}
try {
importer.importSources(folder, sourceStorage, lineConsumerFactory);
} catch (final Exception e) {
folder.remove();
throw e;
}
final String name = folder.getPath().getName();
for (ProjectConfig project : workspaceProjectsHolder.getProjects()) {
if (normalizePath.equals(project.getPath())) {
// TODO Needed for factory project importing with keepDir. It needs to find more appropriate solution
List<String> innerProjects = projectRegistry.getProjects(normalizePath);
for (String innerProject : innerProjects) {
RegisteredProject registeredProject = projectRegistry.getProject(innerProject);
projectRegistry.putProject(registeredProject, asFolder(registeredProject.getPath()), true, false);
}
RegisteredProject rp = projectRegistry.putProject(project, folder, true, false);
workspaceProjectsHolder.sync(projectRegistry);
return rp;
}
}
RegisteredProject rp = projectRegistry.putProject(new NewProjectConfigImpl(normalizePath, name, BaseProjectType.ID, sourceStorage), folder, true, false);
workspaceProjectsHolder.sync(projectRegistry);
return rp;
}
use of org.eclipse.che.api.core.UnauthorizedException in project che by eclipse.
the class SubversionApi method runCommand.
private CommandLineResult runCommand(@Nullable Map<String, String> env, List<String> args, File projectPath, List<String> paths, @Nullable String username, @Nullable String password, String repoUrl) throws SubversionException, UnauthorizedException {
final List<String> lines = new ArrayList<>();
final CommandLineResult result;
final StringBuffer buffer;
boolean isWarning = false;
// Add paths to the end of the list of arguments
for (final String path : paths) {
args.add(path);
}
String[] credentialsArgs;
if (!isNullOrEmpty(username) && !isNullOrEmpty(password)) {
credentialsArgs = new String[] { "--username", username, "--password", password };
} else {
credentialsArgs = null;
}
SshEnvironment sshEnvironment = null;
if (SshEnvironment.isSSH(repoUrl)) {
sshEnvironment = new SshEnvironment(sshScriptProvider, repoUrl);
if (env == null) {
env = new HashMap<>();
}
env.putAll(sshEnvironment.get());
}
try {
result = UpstreamUtils.executeCommandLine(env, "svn", args.toArray(new String[args.size()]), credentialsArgs, -1, projectPath, svnOutputPublisherFactory);
} catch (IOException e) {
throw new SubversionException(e);
} finally {
if (sshEnvironment != null) {
sshEnvironment.cleanUp();
}
}
if (result.getExitCode() != 0) {
buffer = new StringBuffer();
lines.addAll(result.getStdout());
lines.addAll(result.getStderr());
for (final String line : lines) {
// Subversion returns an error code of 1 even when the "error" is just a warning
if (line.startsWith("svn: warning: ")) {
isWarning = true;
}
buffer.append(line);
buffer.append("\n");
}
if (!isWarning) {
String errorMessage = buffer.toString();
if (errorMessage.endsWith("Authentication failed\n")) {
throw new UnauthorizedException("Authentication failed", ErrorCodes.UNAUTHORIZED_SVN_OPERATION);
} else {
throw new SubversionException(errorMessage);
}
}
}
return result;
}
use of org.eclipse.che.api.core.UnauthorizedException in project che by eclipse.
the class SshKeyProviderImpl method getPrivateKey.
/**
* Get private ssh key and upload public ssh key to repository hosting service.
*
* @param url
* url to the repository
* @return private ssh key
* @throws ServerException
* if an error occurs while generating or uploading keys
*/
@Override
public byte[] getPrivateKey(String url) throws ServerException {
String host = UrlUtils.getHost(url);
SshPair pair;
try {
pair = sshService.getPair("vcs", host);
} catch (ServerException | NotFoundException e) {
throw new ServerException(DtoFactory.newDto(ExtendedError.class).withMessage("Unable get private ssh key").withErrorCode(ErrorCodes.UNABLE_GET_PRIVATE_SSH_KEY));
}
// check keys existence
String privateKey = pair.getPrivateKey();
if (privateKey == null) {
throw new ServerException(DtoFactory.newDto(ExtendedError.class).withMessage("Unable get private ssh key").withErrorCode(ErrorCodes.UNABLE_GET_PRIVATE_SSH_KEY));
}
final String publicKey = pair.getPublicKey();
if (publicKey != null) {
final Optional<SshKeyUploader> optionalKeyUploader = sshKeyUploaders.stream().filter(keyUploader -> keyUploader.match(url)).findFirst();
if (optionalKeyUploader.isPresent()) {
final SshKeyUploader uploader = optionalKeyUploader.get();
try {
uploader.uploadKey(publicKey);
} catch (IOException e) {
throw new ServerException(e.getMessage(), e);
} catch (UnauthorizedException e) {
// action might fail without uploaded public SSH key.
LOG.warn(String.format("Unable upload public SSH key with %s", uploader.getClass().getSimpleName()), e);
}
} else {
// action might fail without uploaded public SSH key.
LOG.warn(String.format("Not found ssh key uploader for %s", host));
}
}
return privateKey.getBytes();
}
use of org.eclipse.che.api.core.UnauthorizedException in project che by eclipse.
the class GitHubFactory method getToken.
private String getToken() throws ServerException, UnauthorizedException {
OAuthToken token;
try {
token = oauthTokenProvider.getToken("github", EnvironmentContext.getCurrent().getSubject().getUserId());
} catch (IOException e) {
throw new ServerException(e.getMessage());
}
String oauthToken = token != null ? token.getToken() : null;
if (oauthToken == null || oauthToken.isEmpty()) {
throw new UnauthorizedException("User doesn't have access token to github");
}
return oauthToken;
}
Aggregations