-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
AuthorizationBehavior.cs
169 lines (147 loc) · 7.14 KB
/
AuthorizationBehavior.cs
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
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Dispatcher
{
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.IdentityModel.Policy;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.ServiceModel;
using System.ServiceModel.Diagnostics;
using System.ServiceModel.Diagnostics.Application;
using System.ServiceModel.Security;
sealed class AuthorizationBehavior
{
static ServiceAuthorizationManager DefaultServiceAuthorizationManager = new ServiceAuthorizationManager();
ReadOnlyCollection<IAuthorizationPolicy> externalAuthorizationPolicies;
ServiceAuthorizationManager serviceAuthorizationManager;
AuditLogLocation auditLogLocation;
bool suppressAuditFailure;
AuditLevel serviceAuthorizationAuditLevel;
AuthorizationBehavior() { }
public void Authorize(ref MessageRpc rpc)
{
if (TD.DispatchMessageBeforeAuthorizationIsEnabled())
{
TD.DispatchMessageBeforeAuthorization(rpc.EventTraceActivity);
}
SecurityMessageProperty security = SecurityMessageProperty.GetOrCreate(rpc.Request);
security.ExternalAuthorizationPolicies = this.externalAuthorizationPolicies;
ServiceAuthorizationManager serviceAuthorizationManager = this.serviceAuthorizationManager ?? DefaultServiceAuthorizationManager;
bool outputTiming = DS.AuthorizationIsEnabled();
Stopwatch sw = null;
if (outputTiming)
{
sw = Stopwatch.StartNew();
}
try
{
if (!serviceAuthorizationManager.CheckAccess(rpc.OperationContext, ref rpc.Request))
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateAccessDeniedFaultException());
}
if (outputTiming)
{
DS.Authorization(this.serviceAuthorizationManager.GetType(), true, sw.Elapsed);
}
}
catch (Exception ex)
{
if (Fx.IsFatal(ex))
{
throw;
}
if (outputTiming)
{
DS.Authorization(this.serviceAuthorizationManager.GetType(), false, sw.Elapsed);
}
if (PerformanceCounters.PerformanceCountersEnabled)
{
PerformanceCounters.AuthorizationFailed(rpc.Operation.Name);
}
if (AuditLevel.Failure == (this.serviceAuthorizationAuditLevel & AuditLevel.Failure))
{
try
{
string primaryIdentity;
string authContextId = null;
AuthorizationContext authContext = security.ServiceSecurityContext.AuthorizationContext;
if (authContext != null)
{
primaryIdentity = SecurityUtils.GetIdentityNamesFromContext(authContext);
authContextId = authContext.Id;
}
else
{
primaryIdentity = SecurityUtils.AnonymousIdentity.Name;
authContextId = "<null>";
}
SecurityAuditHelper.WriteServiceAuthorizationFailureEvent(this.auditLogLocation,
this.suppressAuditFailure, rpc.Request, rpc.Request.Headers.To, rpc.Request.Headers.Action,
primaryIdentity, authContextId,
serviceAuthorizationManager == DefaultServiceAuthorizationManager ? "<default>" : serviceAuthorizationManager.GetType().Name,
ex);
}
#pragma warning suppress 56500
catch (Exception auditException)
{
if (Fx.IsFatal(auditException))
throw;
DiagnosticUtility.TraceHandledException(auditException, TraceEventType.Error);
}
}
throw;
}
if (AuditLevel.Success == (this.serviceAuthorizationAuditLevel & AuditLevel.Success))
{
string primaryIdentity;
string authContextId;
AuthorizationContext authContext = security.ServiceSecurityContext.AuthorizationContext;
if (authContext != null)
{
primaryIdentity = SecurityUtils.GetIdentityNamesFromContext(authContext);
authContextId = authContext.Id;
}
else
{
primaryIdentity = SecurityUtils.AnonymousIdentity.Name;
authContextId = "<null>";
}
SecurityAuditHelper.WriteServiceAuthorizationSuccessEvent(this.auditLogLocation,
this.suppressAuditFailure, rpc.Request, rpc.Request.Headers.To, rpc.Request.Headers.Action,
primaryIdentity, authContextId,
serviceAuthorizationManager == DefaultServiceAuthorizationManager ? "<default>" : serviceAuthorizationManager.GetType().Name);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
static AuthorizationBehavior CreateAuthorizationBehavior(DispatchRuntime dispatch)
{
AuthorizationBehavior behavior = new AuthorizationBehavior();
behavior.externalAuthorizationPolicies = dispatch.ExternalAuthorizationPolicies;
behavior.serviceAuthorizationManager = dispatch.ServiceAuthorizationManager;
behavior.auditLogLocation = dispatch.SecurityAuditLogLocation;
behavior.suppressAuditFailure = dispatch.SuppressAuditFailure;
behavior.serviceAuthorizationAuditLevel = dispatch.ServiceAuthorizationAuditLevel;
return behavior;
}
public static AuthorizationBehavior TryCreate(DispatchRuntime dispatch)
{
if (dispatch == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("dispatch"));
if (!dispatch.RequiresAuthorization)
return null;
return CreateAuthorizationBehavior(dispatch);
}
internal static Exception CreateAccessDeniedFaultException()
{
// always use default version?
SecurityVersion wss = SecurityVersion.Default;
FaultCode faultCode = FaultCode.CreateSenderFaultCode(wss.FailedAuthenticationFaultCode.Value, wss.HeaderNamespace.Value);
FaultReason faultReason = new FaultReason(SR.GetString(SR.AccessDenied), CultureInfo.CurrentCulture);
return new FaultException(faultReason, faultCode);
}
}
}