public dynamic Process(NancyModule nancyModule, AuthenticateCallbackData model) { Response response = nancyModule.Response.AsRedirect("~/"); if (nancyModule.IsAuthenticated()) { response = nancyModule.Response.AsRedirect("~/account/#identityProviders"); } if (model.Exception != null) { nancyModule.Request.AddAlertMessage("error", model.Exception.Message); } else { UserInformation information = model.AuthenticatedClient.UserInformation; var claims = new List<Claim>(); claims.Add(new Claim(ClaimTypes.NameIdentifier, information.Id)); claims.Add(new Claim(ClaimTypes.AuthenticationMethod, model.AuthenticatedClient.ProviderName)); if (!String.IsNullOrEmpty(information.UserName)) { claims.Add(new Claim(ClaimTypes.Name, information.UserName)); } if (!String.IsNullOrEmpty(information.Email)) { claims.Add(new Claim(ClaimTypes.Email, information.Email)); } nancyModule.SignIn(claims); } return response; }
public dynamic Process(NancyModule nancyModule, AuthenticateCallbackData model) { if (model.AuthenticatedClient == null) { model.AuthenticatedClient = new AuthenticatedClient("github") { AccessToken = "123", AccessTokenExpiresOn = DateTime.MinValue, UserInformation = new UserInformation() { Email = "*****@*****.**", Gender = GenderType.Unknown, Id = "123", Locale = "en-GB", Name = "Jonathan Channon", Picture = "https://secure.gravatar.com/avatar/62e4df82d52221751142c68ee5d2ae0b?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png", UserName = "******" } }; } if (!githubUserRepository.UserRegistered(model.AuthenticatedClient.AccessToken)) { githubUserRepository.AddOAuthToken(model.AuthenticatedClient.AccessToken, model.AuthenticatedClient.UserInformation.Email, model.AuthenticatedClient.UserInformation.UserName); } var githubUser = model.AuthenticatedClient.UserInformation.UserName; return nancyModule.Response.AsRedirect("/repos/#" + githubUser, RedirectResponse.RedirectType.Temporary); }
public WorldDominationAuthenticationModule(IAuthenticationService authenticationService, IAuthenticationCallbackProvider authenticationCallbackProvider) { Get["/authentication/redirect/{providerkey}"] = _ => { if (string.IsNullOrEmpty((string)_.providerkey)) { throw new ArgumentException( "You need to supply a valid provider key so we know where to redirect the user."); } var settings = authenticationService.GetAuthenticateServiceSettings((string)_.providerkey); var guidString = Guid.NewGuid().ToString(); Session[StateKey] = guidString; settings.State = guidString; settings.CallBackUri = GetReturnUrl("/authentication/authenticatecallback", (string)_.providerkey); Uri uri = authenticationService.RedirectToAuthenticationProvider(settings); return Response.AsRedirect(uri.AbsoluteUri); }; Get["/authentication/authenticatecallback"] = _ => { if (string.IsNullOrEmpty(Request.Query.providerkey)) { throw new ArgumentException("No provider key was supplied on the callback."); } var existingState = (Session[StateKey] as string) ?? string.Empty; var model = new AuthenticateCallbackData(); var querystringParameters = new NameValueCollection(); foreach (var item in Request.Query) { querystringParameters.Add(item, Request.Query[item]); } try { model.AuthenticatedClient = authenticationService.GetAuthenticatedClient((string) Request.Query.providerKey, querystringParameters, existingState); } catch (Exception exception) { model.Exception = exception; } return authenticationCallbackProvider.Process(this, model); }; }
public WorldDominationAuthenticationModule(IAuthenticationService authenticationService, IAuthenticationCallbackProvider authenticationCallbackProvider) { Get[RedirectRoute] = _ => { var providerKey = (string)_.providerkey; if (string.IsNullOrEmpty(providerKey)) { throw new ArgumentException( "You need to supply a valid provider key so we know where to redirect the user."); } // Kthxgo! return(RedirectToAuthenticationProvider(authenticationService, authenticationCallbackProvider, providerKey)); }; Post[RedirectRoute] = _ => { var providerKey = (string)_.providerkey; if (string.IsNullOrEmpty(providerKey)) { throw new ArgumentException( "You need to supply a valid provider key so we know where to redirect the user."); } Uri identifier = null; if (string.IsNullOrEmpty(Request.Form.Identifier) || !Uri.TryCreate(Request.Form.Identifier, UriKind.RelativeOrAbsolute, out identifier)) { throw new ArgumentException( "You need to POST the identifier to redirect the user. Eg. http://myopenid.com"); } return(RedirectToAuthenticationProvider(authenticationService, authenticationCallbackProvider, providerKey, identifier)); }; Get[CallbackRoute] = _ => { var providerKey = Request != null && Request.Query != null ? (string)Request.Query.providerkey : null; if (string.IsNullOrEmpty(providerKey)) { throw new ArgumentException("No provider key was supplied on the callback."); } var settings = authenticationService.GetAuthenticateServiceSettings(providerKey, Request.Url); settings.State = (Session[StateKey] as string) ?? string.Empty; var model = new AuthenticateCallbackData(); try { model.AuthenticatedClient = authenticationService.GetAuthenticatedClient(settings, Request.Query); Session.Delete(StateKey); // Clean up :) } catch (Exception exception) { model.Exception = exception; } var redirectUrl = Session[RedirectUrlKey] as string; if (!string.IsNullOrEmpty(redirectUrl)) { model.RedirectUrl = new Uri(redirectUrl); } return(authenticationCallbackProvider.Process(this, model)); }; }
public dynamic Process(NancyModule nancyModule, AuthenticateCallbackData model) { return nancyModule.Negotiate.WithView("AuthenticateCallback").WithModel(model); }
public dynamic Process(NancyModule nancyModule, AuthenticateCallbackData model) { ChatUser loggedInUser = null; if (nancyModule.Context.CurrentUser != null) { loggedInUser = _repository.GetUserById(nancyModule.Context.CurrentUser.UserName); } if (model.Exception == null) { UserInformation userInfo = model.AuthenticatedClient.UserInformation; string providerName = model.AuthenticatedClient.ProviderName; ChatUser user = _repository.GetUserByIdentity(providerName, userInfo.Id); // User with that identity doesn't exist, check if a user is logged in if (user == null) { if (loggedInUser != null) { // Link to the logged in user LinkIdentity(userInfo, providerName, loggedInUser); // If a user is already logged in, then we know they could only have gotten here via the account page, // so we will redirect them there nancyModule.AddAlertMessage("success", String.Format("Successfully linked {0} account.", providerName)); return nancyModule.Response.AsRedirect("~/account"); } else { // Check the identity field to see if we need to migrate this user to the new // non janrain identity model string legacyIdentity = IdentityUtility.MakeLegacyIdentity(providerName, userInfo.Id); if (legacyIdentity == null) { // No identity found so just add a new user user = _membershipService.AddUser(userInfo.UserName, providerName, userInfo.Id, userInfo.Email); } else { // Try to get a legacy identity user = _repository.GetUserByLegacyIdentity(legacyIdentity); if (user == null) { // User doesn't exist user = _membershipService.AddUser(userInfo.UserName, providerName, userInfo.Id, userInfo.Email); } else { // We found a legacy user via this id so convert them to the new format LinkIdentity(userInfo, providerName, user); } } } } else if (loggedInUser != null && user != loggedInUser) { // You can't link an account that's already attached to another user nancyModule.AddAlertMessage("error", String.Format("This {0} account has already been linked to another user.", providerName)); // If a user is logged in then we know they got here from the account page, and we should redirect them back there return nancyModule.Response.AsRedirect("~/account"); } return nancyModule.CompleteLogin(_authenticationTokenService, user); } nancyModule.AddAlertMessage("error", model.Exception.Message); // If a user is logged in, then they got here from the account page, send them back there if (loggedInUser != null) { return nancyModule.Response.AsRedirect("~/account"); } // At this point, send the user back to the root, everything else will work itself out return nancyModule.Response.AsRedirect("~/"); }
public WorldDominationAuthenticationModule(IAuthenticationService authenticationService, IAuthenticationCallbackProvider authenticationCallbackProvider) { Get[RedirectRoute] = _ => { var providerKey = (string)_.providerkey; if (string.IsNullOrEmpty(providerKey)) { throw new ArgumentException( "You need to supply a valid provider key so we know where to redirect the user."); } // Kthxgo! return RedirectToAuthenticationProvider(authenticationService, authenticationCallbackProvider, providerKey); }; Post[RedirectRoute] = _ => { var providerKey = (string)_.providerkey; if (string.IsNullOrEmpty(providerKey)) { throw new ArgumentException( "You need to supply a valid provider key so we know where to redirect the user."); } Uri identifier = null; if (string.IsNullOrEmpty(Request.Form.Identifier) || !Uri.TryCreate(Request.Form.Identifier, UriKind.RelativeOrAbsolute, out identifier)) { throw new ArgumentException( "You need to POST the identifier to redirect the user. Eg. http://myopenid.com"); } return RedirectToAuthenticationProvider(authenticationService, authenticationCallbackProvider, providerKey, identifier); }; Get[CallbackRoute] = _ => { var providerKey = Request != null && Request.Query != null ? (string)Request.Query.providerkey : null; if (string.IsNullOrEmpty(providerKey)) { throw new ArgumentException("No provider key was supplied on the callback."); } var settings = authenticationService.GetAuthenticateServiceSettings(providerKey, Request.Url); settings.State = (Session[StateKey] as string) ?? string.Empty; var model = new AuthenticateCallbackData(); try { model.AuthenticatedClient = authenticationService.GetAuthenticatedClient(settings, Request.Query); Session.Delete(StateKey); // Clean up :) } catch (Exception exception) { model.Exception = exception; } var redirectUrl = Session[RedirectUrlKey] as string; if (!string.IsNullOrEmpty(redirectUrl)) { model.RedirectUrl = new Uri(redirectUrl); } return authenticationCallbackProvider.Process(this, model); }; }