Skip to content

Commit b909cab

Browse files
committed
Adding support for authentication per binding
1 parent 5a70bad commit b909cab

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using WampSharp.V2.Authentication;
5+
using WampSharp.V2.Binding;
6+
using WampSharp.V2.Binding.Transports;
7+
using WampSharp.V2.Core;
8+
using WampSharp.V2.Realm;
9+
10+
// ReSharper disable once CheckNamespace
11+
namespace WampSharp.V2
12+
{
13+
/// <summary>
14+
/// An implementation of <see cref="IWampHost"/> that supports authentication.
15+
/// </summary>
16+
public class WampMultiAuthenticationHost : WampHost, IWampMultiAuthenticationHost
17+
{
18+
private readonly IWampSessionAuthenticatorFactory mDefaultAuthenticatorFactory;
19+
20+
/// <summary>
21+
/// Initializes a new instance of <see cref="WampMultiAuthenticationHost"/> given the
22+
/// <see cref="IWampSessionAuthenticatorFactory"/> to use.
23+
/// </summary>
24+
/// <param name="defaultAuthenticatorFactory">A default <see cref="IWampSessionAuthenticatorFactory"/> that will be used for the overload <see cref="RegisterTransport(IWampTransport,IEnumerable{WampSharp.V2.Binding.IWampBinding})"/></param>
25+
/// <param name="realmContainer">The <see cref="IWampRealmContainer"/> associated with this
26+
/// host.</param>
27+
/// <param name="uriValidator">The <see cref="IWampUriValidator"/> used to validate uris.</param>
28+
public WampMultiAuthenticationHost(IWampSessionAuthenticatorFactory defaultAuthenticatorFactory = null,
29+
IWampRealmContainer realmContainer = null,
30+
IWampUriValidator uriValidator = null)
31+
: base(realmContainer, uriValidator)
32+
{
33+
mDefaultAuthenticatorFactory = defaultAuthenticatorFactory;
34+
}
35+
36+
public void RegisterTransport(IWampTransport transport, IDictionary<IWampBinding, IWampSessionAuthenticatorFactory> bindingToAuthenticatorFactory)
37+
{
38+
IEnumerable<IWampBinding> authenticationBindings =
39+
bindingToAuthenticatorFactory
40+
.Select(binding =>
41+
CreateAuthenticationBinding((dynamic) binding.Key,
42+
binding.Value))
43+
.Cast<IWampBinding>()
44+
.Where(x => x != null);
45+
46+
base.RegisterTransport(transport, authenticationBindings);
47+
}
48+
49+
public override void RegisterTransport(IWampTransport transport, IEnumerable<IWampBinding> bindings)
50+
{
51+
if (mDefaultAuthenticatorFactory == null)
52+
{
53+
ThrowHelper.NoDefaultSessionAuthenticatorWasProvided();
54+
}
55+
56+
IDictionary<IWampBinding, IWampSessionAuthenticatorFactory> bindingToDefaultAuthenticator =
57+
bindings.ToDictionary(x => x, x => mDefaultAuthenticatorFactory);
58+
59+
RegisterTransport(transport, bindingToDefaultAuthenticator);
60+
}
61+
62+
private IWampBinding CreateAuthenticationBinding<TMessage>
63+
(IWampTextBinding<TMessage> binding, IWampSessionAuthenticatorFactory authenticatorFactory)
64+
{
65+
ThrowHelper.ValidateSessionAuthenticatorFactoryWasProvided(authenticatorFactory, binding);
66+
return new WampAuthenticationTextBinding<TMessage>(binding, authenticatorFactory, this.UriValidator);
67+
}
68+
69+
private IWampBinding CreateAuthenticationBinding<TMessage>
70+
(IWampBinaryBinding<TMessage> binding, IWampSessionAuthenticatorFactory authenticatorFactory)
71+
{
72+
ThrowHelper.ValidateSessionAuthenticatorFactoryWasProvided(authenticatorFactory, binding);
73+
return new WampAuthenticationBinaryBinding<TMessage>(binding, authenticatorFactory, this.UriValidator);
74+
}
75+
76+
private IWampBinding CreateAuthenticationBinding<TMessage>
77+
(IWampBinding<TMessage> binding, IWampSessionAuthenticatorFactory authenticatorFactory)
78+
{
79+
ThrowHelper.ValidateSessionAuthenticatorFactoryWasProvided(authenticatorFactory, binding);
80+
return new WampAuthenticationBinding<TMessage>(binding, authenticatorFactory, this.UriValidator);
81+
}
82+
83+
/// <summary>
84+
/// Fallback in case that binding doesn't implement
85+
/// IWampBinding{TMessage}
86+
/// </summary>
87+
/// <param name="binding"></param>
88+
/// <param name="authenticatorFactory"></param>
89+
/// <returns></returns>
90+
private IWampBinding CreateAuthenticationBinding(IWampBinding binding, IWampSessionAuthenticatorFactory authenticatorFactory)
91+
{
92+
return null;
93+
}
94+
95+
private static class ThrowHelper
96+
{
97+
public static void ValidateSessionAuthenticatorFactoryWasProvided(IWampSessionAuthenticatorFactory authenticatorFactory, IWampBinding binding)
98+
{
99+
if (authenticatorFactory == null)
100+
{
101+
throw new
102+
ArgumentException($"No IWampSessionAuthenticatorFactory was provided for the binding named '{binding.Name}'.");
103+
}
104+
}
105+
106+
public static void NoDefaultSessionAuthenticatorWasProvided()
107+
{
108+
throw new
109+
ArgumentException("No default IWampSessionAuthenticatorFactory was provided. Either provide a default IWampSessionAuthenticatorFactory in the constructor, or call RegisterTransport with the desired IWampSessionAuthenticatorFactory");
110+
}
111+
}
112+
}
113+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Collections.Generic;
2+
using WampSharp.V2.Binding;
3+
using WampSharp.V2.Binding.Transports;
4+
5+
namespace WampSharp.V2.Authentication
6+
{
7+
public interface IWampMultiAuthenticationHost : IWampHost
8+
{
9+
void RegisterTransport(IWampTransport transport,
10+
IDictionary<IWampBinding, IWampSessionAuthenticatorFactory> bindingToAuthenticatorFactory);
11+
}
12+
}

0 commit comments

Comments
 (0)