private ServerContext createServerContext( String gitRemoteUrl, AuthenticationInfo authenticationInfo) { ServerContext.Type type = UrlHelper.isVSO(UrlHelper.getBaseUri(gitRemoteUrl)) ? ServerContext.Type.VSO_DEPLOYMENT : ServerContext.Type.TFS; final Client client = ServerContext.getClient(type, authenticationInfo); final Validator validator = new Validator(client); final UrlHelper.ParseResult uriParseResult = UrlHelper.tryParse(gitRemoteUrl, validator); if (uriParseResult.isSuccess()) { final ServerContextBuilder builder = new ServerContextBuilder() .type(type) .uri(gitRemoteUrl) .authentication(authenticationInfo) .teamProject(validator.getRepository().getProjectReference()) .repository(validator.getRepository()) .collection(validator.getCollection()); // Set the uri of the context to the server uri (TODO change context so that it can be any URI // in the hierarchy) final URI serverUri = URI.create(uriParseResult.getServerUrl()); builder.uri(serverUri); return builder.buildWithClient(client); } return null; }
/** * Use this method to get the appropriate AuthenticationProvider based on an url. * * @param url * @return */ public AuthenticationProvider getAuthenticationProvider(final String url) { if (UrlHelper.isVSO(UrlHelper.getBaseUri(url))) { return VsoAuthenticationProvider.getInstance(); } return new TfsAuthenticationProvider(); }
@Override public void loadRepositories() { clearErrors(); // Make sure we have a server url final String serverName = getServerName(); if (StringUtils.isEmpty(serverName)) { addError( ModelValidationInfo.createWithResource( PROP_SERVER_NAME, TfPluginBundle.KEY_LOGIN_FORM_TFS_ERRORS_NO_SERVER_NAME)); setConnectionStatus(false); return; } // verify server url is a valid url if (!UrlHelper.isValidServerUrl(serverName)) { addError( ModelValidationInfo.createWithResource( PROP_SERVER_NAME, TfPluginBundle.KEY_LOGIN_FORM_TFS_ERRORS_INVALID_SERVER_URL, serverName)); setConnectionStatus(false); return; } if (authenticationProvider.isAuthenticated()) { loadRepositoriesInternal(); } else { authenticationProvider.authenticateAsync( getServerName(), new AuthenticationListener() { @Override public void authenticating() { // We are starting to authenticate, so set the boolean setAuthenticating(true); } @Override public void authenticated( final AuthenticationInfo authenticationInfo, final Throwable throwable) { // Push this event back onto the UI thread IdeaHelper.runOnUIThread( new Runnable() { @Override public void run() { // Authentication is over, so set the boolean setAuthenticating(false); // Log exception if it failed if (throwable != null) { logger.warn("Connecting to TFS server failed", throwable); } // try to load the repos loadRepositoriesInternal(); } }); } }); } }
private void loadRepositoriesInternal() { if (!authenticationProvider.isAuthenticated()) { addError( ModelValidationInfo.createWithResource( PROP_SERVER_NAME, TfPluginBundle.KEY_LOGIN_PAGE_ERRORS_TFS_CONNECT_FAILED, getServerName())); signOut(); return; } setConnectionStatus(true); setLoading(true); setUserName(authenticationProvider.getAuthenticationInfo().getUserNameForDisplay()); clearContexts(); final URI serverUrl = UrlHelper.getBaseUri(getServerName()); final ServerContext context = new ServerContextBuilder() .type(ServerContext.Type.TFS) .uri(serverUrl) .authentication(authenticationProvider.getAuthenticationInfo()) .build(); // successfully logged in and loading repositories, save this context if there is no active // context if (ServerContextManager.getInstance().getActiveContext() == ServerContext.NO_CONTEXT) { ServerContextManager.getInstance().setActiveContext(context); } getRepositoryProvider() .loadContexts( Collections.singletonList(context), ServerContextLookupOperation.ContextScope.REPOSITORY); }
// The url string obtained from the REST SDK is not encoded. // Replace space which is the only known valid character in team project and repository name that // is not a valid character in URI public String getUsableGitUrl() { GitRepository repo = getGitRepository(); if (repo != null && repo.getRemoteUrl() != null) { return UrlHelper.getCmdLineFriendlyGitRemoteUrl(this.getGitRepository().getRemoteUrl()); } return null; }
/** * This method takes a VSO_DEPLOYMENT context and returns the VSO context that has the correct PAT * * @param originalContext the VSO_DEPLOYMENT context to use to generate the PAT * @param authenticationProvider the provider used to create the original context * @param tokenDescription the description to use for the generated PAT * @return a new VSO context object */ public ServerContext createVsoContext( ServerContext originalContext, VsoAuthenticationProvider authenticationProvider, String tokenDescription) { // If the context is a VSO_DEPLOYMENT, then we can generate the PAT and create a new context // Otherwise, throw an exception if (originalContext == null || originalContext.getType() != ServerContext.Type.VSO_DEPLOYMENT) { throw new IllegalArgumentException("originalContext must be a VSO_DEPLOYMENT context"); } if (authenticationProvider == null) { throw new IllegalArgumentException("authenticationProvider must be set"); } // generate PAT final AuthenticationResult result = authenticationProvider.getAuthenticationResult(); final PersonalAccessTokenFactory patFactory = new PersonalAccessTokenFactoryImpl(result); final String accountName = UrlHelper.getVSOAccountName(originalContext.getUri()); final Account account = AccountLookupOperation.getAccount(result, accountName); if (account != null) { // TODO: handle case where session token cannot be created SessionToken sessionToken = patFactory.createSessionToken( tokenDescription, Arrays.asList(TokenScope.CODE_READ, TokenScope.CODE_WRITE, TokenScope.CODE_MANAGE), account.getAccountId()); // create a VSO context with session token (remove the original client and allow that to be // recreated) final AuthenticationInfo finalAuthenticationInfo = AuthHelper.createAuthenticationInfo( originalContext.getUri().toString(), result, sessionToken); final ServerContext newContext = new ServerContextBuilder(originalContext) .type(ServerContext.Type.VSO) .authentication(finalAuthenticationInfo) .buildWithClient(null); return newContext; } logger.debug("Account not found: " + accountName); return null; }
public synchronized GitHttpClient getGitHttpClient() { if (teamProjectCollectionReference == null) { // We don't have enough context to create a GitHttpClient return null; } // Get the collection name. Find it in the uri. Use that as the collection URI. final String collectionName = teamProjectCollectionReference.getName().toLowerCase(); final String uri = UrlHelper.asString(getUri()).toLowerCase(); final int index = uri.indexOf(UrlHelper.URL_SEPARATOR + collectionName); if (index >= 0) { // Get the index of the next char final int endIndex = index + 1 + collectionName.length(); // Make sure the collection name is terminated by the end of the uri or a uri separator if (endIndex == uri.length() || uri.charAt(endIndex) == UrlHelper.URL_SEPARATOR.charAt(0)) { final String collectionUri = uri.substring(0, endIndex); final GitHttpClient gitClient = new GitHttpClient(getClient(), URI.create(collectionUri)); return gitClient; } } return null; }