예제 #1
0
        /// <summary>
        /// Looks for Simple Registration and Attribute Exchange (all known formats)
        /// request extensions and returns them as a Simple Registration extension,
        /// and adds the new extension to the original request message if it was absent.
        /// </summary>
        /// <param name="request">The authentication request.</param>
        /// <returns>
        /// The Simple Registration request if found,
        /// or a fabricated one based on the Attribute Exchange extension if found,
        /// or <c>null</c> if no attribute extension request is found.</returns>
        internal static ClaimsRequest UnifyExtensionsAsSreg(this Provider.IHostProcessedRequest request)
        {
            Requires.NotNull(request, "request");

            var req  = (Provider.HostProcessedRequest)request;
            var sreg = req.GetExtension <ClaimsRequest>();

            if (sreg != null)
            {
                return(sreg);
            }

            var ax = req.GetExtension <FetchRequest>();

            if (ax != null)
            {
                sreg             = new ClaimsRequest(DotNetOpenAuth.OpenId.Extensions.SimpleRegistration.Constants.TypeUris.Standard);
                sreg.Synthesized = true;
                ((IProtocolMessageWithExtensions)req.RequestMessage).Extensions.Add(sreg);
                sreg.BirthDate  = GetDemandLevelFor(ax, WellKnownAttributes.BirthDate.WholeBirthDate);
                sreg.Country    = GetDemandLevelFor(ax, WellKnownAttributes.Contact.HomeAddress.Country);
                sreg.Email      = GetDemandLevelFor(ax, WellKnownAttributes.Contact.Email);
                sreg.FullName   = GetDemandLevelFor(ax, WellKnownAttributes.Name.FullName);
                sreg.Gender     = GetDemandLevelFor(ax, WellKnownAttributes.Person.Gender);
                sreg.Language   = GetDemandLevelFor(ax, WellKnownAttributes.Preferences.Language);
                sreg.Nickname   = GetDemandLevelFor(ax, WellKnownAttributes.Name.Alias);
                sreg.PostalCode = GetDemandLevelFor(ax, WellKnownAttributes.Contact.HomeAddress.PostalCode);
                sreg.TimeZone   = GetDemandLevelFor(ax, WellKnownAttributes.Preferences.TimeZone);
            }

            return(sreg);
        }
예제 #2
0
        /// <summary>
        /// Converts the Simple Registration extension response to whatever format the original
        /// attribute request extension came in.
        /// </summary>
        /// <param name="request">The authentication request with the response extensions already added.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>
        /// A task that completes with the asynchronous operation.
        /// </returns>
        /// <remarks>
        /// If the original attribute request came in as AX, the Simple Registration extension is converted
        /// to an AX response and then the Simple Registration extension is removed from the response.
        /// </remarks>
        internal static async Task ConvertSregToMatchRequestAsync(this Provider.IHostProcessedRequest request, CancellationToken cancellationToken)
        {
            var req             = (Provider.HostProcessedRequest)request;
            var protocolMessage = await req.GetResponseAsync(cancellationToken);

            var response    = protocolMessage as IProtocolMessageWithExtensions;          // negative responses don't support extensions.
            var sregRequest = request.GetExtension <ClaimsRequest>();

            if (sregRequest != null && response != null)
            {
                if (sregRequest.Synthesized)
                {
                    var axRequest = request.GetExtension <FetchRequest>();
                    ErrorUtilities.VerifyInternal(axRequest != null, "How do we have a synthesized Sreg request without an AX request?");

                    var sregResponse = response.Extensions.OfType <ClaimsResponse>().SingleOrDefault();
                    if (sregResponse == null)
                    {
                        // No Sreg response to copy from.
                        return;
                    }

                    // Remove the sreg response since the RP didn't ask for it.
                    response.Extensions.Remove(sregResponse);

                    AXAttributeFormats format = OpenIdExtensionsInteropHelper.DetectAXFormat(axRequest.Attributes.Select(att => att.TypeUri));
                    if (format == AXAttributeFormats.None)
                    {
                        // No recognized AX attributes were requested.
                        return;
                    }

                    var axResponse = response.Extensions.OfType <FetchResponse>().SingleOrDefault();
                    if (axResponse == null)
                    {
                        axResponse = new FetchResponse();
                        response.Extensions.Add(axResponse);
                    }

                    AddAXAttributeValue(axResponse, WellKnownAttributes.BirthDate.WholeBirthDate, format, sregResponse.BirthDateRaw);
                    AddAXAttributeValue(axResponse, WellKnownAttributes.Contact.HomeAddress.Country, format, sregResponse.Country);
                    AddAXAttributeValue(axResponse, WellKnownAttributes.Contact.HomeAddress.PostalCode, format, sregResponse.PostalCode);
                    AddAXAttributeValue(axResponse, WellKnownAttributes.Contact.Email, format, sregResponse.Email);
                    AddAXAttributeValue(axResponse, WellKnownAttributes.Name.FullName, format, sregResponse.FullName);
                    AddAXAttributeValue(axResponse, WellKnownAttributes.Name.Alias, format, sregResponse.Nickname);
                    AddAXAttributeValue(axResponse, WellKnownAttributes.Preferences.TimeZone, format, sregResponse.TimeZone);
                    AddAXAttributeValue(axResponse, WellKnownAttributes.Preferences.Language, format, sregResponse.Language);
                    if (sregResponse.Gender.HasValue)
                    {
                        AddAXAttributeValue(axResponse, WellKnownAttributes.Person.Gender, format, OpenIdExtensionsInteropHelper.GenderEncoder.Encode(sregResponse.Gender));
                    }
                }
            }
        }