Skip to content

Commit

Permalink
Added generic OS authentication provider
Browse files Browse the repository at this point in the history
  • Loading branch information
theRainbird committed Jan 24, 2021
1 parent a004fb9 commit ff9e563
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 92 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
/CoreRemoting.Authentication.LinuxPamAuthProvider/obj/
/CoreRemoting.Tests/bin/
/CoreRemoting.Tests/obj/
/Examples/HelloWorld.Client/obj/
/CoreRemoting.Authentication.WindowsAuthProvider/bin/Debug/netstandard2.0
/CoreRemoting.Authentication.WindowsAuthProvider/obj
/WindowsAuthTest/bin/Debug/netcoreapp3.1
Expand Down
27 changes: 1 addition & 26 deletions .idea/.idea.CoreRemoting/.idea/riderModule.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>CoreRemoting.Authentication</RootNamespace>
<AssemblyName>CoreRemoting.Authentication.GenericOsAuthProvider</AssemblyName>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\CoreRemoting.Authentication.LinuxPamAuthProvider\CoreRemoting.Authentication.LinuxPamAuthProvider.csproj" />
<ProjectReference Include="..\CoreRemoting.Authentication.WindowsAuthProvider\CoreRemoting.Authentication.WindowsAuthProvider.csproj" />
<ProjectReference Include="..\CoreRemoting\CoreRemoting.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

namespace CoreRemoting.Authentication
{
/// <summary>
/// Authentication provider to check credentials against local operationg system user accounts.
/// Works with Windows user accounts (local or domain) an local linux user accounts (passwd).
/// </summary>
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public class GenericOsAuthProvider : IAuthenticationProvider
{
public const string CREDENTIAL_TYPE_USERNAME = "username";
public const string CREDENTIAL_TYPE_PASSWORD = "password";

/// <summary>
/// Authenticates the provided credentials and returns the authenticated identity, if successful.
/// </summary>
/// <param name="credentials">Array of credentials ("username", "password" and optional "domain" [Windows AD only])</param>
/// <param name="authenticatedIdentity">Authenticated Identity</param>
/// <returns>Indicates whether the authentication was successful.</returns>
public bool Authenticate(Credential[] credentials, out RemotingIdentity authenticatedIdentity)
{
authenticatedIdentity = null;

IAuthenticationProvider authProvider = null;

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
authProvider = new WindowsAuthProvider();
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
authProvider = new LinuxPamAuthProvider();

if (authProvider == null)
throw new PlatformNotSupportedException();

return authProvider.Authenticate(credentials, out authenticatedIdentity);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using CoreRemoting.Authentication;
using Npam;

namespace CoreRemoting.Authentication
{
/// <summary>
/// Authentication provider to check credentials against local Linux user accounts.
/// </summary>
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public class LinuxPamAuthProvider : IAuthenticationProvider
{
public const string CREDENTIAL_TYPE_USERNAME = "username";
public const string CREDENTIAL_TYPE_PASSWORD = "password";

/// <summary>
/// Authenticates the provided credentials and returns the authenticated identity, if successful.
/// </summary>
/// <param name="credentials">Array of credentials ("username", "password")</param>
/// <param name="authenticatedIdentity">Authenticated Identity</param>
/// <returns>Indicates whether the authentication was successful.</returns>
public bool Authenticate(Credential[] credentials, out RemotingIdentity authenticatedIdentity)
{
authenticatedIdentity = null;
Expand All @@ -15,13 +28,13 @@ public bool Authenticate(Credential[] credentials, out RemotingIdentity authenti

var userName =
credentials
.Where(c => c.Name.ToLower() == "username")
.Where(c => c.Name.ToLower() == CREDENTIAL_TYPE_USERNAME)
.Select(c => c.Value)
.FirstOrDefault();

var password =
credentials
.Where(c => c.Name.ToLower() == "password")
.Where(c => c.Name.ToLower() == CREDENTIAL_TYPE_PASSWORD)
.Select(c => c.Value)
.FirstOrDefault();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>CoreRemoting.Authentication</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.DirectoryServices.AccountManagement;
using System.Security.Principal;

namespace CoreRemoting.Authentication
{
/// <summary>
/// Authentication provider to check credentials against Windows user accounts.
/// </summary>
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public class WindowsAuthProvider : IAuthenticationProvider
{
public const string CREDENTIAL_TYPE_DOMAIN = "domain";
public const string CREDENTIAL_TYPE_USERNAME = "username";
public const string CREDENTIAL_TYPE_PASSWORD = "password";

/// <summary>
/// Authenticates the provided credentials and returns the authenticated identity, if successful.
/// </summary>
/// <param name="credentials">Array of credentials ("username", "password" and optional "domain")</param>
/// <param name="authenticatedIdentity">Authenticated Identity</param>
/// <returns>Indicates whether the authentication was successful.</returns>
public bool Authenticate(Credential[] credentials, out RemotingIdentity authenticatedIdentity)
{
authenticatedIdentity = null;
Expand All @@ -16,19 +31,19 @@ public bool Authenticate(Credential[] credentials, out RemotingIdentity authenti

var domain =
credentials
.Where(c => c.Name.ToLower() == "domain")
.Where(c => c.Name.ToLower() == CREDENTIAL_TYPE_DOMAIN)
.Select(c => c.Value)
.FirstOrDefault();

var userName =
credentials
.Where(c => c.Name.ToLower() == "username")
.Where(c => c.Name.ToLower() == CREDENTIAL_TYPE_USERNAME)
.Select(c => c.Value)
.FirstOrDefault();

var password =
credentials
.Where(c => c.Name.ToLower() == "password")
.Where(c => c.Name.ToLower() == CREDENTIAL_TYPE_PASSWORD)
.Select(c => c.Value)
.FirstOrDefault();

Expand Down
11 changes: 5 additions & 6 deletions CoreRemoting.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreRemoting.Authentication.WindowsAuthProvider", "CoreRemoting.Authentication.WindowsAuthProvider\CoreRemoting.Authentication.WindowsAuthProvider.csproj", "{34E0ADAE-B7FF-4811-BBCD-60473E63A3A5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WindowsAuthTest", "WindowsAuthTest\WindowsAuthTest.csproj", "{E362AA89-9793-4CC3-95FD-7EA3EAFE94B6}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreRemoting.Authentication.GenericOsAuthProvider", "CoreRemoting.Authentication.GenericOsAuthProvider\CoreRemoting.Authentication.GenericOsAuthProvider.csproj", "{BC94361A-CA63-4D28-9FD0-BEF492952B6E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -62,10 +62,10 @@ Global
{34E0ADAE-B7FF-4811-BBCD-60473E63A3A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{34E0ADAE-B7FF-4811-BBCD-60473E63A3A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{34E0ADAE-B7FF-4811-BBCD-60473E63A3A5}.Release|Any CPU.Build.0 = Release|Any CPU
{E362AA89-9793-4CC3-95FD-7EA3EAFE94B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E362AA89-9793-4CC3-95FD-7EA3EAFE94B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E362AA89-9793-4CC3-95FD-7EA3EAFE94B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E362AA89-9793-4CC3-95FD-7EA3EAFE94B6}.Release|Any CPU.Build.0 = Release|Any CPU
{BC94361A-CA63-4D28-9FD0-BEF492952B6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BC94361A-CA63-4D28-9FD0-BEF492952B6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BC94361A-CA63-4D28-9FD0-BEF492952B6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BC94361A-CA63-4D28-9FD0-BEF492952B6E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -75,7 +75,6 @@ Global
{3D722B5E-00DD-4FFC-8057-5C3C16FEBA16} = {530CD64A-0D82-445A-8360-30E27FD7403C}
{7E1B5031-0265-4BEF-B4FE-382E5CAEBA8B} = {530CD64A-0D82-445A-8360-30E27FD7403C}
{530CD64A-0D82-445A-8360-30E27FD7403C} = {A60590C3-9800-4945-AC2B-F9770370F8FE}
{E362AA89-9793-4CC3-95FD-7EA3EAFE94B6} = {A60590C3-9800-4945-AC2B-F9770370F8FE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {56E821C2-FAE1-4C5F-8043-1A141A74F1FE}
Expand Down
41 changes: 0 additions & 41 deletions WindowsAuthTest/Program.cs

This file was deleted.

12 changes: 0 additions & 12 deletions WindowsAuthTest/WindowsAuthTest.csproj

This file was deleted.

0 comments on commit ff9e563

Please sign in to comment.