//----------------------------------------------------------------------- // // Copyright (c) Andrew Arnott. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOpenAuth.OpenId.Extensions { using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Linq; using DotNetOpenAuth.Messaging; using DotNetOpenAuth.OpenId.Extensions.AttributeExchange; using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration; using DotNetOpenAuth.OpenId.Messages; /// /// A set of methods designed to assist in improving interop across different /// OpenID implementations and their extensions. /// internal static class OpenIdExtensionsInteropHelper { /// /// The gender decoder to translate AX genders to Sreg. /// private static GenderEncoder genderEncoder = new GenderEncoder(); /// /// Gets the gender decoder to translate AX genders to Sreg. /// internal static GenderEncoder GenderEncoder { get { return genderEncoder; } } /// /// Splits the AX attribute format flags into individual values for processing. /// /// The formats to split up into individual flags. /// A sequence of individual flags. internal static IEnumerable ForEachFormat(AXAttributeFormats formats) { if ((formats & AXAttributeFormats.AXSchemaOrg) != 0) { yield return AXAttributeFormats.AXSchemaOrg; } if ((formats & AXAttributeFormats.OpenIdNetSchema) != 0) { yield return AXAttributeFormats.OpenIdNetSchema; } if ((formats & AXAttributeFormats.SchemaOpenIdNet) != 0) { yield return AXAttributeFormats.SchemaOpenIdNet; } } /// /// Transforms an AX attribute type URI from the axschema.org format into a given format. /// /// The ax schema org format type URI. /// The target format. Only one flag should be set. /// The AX attribute type URI in the target format. internal static string TransformAXFormat(string axSchemaOrgFormatTypeUri, AXAttributeFormats targetFormat) { Requires.NotNullOrEmpty(axSchemaOrgFormatTypeUri, "axSchemaOrgFormatTypeUri"); switch (targetFormat) { case AXAttributeFormats.AXSchemaOrg: return axSchemaOrgFormatTypeUri; case AXAttributeFormats.SchemaOpenIdNet: return axSchemaOrgFormatTypeUri.Replace("axschema.org", "schema.openid.net"); case AXAttributeFormats.OpenIdNetSchema: return axSchemaOrgFormatTypeUri.Replace("axschema.org", "openid.net/schema"); default: throw new ArgumentOutOfRangeException("targetFormat"); } } /// /// Detects the AX attribute type URI format from a given sample. /// /// The type URIs to scan for recognized formats. /// The first AX type URI format recognized in the list. internal static AXAttributeFormats DetectAXFormat(IEnumerable typeURIs) { Requires.NotNull(typeURIs, "typeURIs"); if (typeURIs.Any(uri => uri.StartsWith("http://axschema.org/", StringComparison.Ordinal))) { return AXAttributeFormats.AXSchemaOrg; } if (typeURIs.Any(uri => uri.StartsWith("http://schema.openid.net/", StringComparison.Ordinal))) { return AXAttributeFormats.SchemaOpenIdNet; } if (typeURIs.Any(uri => uri.StartsWith("http://openid.net/schema/", StringComparison.Ordinal))) { return AXAttributeFormats.OpenIdNetSchema; } return AXAttributeFormats.None; } /// /// Adds an attribute fetch request if it is not already present in the AX request. /// /// The AX request to add the attribute request to. /// The format of the attribute's Type URI to use. /// The attribute in axschema.org format. /// The demand level. internal static void FetchAttribute(FetchRequest ax, AXAttributeFormats format, string axSchemaOrgFormatAttribute, DemandLevel demandLevel) { Requires.NotNull(ax, "ax"); Requires.NotNullOrEmpty(axSchemaOrgFormatAttribute, "axSchemaOrgFormatAttribute"); string typeUri = TransformAXFormat(axSchemaOrgFormatAttribute, format); if (!ax.Attributes.Contains(typeUri)) { switch (demandLevel) { case DemandLevel.Request: ax.Attributes.AddOptional(typeUri); break; case DemandLevel.Require: ax.Attributes.AddRequired(typeUri); break; default: break; } } } } }