owin-middleware/OwinOAuthProviders — pull request #11 by tomasherceg (2014-05-01 12:29:17+00:00)
Title: OpenID - generic protocol extension infrastructure & Simple Registration Extension implementation

Some OpenID providers use _Simple Registration Extension_ to provide additional user info (full name, e-mail etc.) instead of _Attribute Exchange Extension_ that is built in the providers.
I have added some extension points in the OpenID handler to be able to write an extension that implements _Simple Registration Extension_ (http://openid.net/specs/openid-simple-registration-extension-1_0.html).
The usage is pretty simple:
```C#
app.UseOpenIDAuthentication(new OpenIDAuthenticationOptions()
{
...,
ProtocolExtensions =
{
new OpenIDSimpleRegistrationExtension()
{
RequiredFields = ..., // default value is "fullname" and "email"
OptionalFields = ...,
PolicyUrl = ...
}
},
Provider = new OpenIDAuthenticationProvider()
{
OnAuthenticated = async context =>
{
...
// read the simple registration attributes
var sregResults = context.GetSimpleRegistrationResult();
context.Identity.AddClaim(new Claim("Name", sregResults.GetFieldValue(OpenIDSimpleRegistrationField.FullName)));
context.Identity.AddClaim(new Claim("Email", sregResults.GetFieldValue(OpenIDSimpleRegistrationField.Email)));
...
}
}
});
```
There is an interface `IOpenIDProtocolExtension` which can be used to write any protocol extension - there are 3 extension points:
* `OnChallengeAsync` - allows to append some URL parameters to the security endpoint URL where the user is redirected during the challenge
* `OnValidateMessageAsync` - allows to add custom message validation logic that is applied when the user is redirected from the login page back to the application
* `OnExtractResultsAsync` - allows the extension to read the response message and extract the information into a custom object - the object is stored in OpenIDAuthenticationContext's `ProtocolExtensionData` collection. An extension method can be added to provide more convenient use, like I did with `GetSimpleRegistrationResult` method.