/// <summary>
        /// Create an LtiRequestViewModel that contains a ContentItemPlacementResponse.
        /// </summary>
        /// <param name="url">The content_item_return_url from the content-item message.</param>
        /// <param name="consumerKey">The OAuth consumer key to use to sign the request.</param>
        /// <param name="consumerSecret">The OAuth consumer secret to use to sign the request.</param>
        /// <param name="contentItems">The ContentItemPlacementResponse to send.</param>
        /// <param name="data">The data received in the original content-item message.</param>
        /// <param name="ltiErrorLog">Optional plain text error message to be logged by the Tool Consumer.</param>
        /// <param name="ltiErrorMsg">Optional plain text error message to be displayed by the Tool Consumer.</param>
        /// <param name="ltiLog">Optional plain text message to be logged by the Tool Consumer.</param>
        /// <param name="ltiMsg">Optional plain text message to be displayed by the Tool Consumer.</param>
        /// <returns>The LtiRequestViewModel which contains a signed version of the response.</returns>
        public static LtiRequestViewModel CreateContentItemSelectionViewModel(
            string url, string consumerKey, string consumerSecret,
            ContentItemSelectionGraph contentItems, string data,
            string ltiErrorLog, string ltiErrorMsg, string ltiLog, string ltiMsg)
        {
            var ltiRequest = new LtiRequest(LtiConstants.ContentItemSelectionLtiMessageType)
            {
                Url = new Uri(url),
                ConsumerKey = consumerKey,
                ContentItems = contentItems.ToJsonLdString(),
                Data = data,
                LtiErrorLog = ltiErrorLog,
                LtiErrorMsg = ltiErrorMsg,
                LtiLog = ltiLog,
                LtiMsg = ltiMsg
            };

            return ltiRequest.GetViewModel(consumerSecret);
        }
        /// <summary>
        /// Writes the LtiConsumerRequest to the HttpResponse so that the client browser
        /// executes the LTI launch request.
        /// </summary>
        /// <param name="response">The HttpResponse to write the launch request to.</param>
        /// <param name="request">The LtiConsumerRequest to write.</param>
        /// <param name="consumerSecret">The OAuth secret to use when signing the request.</param>
        public static void WriteLtiRequest(this HttpResponse response, LtiRequest request, string consumerSecret)
        {
            var model = request.GetViewModel(consumerSecret);
            var form = new StringBuilder();
            form.AppendLine("<html>");
            form.AppendLine("<head><title></title></head>");
            form.AppendLine("<body>");
            form.AppendFormat("<form method='post' action='{0}' id='form'>", model.Action).AppendLine();
            foreach (var key in model.Fields.AllKeys)
            {
                form.AppendFormat("<input type='hidden' name='{0}' value='{1}' />", key, model.Fields[key])
                    .AppendLine();
            }
            form.AppendFormat("<input type='hidden' name='oauth_signature' value='{0}' />", model.Signature).AppendLine();
            form.AppendLine("</form>");
            form.AppendLine("<script>");
            form.AppendLine(" document.getElementById('form').submit();");
            form.AppendLine("</script>");
            form.AppendLine("</body>");
            form.AppendLine("</html>");

            response.ContentType = "text/html";
            response.Write(form.ToString());
        }
Beispiel #3
0
        public ActionResult LtiLaunch(Lti1TestLaunch model, int link)
        {
            var request = new LtiRequest(LtiConstants.BasicLaunchLtiMessageType);
            request.ConsumerKey = model.ConsumerKey;
            request.Url = new Uri(model.Url);
            request.AddCustomParameters(CustomParameters);
            request.LaunchPresentationLocale = Thread.CurrentThread.CurrentUICulture.Name;
            request.LaunchPresentationDocumentTarget = DocumentTarget.frame;
            request.LaunchPresentationWidth = 400;
            request.LaunchPresentationHeight = 300;
            request.LaunchPresentationCssUrl = GetLaunchPresentationCssUrl();
            request.LaunchPresentationReturnUrl = Request.Url.AbsoluteUri;
            request.LisOutcomeServiceUrl = GetLisOutcomeServiceUrl();
            request.ToolConsumerInfoProductFamilyCode = "LtiLibrary";
            request.ToolConsumerInfoVersion = "1";
            request.ToolConsumerProfileUrl = GetToolConsumerProfileUrl();
            request.ToolConsumerInstanceContactEmail = "*****@*****.**";
            request.ToolConsumerInstanceDescription = Assembly.GetExecutingAssembly().GetName().ToString();
            request.ToolConsumerInstanceGuid = Assembly.GetExecutingAssembly().GetName().Name;
            request.ToolConsumerInstanceName = "andyfmiller.com";

            switch (link)
            {
                case 1: // Instructor, Course 1, Resource 1
                    request.ContextId = Course1.CourseId.ToString(CultureInfo.InvariantCulture);
                    request.ContextLabel = request.ContextId;
                    request.ContextTitle = ConvertToPlainText(Course1.Title);
                    request.ContextType = LisContextType.CourseSection;
                    request.LisCourseOfferingSourcedId = request.ContextId;
                    request.LisCourseSectionSourcedId = request.ContextId;
                    request.ResourceLinkId = Course1.Links[0].LinkId;
                    request.ResourceLinkTitle = ConvertToPlainText(Course1.Links[0].Title);
                    request.ResourceLinkDescription = ConvertToPlainText(Course1.Links[0].Description);
                    request.LisPersonEmailPrimary = Instructor.Email;
                    request.LisPersonNameFamily = Instructor.FamilyName;
                    request.LisPersonNameGiven = Instructor.GivenName;
                    request.LisPersonSourcedId = Instructor.UserId;
                    request.UserId = Instructor.UserId;
                    request.UserName = Instructor.Username;
                    request.Roles = Role.Instructor.ToString();
                    break;
                case 2: // Instructor, Course 1, Resource 2
                    request.ContextId = Course1.CourseId.ToString(CultureInfo.InvariantCulture);
                    request.ContextLabel = request.ContextId;
                    request.ContextTitle = ConvertToPlainText(Course1.Title);
                    request.ContextType = LisContextType.CourseSection;
                    request.ResourceLinkId = Course1.Links[1].LinkId;
                    request.ResourceLinkTitle = ConvertToPlainText(Course1.Links[1].Title);
                    request.ResourceLinkDescription = ConvertToPlainText(Course1.Links[1].Description);
                    request.LisPersonEmailPrimary = Instructor.Email;
                    request.LisPersonNameFamily = Instructor.FamilyName;
                    request.LisPersonNameGiven = Instructor.GivenName;
                    request.LisPersonSourcedId = Instructor.UserId;
                    request.UserId = Instructor.UserId;
                    request.UserName = Instructor.Username;
                    request.Roles = Role.Instructor.ToString();
                    break;
                case 3: // Instructor, Course 2, Resource 1
                    request.ContextId = Course2.CourseId.ToString(CultureInfo.InvariantCulture);
                    request.ContextLabel = request.ContextId;
                    request.ContextTitle = ConvertToPlainText(Course2.Title);
                    request.ContextType = LisContextType.CourseSection;
                    request.ResourceLinkId = Course2.Links[0].LinkId;
                    request.ResourceLinkTitle = ConvertToPlainText(Course2.Links[0].Title);
                    request.ResourceLinkDescription = ConvertToPlainText(Course2.Links[0].Description);
                    request.LisPersonEmailPrimary = Instructor.Email;
                    request.LisPersonNameFamily = Instructor.FamilyName;
                    request.LisPersonNameGiven = Instructor.GivenName;
                    request.LisPersonSourcedId = Instructor.UserId;
                    request.UserId = Instructor.UserId;
                    request.UserName = Instructor.Username;
                    request.Roles = Role.Instructor.ToString();
                    break;
                case 4: // Student, Course 1, Resource 1
                    request.ContextId = Course1.CourseId.ToString(CultureInfo.InvariantCulture);
                    request.ContextLabel = request.ContextId;
                    request.ContextTitle = ConvertToPlainText(Course1.Title);
                    request.ContextType = LisContextType.CourseSection;
                    request.ResourceLinkId = Course1.Links[0].LinkId;
                    request.ResourceLinkTitle = ConvertToPlainText(Course1.Links[0].Title);
                    request.ResourceLinkDescription = ConvertToPlainText(Course1.Links[0].Description);
                    request.LisPersonEmailPrimary = Student.Email;
                    request.LisPersonNameFamily = Student.FamilyName;
                    request.LisPersonNameGiven = Student.GivenName;
                    request.LisPersonSourcedId = Student.UserId;
                    request.LisResultSourcedId = Student.UserId;
                    request.UserId = Student.UserId;
                    request.UserName = Student.Username;
                    request.Roles = Role.Learner.ToString();
                    break;
                case 5: // Student, Course 1, Resource 2
                    request.ContextId = Course1.CourseId.ToString(CultureInfo.InvariantCulture);
                    request.ContextLabel = request.ContextId;
                    request.ContextTitle = ConvertToPlainText(Course1.Title);
                    request.ContextType = LisContextType.CourseSection;
                    request.ResourceLinkId = Course1.Links[1].LinkId;
                    request.ResourceLinkTitle = ConvertToPlainText(Course1.Links[1].Title);
                    request.ResourceLinkDescription = ConvertToPlainText(Course1.Links[1].Description);
                    //request.LisPersonEmailPrimary = Student.Email;
                    request.LisPersonNameFamily = Student.FamilyName;
                    request.LisPersonNameGiven = Student.GivenName;
                    request.LisPersonSourcedId = Student.UserId;
                    request.LisResultSourcedId = Student.UserId;
                    request.UserId = Student.UserId;
                    request.UserName = Student.Username;
                    request.Roles = Role.Learner.ToString();
                    break;
                case 6: // Student, Course 2, Resource 1
                    request.ContextId = Course2.CourseId.ToString(CultureInfo.InvariantCulture);
                    request.ContextLabel = request.ContextId;
                    request.ContextTitle = ConvertToPlainText(Course2.Title);
                    request.ContextType = LisContextType.CourseSection;
                    request.ResourceLinkId = Course2.Links[0].LinkId;
                    request.ResourceLinkTitle = ConvertToPlainText(Course2.Links[0].Title);
                    request.ResourceLinkDescription = ConvertToPlainText(Course2.Links[0].Description);
                    //request.LisPersonEmailPrimary = Student.Email;
                    //request.LisPersonNameFamily = Student.FamilyName;
                    //request.LisPersonNameGiven = Student.GivenName;
                    request.LisPersonSourcedId = Student.UserId;
                    request.LisResultSourcedId = Student.UserId;
                    request.UserId = Student.UserId;
                    request.UserName = Student.Username;
                    request.Roles = Role.Learner.ToString();
                    break;
                case 7: // Mentor, Course 1, Resource 1
                        // Tests passed with this launch
                        // 5.9
                    request.ContextId = Course1.CourseId.ToString(CultureInfo.InvariantCulture);
                    request.ContextLabel = request.ContextId;
                    request.ContextTitle = ConvertToPlainText(Course1.Title);
                    request.ContextType = LisContextType.CourseSection;
                    request.ResourceLinkId = Course1.Links[0].LinkId;
                    request.ResourceLinkTitle = ConvertToPlainText(Course1.Links[0].Title);
                    request.ResourceLinkDescription = ConvertToPlainText(Course1.Links[0].Description);
                    request.LisPersonEmailPrimary = Mentor.Email;
                    request.LisPersonNameFamily = Mentor.FamilyName;
                    request.LisPersonNameGiven = Mentor.GivenName;
                    request.UserId = Mentor.UserId;
                    request.UserName = Mentor.Username;
                    request.Roles = Role.Mentor.ToString();
                    request.RoleScopeMentor = Mentor.MentoringUserId;
                    break;
                case 8: // Instructor, No context (6.4)
                        // Tests passed with this launch
                        // 6.4
                    request.ResourceLinkId = GradebookTool.LinkId;
                    request.ResourceLinkTitle = ConvertToPlainText(GradebookTool.Title);
                    request.ResourceLinkDescription = ConvertToPlainText(GradebookTool.Description);
                    request.LisPersonEmailPrimary = Instructor.Email;
                    request.LisPersonNameFamily = Instructor.FamilyName;
                    request.LisPersonNameGiven = Instructor.GivenName;
                    request.LisPersonSourcedId = Instructor.UserId;
                    request.UserId = Instructor.UserId;
                    request.UserName = Instructor.Username;
                    request.Roles = Role.Instructor.ToString();
                    break;
            }
            return View(request.GetViewModel(model.ConsumerSecret));
        }
        /// <summary>
        /// Send a basic LTI launch request to the Tool Provider.
        /// </summary>
        /// <remarks>
        /// This is the basic function of a Tool Consumer.
        /// </remarks>
        public ActionResult Launch()
        {
            Uri launchUri;
            var ltiRequest = new LtiRequest(LtiConstants.BasicLaunchLtiMessageType)
            {
                ConsumerKey = "12345",
                ResourceLinkId = "launch",
                Url = Uri.TryCreate(Request.Url, Url.Action("Tool", "Provider"), out launchUri) ? launchUri : null
            };

            // Tool
            ltiRequest.ToolConsumerInfoProductFamilyCode = "LtiLibrary";
            ltiRequest.ToolConsumerInfoVersion = "1.2";

            // Context
            ltiRequest.ContextId = "Home";
            ltiRequest.ContextTitle = "Home";
            ltiRequest.ContextType = LisContextType.CourseSection;

            // Instance
            ltiRequest.ToolConsumerInstanceGuid = Request.Url == null ? null : Request.Url.Authority;
            ltiRequest.ToolConsumerInstanceName = "LtiLibrary Sample";
            ltiRequest.ResourceLinkTitle = "Launch";
            ltiRequest.ResourceLinkDescription = "Perform a basic LTI 1.2 launch";

            // User
            ltiRequest.LisPersonEmailPrimary = "*****@*****.**";
            ltiRequest.LisPersonNameFamily = "Doe";
            ltiRequest.LisPersonNameGiven = "Joan";
            ltiRequest.UserId = "1";
            ltiRequest.SetRoles(new[] { Role.Instructor });

            // Basic Outcomes service (WebApi controller)
            var controllerUrl = UrlHelper.GenerateUrl("DefaultApi", null, "Outcomes",
                new RouteValueDictionary { { "httproute", string.Empty } }, RouteTable.Routes,
                Request.RequestContext, false);
            Uri controllerUri;
            if (Uri.TryCreate(Request.Url, controllerUrl, out controllerUri))
            {
                ltiRequest.LisOutcomeServiceUrl = controllerUri.AbsoluteUri;
            }
            ltiRequest.LisResultSourcedId = "ltilibrary-jdoe-1";

            // Outcomes V2 service (WebApi controller)
            controllerUrl = UrlHelper.GenerateUrl("LineItemsApi", null, "LineItems",
                new RouteValueDictionary
                {
                    { "httproute", string.Empty }, 
                    { "contextId", ltiRequest.ContextId }
                },
                RouteTable.Routes,
                Request.RequestContext, false);
            if (Uri.TryCreate(Request.Url, controllerUrl, out controllerUri))
            {
                ltiRequest.LineItemsServiceUrl = controllerUri.AbsoluteUri;
            }
            controllerUrl = UrlHelper.GenerateUrl("LineItemsApi", null, "LineItems",
                new RouteValueDictionary
                {
                    { "httproute", string.Empty }, 
                    { "contextId", ltiRequest.ContextId },
                    { "id", LineItemsController.LineItemId }
                },
                RouteTable.Routes,
                Request.RequestContext, false);
            if (Uri.TryCreate(Request.Url, controllerUrl, out controllerUri))
            {
                ltiRequest.LineItemServiceUrl = controllerUri.AbsoluteUri;
            }
            ltiRequest.AddCustomParameter("lineitem_url", "$LineItem.url");
            ltiRequest.AddCustomParameter("lineitems_url", "$LineItems.url");

            // Tool Consumer Profile service (WebApi controller)
            controllerUrl = UrlHelper.GenerateUrl("ToolConsumerProfileApi", null, "ToolConsumerProfile",
                new RouteValueDictionary { { "httproute", string.Empty } }, RouteTable.Routes,
                Request.RequestContext, false);
            if (Uri.TryCreate(Request.Url, controllerUrl, out controllerUri))
            {
                ltiRequest.ToolConsumerProfileUrl = controllerUri.AbsoluteUri;
            }
            ltiRequest.AddCustomParameter("tc_profile_url", "$ToolConsumerProfile.url");

            return View(ltiRequest.GetViewModel("secret"));
        }
Beispiel #5
0
        /// <summary>
        /// Create a basic launch request for the assignment.
        /// </summary>
        /// <param name="request">The browser request.</param>
        /// <param name="assignment">The assignment to launch.</param>
        /// <param name="user">The user that is launching the assignment.</param>
        /// <returns>An LtiRequestViewModel which can be displayed by the View in such
        /// a way that the browser will invoke the LTI launch.</returns>
        public static LtiRequestViewModel CreateBasicLaunchRequestViewModel(HttpRequestBase request, Assignment assignment, ApplicationUser user)
        {
            var ltiRequest = new LtiRequest(LtiConstants.BasicLaunchLtiMessageType)
            {
                ConsumerKey = assignment.ConsumerKey,
                ResourceLinkId = assignment.AssignmentId.ToString(CultureInfo.InvariantCulture),
                Url = new Uri(assignment.Url)
            };

            var course = assignment.Course;

            // Tool
            ltiRequest.ToolConsumerInfoProductFamilyCode = Assembly.GetExecutingAssembly().GetName().Name;
            ltiRequest.ToolConsumerInfoVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();

            // Context
            ltiRequest.ContextId = course.CourseId.ToString(CultureInfo.InvariantCulture);
            ltiRequest.ContextTitle = course.Name;
            ltiRequest.ContextType = course.EnrolledUsers.Any(u => u.Id == user.Id)
                ? LisContextType.CourseSection
                : LisContextType.CourseTemplate;

            // Instance
            ltiRequest.ToolConsumerInstanceGuid = "~/".ToAbsoluteUrl();
            var title = Assembly.GetExecutingAssembly().GetCustomAttributes<AssemblyTitleAttribute>().FirstOrDefault();
            ltiRequest.ToolConsumerInstanceName = title == null ? null : title.Title;
            ltiRequest.ResourceLinkTitle = assignment.Name;
            ltiRequest.ResourceLinkDescription = assignment.Description;

            // User
            if (user.SendEmail.GetValueOrDefault(true))
            {
                ltiRequest.LisPersonEmailPrimary = user.Email;
            }
            if (user.SendName.GetValueOrDefault(true))
            {
                ltiRequest.LisPersonNameFamily = user.LastName;
                ltiRequest.LisPersonNameGiven = user.FirstName;
            }
            ltiRequest.UserId = user.Id;
            ltiRequest.UserName = user.UserName;
            ltiRequest.SetRoles(GetLtiRolesForUser(user));

            // Outcomes service
            if (course.EnrolledUsers.Any(u => u.Id == user.Id))
            {
                var outcomesUrl = UrlHelper.GenerateUrl("DefaultApi", null, "Outcomes",
                    new RouteValueDictionary { { "httproute", string.Empty } }, RouteTable.Routes,
                    request.RequestContext, false);
                Uri outcomesUri;
                if (Uri.TryCreate(request.Url, outcomesUrl, out outcomesUri))
                {
                    ltiRequest.LisOutcomeServiceUrl = outcomesUri.AbsoluteUri;
                }
                ltiRequest.LisResultSourcedId =
                    JsonConvert.SerializeObject(new LisResultSourcedId
                    {
                        AssignmentId = assignment.AssignmentId, 
                        UserId = user.Id
                    });
            }

            // Tool Consumer Profile service
            var profileUrl = UrlHelper.GenerateUrl("DefaultApi", null, "ToolConsumerProfile",
                new RouteValueDictionary { { "httproute", string.Empty } }, RouteTable.Routes,
                request.RequestContext, false);
            Uri profileUri;
            if (Uri.TryCreate(request.Url, profileUrl, out profileUri))
            {
                ltiRequest.ToolConsumerProfileUrl = profileUri.AbsoluteUri;
            }

            // Add the custom parameters. This consumer supports 3 special substitutions.
            var customParameters = assignment.CustomParameters;

            if (!string.IsNullOrWhiteSpace(customParameters))
            {
                ltiRequest.AddCustomParameters(customParameters);
            }

            return ltiRequest.GetViewModel(assignment.ConsumerSecret);
        }
Beispiel #6
0
        public static LtiRequestViewModel CreateContentItemSelectionRequestViewModel(HttpRequestBase request, ContentItemTool contentItemTool, Course course, ApplicationUser user, string returnUrl)
        {
            var ltiRequest = new LtiRequest(LtiConstants.ContentItemSelectionRequestLtiMessageType)
            {
                ConsumerKey = contentItemTool.ConsumerKey,
                Url = new Uri(contentItemTool.Url)
            };

            // Tool Consumer
            ltiRequest.ToolConsumerInfoProductFamilyCode = Assembly.GetExecutingAssembly().GetName().Name;
            ltiRequest.ToolConsumerInfoVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();

            // Context
            ltiRequest.ContextId = course.CourseId.ToString(CultureInfo.InvariantCulture);
            ltiRequest.ContextTitle = course.Name;
            ltiRequest.ContextType = course.EnrolledUsers.Any(u => u.Id == user.Id)
                ? LisContextType.CourseSection
                : LisContextType.CourseTemplate;
            ltiRequest.LisCourseSectionSourcedId = course.CourseId.ToString(CultureInfo.InvariantCulture);

            // Instance
            ltiRequest.ToolConsumerInstanceGuid = "~/".ToAbsoluteUrl();
            var title = Assembly.GetExecutingAssembly().GetCustomAttributes<AssemblyTitleAttribute>().FirstOrDefault();
            ltiRequest.ToolConsumerInstanceName = title == null ? null : title.Title;

            // User
            if (user.SendEmail.GetValueOrDefault(true))
            {
                ltiRequest.LisPersonEmailPrimary = user.Email;
            }
            if (user.SendName.GetValueOrDefault(true))
            {
                ltiRequest.LisPersonNameFamily = user.LastName;
                ltiRequest.LisPersonNameGiven = user.FirstName;
            }
            ltiRequest.UserId = user.Id;
            ltiRequest.UserName = user.UserName;
            ltiRequest.SetRoles(GetLtiRolesForUser(user));

            // Presentation
            ltiRequest.LaunchPresentationDocumentTarget = DocumentTarget.iframe;

            // Content Item Tool
            ltiRequest.AcceptMediaTypes = LtiConstants.LtiLinkMediaType; // Only accept LTI Link
            ltiRequest.AcceptMultiple = true;
            ltiRequest.AcceptPresentationDocumentTargets = "iframe";
            ltiRequest.AcceptUnsigned = false;
            ltiRequest.ContentItemReturnUrl = returnUrl;
            //ltiRequest.ContentItemServiceUrl = "about:blank";
            ltiRequest.Data = JsonConvert.SerializeObject(new ContentItemData
            {
                ContentItemToolId = contentItemTool.ContentItemToolId, 
                CourseId = course.CourseId
            });

            // Add the custom parameters.
            var customParameters = contentItemTool.CustomParameters;

            // This tool consumer supports 3 non-spec custom parameters
            if (!string.IsNullOrWhiteSpace(customParameters))
            {
                ltiRequest.AddCustomParameters(customParameters);
            }

            return ltiRequest.GetViewModel(contentItemTool.ConsumerSecret);
        }