Skip to content

Commit

Permalink
Merge pull request #5 from Talegen/develop
Browse files Browse the repository at this point in the history
Added additional ToAddress extension method for string claim values.
  • Loading branch information
RobK410 authored May 5, 2021
2 parents 2106071 + 58e5a01 commit d3772c3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
45 changes: 29 additions & 16 deletions src/Extensions/ContactExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static class ContactExtensions
/// This extension method converts a JWT address claim to a <see cref="Address" /> model object.
/// </summary>
/// <param name="claim">The claim containing the address information.</param>
/// <returns></returns>
/// <returns>Returns a new Address model containing found claim address details.</returns>
/// <exception cref="ArgumentNullException">Exception is thrown if <paramref name="claim" /> is null.</exception>
public static Address ToAddress(this Claim claim)
{
Expand All @@ -50,23 +50,36 @@ public static Address ToAddress(this Claim claim)
// is a JWT address claim
if (claim.Type == JwtAddressClaimType && !string.IsNullOrWhiteSpace(claim.Value))
{
JwtAddressModel jwtModel = JsonConvert.DeserializeObject<JwtAddressModel>(claim.Value);
result = claim.Value.ToAddress();
}

if (jwtModel != null)
{
string[] streetLines = jwtModel.StreetAddress.Split('\n');
return result;
}

result = new Address
{
Street1 = streetLines != null ? streetLines[0].Replace("\r", string.Empty).Replace("\n", string.Empty) : string.Empty,
Street2 = streetLines != null && streetLines.Length > 1 ? streetLines[1].Replace("\r", string.Empty).Replace("\n", string.Empty) : string.Empty,
City = jwtModel.Locality,
Country = jwtModel.Country,
PostalCode = jwtModel.PostalCode,
RegionState = jwtModel.Region,
Formatted = jwtModel.Formatted
};
}
/// <summary>
/// This extension method converts a JWT address claim to a <see cref="Address" /> model object.
/// </summary>
/// <param name="claimValue">The claim value containing the address information.</param>
/// <returns>Returns a new Address model containing found claim address details.</returns>
public static Address ToAddress(this string claimValue)
{
Address result = null;
JwtAddressModel jwtModel = JsonConvert.DeserializeObject<JwtAddressModel>(claimValue);

if (jwtModel != null)
{
string[] streetLines = jwtModel.StreetAddress.Split('\n');

result = new Address
{
Street1 = streetLines != null ? streetLines[0].Replace("\r", string.Empty).Replace("\n", string.Empty) : string.Empty,
Street2 = streetLines != null && streetLines.Length > 1 ? streetLines[1].Replace("\r", string.Empty).Replace("\n", string.Empty) : string.Empty,
City = jwtModel.Locality,
Country = jwtModel.Country,
PostalCode = jwtModel.PostalCode,
RegionState = jwtModel.Region,
Formatted = jwtModel.Formatted
};
}

return result;
Expand Down
4 changes: 2 additions & 2 deletions src/Talegen.Common.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<RepositoryUrl>https://github.com/Talegen/Talegen.Common.Models</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>Models</PackageTags>
<PackageReleaseNotes>Added additional modelling and extensions for address models.</PackageReleaseNotes>
<PackageReleaseNotes>Added additional ToAddress extension method for string claim values.</PackageReleaseNotes>
<ApplicationIcon>Assets\logo.ico</ApplicationIcon>
<SignAssembly>false</SignAssembly>
<Version>1.0.3.0</Version>
<Version>1.0.4.0</Version>
<NeutralLanguage>en</NeutralLanguage>
</PropertyGroup>

Expand Down

0 comments on commit d3772c3

Please sign in to comment.