LTI Demo for canvas(Java版)

前言

为canvas开发或添加新功能最简单的方法是开发一个LTI。下面是为canvas开发一个可以随意修改sis id的Demo。

介绍

LTI是一个用External Tool对接LMS的标准。它采用OAuth进行签名认证,来完成两个Web之间的用户认证。

工作流

准备: 在LMS里配置好LTI启动链接,

1. LMS根据配置好的key和secret签名接下来的POST请求

2. LTI收到请求后,认证签名。

3. 第2步认证成功后,根据对接的web服务的不同进行相应的注册登录步骤(这一步应是系统自动进行的,而不应该引导用户来进行)。

4. 完成对于的登录过程后,跳转至对接的Web应用。在使用其他web应用的整个过程中可以向canvas回传数据。

扫描二维码关注公众号,回复: 37830 查看本文章

5. 退出Web应用并返回canvas。根据对应的return_uri,返回到对应的canvas页面。

开发

如果有LTI认证的库,将会省很多事。

Ruby使用ims-lti这个gem。而Java则如下:

<dependency>
    <groupId>org.imsglobal</groupId>
    <artifactId>basiclti-util</artifactId>
    <version>1.2.0</version>
</dependency>

LTI认证类:

public interface LTIAuthI {// 验证成功后不同的登录方式
    void login(HttpServletRequest request, HttpServletResponse response);

    default void auth(HttpServletRequest request, HttpServletResponse response) {
        LtiVerifier ltiVerifier = new LtiOauthVerifier();
        String key = request.getParameter("oauth_consumer_key");
        try {
            LtiVerificationResult ltiResult = ltiVerifier.verify(request, LTIAuthKeys.getSecret(key));
            if(ltiResult.getSuccess()) {
                //认证成功
                login(request, response);
            }
        } catch (LtiVerificationException e) {
            e.printStackTrace();
        }
    }
}

可能的登录方法:

    public void login(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().setAttribute("return_url", request.getParameter("launch_presentation_return_url"));
        request.getSession().setAttribute("user_id",userService.getUserId(request.getParameter("user_id")));
    }

登录成功后跳转到一个已有的Web应用的链接或者跳转至LTI的某个链接。LTI也可以看作是一个web应用:

@RequestMapping(value="/")
public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws IOException {
        if(request.getSession().getAttribute("user_id") == null) {
            response.sendError(403, "Forbidden");
            return null;
        }else {
            Long userId = (Long)request.getSession().getAttribute("user_id");
            return new ModelAndView("studentinfo", "sisId", pseudonymsService.getSisIdByUserId(userId));
        }
}

总结:

1. LTI相当于一个只能通过LMS的POST请求登录的Web程序。

2. Java版的basiclti-util要使用最新版,目前是1.2.0。github上的wiki内容写的版本还是1.1.2。

3. 附上canvasLTI配置xml示例:

<?xml version="1.0" encoding="UTF-8"?>
<cartridge_basiclti_link xmlns="http://www.imsglobal.org/xsd/imslticc_v1p0"
                         xmlns:blti = "http://www.imsglobal.org/xsd/imsbasiclti_v1p0"
                         xmlns:lticm ="http://www.imsglobal.org/xsd/imslticm_v1p0"
                         xmlns:lticp ="http://www.imsglobal.org/xsd/imslticp_v1p0"
                         xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
                         xsi:schemaLocation = "http://www.imsglobal.org/xsd/imslticc_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticc_v1p0.xsd
    http://www.imsglobal.org/xsd/imsbasiclti_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imsbasiclti_v1p0.xsd
    http://www.imsglobal.org/xsd/imslticm_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticm_v1p0.xsd
    http://www.imsglobal.org/xsd/imslticp_v1p0 http://www.imsglobal.org/xsd/lti/ltiv1p0/imslticp_v1p0.xsd">
    <blti:launch_url>http://127.0.0.1/canvas_lti/studentinfo/launch</blti:launch_url>
    <blti:title>SetStudentInfo</blti:title>
    <blti:description>Add student info to canvas</blti:description>
    <blti:extensions platform="canvas.instructure.com">
        <lticm:property name="privacy_level">public</lticm:property>
        <lticm:options name="user_navigation">
            <lticm:options name="labels">
                <lticm:property name="en">SetStudentInfo</lticm:property>
                <lticm:property name="zh">配置学籍信息</lticm:property>
            </lticm:options>
            <lticm:property name="enabled">true</lticm:property>
        </lticm:options>
    </blti:extensions>
</cartridge_basiclti_link>

4. 附上canvas的post请求,提交的参数:

tool_consumer_info_product_family_code : canvas
resource_link_title : 测试
context_title : canvas使用学习
ext_outcomes_tool_placement_url : http://127.0.0.1:4567/api/lti/v1/turnitin/outcomes_placement/1
roles : urn:lti:instrole:ims/lis/Administrator
tool_consumer_instance_name : test
ext_lti_assignment_id : f4e5f0c9-11f0-408d-b1d6-3ec9e858ce4e
tool_consumer_instance_guid : b7ba4e5ca3e16754b4465d4949721cfabf92b2c1.127.0.0.1
custom_canvas_assignment_title : 测试
resource_link_id : 8edac495567b06c1756f5dc6be98a119dc575749
oauth_signature_method : HMAC-SHA1
oauth_version : 1.0
launch_presentation_return_url : http://127.0.0.1:4567/courses/1/assignments
ext_outcome_data_values_accepted : url,text
ext_ims_lis_basic_outcome_url : http://127.0.0.1:4567/api/lti/v1/tools/1/ext_grade_passback
custom_canvas_enrollment_state : 
lti_version : LTI-1p0
oauth_signature : wVkTM2lkBF9Fk6EdZAqbSNtAm4c=
ext_roles : urn:lti:instrole:ims/lis/Administrator,urn:lti:sysrole:ims/lis/SysAdmin,urn:lti:sysrole:ims/lis/User
oauth_consumer_key : ojqd
launch_presentation_locale : zh-Hans
oauth_timestamp : 1523941687
tool_consumer_instance_contact_email : [email protected]
lis_outcome_service_url : http://127.0.0.1:4567/api/lti/v1/tools/1/grade_passback
ext_outcome_result_total_score_accepted : true
oauth_nonce : 17LRyLaG7ud3CFBiC9M3ZuSdXzNkO9T5qcDnuopUVI
custom_canvas_assignment_points_possible : 0
lti_message_type : basic-lti-launch-request
oauth_callback : about:blank
user_id : 29ffb607a1eab7755f771ba442df023dc42b9042
tool_consumer_info_version : cloud
context_id : abff60ace038d671c7300fea7212bbff16fb1fef
context_label : canvas
launch_presentation_document_target : iframe

后记:

本人也是新手,记录一下第一个Demo。欢迎批评指正:[email protected]

猜你喜欢

转载自www.cnblogs.com/error250/p/8903583.html
今日推荐