Skip to content

Commit 15dcd92

Browse files
committed
Merge pull request #49 from Code-Sharp/wampv2-authentication-welcome-merge
WAMPv2 client authentication features
2 parents a8aadc6 + 8fd8e15 commit 15dcd92

File tree

18 files changed

+655
-182
lines changed

18 files changed

+655
-182
lines changed

src/mono/WampSharp/WampSharp.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,9 @@
891891
<Compile Include="..\..\net45\WampSharp\WAMP2\V2\Client\Rpc\WampRpcOperationCatalogProxy.cs">
892892
<Link>WAMP2\V2\Client\Rpc\WampRpcOperationCatalogProxy.cs</Link>
893893
</Compile>
894+
<Compile Include="..\..\net45\WampSharp\WAMP2\V2\Client\Session\IWampClientAutenticator.cs">
895+
<Link>WAMP2\V2\Client\Session\IWampClientAutenticator.cs</Link>
896+
</Compile>
894897
<Compile Include="..\..\net45\WampSharp\WAMP2\V2\Client\Session\IWampSessionClientExtended.cs">
895898
<Link>WAMP2\V2\Client\Session\IWampSessionClientExtended.cs</Link>
896899
</Compile>
@@ -1217,4 +1220,4 @@
12171220
<Target Name="AfterBuild">
12181221
</Target>
12191222
-->
1220-
</Project>
1223+
</Project>

src/net40/WampSharp/WampSharp.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,9 @@
601601
<Compile Include="..\..\net45\WampSharp\WAMP2\V2\Client\IWampPendingRequest.cs">
602602
<Link>WAMP2\V2\Client\IWampPendingRequest.cs</Link>
603603
</Compile>
604+
<Compile Include="..\..\net45\WampSharp\WAMP2\V2\Client\Session\IWampClientAutenticator.cs">
605+
<Link>WAMP2\V2\Client\Session\IWampClientAutenticator.cs</Link>
606+
</Compile>
604607
<Compile Include="..\..\net45\WampSharp\WAMP2\V2\Client\Session\IWampClientConnectionMonitor.cs">
605608
<Link>WAMP2\V2\Client\Session\IWampClientConnectionMonitor.cs</Link>
606609
</Compile>
@@ -1221,4 +1224,4 @@
12211224
<Target Name="AfterBuild">
12221225
</Target>
12231226
-->
1224-
</Project>
1227+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
</configuration>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using WampSharp.Binding;
7+
using WampSharp.V2;
8+
using WampSharp.V2.Client;
9+
using WampSharp.V2.Rpc;
10+
11+
namespace WampSharp.Samples.Authentication
12+
{
13+
public interface IArgumentsService
14+
{
15+
[WampProcedure("com.timeservice.now")]
16+
string timeservice();
17+
}
18+
19+
public class CustomAuthenticator : IWampClientAutenticator
20+
{
21+
private string[] authenticationMethods;
22+
private IDictionary<string, string> tickets = new Dictionary<string, string>()
23+
{
24+
{ "peter", "md5f39d45e1da71cf755a7ee5d5840c7b0d" },
25+
{ "joe", "magic_secret_2" }
26+
};
27+
private string user = "peter";
28+
29+
public CustomAuthenticator()
30+
{
31+
this.authenticationMethods = new string[] { "ticket" };
32+
}
33+
34+
public ChallengeResult Authenticate(string challenge, ChallengeDetails extra)
35+
{
36+
var challengeExtra = extra.OriginalValue.Deserialize<IDictionary<string, object>>();
37+
38+
var method = (string)challengeExtra["authmethod"];
39+
Console.WriteLine(method);
40+
foreach (var ce in challengeExtra)
41+
{
42+
Console.WriteLine(ce);
43+
}
44+
45+
if (method == "ticket")
46+
{
47+
Console.WriteLine("authenticating via '" + method + "'");
48+
var result = new ChallengeResult();
49+
result.Signature = tickets[user];
50+
result.Extra = new Dictionary<string, object>() { };
51+
return result;
52+
}
53+
else
54+
{
55+
throw new WampAuthenticationException("don't know how to authenticate using '" + method + "'");
56+
}
57+
}
58+
59+
public string[] AuthenticationMethods
60+
{
61+
get { return authenticationMethods; }
62+
}
63+
64+
public string AuthenticationId
65+
{
66+
get { return user; }
67+
}
68+
}
69+
70+
class Program
71+
{
72+
private static IWampRealmProxy proxy;
73+
74+
private static void Test(IWampRealmServiceProvider serviceProvider)
75+
{
76+
IArgumentsService proxy = serviceProvider.GetCalleeProxy<IArgumentsService>();
77+
string now;
78+
try
79+
{
80+
now = proxy.timeservice();
81+
Console.WriteLine("call result {0}", now);
82+
}
83+
catch (Exception e)
84+
{
85+
Console.WriteLine("call error {0}", e);
86+
}
87+
}
88+
89+
static void Main(string[] args)
90+
{
91+
string url = "ws://127.0.0.1:8080/";
92+
string realm = "integra-s";
93+
94+
DefaultWampChannelFactory channelFactory = new DefaultWampChannelFactory();
95+
96+
IWampClientAutenticator authenticator = new CustomAuthenticator();
97+
IWampChannel channel = channelFactory.CreateJsonChannel(url, realm, authenticator);
98+
channel.RealmProxy.Monitor.ConnectionEstablished += Monitor_ConnectionEstablished;
99+
channel.RealmProxy.Monitor.ConnectionBroken += Monitor_ConnectionBroken;
100+
Program.proxy = channel.RealmProxy;
101+
channel.Open().Wait();
102+
Test(channel.RealmProxy.Services);
103+
Console.ReadLine();
104+
}
105+
106+
static void Monitor_ConnectionEstablished(object sender, V2.Realm.WampSessionEventArgs e)
107+
{
108+
var details = e.Details.Deserialize<IDictionary<string, object>>();
109+
110+
Console.WriteLine("connected session with ID " + e.SessionId);
111+
Console.WriteLine("authenticated using method '" + details["authmethod"] + "' and provider '" + details["authprovider"] + "'");
112+
Console.WriteLine("authenticated with authid '" + details["authid"] + "' and authrole '" + details["authrole"] + "'");
113+
114+
//Test(Program.proxy.Services);
115+
}
116+
117+
static void Monitor_ConnectionBroken(object sender, V2.Realm.WampSessionCloseEventArgs e)
118+
{
119+
Console.WriteLine("disconnected {0}", e.Reason);
120+
}
121+
}
122+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("WampSharp.Samples.Authentication")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("WampSharp.Samples.Authentication")]
13+
[assembly: AssemblyCopyright("Copyright © 2014")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("3e3df86c-9d5d-45f5-858d-5e653c196d58")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{305C0F3D-1A51-4D8C-8729-481F73A7C7D6}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>WampSharp.Samples.Authentication</RootNamespace>
11+
<AssemblyName>WampSharp.Samples.Authentication</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="System" />
36+
<Reference Include="System.Core" />
37+
<Reference Include="System.Xml.Linq" />
38+
<Reference Include="System.Data.DataSetExtensions" />
39+
<Reference Include="Microsoft.CSharp" />
40+
<Reference Include="System.Data" />
41+
<Reference Include="System.Xml" />
42+
</ItemGroup>
43+
<ItemGroup>
44+
<Compile Include="Program.cs" />
45+
<Compile Include="Properties\AssemblyInfo.cs" />
46+
</ItemGroup>
47+
<ItemGroup>
48+
<None Include="App.config" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<ProjectReference Include="..\..\..\WampSharp.Default\WampSharp.Default.csproj">
52+
<Project>{6ab75291-d296-457e-88a3-b41b16a1a247}</Project>
53+
<Name>WampSharp.Default</Name>
54+
</ProjectReference>
55+
<ProjectReference Include="..\..\..\WampSharp\WampSharp.csproj">
56+
<Project>{653a76dc-00d7-4eff-a25e-2fa10c5c927d}</Project>
57+
<Name>WampSharp</Name>
58+
</ProjectReference>
59+
</ItemGroup>
60+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
61+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
62+
Other similar extension points exist, see Microsoft.Common.targets.
63+
<Target Name="BeforeBuild">
64+
</Target>
65+
<Target Name="AfterBuild">
66+
</Target>
67+
-->
68+
</Project>

src/net45/WampSharp.Default/WAMP2/V2/DefaultWampChannelFactory.cs

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace WampSharp.V2
1111
public class DefaultWampChannelFactory : WampChannelFactory
1212
{
1313
private readonly JTokenMsgpackBinding mMsgpackBinding = new JTokenMsgpackBinding();
14-
private readonly JTokenJsonBinding mJsonBinding = new JTokenJsonBinding();
14+
private readonly JTokenJsonBinding mJsonBinding = new JTokenJsonBinding();
1515

1616
/// <summary>
1717
/// Creates a <see cref="IWampChannel"/> that connects to a given realm,
@@ -26,9 +26,29 @@ public IWampChannel CreateChannel<TMessage>(string address,
2626
IWampTextBinding<TMessage> binding)
2727
{
2828
var connection =
29-
new WebSocket4NetTextConnection<TMessage>(address, binding);
30-
29+
new WebSocket4NetTextConnection<TMessage>(address, binding);
30+
3131
return this.CreateChannel(realm, connection, binding);
32+
}
33+
34+
/// <summary>
35+
/// Creates a <see cref="IWampChannel"/> that connects to a given realm,
36+
/// using the given address and the given text binding
37+
/// </summary>
38+
/// <param name="address">The given address.</param>
39+
/// <param name="realm">The given realm to connect to.</param>
40+
/// <param name="binding">The given text binding.</param>
41+
/// <param name="autenticator">The authenticator object to handle CHALLENGE request.</param>
42+
/// <returns></returns>
43+
public IWampChannel CreateChannel<TMessage>(string address,
44+
string realm,
45+
IWampTextBinding<TMessage> binding,
46+
IWampClientAutenticator autenticator)
47+
{
48+
var connection =
49+
new WebSocket4NetTextConnection<TMessage>(address, binding);
50+
51+
return this.CreateChannel(realm, connection, binding, autenticator);
3252
}
3353

3454
/// <summary>
@@ -44,11 +64,31 @@ public IWampChannel CreateChannel<TMessage>(string address,
4464
IWampBinaryBinding<TMessage> binding)
4565
{
4666
var connection =
47-
new WebSocket4NetBinaryConnection<TMessage>(address, binding);
48-
67+
new WebSocket4NetBinaryConnection<TMessage>(address, binding);
68+
4969
return this.CreateChannel(realm, connection, binding);
5070
}
5171

72+
/// <summary>
73+
/// Creates a <see cref="IWampChannel"/> that connects to a given realm,
74+
/// using the given address and the given binary binding
75+
/// </summary>
76+
/// <param name="address">The given address.</param>
77+
/// <param name="realm">The given realm to connect to.</param>
78+
/// <param name="binding">The given binary binding.</param>
79+
/// <param name="autenticator">The authenticator object to handle CHALLENGE request.</param>
80+
/// <returns></returns>
81+
public IWampChannel CreateChannel<TMessage>(string address,
82+
string realm,
83+
IWampBinaryBinding<TMessage> binding,
84+
IWampClientAutenticator autenticator)
85+
{
86+
var connection =
87+
new WebSocket4NetBinaryConnection<TMessage>(address, binding);
88+
89+
return this.CreateChannel(realm, connection, binding, autenticator);
90+
}
91+
5292
/// <summary>
5393
/// Creates a <see cref="IWampChannel"/> that connects to a given realm,
5494
/// using the given address and json binding
@@ -62,6 +102,21 @@ public IWampChannel CreateJsonChannel(string address,
62102
return this.CreateChannel(address, realm, mJsonBinding);
63103
}
64104

105+
/// <summary>
106+
/// Creates a <see cref="IWampChannel"/> that connects to a given realm,
107+
/// using the given address and json binding
108+
/// </summary>
109+
/// <param name="address">The given address.</param>
110+
/// <param name="realm">The given realm to connect to.</param>
111+
/// <param name="autenticator">The authenticator object to handle CHALLENGE request.</param>
112+
/// <returns></returns>
113+
public IWampChannel CreateJsonChannel(string address,
114+
string realm,
115+
IWampClientAutenticator autenticator)
116+
{
117+
return this.CreateChannel(address, realm, mJsonBinding, autenticator);
118+
}
119+
65120
/// <summary>
66121
/// Creates a <see cref="IWampChannel"/> that connects to a given realm,
67122
/// using the given address and msgpack binding
@@ -73,6 +128,21 @@ public IWampChannel CreateMsgpackChannel(string address,
73128
string realm)
74129
{
75130
return this.CreateChannel(address, realm, mMsgpackBinding);
131+
}
132+
133+
/// <summary>
134+
/// Creates a <see cref="IWampChannel"/> that connects to a given realm,
135+
/// using the given address and msgpack binding
136+
/// </summary>
137+
/// <param name="address">The given address.</param>
138+
/// <param name="realm">The given realm to connect to.</param>
139+
/// <param name="autenticator">The authenticator object to handle CHALLENGE request.</param>
140+
/// <returns></returns>
141+
public IWampChannel CreateMsgpackChannel(string address,
142+
string realm,
143+
IWampClientAutenticator autenticator)
144+
{
145+
return this.CreateChannel(address, realm, mMsgpackBinding, autenticator);
76146
}
77147
}
78148
}

0 commit comments

Comments
 (0)