Skip to content

Commit

Permalink
Merge pull request #49 from Code-Sharp/wampv2-authentication-welcome-…
Browse files Browse the repository at this point in the history
…merge

WAMPv2 client authentication features
  • Loading branch information
darkl committed Dec 21, 2014
2 parents a8aadc6 + 8fd8e15 commit 15dcd92
Show file tree
Hide file tree
Showing 18 changed files with 655 additions and 182 deletions.
5 changes: 4 additions & 1 deletion src/mono/WampSharp/WampSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,9 @@
<Compile Include="..\..\net45\WampSharp\WAMP2\V2\Client\Rpc\WampRpcOperationCatalogProxy.cs">
<Link>WAMP2\V2\Client\Rpc\WampRpcOperationCatalogProxy.cs</Link>
</Compile>
<Compile Include="..\..\net45\WampSharp\WAMP2\V2\Client\Session\IWampClientAutenticator.cs">
<Link>WAMP2\V2\Client\Session\IWampClientAutenticator.cs</Link>
</Compile>
<Compile Include="..\..\net45\WampSharp\WAMP2\V2\Client\Session\IWampSessionClientExtended.cs">
<Link>WAMP2\V2\Client\Session\IWampSessionClientExtended.cs</Link>
</Compile>
Expand Down Expand Up @@ -1217,4 +1220,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
5 changes: 4 additions & 1 deletion src/net40/WampSharp/WampSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,9 @@
<Compile Include="..\..\net45\WampSharp\WAMP2\V2\Client\IWampPendingRequest.cs">
<Link>WAMP2\V2\Client\IWampPendingRequest.cs</Link>
</Compile>
<Compile Include="..\..\net45\WampSharp\WAMP2\V2\Client\Session\IWampClientAutenticator.cs">
<Link>WAMP2\V2\Client\Session\IWampClientAutenticator.cs</Link>
</Compile>
<Compile Include="..\..\net45\WampSharp\WAMP2\V2\Client\Session\IWampClientConnectionMonitor.cs">
<Link>WAMP2\V2\Client\Session\IWampClientConnectionMonitor.cs</Link>
</Compile>
Expand Down Expand Up @@ -1221,4 +1224,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
122 changes: 122 additions & 0 deletions src/net45/Samples/WAMP2/WampSharp.Samples.Authentication/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WampSharp.Binding;
using WampSharp.V2;
using WampSharp.V2.Client;
using WampSharp.V2.Rpc;

namespace WampSharp.Samples.Authentication
{
public interface IArgumentsService
{
[WampProcedure("com.timeservice.now")]
string timeservice();
}

public class CustomAuthenticator : IWampClientAutenticator
{
private string[] authenticationMethods;
private IDictionary<string, string> tickets = new Dictionary<string, string>()
{
{ "peter", "md5f39d45e1da71cf755a7ee5d5840c7b0d" },
{ "joe", "magic_secret_2" }
};
private string user = "peter";

public CustomAuthenticator()
{
this.authenticationMethods = new string[] { "ticket" };
}

public ChallengeResult Authenticate(string challenge, ChallengeDetails extra)
{
var challengeExtra = extra.OriginalValue.Deserialize<IDictionary<string, object>>();

var method = (string)challengeExtra["authmethod"];
Console.WriteLine(method);
foreach (var ce in challengeExtra)
{
Console.WriteLine(ce);
}

if (method == "ticket")
{
Console.WriteLine("authenticating via '" + method + "'");
var result = new ChallengeResult();
result.Signature = tickets[user];
result.Extra = new Dictionary<string, object>() { };
return result;
}
else
{
throw new WampAuthenticationException("don't know how to authenticate using '" + method + "'");
}
}

public string[] AuthenticationMethods
{
get { return authenticationMethods; }
}

public string AuthenticationId
{
get { return user; }
}
}

class Program
{
private static IWampRealmProxy proxy;

private static void Test(IWampRealmServiceProvider serviceProvider)
{
IArgumentsService proxy = serviceProvider.GetCalleeProxy<IArgumentsService>();
string now;
try
{
now = proxy.timeservice();
Console.WriteLine("call result {0}", now);
}
catch (Exception e)
{
Console.WriteLine("call error {0}", e);
}
}

static void Main(string[] args)
{
string url = "ws://127.0.0.1:8080/";
string realm = "integra-s";

DefaultWampChannelFactory channelFactory = new DefaultWampChannelFactory();

IWampClientAutenticator authenticator = new CustomAuthenticator();
IWampChannel channel = channelFactory.CreateJsonChannel(url, realm, authenticator);
channel.RealmProxy.Monitor.ConnectionEstablished += Monitor_ConnectionEstablished;
channel.RealmProxy.Monitor.ConnectionBroken += Monitor_ConnectionBroken;
Program.proxy = channel.RealmProxy;
channel.Open().Wait();
Test(channel.RealmProxy.Services);
Console.ReadLine();
}

static void Monitor_ConnectionEstablished(object sender, V2.Realm.WampSessionEventArgs e)
{
var details = e.Details.Deserialize<IDictionary<string, object>>();

Console.WriteLine("connected session with ID " + e.SessionId);
Console.WriteLine("authenticated using method '" + details["authmethod"] + "' and provider '" + details["authprovider"] + "'");
Console.WriteLine("authenticated with authid '" + details["authid"] + "' and authrole '" + details["authrole"] + "'");

//Test(Program.proxy.Services);
}

static void Monitor_ConnectionBroken(object sender, V2.Realm.WampSessionCloseEventArgs e)
{
Console.WriteLine("disconnected {0}", e.Reason);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("WampSharp.Samples.Authentication")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("WampSharp.Samples.Authentication")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("3e3df86c-9d5d-45f5-858d-5e653c196d58")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{305C0F3D-1A51-4D8C-8729-481F73A7C7D6}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>WampSharp.Samples.Authentication</RootNamespace>
<AssemblyName>WampSharp.Samples.Authentication</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\WampSharp.Default\WampSharp.Default.csproj">
<Project>{6ab75291-d296-457e-88a3-b41b16a1a247}</Project>
<Name>WampSharp.Default</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\WampSharp\WampSharp.csproj">
<Project>{653a76dc-00d7-4eff-a25e-2fa10c5c927d}</Project>
<Name>WampSharp</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
80 changes: 75 additions & 5 deletions src/net45/WampSharp.Default/WAMP2/V2/DefaultWampChannelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace WampSharp.V2
public class DefaultWampChannelFactory : WampChannelFactory
{
private readonly JTokenMsgpackBinding mMsgpackBinding = new JTokenMsgpackBinding();
private readonly JTokenJsonBinding mJsonBinding = new JTokenJsonBinding();
private readonly JTokenJsonBinding mJsonBinding = new JTokenJsonBinding();

/// <summary>
/// Creates a <see cref="IWampChannel"/> that connects to a given realm,
Expand All @@ -26,9 +26,29 @@ public IWampChannel CreateChannel<TMessage>(string address,
IWampTextBinding<TMessage> binding)
{
var connection =
new WebSocket4NetTextConnection<TMessage>(address, binding);
new WebSocket4NetTextConnection<TMessage>(address, binding);

return this.CreateChannel(realm, connection, binding);
}

/// <summary>
/// Creates a <see cref="IWampChannel"/> that connects to a given realm,
/// using the given address and the given text binding
/// </summary>
/// <param name="address">The given address.</param>
/// <param name="realm">The given realm to connect to.</param>
/// <param name="binding">The given text binding.</param>
/// <param name="autenticator">The authenticator object to handle CHALLENGE request.</param>
/// <returns></returns>
public IWampChannel CreateChannel<TMessage>(string address,
string realm,
IWampTextBinding<TMessage> binding,
IWampClientAutenticator autenticator)
{
var connection =
new WebSocket4NetTextConnection<TMessage>(address, binding);

return this.CreateChannel(realm, connection, binding, autenticator);
}

/// <summary>
Expand All @@ -44,11 +64,31 @@ public IWampChannel CreateChannel<TMessage>(string address,
IWampBinaryBinding<TMessage> binding)
{
var connection =
new WebSocket4NetBinaryConnection<TMessage>(address, binding);

new WebSocket4NetBinaryConnection<TMessage>(address, binding);

return this.CreateChannel(realm, connection, binding);
}

/// <summary>
/// Creates a <see cref="IWampChannel"/> that connects to a given realm,
/// using the given address and the given binary binding
/// </summary>
/// <param name="address">The given address.</param>
/// <param name="realm">The given realm to connect to.</param>
/// <param name="binding">The given binary binding.</param>
/// <param name="autenticator">The authenticator object to handle CHALLENGE request.</param>
/// <returns></returns>
public IWampChannel CreateChannel<TMessage>(string address,
string realm,
IWampBinaryBinding<TMessage> binding,
IWampClientAutenticator autenticator)
{
var connection =
new WebSocket4NetBinaryConnection<TMessage>(address, binding);

return this.CreateChannel(realm, connection, binding, autenticator);
}

/// <summary>
/// Creates a <see cref="IWampChannel"/> that connects to a given realm,
/// using the given address and json binding
Expand All @@ -62,6 +102,21 @@ public IWampChannel CreateJsonChannel(string address,
return this.CreateChannel(address, realm, mJsonBinding);
}

/// <summary>
/// Creates a <see cref="IWampChannel"/> that connects to a given realm,
/// using the given address and json binding
/// </summary>
/// <param name="address">The given address.</param>
/// <param name="realm">The given realm to connect to.</param>
/// <param name="autenticator">The authenticator object to handle CHALLENGE request.</param>
/// <returns></returns>
public IWampChannel CreateJsonChannel(string address,
string realm,
IWampClientAutenticator autenticator)
{
return this.CreateChannel(address, realm, mJsonBinding, autenticator);
}

/// <summary>
/// Creates a <see cref="IWampChannel"/> that connects to a given realm,
/// using the given address and msgpack binding
Expand All @@ -73,6 +128,21 @@ public IWampChannel CreateMsgpackChannel(string address,
string realm)
{
return this.CreateChannel(address, realm, mMsgpackBinding);
}

/// <summary>
/// Creates a <see cref="IWampChannel"/> that connects to a given realm,
/// using the given address and msgpack binding
/// </summary>
/// <param name="address">The given address.</param>
/// <param name="realm">The given realm to connect to.</param>
/// <param name="autenticator">The authenticator object to handle CHALLENGE request.</param>
/// <returns></returns>
public IWampChannel CreateMsgpackChannel(string address,
string realm,
IWampClientAutenticator autenticator)
{
return this.CreateChannel(address, realm, mMsgpackBinding, autenticator);
}
}
}
Loading

0 comments on commit 15dcd92

Please sign in to comment.