public ActionResult Index(HarnessViewModel model)
        {
            if (!string.IsNullOrWhiteSpace(Request.Params["btnLoginWithMiiCard"]))
            {
                // Cover off certain edge cases where the test harness is started then restarted
                // killing session state - we need a session active to perform the OAuth exchange and the
                // redirect that LoginWithMiiCard causes can prevent that from happening unless it's in place
                // before it's called
                if (!(Session["Running"] as bool? ?? false))
                {
                    Session["Running"] = true;
                    return View(model);
                }
                else
                {
                    return this.LoginWithMiiCard(model);
                }
            }

            if (!string.IsNullOrWhiteSpace(this.Request.Params["btn-invoke"]))
            {
                if (this.Request.Params["btn-invoke"] == "directory-search")
                {
                    var response = new MiiCardDirectoryService().FindBy(model.DirectoryCriterion, model.DirectoryCriterionValue, model.DirectoryCriterionValueHashed);
                    if (response != null)
                    {
                        model.LastDirectorySearchResult = MiiApiResponseExtensions.RenderUserProfile(response);
                    }
                }
                else if (string.IsNullOrWhiteSpace(model.ConsumerKey) || string.IsNullOrWhiteSpace(model.ConsumerSecret) || string.IsNullOrWhiteSpace(model.AccessToken) || string.IsNullOrWhiteSpace(model.AccessTokenSecret))
                {
                    model.ShowOAuthDetailsRequiredError = true;
                }
                else
                {
                    var apiWrapper = new MiiCardOAuthClaimsService(model.ConsumerKey, model.ConsumerSecret, model.AccessToken, model.AccessTokenSecret);
                    var financialWrapper = new MiiCardOAuthFinancialService(model.ConsumerKey, model.ConsumerSecret, model.AccessToken, model.AccessTokenSecret);

                    switch (this.Request.Params["btn-invoke"])
                    {
                        case "get-claims":
                            model.LastGetClaimsResult = apiWrapper.GetClaims().Prettify();
                            break;
                        case "is-user-assured":
                            model.LastIsUserAssuredResult = apiWrapper.IsUserAssured().Prettify();
                            break;
                        case "is-social-account-assured":
                            if (!string.IsNullOrWhiteSpace(model.SocialAccountId) && !string.IsNullOrWhiteSpace(model.SocialAccountType))
                            {
                                model.LastIsSocialAccountAssuredResult = apiWrapper.IsSocialAccountAssured(model.SocialAccountId, model.SocialAccountType).Prettify();
                            }
                            break;
                        case "assurance-image":
                            if (!string.IsNullOrWhiteSpace(model.AssuranceImageType))
                            {
                                model.ShowAssuranceImage = true;
                            }
                            break;
                        case "card-image":
                            model.ShowCardImage = true;
                            break;
                        case "get-identity-snapshot-details":
                            model.LastGetIdentitySnapshotDetailsResult = apiWrapper.GetIdentitySnapshotDetails(model.SnapshotDetailsId).Prettify();
                            break;
                        case "get-identity-snapshot":
                            if (!string.IsNullOrWhiteSpace(model.SnapshotId))
                            {
                                model.LastGetIdentitySnapshotResult = apiWrapper.GetIdentitySnapshot(model.SnapshotId).Prettify();
                            }
                            break;
                        case "get-identity-snapshot-pdf":
                            if (!string.IsNullOrWhiteSpace(model.SnapshotPdfId))
                            {
                                return new FileStreamResult(apiWrapper.GetIdentitySnapshotPdf(model.SnapshotPdfId), "application/pdf")
                                {
                                    FileDownloadName = model.SnapshotPdfId
                                };
                            }
                            break;
                        case "get-authentication-details":
                            model.LastGetAuthenticationDetailsResult = apiWrapper.GetAuthenticationDetails(model.AuthenticationDetailsSnapshotId).Prettify();
                            break;
                        case "is-credit-bureau-refresh-in-progress":
                            model.LastIsRefreshInProgressCreditBureauResult = apiWrapper.IsCreditBureauRefreshInProgress().Prettify();
                            break;
                        case "refresh-credit-bureau-data":
                            model.LastRefreshCreditBureauDataResult = apiWrapper.RefreshCreditBureauData().Prettify();
                            break;
                        case "refresh-financial-data":
                            model.LastRefreshFinancialDataResult = financialWrapper.RefreshFinancialData().Prettify();
                            break;
                        case "refresh-financial-data-credit-cards":
                            model.LastRefreshFinancialDataCreditCardsResult = financialWrapper.RefreshFinancialDataCreditCards().Prettify();
                            break;
                        case "is-refresh-in-progress":
                            model.LastIsRefreshInProgressResult = financialWrapper.IsRefreshInProgress().Prettify();
                            break;
                        case "is-refresh-in-progress-credit-cards":
                            model.LastIsRefreshInProgressCreditCardsResult = financialWrapper.IsRefreshInProgressCreditCards().Prettify();
                            break;
                        case "get-financial-transactions":
                            model.LastGetFinancialTransactionsResult = financialWrapper.GetFinancialTransactions().Prettify(new PrettifyConfiguration { ModestyLimit = model.FinancialDataModestyLimit });
                            break;
                        case "get-financial-transactions-credit-cards":
                            model.LastGetFinancialTransactionsCreditCardsResult = financialWrapper.GetFinancialTransactionsCreditCards().Prettify(new PrettifyConfiguration { ModestyLimit = model.FinancialDataCreditCardsModestyLimit });
                            break;
                    }
                }
            }

            return View(model);
        }
Exemple #2
0
        protected override void OnLoad(EventArgs e)
        {
            // If we've not yet been supplied a TokenManager then build a session-based one
            if (this.TokenManager == null)
            {
                // First try pulling key and secret information from application settings
                string consumerKey    = ConfigurationManager.AppSettings[MiiCard.ConfigSettingNameMiiCardConsumerKey];
                string consumerSecret = ConfigurationManager.AppSettings[MiiCard.ConfigSettingNameMiiCardConsumerSecret];

                // We require at least a consumer key be available - if it's not, bail out as there's no further
                // we can realistically go
                if (string.IsNullOrWhiteSpace(consumerKey))
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  "The TokenManager was not initialised with suitable consumer key and secret information, and the information could not " +
                                  "be found in web.config. Either explicitly specify an IConsumerTokenManager to be used via the TokenManager property, or " +
                                  "add appropriate entries to your web.config's appSettings section with key names {0} and {1}.",
                                  MiiCard.ConfigSettingNameMiiCardConsumerKey,
                                  MiiCard.ConfigSettingNameMiiCardConsumerSecret)
                              );
                }

                this.TokenManager = new SessionStateConsumerTokenManager(consumerKey, consumerSecret);
            }

            Page.RegisterRequiresViewStateEncryption();

            var consumer = this.GetConsumer();
            AuthorizedTokenResponse authTokenResponse = null;

            try
            {
                authTokenResponse = consumer.ProcessUserAuthorization();
            }
            catch (Exception ex)
            {
                this.AuthorisationFailed(this, new MiiCardAuthorisationFailureEventArgs(ex));
            }

            if (authTokenResponse != null)
            {
                // We've been successfully authenticated - if we've been configured to do so then pull down the
                // user's profile so that it can be made available to the event handler
                MiiApiResponse <MiiUserProfile> response = null;

                if (this.LoadUserProfileOnAuthorise)
                {
                    var service = new MiiCardOAuthClaimsService(this.TokenManager.ConsumerKey, this.TokenManager.ConsumerSecret, authTokenResponse.AccessToken, this.TokenManager.GetTokenSecret(authTokenResponse.AccessToken));
                    response = service.GetClaims();

                    if (response.Status == MiiApiCallStatus.Success)
                    {
                        // User profile will be stored in the correct location based on the setting of the
                        // this.UserProfileStorage property
                        this.UserProfile = response;
                        this.AuthorisationSucceeded(this, new MiiCardAuthorisationSuccessEventArgs(authTokenResponse.AccessToken, this.TokenManager.GetTokenSecret(authTokenResponse.AccessToken), authTokenResponse.ExtraData, response));
                    }
                    else
                    {
                        this.AuthorisationFailed(this, new MiiCardAuthorisationFailureEventArgs(response.ErrorCode, response.ErrorMessage));
                    }
                }
                else
                {
                    this.AuthorisationSucceeded(this, new MiiCardAuthorisationSuccessEventArgs(authTokenResponse.AccessToken, this.TokenManager.GetTokenSecret(authTokenResponse.AccessToken), authTokenResponse.ExtraData, null));
                }
            }

            base.OnLoad(e);
        }
        protected override void OnLoad(EventArgs e)
        {
            // If we've not yet been supplied a TokenManager then build a session-based one
            if (this.TokenManager == null)
            {
                // First try pulling key and secret information from application settings
                string consumerKey = ConfigurationManager.AppSettings[MiiCard.ConfigSettingNameMiiCardConsumerKey];
                string consumerSecret = ConfigurationManager.AppSettings[MiiCard.ConfigSettingNameMiiCardConsumerSecret];
                
                // We require at least a consumer key be available - if it's not, bail out as there's no further
                // we can realistically go
                if (string.IsNullOrWhiteSpace(consumerKey))
                {
                    throw new InvalidOperationException(
                        string.Format(
                        "The TokenManager was not initialised with suitable consumer key and secret information, and the information could not " +
                        "be found in web.config. Either explicitly specify an IConsumerTokenManager to be used via the TokenManager property, or " +
                        "add appropriate entries to your web.config's appSettings section with key names {0} and {1}.", 
                        MiiCard.ConfigSettingNameMiiCardConsumerKey, 
                        MiiCard.ConfigSettingNameMiiCardConsumerSecret)
                        );
                }

                this.TokenManager = new SessionStateConsumerTokenManager(consumerKey, consumerSecret);
            }

            Page.RegisterRequiresViewStateEncryption();

            var consumer = this.GetConsumer();
            AuthorizedTokenResponse authTokenResponse = null;

            try
            {
                authTokenResponse = consumer.ProcessUserAuthorization();
            }
            catch (Exception ex)
            {
                this.AuthorisationFailed(this, new MiiCardAuthorisationFailureEventArgs(ex));
            }

            if (authTokenResponse != null)
            {
                // We've been successfully authenticated - if we've been configured to do so then pull down the
                // user's profile so that it can be made available to the event handler
                MiiApiResponse<MiiUserProfile> response = null;

                if (this.LoadUserProfileOnAuthorise)
                {
                    var service = new MiiCardOAuthClaimsService(this.TokenManager.ConsumerKey, this.TokenManager.ConsumerSecret, authTokenResponse.AccessToken, this.TokenManager.GetTokenSecret(authTokenResponse.AccessToken));
                    response = service.GetClaims();

                    if (response.Status == MiiApiCallStatus.Success)
                    {
                        // User profile will be stored in the correct location based on the setting of the
                        // this.UserProfileStorage property
                        this.UserProfile = response;
                        this.AuthorisationSucceeded(this, new MiiCardAuthorisationSuccessEventArgs(authTokenResponse.AccessToken, this.TokenManager.GetTokenSecret(authTokenResponse.AccessToken), authTokenResponse.ExtraData, response));
                    }
                    else
                    {
                        this.AuthorisationFailed(this, new MiiCardAuthorisationFailureEventArgs(response.ErrorCode, response.ErrorMessage));
                    }
                }
                else
                {
                    this.AuthorisationSucceeded(this, new MiiCardAuthorisationSuccessEventArgs(authTokenResponse.AccessToken, this.TokenManager.GetTokenSecret(authTokenResponse.AccessToken), authTokenResponse.ExtraData, null));
                }
            }

            base.OnLoad(e);
        }