File: SecurityTokenDescriptor.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (253 lines) | stat: -rw-r--r-- 8,919 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//-----------------------------------------------------------------------
// <copyright file="SecurityTokenDescriptor.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace System.IdentityModel.Tokens
{
    using System;
    using System.Collections.Generic;
    using System.IdentityModel;
    using System.IdentityModel.Protocols.WSTrust;
    using System.Security.Claims;
    using System.Xml;

    using RST = System.IdentityModel.Protocols.WSTrust.RequestSecurityToken;
    using RSTR = System.IdentityModel.Protocols.WSTrust.RequestSecurityTokenResponse;

    /// <summary>
    /// This is a place holder for all the attributes related to the issued token.
    /// </summary>
    public class SecurityTokenDescriptor
    {
        private SecurityKeyIdentifierClause attachedReference;
        private AuthenticationInformation authenticationInfo;
        private string tokenIssuerName;
        private ProofDescriptor proofDescriptor;
        private ClaimsIdentity subject;
        private SecurityToken token;
        private string tokenType;
        private SecurityKeyIdentifierClause unattachedReference;
        private Lifetime lifetime;
        private string appliesToAddress;
        private string replyToAddress;
        private EncryptingCredentials encryptingCredentials;
        private SigningCredentials signingCredentials;

        private Dictionary<string, object> properties = new Dictionary<string, object>(); // for any custom data

        /// <summary>
        /// Gets or sets the address for the <see cref="RequestSecurityTokenResponse"/> AppliesTo property.
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown if not an absolute URI.</exception>
        public string AppliesToAddress
        {
            get 
            { 
                return this.appliesToAddress; 
            }

            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    if (!UriUtil.CanCreateValidUri(value, UriKind.Absolute))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ID2002)));
                    }
                }

                this.appliesToAddress = value;
            }
        }

        /// <summary>
        /// Sets the appropriate things, such as requested security token, inside the RSTR 
        /// based on what is inside this token descriptor instance.  
        /// </summary>
        /// <param name="response">The RSTR object that this security token descriptor needs to modify.</param>
        /// <exception cref="ArgumentNullException">When response is null.</exception>
        public virtual void ApplyTo(RSTR response)
        {
            if (response == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("response");
            }

            if (tokenType != null)
            {
                response.TokenType = tokenType;
            }

            if (token != null)
            {
                response.RequestedSecurityToken = new RequestedSecurityToken(token);
            }

            if (attachedReference != null)
            {
                response.RequestedAttachedReference = attachedReference;
            }

            if (unattachedReference != null)
            {
                response.RequestedUnattachedReference = unattachedReference;
            }

            if (lifetime != null)
            {
                response.Lifetime = lifetime;
            }

            if (proofDescriptor != null)
            {
                proofDescriptor.ApplyTo(response);
            }
        }

        /// <summary>
        /// Gets or sets the address for the <see cref="RequestSecurityTokenResponse"/> ReplyToAddress property.
        /// </summary>
        public string ReplyToAddress
        {
            get { return this.replyToAddress; }
            set { this.replyToAddress = value; }
        }

        /// <summary>
        /// Gets or sets the credentials used to encrypt the token.
        /// </summary>
        public EncryptingCredentials EncryptingCredentials
        {
            get { return this.encryptingCredentials; }
            set { this.encryptingCredentials = value; }
        }

        /// <summary>
        /// Gets or sets the credentials used to sign the token.
        /// </summary>
        public SigningCredentials SigningCredentials
        {
            get { return this.signingCredentials; }
            set { this.signingCredentials = value; }
        }

        /// <summary>
        /// Gets or sets the SecurityKeyIdentifierClause when the token is attached 
        /// to the message.
        /// </summary>
        public SecurityKeyIdentifierClause AttachedReference
        {
            get { return this.attachedReference; }
            set { this.attachedReference = value; }
        }

        /// <summary>
        /// Gets or sets the issuer name, which may be used inside the issued token as well.
        /// </summary>
        public string TokenIssuerName
        {
            get { return this.tokenIssuerName; }
            set { this.tokenIssuerName = value; }
        }

        /// <summary>
        /// Gets or sets the proof descriptor, which can be used to modify some fields inside
        /// the RSTR, such as requested proof token.
        /// </summary>
        public ProofDescriptor Proof
        {
            get { return this.proofDescriptor; }
            set { this.proofDescriptor = value; }
        }

        /// <summary>
        /// Gets the properties bag to extend the object.
        /// </summary>
        public Dictionary<string, object> Properties
        {
            get { return this.properties; }
        }

        /// <summary>
        /// Gets or sets the issued security token.
        /// </summary>
        public SecurityToken Token
        {
            get { return this.token; }
            set { this.token = value; }
        }

        /// <summary>
        /// Gets or sets the token type of the issued token.
        /// </summary>
        public string TokenType
        {
            get { return this.tokenType; }
            set { this.tokenType = value; }
        }

        /// <summary>
        /// Gets or sets the unattached token reference to refer to the issued token when it is not 
        /// attached to the message.
        /// </summary>
        public SecurityKeyIdentifierClause UnattachedReference
        {
            get { return this.unattachedReference; }
            set { this.unattachedReference = value; }
        }

        /// <summary>
        /// Gets or sets the lifetime information for the issued token.
        /// </summary>
        public Lifetime Lifetime
        {
            get { return this.lifetime; }
            set { this.lifetime = value; }
        }

        /// <summary>
        /// Gets or sets the OutputClaims to be included in the issued token.
        /// </summary>
        public ClaimsIdentity Subject
        {
            get { return this.subject; }
            set { this.subject = value; }
        }

        /// <summary>
        /// Gets or sets the AuthenticationInformation.
        /// </summary>
        public AuthenticationInformation AuthenticationInfo
        {
            get { return this.authenticationInfo; }
            set { this.authenticationInfo = value; }
        }

        /// <summary>
        /// Adds a <see cref="Claim"/> for the authentication type to the claim collection of 
        /// the <see cref="SecurityTokenDescriptor"/>
        /// </summary>
        /// <param name="authType">The authentication type.</param>
        public void AddAuthenticationClaims(string authType)
        {
            this.AddAuthenticationClaims(authType, DateTime.UtcNow);
        }

        /// <summary>
        /// Adds <see cref="Claim"/>s for the authentication type and the authentication instant 
        /// to the claim collection of the <see cref="SecurityTokenDescriptor"/>
        /// </summary>
        /// <param name="authType">Specifies the authentication type</param>
        /// <param name="time">Specifies the authentication instant in UTC. If the input is not in UTC, it is converted to UTC.</param> 
        public void AddAuthenticationClaims(string authType, DateTime time)
        {
            this.Subject.AddClaim(
                new Claim(ClaimTypes.AuthenticationMethod, authType, ClaimValueTypes.String));

            this.Subject.AddClaim(
                new Claim(ClaimTypes.AuthenticationInstant, XmlConvert.ToString(time.ToUniversalTime(), DateTimeFormats.Generated), ClaimValueTypes.DateTime));
        }
    }
}