Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using cdmdotnet.StateManagement;
11 :
12 : namespace Cqrs.Authentication
13 : {
14 : /// <summary>
15 : /// A helper for setting and retrieving authentication tokens of type
16 : /// <see cref="ISingleSignOnToken"/>, <see cref="ISingleSignOnTokenWithUserRsn"/>, <see cref="ISingleSignOnTokenWithCompanyRsn"/>
17 : /// or <see cref="ISingleSignOnTokenWithUserRsnAndCompanyRsn"/>.
18 : /// </summary>
19 : public class AuthenticationTokenHelper
20 : : AuthenticationTokenHelper<ISingleSignOnToken>
21 : , IAuthenticationTokenHelper<ISingleSignOnTokenWithUserRsn>
22 : , IAuthenticationTokenHelper<ISingleSignOnTokenWithCompanyRsn>
23 : , IAuthenticationTokenHelper<ISingleSignOnTokenWithUserRsnAndCompanyRsn>
24 : {
25 : private const string CallContextPermissionScopeValueKey = "SingleSignOnTokenValue";
26 :
27 : /// <summary>
28 : /// Instantiate a new instance of <see cref="AuthenticationTokenHelper"/>
29 : /// </summary>
30 2 : public AuthenticationTokenHelper(IContextItemCollectionFactory factory)
31 : : base(factory)
32 : {
33 : CacheKey = CallContextPermissionScopeValueKey;
34 : }
35 :
36 : /// <summary>
37 : /// Set the provided <paramref name="token"/> for the current context/request.
38 : /// </summary>
39 2 : public ISingleSignOnTokenWithUserRsnAndCompanyRsn SetAuthenticationToken(ISingleSignOnTokenWithUserRsnAndCompanyRsn token)
40 : {
41 : SetAuthenticationToken((ISingleSignOnToken)token);
42 : return token;
43 : }
44 :
45 : /// <summary>
46 : /// Set the provided <paramref name="token"/> for the current context/request.
47 : /// </summary>
48 2 : public ISingleSignOnTokenWithCompanyRsn SetAuthenticationToken(ISingleSignOnTokenWithCompanyRsn token)
49 : {
50 : SetAuthenticationToken((ISingleSignOnToken)token);
51 : return token;
52 : }
53 :
54 : /// <summary>
55 : /// Set the provided <paramref name="token"/> for the current context/request.
56 : /// </summary>
57 2 : public ISingleSignOnTokenWithUserRsn SetAuthenticationToken(ISingleSignOnTokenWithUserRsn token)
58 : {
59 : SetAuthenticationToken((ISingleSignOnToken)token);
60 : return token;
61 : }
62 :
63 : /// <summary>
64 : /// Get the current <see cref="ISingleSignOnTokenWithUserRsn">authentication token</see> for the current context/request.
65 : /// </summary>
66 : ISingleSignOnTokenWithUserRsn IAuthenticationTokenHelper<ISingleSignOnTokenWithUserRsn>.GetAuthenticationToken()
67 : {
68 : return Cache.GetData<ISingleSignOnTokenWithUserRsn>(CallContextPermissionScopeValueKey);
69 : }
70 :
71 : /// <summary>
72 : /// Get the current <see cref="ISingleSignOnTokenWithCompanyRsn">authentication token</see> for the current context/request.
73 : /// </summary>
74 : ISingleSignOnTokenWithCompanyRsn IAuthenticationTokenHelper<ISingleSignOnTokenWithCompanyRsn>.GetAuthenticationToken()
75 : {
76 : return Cache.GetData<ISingleSignOnTokenWithCompanyRsn>(CallContextPermissionScopeValueKey);
77 : }
78 :
79 : /// <summary>
80 : /// Get the current <see cref="ISingleSignOnTokenWithUserRsnAndCompanyRsn">authentication token</see> for the current context/request.
81 : /// </summary>
82 : ISingleSignOnTokenWithUserRsnAndCompanyRsn IAuthenticationTokenHelper<ISingleSignOnTokenWithUserRsnAndCompanyRsn>.GetAuthenticationToken()
83 : {
84 : return Cache.GetData<ISingleSignOnTokenWithUserRsnAndCompanyRsn>(CallContextPermissionScopeValueKey);
85 : }
86 : }
87 :
88 : /// <summary>
89 : /// A helper for setting and retrieving authentication tokens of type <typeparamref name="TAuthenticationToken"/>
90 : /// </summary>
91 : /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of authentication token.</typeparam>
92 : public class AuthenticationTokenHelper<TAuthenticationToken>
93 : : IAuthenticationTokenHelper<TAuthenticationToken>
94 2 : {
95 : /// <summary>
96 : /// The key used to store the authentication token in the <see cref="Cache"/>.
97 : /// </summary>
98 : protected string CacheKey = string.Format("{0}AuthenticationToken", typeof(TAuthenticationToken).FullName);
99 :
100 : /// <summary>
101 : /// Get or set the Cache.
102 : /// </summary>
103 : protected IContextItemCollection Cache { get; private set; }
104 :
105 :
106 : /// <summary>
107 : /// Instantiate a new instance of <see cref="AuthenticationTokenHelper{TAuthenticationToken}"/>
108 : /// </summary>
109 2 : public AuthenticationTokenHelper(IContextItemCollectionFactory factory)
110 : {
111 : Cache = factory.GetCurrentContext();
112 : }
113 :
114 : #region Implementation of IAuthenticationTokenHelper<out TAuthenticationToken>
115 :
116 : /// <summary>
117 : /// Set the provided <paramref name="token"/> for the current context/request.
118 : /// </summary>
119 2 : public TAuthenticationToken SetAuthenticationToken(TAuthenticationToken token)
120 : {
121 : Cache.SetData(CacheKey, token);
122 : return token;
123 : }
124 :
125 : /// <summary>
126 : /// Get the current <typeparamref name="TAuthenticationToken">authentication token</typeparamref> for the current context/request.
127 : /// </summary>
128 2 : public TAuthenticationToken GetAuthenticationToken()
129 : {
130 : return Cache.GetData<TAuthenticationToken>(CacheKey);
131 : }
132 :
133 : #endregion
134 : }
135 : }
|