Skip to content

Exercise: Implementing OAuth2 support in Visual Studio Client

Josh Gough edited this page Sep 17, 2013 · 5 revisions

This document outlines how to update the Visual Studio Client to support the new OAuth2 VersionOne API Connector into the Visual Studio Client. It will be updated as we actually perform the work and learn more.

  • Load the solution in Visual Studio 2012
  • Change the target framework from .NET 4 to 4.5 because the new SDK client targets 4.5.
  • Enable Nuget package restore if not already enabled
  • Run manage Nuget packages and upgrade the VersionOne dependencies. For today we may want to just point to https://www.myget.org/F/versionone/ in our Nuget sources in order to get v14.0.2.212 which is not public on NuGet.org yet

The IAPIConnector interface

This interface is defined in APIClient as:

namespace VersionOne.SDK.APIClient
{
	public interface IAPIConnector
	{
		IDictionary<string, string> CustomHttpHeaders { get; }

		Stream BeginRequest(string path);
		Stream EndRequest(string path, string contentType);
		Stream GetData();
		Stream GetData(string path);
		Stream SendData(string path, string data);
	}
}

Currently, Visual Studio Client uses an implementation of this interface to communicate with VersionOne instances via the REST API endpoint. Inside of Visual Studio Client, you'll see class VersionOneConnector inside the ApiDataLayer.cs file in its Connect method:

        public void Connect(VersionOneSettings settings) {
            var path = settings.Path;
            var username = settings.Username;
            var password = settings.Password;
            var integrated = settings.Integrated;
            var proxy = GetProxy(settings.ProxySettings);
            VersionOneSettings = settings;

            var metaConnector = new V1APIConnector(path + MetaUrlSuffix, username, password, integrated, proxy);
            MetaModel = new MetaModel(metaConnector);

            var localizerConnector = new V1APIConnector(path + LocalizerUrlSuffix, username, password, integrated, proxy);
            Localizer = new Localizer(localizerConnector);

            var dataConnector = new V1APIConnector(path + DataUrlSuffix, username, password, integrated, proxy);
            Services = new Services(MetaModel, dataConnector);

            V1Configuration = LoadV1Configuration();
        }

Internally, this class takes a dependency on VersionOne.SDK.APIClient.V1APIConnector, which is essentially the 'default' implementation of the IAPIConnector interface just described:

namespace VersionOne.SDK.APIClient
{
	public class V1APIConnector : IAPIConnector
	{
		public static AssemblyName MyAssemblyName;
		public static AssemblyName RunningAssemblyName;

		public V1APIConnector(string urlPrefix, string username = null, string password = null, bool? integratedAuth = null, ProxyProvider proxyProvider = null);

		public IDictionary<string, string> CustomHttpHeaders { get; }

		public Stream BeginRequest(string apipath);
		public Stream EndRequest(string apipath, string contentType);
		public Stream GetData();
		public Stream GetData(string apipath);
		public Stream SendData(string apipath, string data);
		public void SetCallerUserAgent(string userAgent);
	}
}

As of v14 of APIClient, there is a new implementation of IAPIConnector: V1OAuth2APIConnector that Joe built. It's public interface looks like this:

using OAuth2Client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace VersionOne.SDK.APIClient
{
	public class V1OAuth2APIConnector : IAPIConnector
	{
		public static AssemblyName MyAssemblyName;

		public V1OAuth2APIConnector(string urlPrefix, IStorage storage = null, ProxyProvider proxy = null);

		public IDictionary<string, string> CustomHttpHeaders { get; }

		public Stream BeginRequest(string apipath);
		public Stream EndRequest(string apipath, string contentType);
		public Stream GetData();
		public Stream GetData(string apipath);
		public Stream HttpGet(string apipath, bool refreshTokenIfNeeded = true, string contentType = "text/xml");
		public Stream HttpPost(string apipath, byte[] body, bool refreshTokenIfNeeded = true, string contentType = "text/xml");
		public Stream SendData(string apipath, string data);
		public void SetCallerUserAgent(string userAgent);
	}
}

The original V1APIConnector class, as you can see by its interface, deals with username and password credentials, or integrated authentication. The new one provides support for connecting via OAuth2. So, we will start the process of upgrading Visual Studio Client to utilize this new connector.

Clone this wiki locally