在Java中生成OAuth签名

时间:2015-04-08 07:16:17

标签: java oauth

我正在开发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">

我该怎么做?

2 个答案:

答案 0 :(得分:0)

使用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 ...
}

答案 1 :(得分:0)

您可以使用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应用程序的任何存储或其他方面都非常独立。