public dynamic Process(NancyModule nancyModule, AuthenticateCallbackData model)
        {
            AuthenticatedUser user = null;
            unitOfWork.DoInTransaction(() =>
             user = userMapper.MapUser(model.AuthenticatedClient)
            );

            string token = tokeniser.CreateToken(user.UserName, user.Id);

            return new {Token = token};
        }
        public dynamic Process(NancyModule nancyModule, AuthenticateCallbackData model)
        {
            AuthenticatedUser user = null;

            unitOfWork.DoInTransaction(() =>
                                       user = userMapper.MapUser(model.AuthenticatedClient)
                                       );

            string token = tokeniser.CreateToken(user.UserName, user.Id);

            return(new { Token = token });
        }
        private dynamic AuthenticateCallback()
        {
            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];
            TraceSource.TraceInformation("Previous Redirect Url: " + previousRedirectUrl);

            #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.
            TraceSource.TraceVerbose("Retrieving (local serializaed) AccessToken, State and RedirectToUrl.");
            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 = GetAuthenticationProvider(providerKey);
                model.ProviderName = provider.Name;

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

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

                if (Request.Method.Equals("POST"))
                {
                    Code form = this.Bind<Code>();
                    queryString.Add("code", form.code);
                    state = Guid.NewGuid().ToString();
                    queryString.Add("state", state);
                    callbackUri = new Uri(form.redirectUri);
                }

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

            #endregion

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

            // Finally! We can hand over the logic to the consumer to do whatever they want.
            TraceSource.TraceVerbose("About to execute your custom callback provider logic.");
            return _callbackProvider.Process(this, model);
        }
Example #4
0
        private dynamic AuthenticateCallback()
        {
            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];

            TraceSource.TraceInformation("Previous Redirect Url: " + previousRedirectUrl);

            #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.
            TraceSource.TraceVerbose("Retrieving (local serializaed) AccessToken, State and RedirectToUrl.");
            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 = GetAuthenticationProvider(providerKey);
                model.ProviderName = provider.Name;

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

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

                if (Request.Method.Equals("POST"))
                {
                    Code form = this.Bind <Code>();
                    queryString.Add("code", form.code);
                    state = Guid.NewGuid().ToString();
                    queryString.Add("state", state);
                    callbackUri = new Uri(form.redirectUri);
                }

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


            #endregion

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

            // Finally! We can hand over the logic to the consumer to do whatever they want.
            TraceSource.TraceVerbose("About to execute your custom callback provider logic.");
            return(_callbackProvider.Process(this, model));
        }