【问题标题】:Generate OAuth signature in Java在 Java 中生成 OAuth 签名
【发布时间】:2015-04-08 07:16:17
【问题描述】:

我正在开发 Java Web 应用程序,现在我正在尝试将我的网站与 QTIWorks 连接

我有一个消费者密钥和秘密,我需要生成 OAuth 签名以通过 LTI 与 QTIWorks 连接

<html>

<head> </head>

<body>
<form action="https://webapps.ph.ed.ac.uk/qtiworks-dev2/lti/domainlaunch" name="ltiLaunchForm" id="ltiLaunchForm" method="post" target="basicltiLaunchFrame" enctype="application/x-www-form-urlencoded" style="display: block;">
   <input type="hidden" name="context_id" value="cid-00113">
   <input type="hidden" name="context_label" value="SI106">
   <input type="hidden" name="context_title" value="Design of Personal Environments 1">
   <input type="hidden" name="ext_note" value="Instructor from first course">
   <input type="hidden" name="launch_presentation_locale" value="en_us">
   <input type="hidden" name="lis_person_contact_email_primary" value="sian@imscert.org">
   <input type="hidden" name="lis_person_name_family" value="Instructor">
   <input type="hidden" name="lis_person_name_given" value="Siân">
   <input type="hidden" name="lis_person_sourcedid" value="school.edu:user">
   <input type="hidden" name="resource_link_description" value="This learning space is private">
   <input type="hidden" name="resource_link_id" value="res-0012612">
   <input type="hidden" name="resource_link_title" value="My Weekly Wiki">
   <input type="hidden" name="roles" value="Instructor">
   <input type="hidden" name="tool_consumer_info_product_family_code" value="sakai-unit">
   <input type="hidden" name="tool_consumer_info_version" value="0.9">
   <input type="hidden" name="tool_consumer_instance_description" value="University of School (LMSng)">
   <input type="hidden" name="tool_consumer_instance_guid" value="lmsng.school.edu">
   <input type="hidden" name="user_id" value="user-0016">
   <input type="hidden" name="oauth_callback" value="about:blank">
   <input type="hidden" name="lis_outcome_service_url" value="....">
   <input type="hidden" name="lis_result_sourcedid" value="{&quot;zap&quot; : &quot;Siân JSON 1234 Sourcedid <>&amp;lt;&quot;}">
   <input type="hidden" name="custom_simple_key" value="custom_simple_value">
   <input type="hidden" name="custom_complex____________key" value="Complex!@#$^*(){}[]½Value">
   <input type="hidden" name="lti_version" value="LTI-1p0">
   <input type="hidden" name="lti_message_type" value="basic-lti-launch-request">
   <input type="submit" name="ext_submit" value="Finish Launch">
   <input type="hidden" name="oauth_version" value="1.0">
   <input type="hidden" name="oauth_nonce" value="505005bff1c2cf2e1750ed914e489a9b">
   <input type="hidden" name="oauth_timestamp" value="1427022367">
   <input type="hidden" name="oauth_consumer_key" value="my consumer key">
   <input type="hidden" name="oauth_signature_method" value="HMAC-SHA1">
   <input type="hidden" name="oauth_signature" value="I need generate this value">
   <input type="hidden" name="ext_submit" value="Finish Launch">
   <input type="submit" name="submit" value="submit"></form>


<iframe name="basicltiLaunchFrame" id="basicltiLaunchFrame" src="" width="100%" height="900" scrolling="auto" frameborder="1" transparency="">

</iframe>
</body>

我需要在这一行生成 OAuth 签名

<input type="hidden" name="oauth_signature" value="I need generate this value">

我该怎么做?

【问题讨论】:

    标签: java oauth


    【解决方案1】:

    用 JavaScript 来做:

    document.getElementById("oauth_signature").value = "Value you want to generate";
    

    编辑:

    您可以使用加密算法库(例如https://code.google.com/p/crypto-js/)来计算 oauth 签名:

    var key = document.getElementById("oauth_consumer_key").value
    var hash = CryptoJS.SHA1(key);
    document.getElementById("oauth_signature").value = hash
    

    根据 oauth_signature_method 的值,您应该使用适当的加密算法。

    switch(document.getElementById("oauth_signature_method").value) {
        case "HMAC-SHA1":
            var hash = CryptoJS.SHA1(key)
            break;
        case "HMAC-SHA-2":
            var hash = CryptoJS.SHA256(key)
            break;
        case ...
    }
    

    【讨论】:

    • 这实际上不太正确。它并不像运行 SHA-1 那样简单 - 您需要根据姿势 和 被发布到的 URL 上的所有属性计算一个基本字符串,添加密钥和秘密,计算签名,然后将签名添加到帖子数据中。 LTI 1.x 使用 OAuth 1.0a tools.ietf.org/html/rfc5849 进行签名。
    【解决方案2】:

    您可以使用 Sakai LTI 代码(开源 Apache)来进行签名。我在以下位置提取了 Sakai LTI 实现的可移植部分:

    https://github.com/tsugiproject/tsugi-util

    工件甚至在 Sonatype 中,因此您可以将其添加到您的 maven 配置中 - 文档位于 github 的 README 中。您正在寻找的是两种方法:

    • signProperties 添加 oauth 字段和签名

    • postLaunchHTML 生成 HTML 表单并自动提交

    https://github.com/tsugiproject/tsugi-util/blob/master/src/java/org/tsugi/basiclti/BasicLTIUtil.java

    这些方法有很多参数,但这样它们就非常独立于 Java 应用程序的任何存储或其他方面。

    【讨论】:

      猜你喜欢
      • 2017-03-17
      • 2012-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多