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 : /// <summary>
106 : /// Instantiate a new instance of <see cref="AuthenticationTokenHelper{TAuthenticationToken}"/>
107 : /// </summary>
108 2 : public AuthenticationTokenHelper(IContextItemCollectionFactory factory)
109 : {
110 : Cache = factory.GetCurrentContext();
111 : }
112 :
113 : #region Implementation of IAuthenticationTokenHelper<out TAuthenticationToken>
114 :
115 : /// <summary>
116 : /// Set the provided <paramref name="token"/> for the current context/request.
117 : /// </summary>
118 2 : public TAuthenticationToken SetAuthenticationToken(TAuthenticationToken token)
119 : {
120 : Cache.SetData(CacheKey, token);
121 : return token;
122 : }
123 :
124 : /// <summary>
125 : /// Get the current <typeparamref name="TAuthenticationToken">authentication token</typeparamref> for the current context/request.
126 : /// </summary>
127 2 : public TAuthenticationToken GetAuthenticationToken()
128 : {
129 : return Cache.GetData<TAuthenticationToken>(CacheKey);
130 : }
131 :
132 : #endregion
133 : }
134 : }
|