| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Globalization; |
| | | 5 | | using System.Net; |
| | | 6 | | using System.Security.Claims; |
| | | 7 | | using System.Security.Principal; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Security |
| | | 11 | | { |
| | | 12 | | internal static class SecurityUtils |
| | | 13 | | { |
| | | 14 | | internal static EndpointIdentity CreateWindowsIdentity(NetworkCredential serverCredential) |
| | | 15 | | { |
| | 0 | 16 | | if (serverCredential != null && !NetworkCredentialHelper.IsDefault(serverCredential)) |
| | | 17 | | { |
| | | 18 | | string upn; |
| | 0 | 19 | | if (serverCredential.Domain != null && serverCredential.Domain.Length > 0) |
| | | 20 | | { |
| | 0 | 21 | | upn = serverCredential.UserName + "@" + serverCredential.Domain; |
| | | 22 | | } |
| | | 23 | | else |
| | | 24 | | { |
| | 0 | 25 | | upn = serverCredential.UserName; |
| | | 26 | | } |
| | 0 | 27 | | return new UpnEndpointIdentity(upn); |
| | | 28 | | } |
| | | 29 | | else |
| | | 30 | | { |
| | 0 | 31 | | return CreateWindowsIdentity(); |
| | | 32 | | } |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | internal static EndpointIdentity CreateWindowsIdentity() |
| | | 36 | | { |
| | 0 | 37 | | EndpointIdentity identity = null; |
| | 0 | 38 | | using (WindowsIdentity self = WindowsIdentity.GetCurrent()) |
| | | 39 | | { |
| | 0 | 40 | | identity = new SpnEndpointIdentity(string.Format(CultureInfo.InvariantCulture, "host/{0}", DnsCache.Mach |
| | 0 | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | return identity; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | private static class NetworkCredentialHelper |
| | | 47 | | { |
| | | 48 | | internal static bool IsDefault(NetworkCredential credential) |
| | | 49 | | { |
| | 0 | 50 | | return UnsafeGetDefaultNetworkCredentials().Equals(credential); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | private static NetworkCredential UnsafeGetDefaultNetworkCredentials() |
| | | 54 | | { |
| | 0 | 55 | | return CredentialCache.DefaultNetworkCredentials; |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | internal static void AddPosixClaims(ClaimsIdentity claimsIdentity, string groupName, uint groupId, uint processI |
| | | 60 | | { |
| | 1 | 61 | | AddPosixGroupIdentityClaim(claimsIdentity, groupName, groupId); |
| | 1 | 62 | | AddProcessIdClaim(claimsIdentity, processId); |
| | 1 | 63 | | } |
| | | 64 | | |
| | | 65 | | private static void AddPosixGroupIdentityClaim(ClaimsIdentity claimsIdentity, string groupName, uint groupId) |
| | | 66 | | { |
| | 1 | 67 | | claimsIdentity.AddClaim(new Claim(CoreWCF.IdentityModel.Claims.ClaimTypes.PosixGroupId, groupId.ToString())) |
| | 1 | 68 | | claimsIdentity.AddClaim(new Claim(CoreWCF.IdentityModel.Claims.ClaimTypes.PosixGroupName, groupName)); |
| | 1 | 69 | | } |
| | | 70 | | |
| | | 71 | | internal static void AddProcessIdClaim(ClaimsIdentity claimsIdentity, uint processId) |
| | | 72 | | { |
| | 1 | 73 | | claimsIdentity.AddClaim(new Claim(CoreWCF.IdentityModel.Claims.ClaimTypes.ProcessId, processId.ToString())); |
| | 1 | 74 | | } |
| | | 75 | | } |
| | | 76 | | } |
| | | 77 | | |