Created
February 1, 2013 23:40
-
-
Save Buildstarted/4694958 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace WorldDomination.Web.Authentication.OpenId | |
| { | |
| using System; | |
| using System.Collections.Specialized; | |
| using DotNetOpenAuth.Messaging; | |
| using DotNetOpenAuth.OpenId; | |
| using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration; | |
| using DotNetOpenAuth.OpenId.RelyingParty; | |
| /// <summary> | |
| /// OpenId Provider for World Domination | |
| /// </summary> | |
| public class OpenIdProvider : IAuthenticationProvider | |
| { | |
| static readonly OpenIdRelyingParty OpenId = new OpenIdRelyingParty(new StandardRelyingPartyApplicationStore()); | |
| public string Name { get { return "OpenId"; } } | |
| public Uri CallBackUri { get; private set; } | |
| public IAuthenticationServiceSettings DefaultAuthenticationServiceSettings { get { return new OpenIdAuthenticationServiceSettings(); } } | |
| //as this is an alternateidentifier request | |
| //we need to identifier they're attempting to authenticate | |
| protected virtual string Identifier(IAlternateIdentifier settings) | |
| { | |
| return settings.Identifier; | |
| } | |
| public Uri RedirectToAuthenticate(IAuthenticationServiceSettings authenticationServiceSettings) | |
| { | |
| IAlternateIdentifier settings; | |
| if ((settings = authenticationServiceSettings as IAlternateIdentifier) == null) | |
| { | |
| throw new ArgumentNullException("authenticationServiceSettings"); | |
| } | |
| try | |
| { | |
| //local server | |
| string realm = "http://" + settings.CallBackUri.Authority; | |
| Identifier identifier; | |
| if (DotNetOpenAuth.OpenId.Identifier.TryParse(Identifier(settings), out identifier)) | |
| { | |
| //create the openid request | |
| //and add request for personal information | |
| var request = OpenId.CreateRequest(identifier, new Realm(realm), settings.CallBackUri); | |
| request.AddExtension(new ClaimsRequest | |
| { | |
| FullName = DemandLevel.Request, | |
| Language = DemandLevel.Request, | |
| Country = DemandLevel.Request, | |
| Email = DemandLevel.Request, | |
| Gender = DemandLevel.Request, | |
| }); | |
| var response = request.RedirectingResponse; | |
| //grab the redirect from the headers of the response | |
| //eg: https://www.myopenid.com/server?openid.claimed_id=<identifier>%2F&openid.identity=<server>%2F&openid.assoc_handle=%7BHMAC-SHA256%7D%7B5109e243%7D%7BBMe45Q%3D%3D%7D&openid.return_to=http%3A%2F%2Flocalhost%3A6969%2FOpenId%2FAuthenticateCallback%3FproviderKey%3Dopenid%26dnoa.userSuppliedIdentifier%3Dhttp%253A%252F%252Fbendornis.com%252F&openid.realm=http%3A%2F%2Flocalhost%3A6969%2F&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.sreg=http%3A%2F%2Fopenid.net%2Fextensions%2Fsreg%2F1.1&openid.sreg.required=&openid.sreg.optional=email%2Cfullname%2Cgender%2Ccountry%2Clanguage&no_ssl=true | |
| return new Uri(response.Headers["Location"]); | |
| } | |
| else | |
| { | |
| throw new ProtocolException(); | |
| } | |
| } | |
| catch (ProtocolException ex) | |
| { | |
| throw new InvalidOperationException(); | |
| } | |
| } | |
| public IAuthenticatedClient AuthenticateClient(NameValueCollection parameters, string existingState) | |
| { | |
| //use openid to parse the response into something usable | |
| var response = OpenId.GetResponse(); | |
| //we requested personal info, attempt to retrieve that | |
| var userInfo = GetUserInfo(response); | |
| //return the new authenticated client | |
| //by default the openid provider doesn't | |
| //have a max value for the access token | |
| //expiration. Not sure if this even exists | |
| return new AuthenticatedClient(Name.ToLowerInvariant()) | |
| { | |
| AccessToken = response.ClaimedIdentifier, | |
| AccessTokenExpiresOn = DateTime.MaxValue, | |
| UserInformation = userInfo | |
| }; | |
| } | |
| private UserInformation GetUserInfo(IAuthenticationResponse response) | |
| { | |
| var fetch = response.GetExtension<ClaimsResponse>(); | |
| if (fetch != null) | |
| { | |
| return new UserInformation() | |
| { | |
| Email = fetch.Email, | |
| Gender = (GenderType)Enum.Parse(typeof(GenderType), fetch.Gender.GetValueOrDefault().ToString(), true), | |
| Locale = fetch.Language, | |
| Name = fetch.FullName, | |
| Picture = null, | |
| UserName = fetch.Nickname | |
| }; | |
| } | |
| return new UserInformation(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment