Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions IntelliTect.Utilities.Tests/ClaimsPrincipalGetRolesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Security.Claims;
using System.Security.Principal;
using IntelliTect.Utilities.Security;
using Xunit;

namespace IntelliTect.Utilities.Tests
{
public class ClaimsPrincipalGetRolesTests
{
[Fact]
public void WhenClaimsPrincipalNull_Should_Throw()
{
ClaimsPrincipal sut = null;

Assert.Throws<ArgumentNullException>(() => sut.GetRoles());
}

[Fact]
public void WhenClaimsPrincipalHasNoRoles_Should_ReturnEmpty()
{
ClaimsPrincipal sut = new ClaimsPrincipal();

Assert.Empty(sut.GetRoles());
}

[Fact]
public void WhenClaimsPrincipalHasRoles_Should_ReturnNotEmpty()
{
ClaimsPrincipal sut = new GenericPrincipal(new GenericIdentity("Uncle Festus"), new[] {"Foo", "Bar"});

Assert.Collection(sut.GetRoles(), s => Assert.Equal("Foo", s), t => Assert.Equal("Bar", t) );
}
}
}
35 changes: 35 additions & 0 deletions IntelliTect.Utilities.Tests/ClaimsPrincipalGetUserIdTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Security.Claims;
using System.Security.Principal;
using IntelliTect.Utilities.Security;
using Xunit;

namespace IntelliTect.Utilities.Tests
{
public class ClaimsPrincipalGetUserIdTests
{
[Fact]
public void WhenClaimsPrincipalNull_Should_Throw()
{
ClaimsPrincipal sut = null;

Assert.Throws<ArgumentNullException>(() => sut.GetUserId());
}

[Fact]
public void WhenClaimsPrincipalHasNoProperty_Should_ReturnNull()
{
ClaimsPrincipal sut = new ClaimsPrincipal();

Assert.Null(sut.GetUserId());
}

[Fact]
public void WhenClaimsPrincipalHasId_Should_ReturnString()
{
ClaimsPrincipal sut = new GenericPrincipal(new GenericIdentity("Taki The Frog"), new []{ "Bar" } );

Assert.Equal("Taki The Frog", sut.GetUserId());
}
}
}
22 changes: 22 additions & 0 deletions IntelliTect.Utilities.Tests/IntelliTect.Utilities.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net462</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="Moq" Version="4.7.99" />
<PackageReference Include="MSTest.TestAdapter" Version="1.1.18" />
<PackageReference Include="MSTest.TestFramework" Version="1.1.18" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\IntelliTect.Utilities\IntelliTect.Utilities.csproj" />
</ItemGroup>

</Project>
50 changes: 8 additions & 42 deletions IntelliTect.Utilities/IntelliTect.Utilities.csproj
Original file line number Diff line number Diff line change
@@ -1,56 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{6E9F4A6A-F611-4ECB-8AAC-B2079C0B9DF5}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>IntelliTect.Utilities</RootNamespace>
<AssemblyName>IntelliTect.Utilities</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<TargetFramework>net462</TargetFramework>
<Description>A utility library for IntelliTect</Description>
<Company>IntelliTect</Company>
<Authors>IntelliTect</Authors>
<Copyright>Copyright © IntelliTect 2017</Copyright>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>..\SolutionRuleset.ruleset</CodeAnalysisRuleSet>
<RunCodeAnalysis>true</RunCodeAnalysis>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="IntelliTect.Utilities.nuspec" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="CustomDictionary.xml" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
2 changes: 1 addition & 1 deletion IntelliTect.Utilities/IntelliTect.Utilities.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<projectUrl>https://github.com/IntelliTect/IntelliTect</projectUrl>
<requireLicenseAcceptance>true</requireLicenseAcceptance>
<description>$description$</description>
<releaseNotes>Added assembly linker date.</releaseNotes>
<releaseNotes>Added ClaimsPrincipal extensions.</releaseNotes>
<copyright>Copyright 2017</copyright>
<tags>IntelliTect</tags>
</metadata>
Expand Down
33 changes: 1 addition & 32 deletions IntelliTect.Utilities/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,7 @@
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("IntelliTect.Utilities")]
[assembly: AssemblyDescription("A utility library for IntelliTect")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("IntelliTect")]
[assembly: AssemblyProduct("IntelliTect.Utilities")]
[assembly: AssemblyCopyright("Copyright © IntelliTect 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: CLSCompliant(true)]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: CLSCompliant(true)]
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("6e9f4a6a-f611-4ecb-8aac-b2079c0b9df5")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
41 changes: 41 additions & 0 deletions IntelliTect.Utilities/Security/ClaimsPrincipalExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;

namespace IntelliTect.Utilities.Security
{
public static class ClaimsPrincipalExtensions
{
/// <summary>
/// Gets the user ID from a <see cref="ClaimsPrincipal"/>.
/// </summary>
/// <param name="principal">The <see cref="ClaimsPrincipal"/> to find a user ID for.</param>
/// <returns>A <see cref="string"/>, or null if the <see cref="ClaimsPrincipal"/> doesn't contain a <see cref="ClaimTypes.NameIdentifier"/>.</returns>
/// <exception cref="ArgumentNullException">principal is null.</exception>
public static string GetUserId(this ClaimsPrincipal principal)
{
if (principal == null) throw new ArgumentNullException(nameof(principal));

Claim claim = principal.FindFirst(ClaimTypes.NameIdentifier);
if (claim != null) return claim.Value;
claim = principal.FindFirst(ClaimTypes.Name);
return claim?.Value;
}


/// <summary>
/// Gets all the roles a <see cref="ClaimsPrincipal"/> belongs to.
/// </summary>
/// <param name="principal">The <see cref="ClaimsPrincipal"/> to find roles for.</param>
/// <returns>An <see cref="IEnumerable{T}"/> of <see cref="string"/> if found, or an empty set.</returns>
/// <exception cref="ArgumentNullException">principal is null.</exception>
public static IEnumerable<string> GetRoles(this ClaimsPrincipal principal)
{
if (principal == null) throw new ArgumentNullException(nameof(principal));

IEnumerable<Claim> claims = principal.FindAll(ClaimTypes.Role);
return claims?.Select(claim => claim.Value) ?? Enumerable.Empty<string>();
}
}
}
21 changes: 15 additions & 6 deletions IntelliTect.sln
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26206.0
VisualStudioVersion = 15.0.26730.15
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IntelliTect.Utilities", "IntelliTect.Utilities\IntelliTect.Utilities.csproj", "{6E9F4A6A-F611-4ECB-8AAC-B2079C0B9DF5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IntelliTect.Utilities", "IntelliTect.Utilities\IntelliTect.Utilities.csproj", "{4AEDF7B5-8FD1-4361-B230-173646A0D20C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IntelliTect.Utilities.Tests", "IntelliTect.Utilities.Tests\IntelliTect.Utilities.Tests.csproj", "{B0281AA3-FAED-4A2C-8C5A-C2F3DC39511A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6E9F4A6A-F611-4ECB-8AAC-B2079C0B9DF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6E9F4A6A-F611-4ECB-8AAC-B2079C0B9DF5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6E9F4A6A-F611-4ECB-8AAC-B2079C0B9DF5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6E9F4A6A-F611-4ECB-8AAC-B2079C0B9DF5}.Release|Any CPU.Build.0 = Release|Any CPU
{4AEDF7B5-8FD1-4361-B230-173646A0D20C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4AEDF7B5-8FD1-4361-B230-173646A0D20C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4AEDF7B5-8FD1-4361-B230-173646A0D20C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4AEDF7B5-8FD1-4361-B230-173646A0D20C}.Release|Any CPU.Build.0 = Release|Any CPU
{B0281AA3-FAED-4A2C-8C5A-C2F3DC39511A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B0281AA3-FAED-4A2C-8C5A-C2F3DC39511A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0281AA3-FAED-4A2C-8C5A-C2F3DC39511A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0281AA3-FAED-4A2C-8C5A-C2F3DC39511A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DE282392-8D28-4F0B-8A80-449A5781097B}
EndGlobalSection
EndGlobal
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ Libraries of useful C# things, focused on shipping Nuget packages.

Libraries
=========
### IntelliTect.Utilities: [![NuGet](https://img.shields.io/nuget/v/IntelliTect.Utilities.svg)](https://www.nuget.org/packages/IntelliTect.Utilities/)

* IntelliTect.Utilities [![NuGet](https://img.shields.io/nuget/v/IntelliTect.Utilities.svg)](https://www.nuget.org/packages/IntelliTect.Utilities/)
### Namespaces within this library:
* IntelliTect.Utilities
- AssemblyInfo: Gets an assembly's linker date/time.
* IntelliTect.Utilities.Security
- ClaimsPrincipalExtensions: Extention methods to get a user ID and roles.

Contributing
============
Expand Down