initial commit

This commit is contained in:
Frede Hundewadt 2022-12-14 12:01:50 +01:00
parent b914d50727
commit be3cd1ac85
13 changed files with 563 additions and 0 deletions

58
AzureAuthStore.cs Normal file
View file

@ -0,0 +1,58 @@
// ***********************************************************************
// Assembly : FCS.Lib.Azure
// Author : FH
// Created : 05-10-2022
//
// Last Modified By : FH
// Last Modified On : 05-10-2022
// ***********************************************************************
// <copyright file="AzureAuthStore.cs" company="FCS-TECH">
// Copyright (C) 2022 FCS Frede's Computer Services.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see [https://www.gnu.org/licenses]
// </copyright>
// <summary></summary>
// ***********************************************************************
namespace FCS.Lib.Azure
{
public class AzureAuthStore
{
public AzureAuthStore(
string azureLoginUrl, string azureOAuthEndpoint, string azureTenantId,
string azureClientId, string azureGrantType, string azureSecret,
string azureLoginScope)
{
AzureLoginUrl = azureLoginUrl;
AzureOAuthEndpoint = azureOAuthEndpoint;
AzureTenantId = azureTenantId;
AzureClientId = azureClientId;
AzureGrantType = azureGrantType;
AzureSecret = azureSecret;
AzureLoginScope = azureLoginScope;
}
protected string AzureLoginUrl { get; set; }
protected string AzureOAuthEndpoint { get; set; }
protected string AzureTenantId { get;}
public string AzureClientId { get;}
public string AzureGrantType { get;}
public string AzureSecret { get; }
public string AzureLoginScope { get; }
public string AzureTokenEndpoint()
{
return $"{AzureLoginUrl}/{AzureTenantId}/{AzureOAuthEndpoint}";
}
}
}

39
AzureToken.cs Normal file
View file

@ -0,0 +1,39 @@
// ***********************************************************************
// Assembly : FCS.Lib.Azure
// Author : FH
// Created : 05-10-2022
//
// Last Modified By : FH
// Last Modified On : 05-10-2022
// ***********************************************************************
// <copyright file="AzureToken.cs" company="FCS-TECH">
// Copyright (C) 2022 FCS Frede's Computer Services.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see [https://www.gnu.org/licenses]
// </copyright>
// <summary></summary>
// ***********************************************************************
namespace FCS.Lib.Azure
{
public class AzureToken
{
public string TokenType { get; set; } = "";
public string AccessToken { get; set; } = "";
public long Expires { get; set; }
public bool HasExpired(long timestamp)
{
return timestamp > Expires;
}
}
}

40
AzureTokenDto.cs Normal file
View file

@ -0,0 +1,40 @@
// ***********************************************************************
// Assembly : FCS.Lib.Azure
// Author : FH
// Created : 05-08-2022
//
// Last Modified By : FH
// Last Modified On : 05-08-2022
// ***********************************************************************
// <copyright file="AzureTokenDto.cs" company="FCS-TECH">
// Copyright (C) 2022 FCS Frede's Computer Services.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see [https://www.gnu.org/licenses]
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Text.Json.Serialization;
namespace FCS.Lib.Azure
{
public class AzureTokenDto
{
[JsonPropertyName("token_type")] public string TokenType { get; set; }
[JsonPropertyName("expires_in")] public long ExpiresIn { get; set; }
[JsonPropertyName("ext_expires_in")] public long ExtExpiresIn { get; set; }
[JsonPropertyName("access_token")] public string AccessToken { get; set; }
}
}

45
AzureTokenFetcher.cs Normal file
View file

@ -0,0 +1,45 @@
// ***********************************************************************
// Assembly : FCS.Lib.Azure
// Author : FH
// Created : 05-10-2022
//
// Last Modified By : FH
// Last Modified On : 05-10-2022
// ***********************************************************************
// <copyright file="AzureTokenFetcher.cs" company="FCS-TECH">
// Copyright (C) 2022 FCS Frede's Computer Services.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see [https://www.gnu.org/licenses]
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Threading.Tasks;
namespace FCS.Lib.Azure
{
public class AzureTokenFetcher : IAzureTokenFetcher
{
private readonly AzureAuthStore _config;
public AzureTokenFetcher(AzureAuthStore config)
{
_config = config;
}
public async Task<AzureToken> FetchAzureToken()
{
var result = await AzureTokenHttpRequest.RequestTokenAsync(_config).ConfigureAwait(true);
return !result.IsSuccessStatusCode ? new AzureToken{Expires = -1} : new AzureTokenMapper().MapAzureToken(result.Message);
}
}
}

61
AzureTokenHttpRequest.cs Normal file
View file

@ -0,0 +1,61 @@
// ***********************************************************************
// Assembly : FCS.Lib.Azure
// Author : FH
// Created : 05-10-2022
//
// Last Modified By : FH
// Last Modified On : 05-10-2022
// ***********************************************************************
// <copyright file="AzureTokenHttpRequest.cs" company="FCS-TECH">
// Copyright (C) 2022 FCS Frede's Computer Services.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see [https://www.gnu.org/licenses]
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace FCS.Lib.Azure
{
public class AzureTokenHttpRequest
{
public static async Task<ResponseView> RequestTokenAsync(AzureAuthStore azureAuth)
{
var credentials = new Dictionary<string, string>
{
{ "grant_type", azureAuth.AzureGrantType },
{ "client_id", azureAuth.AzureClientId },
{ "client_secret", azureAuth.AzureSecret },
{ "scope", azureAuth.AzureLoginScope}
};
using var client = new HttpClient();
var content = new FormUrlEncodedContent(credentials);
var responseMessage = await client.PostAsync(azureAuth.AzureTokenEndpoint(), content).ConfigureAwait(true);
var azureResponse = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(true);
return new ResponseView
{
Code = responseMessage.StatusCode,
IsSuccessStatusCode = responseMessage.IsSuccessStatusCode,
Message = azureResponse
};
}
}
}

50
AzureTokenMapper.cs Normal file
View file

@ -0,0 +1,50 @@
// ***********************************************************************
// Assembly : FCS.Lib.Azure
// Author : FH
// Created : 05-08-2022
//
// Last Modified By : FH
// Last Modified On : 05-10-2022
// ***********************************************************************
// <copyright file="AzureTokenMapper.cs" company="FCS-TECH">
// Copyright (C) 2022 FCS Frede's Computer Services.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see [https://www.gnu.org/licenses]
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Text.Json;
using FCS.Lib.Utility;
namespace FCS.Lib.Azure
{
public class AzureTokenMapper
{
public AzureToken MapAzureToken(string json)
{
if(string.IsNullOrWhiteSpace(json))
throw new ArgumentNullException(nameof(json));
var token = JsonSerializer.Deserialize<AzureTokenDto>(json);
return token == null ? null : new AzureToken
{
AccessToken = token.AccessToken,
Expires = Mogrify.CurrentDateTimeToTimeStamp() + token.ExtExpiresIn - 600,
TokenType = token.TokenType
};
}
}
}

77
FCS.Lib.Azure.csproj Normal file
View file

@ -0,0 +1,77 @@
<?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')" />
<PropertyGroup>
<LangVersion>10.0</LangVersion>
</PropertyGroup>
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{63F067FE-925B-4076-86F5-F630F3E5A75B}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>FCS.Lib.Azure</RootNamespace>
<AssemblyName>FCS.Lib.Azure</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</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>
</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>
<Compile Include="ResponseView.cs" />
<Compile Include="AzureToken.cs" />
<Compile Include="AzureTokenDto.cs" />
<Compile Include="AzureTokenFetcher.cs" />
<Compile Include="AzureTokenHttpRequest.cs" />
<Compile Include="AzureTokenMapper.cs" />
<Compile Include="IAzureTokenFetcher.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="AzureAuthStore.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\fcs-utils\FCS.Lib.Utility.csproj">
<Project>{1F1FECFD-E07E-4B5C-A7F9-67FB89EE94A3}</Project>
<Name>FCS.Lib.Utility</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL" />
<Reference Include="System.Net.Http">
<HintPath>..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\System.Net.Http.dll</HintPath>
</Reference>
<Reference Include="System.Text.Json">
<HintPath>..\..\inno\inno.api.v2\packages\System.Text.Json.6.0.4\lib\net461\System.Text.Json.dll</HintPath>
</Reference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\System.Text.Json.6.0.4\build\System.Text.Json.targets" Condition="Exists('..\packages\System.Text.Json.6.0.4\build\System.Text.Json.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\System.Text.Json.6.0.4\build\System.Text.Json.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\System.Text.Json.6.0.4\build\System.Text.Json.targets'))" />
</Target>
</Project>

35
IAzureTokenFetcher.cs Normal file
View file

@ -0,0 +1,35 @@
// ***********************************************************************
// Assembly : FCS.Lib.Azure
// Author : FH
// Created : 05-10-2022
//
// Last Modified By : FH
// Last Modified On : 05-10-2022
// ***********************************************************************
// <copyright file="IAzureTokenFetcher.cs" company="FCS-TECH">
// Copyright (C) 2022 FCS Frede's Computer Services.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see [https://www.gnu.org/licenses]
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Threading.Tasks;
namespace FCS.Lib.Azure
{
public interface IAzureTokenFetcher
{
Task<AzureToken> FetchAzureToken();
}
}

View file

@ -0,0 +1,35 @@
using System.Reflection;
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("FCS.Lib.Azure")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FCS.Lib.Azure")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 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: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("63f067fe-925b-4076-86f5-f630f3e5a75b")]
// 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")]

View file

@ -1,2 +1,43 @@
# fcs-azure
Library to aquire Azure access token
Sample call
```
public class Program
{
static void Main(string[] args)
{
var settings = new MySettings();
var authStore = new AzureAuthStore(settings.LoginUrl, settings.OAuthEndpoint, settings.TenantId,
settings.ClientId, settings.GrantType, settings.ClientSecret, settings.LoginScope);
var tokenFetcher = new AzureTokenFetcher(authStore);
// normally called async - but this is only a test
var token = tokenFetcher.FetchAzureToken().Result;
var ts1 = Mogrify.CurrentDateTimeToTimeStamp();
var ts2 = Mogrify.DateTimeToTimeStamp(DateTime.Now.AddHours(+2));
Console.WriteLine($"AccessToken: {token.AccessToken}");
Console.WriteLine($"Expires : {token.Expires}");
Console.WriteLine($"TokenType : {token.TokenType}");
Console.WriteLine($"HasExpired : {DateTime.Now} {ts1} {token.HasExpired(ts1)}");
Console.WriteLine($"HasExpired : {DateTime.Now.AddHours(+2)} {ts2} {token.HasExpired(ts2)}");
Console.ReadKey();
}
}
internal sealed class MySettings
{
public string LoginUrl => "https://login.microsoftonline.com";
public string OAuthEndpoint => "oauth2/v2.0/token";
public string GrantType => "client_credentials";
public string LoginScope => "";
public string TenantId => "";
public string ClientId => "";
public string ClientSecret => "";
}
```

37
ResponseView.cs Normal file
View file

@ -0,0 +1,37 @@
// ***********************************************************************
// Assembly : Inno.Views
// Author : FH
// Created : 01-01-2022
//
// Last Modified By : FH
// Last Modified On : 04-18-2022
// ***********************************************************************
// <copyright file="ResponseView.cs" company="FCS-TECH">
// Copyright (C) 2022 FCS Frede's Computer Services.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see [https://www.gnu.org/licenses]
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Net;
namespace FCS.Lib.Azure
{
public class ResponseView
{
public HttpStatusCode Code { get; set; }
public bool IsSuccessStatusCode { get; set; }
public string Message { get; set; } = "";
}
}

19
app.config Normal file
View file

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.4" newVersion="6.0.0.4" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Extensions.Configuration" publicKeyToken="adb9793829ddae60" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.1" newVersion="6.0.0.1" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

26
packages.config Normal file
View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Bcl.AsyncInterfaces" version="6.0.0" targetFramework="net48" />
<package id="Microsoft.Extensions.Configuration" version="6.0.1" targetFramework="net48" />
<package id="Microsoft.Extensions.Configuration.Abstractions" version="6.0.0" targetFramework="net48" />
<package id="Microsoft.Extensions.Configuration.FileExtensions" version="6.0.0" targetFramework="net48" />
<package id="Microsoft.Extensions.Configuration.Json" version="6.0.0" targetFramework="net48" />
<package id="Microsoft.Extensions.FileProviders.Abstractions" version="6.0.0" targetFramework="net48" />
<package id="Microsoft.Extensions.FileProviders.Physical" version="6.0.0" targetFramework="net48" />
<package id="Microsoft.Extensions.FileSystemGlobbing" version="6.0.0" targetFramework="net48" />
<package id="Microsoft.Extensions.Primitives" version="6.0.0" targetFramework="net48" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net48" />
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
<package id="System.IO" version="4.3.0" targetFramework="net48" />
<package id="System.Memory" version="4.5.5" targetFramework="net48" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
<package id="System.Runtime" version="4.3.1" targetFramework="net48" />
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net48" />
<package id="System.Security.Cryptography.Algorithms" version="4.3.1" targetFramework="net48" />
<package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net48" />
<package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net48" />
<package id="System.Text.Encodings.Web" version="6.0.0" targetFramework="net48" />
<package id="System.Text.Json" version="6.0.4" targetFramework="net48" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net48" />
</packages>