Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication provider #40

Merged
merged 7 commits into from
Jan 2, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 59 additions & 0 deletions Origam.Service.Core/Extensions/IConfigurationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#region license
/*
Copyright 2005 - 2024 Advantage Solutions, s. r. o.

This file is part of ORIGAM (http://www.origam.org).

ORIGAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ORIGAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ORIGAM. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

using System;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's missing license info.

using Microsoft.Extensions.Configuration;

namespace Origam.Extensions
{
public static class IConfigurationExtensions
{
public static IConfigurationSection GetSectionOrThrow(this IConfiguration configuration, string key)
{
var section = configuration.GetSection(key);
if (!section.Exists())
{
throw new ArgumentException($"Section \"{key}\" was not found in configuration. Check your appsettings.json");
}
return section;
}

public static string[] GetStringArrayOrThrow(this IConfiguration section)
{
string[] stringArray = section.Get<string[]>();
if (stringArray == null || stringArray.Length == 0)
{
throw new ArgumentException($"String array in section \"{section}\" was not found in configuration or was empty. Check your appsettings.json");
}
return stringArray;
}

public static string[] GetStringArrayOrEmpty(this IConfiguration section)
{
string[] stringArray = section.Get<string[]>();
if (stringArray == null || stringArray.Length == 0)
{
return Array.Empty<string>();
}
return stringArray;
}
}
}
73 changes: 73 additions & 0 deletions Origam.Service.Core/Extensions/IConfigurationSectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#region license

/*
Copyright 2005 - 2024 Advantage Solutions, s. r. o.

This file is part of ORIGAM (http://www.origam.org).

ORIGAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ORIGAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ORIGAM. If not, see <http://www.gnu.org/licenses/>.
*/

#endregion

using System;
using Microsoft.Extensions.Configuration;

namespace Origam.Extensions
{
public static class IConfigurationSectionExtensions
{
public static string GetStringOrThrow(this IConfigurationSection section, string key)
{
string stringValue = section[key];
if (stringValue == null)
{
throw new ArgumentException($"Key \"{key}\" was not found in configuration in section \"{section.Path}\". Check your appsettings.json");
}
return stringValue;
}

public static bool GetBoolOrThrow(this IConfigurationSection section, string key)
{
string stringValue = section.GetStringOrThrow(key);
bool success = Boolean.TryParse(stringValue, out bool boolValue);
if (!success)
{
throw new ArgumentException($"Cannot parse \"{key}\" to bool");
}
return boolValue;
}
public static int GetIntOrThrow(this IConfigurationSection section, string key)
{
string stringValue = section.GetStringOrThrow(key);
bool success = int.TryParse(stringValue, out int intValue);
if (!success)
{
throw new ArgumentException($"Cannot parse \"{key}\" to int");
}
return intValue;
}

public static IConfigurationSection GetSectionOrThrow(this IConfigurationSection section, string key)
{
var subSection = section.GetSection(key);
if (!subSection.Exists())
{
throw new ArgumentException($"Sub section \"{key}\" was not found in \"{section.Path}\" or it was empty. Check your appsettings.json");
}
return subSection;
}

}
}
38 changes: 38 additions & 0 deletions Origam.Service.Core/IClientAuthenticationProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#region license
/*
Copyright 2005 - 2023 Advantage Solutions, s. r. o.

This file is part of ORIGAM (http://www.origam.org).

ORIGAM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ORIGAM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ORIGAM. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

using System.Collections;
using Microsoft.Extensions.Configuration;

namespace Origam.Service.Core;

// Interface for authentication providers that can authenticate requests outgoing to external services.
public interface IClientAuthenticationProvider
{
// Implementations should return false if they cannot authenticate request to the url.
// If the request can be authenticated the headers should be modified accordingly.
bool TryAuthenticate(string url, Hashtable headers);

// Receives IConfiguration parsed from the appsettings. There are no constraints on what should be read
// from the configuration. Write what you need to the appsettings.json and read it here.
// The configuration section for a class implementing this interface should have the same name as the class.
void Configure(IConfiguration configuration);
}
9 changes: 5 additions & 4 deletions Origam.Service.Core/Origam.Service.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
<PackageLicenseUrl></PackageLicenseUrl>
<RepositoryUrl>https://github.com/origam/service-core</RepositoryUrl>
<PackageTags>origam</PackageTags>
<AssemblyVersion>2.1.4</AssemblyVersion>
<FileVersion>2.1.4</FileVersion>
<AssemblyVersion>2.2.2</AssemblyVersion>
<FileVersion>2.2.2</FileVersion>
<PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression>
<PackageIcon>origam-icon-large.png</PackageIcon>
<SignAssembly>false</SignAssembly>
<Authors>origam</Authors>
<PackageVersion>2.1.4</PackageVersion>
<Version>2.1.4</Version>
<PackageVersion>2.2.2</PackageVersion>
<Version>2.2.2</Version>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>10</LangVersion>
</PropertyGroup>
Expand Down Expand Up @@ -45,6 +45,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
</ItemGroup>

</Project>