Example #1
0
        private dynamic RedirectToAuthenticationProvider(IAuthenticationService authenticationService,
                                                         IAuthenticationCallbackProvider authenticationCallbackProvider,
                                                         string providerKey, Uri identifier = null)
        {
            if (authenticationService == null)
            {
                throw new ArgumentNullException();
            }

            if (authenticationCallbackProvider == null)
            {
                throw new ArgumentNullException("authenticationCallbackProvider");
            }

            if (string.IsNullOrEmpty(providerKey))
            {
                throw new ArgumentNullException("providerKey");
            }

            // Grab the required Provider settings.

            var settings = authenticationService.GetAuthenticateServiceSettings(providerKey, Request.Url);

            // An OpenId specific settings provided?
            if (identifier != null &&
                settings is IOpenIdAuthenticationServiceSettings)
            {
                ((IOpenIdAuthenticationServiceSettings)settings).Identifier = identifier;
            }

            // Remember the State value (for CSRF protection).
            Session[StateKey] = settings.State;

            //// Convention: If no redirectUrl data has been provided, then default to the Referrer, if one exists.
            //if (RedirectUrl != null &&
            //    !string.IsNullOrEmpty(RedirectUrl.AbsoluteUri))
            //{
            //    // We have extra state information we will need to retrieve.
            //    Session[RedirectUrlKey] = RedirectUrl.AbsoluteUri;
            //}
            //else if (Request != null &&
            //    Request. != null &&
            //    !string.IsNullOrEmpty(Request.UrlReferrer.AbsoluteUri))
            //{
            //    Session[RedirectUrlKey] = Request.UrlReferrer.AbsoluteUri;
            //}

            // Determine the provider's end point Url we need to redirect to.
            // NOTE: It's possible we're trying to goto an OpenId endpoint. But the user has entered
            var uri = authenticationService.RedirectToAuthenticationProvider(settings);

            if (uri == null || string.IsNullOrEmpty(uri.AbsoluteUri))
            {
                return(authenticationCallbackProvider.OnRedirectToAuthenticationProviderError(this,
                                                                                              "No valid Uri was found - not sure where to redirect to?"));
            }

            // Kthxgo!
            return(Response.AsRedirect(uri.AbsoluteUri));
        }
Example #2
0
        public SimpleAuthenticationController(IAuthenticationCallbackProvider callbackProvider)
        {
            if (callbackProvider == null)
            {
                throw new ArgumentNullException("callbackProvider");
            }

            _callbackProvider = callbackProvider;
            _authenticationProviderFactory = new AuthenticationProviderFactory();
        }
Example #3
0
        public SimpleAuthenticationModule(IAuthenticationCallbackProvider callbackProvider)
        {
            _callbackProvider = callbackProvider;
            _authenticationProviderFactory = new AuthenticationProviderFactory();

            // Define the routes and how they are handled.
            Get[RedirectRoute]  = parameters => RedirectToProvider(parameters);
            Post[RedirectRoute] = parameters => RedirectToProvider(parameters);
            Get[CallbackRoute]  = parameters => AuthenticateCallback();
        }
        public SimpleAuthenticationModule(IAuthenticationCallbackProvider callbackProvider)
        {
            _callbackProvider = callbackProvider;
            _authenticationProviderFactory = new AuthenticationProviderFactory();

            // Define the routes and how they are handled.
            Get[RedirectRoute] = parameters => RedirectToProvider(parameters);
            Post[RedirectRoute] = parameters => RedirectToProvider(parameters);
            Get[CallbackRoute] = parameters => AuthenticateCallback();
        }
        public SimpleAuthenticationController(IAuthenticationCallbackProvider callbackProvider)
        {
            if (callbackProvider == null)
            {
                throw new ArgumentNullException("callbackProvider");
            }

            _callbackProvider = callbackProvider;
            _authenticationProviderFactory = new AuthenticationProviderFactory();
        }
        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);
            };
        }
Example #7
0
        public SimpleAuthenticationController(IAuthenticationCallbackProvider callbackProvider,
                                              ICache cache)
        {
            if (callbackProvider == null)
            {
                throw new ArgumentNullException("callbackProvider");
            }

            _callbackProvider = callbackProvider;
            _cache            = cache; // Can be null / not provided.

            _authenticationProviderFactory = new AuthenticationProviderFactory();
        }
        public SimpleAuthenticationController(IAuthenticationCallbackProvider callbackProvider,
            ICache cache)
        {
            if (callbackProvider == null)
            {
                throw new ArgumentNullException("callbackProvider");
            }

            _callbackProvider = callbackProvider;
            _cache = cache; // Can be null / not provided.

            _authenticationProviderFactory = new AuthenticationProviderFactory();
        }
        public WorldDominationAuthenticationController(IAuthenticationService authenticationService,
                                                       IAuthenticationCallbackProvider callbackProvider,
                                                       IAntiForgery antiForgery = null)
        {
            if (authenticationService == null)
            {
                throw new ArgumentNullException("authenticationService");
            }

            if (callbackProvider == null)
            {
                throw new ArgumentNullException("callbackProvider");
            }

            AuthenticationService = authenticationService;
            CallbackProvider = callbackProvider;

            // If no anti forgery class is provided, then we'll just use the default.
            _antiForgery = antiForgery ?? new AspNetAntiForgery();
        }
        public WorldDominationAuthenticationController(IAuthenticationService authenticationService,
                                                       IAuthenticationCallbackProvider callbackProvider,
                                                       IAntiForgery antiForgery = null)
        {
            if (authenticationService == null)
            {
                throw new ArgumentNullException("authenticationService");
            }

            if (callbackProvider == null)
            {
                throw new ArgumentNullException("callbackProvider");
            }

            AuthenticationService = authenticationService;
            CallbackProvider      = callbackProvider;

            // If no anti forgery class is provided, then we'll just use the default.
            _antiForgery = antiForgery ?? new AspNetAntiForgery();
        }
Example #11
0
        public SimpleAuthenticationModule(IAuthenticationCallbackProvider callbackProvider)
        {
            _callbackProvider = callbackProvider;
            _authenticationProviderFactory = new AuthenticationProviderFactory();

            // Define the routes and how they are handled.
            Get[RedirectRoute]  = parameters => RedirectToProvider(parameters);
            Post[RedirectRoute] = parameters => RedirectToProvider(parameters);
            Get[CallbackRoute]  = parameters => AuthenticateCallback();

            // If no Cache type is provided, we'll use a Session as the default.
            Before += context =>
            {
                if (Cache == null)
                {
                    Cache = new SessionCache(context.Request.Session);
                }

                return(null);
            };
        }
        public SimpleAuthenticationModule(IAuthenticationCallbackProvider callbackProvider)
        {
            _callbackProvider = callbackProvider;
            _authenticationProviderFactory = new AuthenticationProviderFactory();

            // Define the routes and how they are handled.
            Get[RedirectRoute] = parameters => RedirectToProvider(parameters);
            Post[RedirectRoute] = parameters => RedirectToProvider(parameters);
            Get[CallbackRoute] = parameters => AuthenticateCallback();

            // If no Cache type is provided, we'll use a Session as the default.
            Before += context =>
            {
                if (Cache == null)
                {
                    Cache = new SessionCache(context.Request.Session);
                }

                return null;
            };
        }
        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);
            };
        }
Example #14
0
        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 WorldDominationAuthenticationController(IAuthenticationService authenticationService, IAuthenticationCallbackProvider callbackProvider)
 {
     _authenticationService = authenticationService;
     _callbackProvider = callbackProvider;
 }
 public SimpleAuthenticationController(IAuthenticationCallbackProvider callbackProvider,
     ICache cache)
     : this(callbackProvider, cache, null)
 {
 }
 public SimpleAuthenticationController(IAuthenticationCallbackProvider callbackProvider,
     IConfigurationOptions configurationOptions)
     : this(callbackProvider, null, configurationOptions)
 {
 }
 public SimpleAuthenticationModule(IAuthenticationCallbackProvider callbackProvider) : this(callbackProvider, null)
 {
 }
        public KeyRockModule(IAuthenticationCallbackProvider callbackProvider)
        {
            Before += context =>
            {
                if (Cache == null)
                {
                    Cache = new SessionCache(context.Request.Session);
                }

                return null;
            };

            _callbackProvider = callbackProvider;
            _authenticationProviderFactory = new AuthenticationProviderFactory();

            Get["/authenticate/keyrock"] = parameters =>
            {
                var providerKey = (string)Request.Query.providerkey;
                if (string.IsNullOrEmpty(providerKey))
                {
                    throw new ArgumentException(
                        "ProviderKey value missing. You need to supply a valid provider key so we know where to redirect the user Eg. providerkey=google.");
                }

                var previousRedirectUrl = string.IsNullOrEmpty((string)Cache[SessionKeyRedirectToProviderUrl])
                                              ? "N.A."
                                              : (string)Cache[SessionKeyRedirectToProviderUrl];

                #region Deserialize Tokens, etc.

                // Retrieve any (previously) serialized access token stuff. (eg. public/private keys and state).
                // TODO: Check if this is an access token or an auth token thingy-thing.
                var state = Cache[SessionKeyState] as string;
                var redirectToUrl = Cache[SessionKeyRedirectToUrl] as string;

                #endregion

                // Lets now start to setup the view model.
                var model = new AuthenticateCallbackData();

                #region Retrieve the User Information

                try
                {
                    // Which provider did we just authenticate with?
                    var provider = _authenticationProviderFactory.AuthenticationProviders["keyrock"];

                    // Where do we return to, after we've authenticated?
                    var callbackUri = GenerateCallbackUri(provider.Name);

                    var queryString = new NameValueCollection();
                    foreach (var key in Request.Query.Keys)
                    {
                        queryString.Add(key, Request.Query[key]);
                    }

                    // Grab the user information.
                    model.AuthenticatedClient = provider.AuthenticateClient(queryString, state, callbackUri);
                }
                catch (Exception exception)
                {
                    model.Exception = exception;
                }

                #endregion

                // Do we have an optional redirect resource? Usually a previous referer?
                if (redirectToUrl != null)
                {
                    model.ReturnUrl = redirectToUrl;
                }

                // Finally! We can hand over the logic to the consumer to do whatever they want.
                return _callbackProvider.Process(this, model);
            };

            Get["/authentication/redirect/keyrock"] = _ =>
            {
                var provider = _authenticationProviderFactory.AuthenticationProviders["keyrock"];

                //// Most providers don't need any pre-setup crap, to redirect to authenticate.
                //// But of course, there's always one - OpenId. We have no idea WHERE we want to
                //// redirect to, so we need to do a particular check here.
                //// Of course, any value here could be used for any other provider. But that would be weird.
                //// TODO: Confirm this is not a security threat / open to abuse in some way.
                //if (identifier != null)
                //{
                //    provider.AuthenticateRedirectionUrl = identifier;
                //}

                //// Where do we return to, after we've authenticated?
                var callbackUri = GenerateCallbackUri("keyrock");

                // Determine where we need to redirect to.
                var redirectToAuthenticateSettings = provider.RedirectToAuthenticate(callbackUri);

                // Remember any important information for after we've come back.
                Cache[SessionKeyState] = redirectToAuthenticateSettings.State;
                Cache[SessionKeyRedirectToUrl] = DetermineReturnUrl();
                Cache[SessionKeyRedirectToProviderUrl] = redirectToAuthenticateSettings.RedirectUri.AbsoluteUri;

                // Now redirect :)
                return Response.AsRedirect(redirectToAuthenticateSettings.RedirectUri.AbsoluteUri);
            };
        }
 public SimpleAuthenticationController(IAuthenticationCallbackProvider callbackProvider,
                                       IConfigurationOptions configurationOptions) : this(callbackProvider, null, configurationOptions)
 {
 }
        private dynamic RedirectToAuthenticationProvider(IAuthenticationService authenticationService,
            IAuthenticationCallbackProvider authenticationCallbackProvider,
            string providerKey, Uri identifier = null)
        {
            if (authenticationService == null)
            {
                throw new ArgumentNullException();
            }

            if (authenticationCallbackProvider == null)
            {
                throw new ArgumentNullException("authenticationCallbackProvider");
            }

            if (string.IsNullOrEmpty(providerKey))
            {
                throw new ArgumentNullException("providerKey");
            }

            // Grab the required Provider settings.

            var settings = authenticationService.GetAuthenticateServiceSettings(providerKey, Request.Url);

            // An OpenId specific settings provided?
            if (identifier != null &&
                settings is IOpenIdAuthenticationServiceSettings)
            {
                ((IOpenIdAuthenticationServiceSettings) settings).Identifier = identifier;
            }

            // Remember the State value (for CSRF protection).
            Session[StateKey] = settings.State;

            //// Convention: If no redirectUrl data has been provided, then default to the Referrer, if one exists.
            //if (RedirectUrl != null &&
            //    !string.IsNullOrEmpty(RedirectUrl.AbsoluteUri))
            //{
            //    // We have extra state information we will need to retrieve.
            //    Session[RedirectUrlKey] = RedirectUrl.AbsoluteUri;
            //}
            //else if (Request != null &&
            //    Request. != null &&
            //    !string.IsNullOrEmpty(Request.UrlReferrer.AbsoluteUri))
            //{
            //    Session[RedirectUrlKey] = Request.UrlReferrer.AbsoluteUri;
            //}

            // Determine the provider's end point Url we need to redirect to.
            // NOTE: It's possible we're trying to goto an OpenId endpoint. But the user has entered
            var uri = authenticationService.RedirectToAuthenticationProvider(settings);
            if (uri == null || string.IsNullOrEmpty(uri.AbsoluteUri))
            {
                return authenticationCallbackProvider.OnRedirectToAuthenticationProviderError(this,
                                                                                                "No valid Uri was found - not sure where to redirect to?");
            }

            // Kthxgo!
            return Response.AsRedirect(uri.AbsoluteUri);
        }
 public SimpleAuthenticationController(IAuthenticationCallbackProvider callbackProvider,
                                       ICache cache) : this(callbackProvider, cache, null)
 {
 }
 public SimpleAuthenticationModule(IAuthenticationCallbackProvider callbackProvider)
     : this(callbackProvider, null)
 {
 }