Skip to content

Commit 8477dcf

Browse files
authored
Merge pull request #82 from adlnet/attachments
Attachments
2 parents f21315f + 1e490e6 commit 8477dcf

File tree

6 files changed

+224
-15
lines changed

6 files changed

+224
-15
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ var stmt = ADL.XAPIStatement(myactor, ADL.verbs.launched, myactivity);
378378
```
379379

380380
##### Send Statement
381-
`function sendStatement(statement, callback)`
381+
`function sendStatement(statement, callback, [attachments])`
382382
Sends a single Statement to the LRS using a PUT request. This
383383
method will automatically create the Statement ID. Providing a
384384
function to call after the send Statement request will make
@@ -407,6 +407,29 @@ ADL.XAPIWrapper.sendStatement(stmt, function(resp, obj){
407407
>> [4edfe763-8b84-41f1-a355-78b7601a6fe8]: 200 - OK
408408
```
409409

410+
###### Send Statement with Attachments
411+
The wrapper can construct a `multipart/mixed` POST for a single statement that includes attachments. Attachments should be
412+
supplied as an array in the 3rd parameter to `sendStatement`. Attachments are optional. The attachments array should consist of
413+
objects that have a `type` and a `value` field. `Type` should be the metadata description of the attachment as described by the spec, and `value`
414+
should be a string containing the data to post. The type field does not need to include the SHA2 or the length. These will be computed
415+
for you. The type may optionally be the string 'signature'. In this case, the wrapper will construct the proper metadata block.
416+
417+
```JavaScript
418+
var attachment = {};
419+
attachment.type = {
420+
"usageType": "http://adlnet.gov/expapi/attachments/signature",
421+
"display": {
422+
"en-US": "A JWT signature"
423+
},
424+
"description": {
425+
"en-US": "A signature proving the statement was not modified"
426+
},
427+
"contentType": "application/octet-stream"
428+
};
429+
attachment.value = "somehugestring";
430+
ADL.XAPIWrapper.sendStatement(stmt,callback,[attachment]);
431+
```
432+
410433
###### Send Statement with URL query string values
411434
The wrapper looks for URL query string values to include in
412435
its internal configuration. If certain keys

dist/xapiwrapper.min.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/cryptojs_v3.1.2.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,3 +927,25 @@ var CryptoJS = CryptoJS || (function (Math, undefined) {
927927

928928
return C;
929929
}(Math));
930+
931+
932+
//add the sha256 functions
933+
934+
/*
935+
CryptoJS v3.1.2
936+
code.google.com/p/crypto-js
937+
(c) 2009-2013 by Jeff Mott. All rights reserved.
938+
code.google.com/p/crypto-js/wiki/License
939+
*/
940+
var CryptoJS=CryptoJS||function(h,s){var f={},g=f.lib={},q=function(){},m=g.Base={extend:function(a){q.prototype=this;var c=new q;a&&c.mixIn(a);c.hasOwnProperty("init")||(c.init=function(){c.$super.init.apply(this,arguments)});c.init.prototype=c;c.$super=this;return c},create:function(){var a=this.extend();a.init.apply(a,arguments);return a},init:function(){},mixIn:function(a){for(var c in a)a.hasOwnProperty(c)&&(this[c]=a[c]);a.hasOwnProperty("toString")&&(this.toString=a.toString)},clone:function(){return this.init.prototype.extend(this)}},
941+
r=g.WordArray=m.extend({init:function(a,c){a=this.words=a||[];this.sigBytes=c!=s?c:4*a.length},toString:function(a){return(a||k).stringify(this)},concat:function(a){var c=this.words,d=a.words,b=this.sigBytes;a=a.sigBytes;this.clamp();if(b%4)for(var e=0;e<a;e++)c[b+e>>>2]|=(d[e>>>2]>>>24-8*(e%4)&255)<<24-8*((b+e)%4);else if(65535<d.length)for(e=0;e<a;e+=4)c[b+e>>>2]=d[e>>>2];else c.push.apply(c,d);this.sigBytes+=a;return this},clamp:function(){var a=this.words,c=this.sigBytes;a[c>>>2]&=4294967295<<
942+
32-8*(c%4);a.length=h.ceil(c/4)},clone:function(){var a=m.clone.call(this);a.words=this.words.slice(0);return a},random:function(a){for(var c=[],d=0;d<a;d+=4)c.push(4294967296*h.random()|0);return new r.init(c,a)}}),l=f.enc={},k=l.Hex={stringify:function(a){var c=a.words;a=a.sigBytes;for(var d=[],b=0;b<a;b++){var e=c[b>>>2]>>>24-8*(b%4)&255;d.push((e>>>4).toString(16));d.push((e&15).toString(16))}return d.join("")},parse:function(a){for(var c=a.length,d=[],b=0;b<c;b+=2)d[b>>>3]|=parseInt(a.substr(b,
943+
2),16)<<24-4*(b%8);return new r.init(d,c/2)}},n=l.Latin1={stringify:function(a){var c=a.words;a=a.sigBytes;for(var d=[],b=0;b<a;b++)d.push(String.fromCharCode(c[b>>>2]>>>24-8*(b%4)&255));return d.join("")},parse:function(a){for(var c=a.length,d=[],b=0;b<c;b++)d[b>>>2]|=(a.charCodeAt(b)&255)<<24-8*(b%4);return new r.init(d,c)}},j=l.Utf8={stringify:function(a){try{return decodeURIComponent(escape(n.stringify(a)))}catch(c){throw Error("Malformed UTF-8 data");}},parse:function(a){return n.parse(unescape(encodeURIComponent(a)))}},
944+
u=g.BufferedBlockAlgorithm=m.extend({reset:function(){this._data=new r.init;this._nDataBytes=0},_append:function(a){"string"==typeof a&&(a=j.parse(a));this._data.concat(a);this._nDataBytes+=a.sigBytes},_process:function(a){var c=this._data,d=c.words,b=c.sigBytes,e=this.blockSize,f=b/(4*e),f=a?h.ceil(f):h.max((f|0)-this._minBufferSize,0);a=f*e;b=h.min(4*a,b);if(a){for(var g=0;g<a;g+=e)this._doProcessBlock(d,g);g=d.splice(0,a);c.sigBytes-=b}return new r.init(g,b)},clone:function(){var a=m.clone.call(this);
945+
a._data=this._data.clone();return a},_minBufferSize:0});g.Hasher=u.extend({cfg:m.extend(),init:function(a){this.cfg=this.cfg.extend(a);this.reset()},reset:function(){u.reset.call(this);this._doReset()},update:function(a){this._append(a);this._process();return this},finalize:function(a){a&&this._append(a);return this._doFinalize()},blockSize:16,_createHelper:function(a){return function(c,d){return(new a.init(d)).finalize(c)}},_createHmacHelper:function(a){return function(c,d){return(new t.HMAC.init(a,
946+
d)).finalize(c)}}});var t=f.algo={};return f}(Math);
947+
(function(h){for(var s=CryptoJS,f=s.lib,g=f.WordArray,q=f.Hasher,f=s.algo,m=[],r=[],l=function(a){return 4294967296*(a-(a|0))|0},k=2,n=0;64>n;){var j;a:{j=k;for(var u=h.sqrt(j),t=2;t<=u;t++)if(!(j%t)){j=!1;break a}j=!0}j&&(8>n&&(m[n]=l(h.pow(k,0.5))),r[n]=l(h.pow(k,1/3)),n++);k++}var a=[],f=f.SHA256=q.extend({_doReset:function(){this._hash=new g.init(m.slice(0))},_doProcessBlock:function(c,d){for(var b=this._hash.words,e=b[0],f=b[1],g=b[2],j=b[3],h=b[4],m=b[5],n=b[6],q=b[7],p=0;64>p;p++){if(16>p)a[p]=
948+
c[d+p]|0;else{var k=a[p-15],l=a[p-2];a[p]=((k<<25|k>>>7)^(k<<14|k>>>18)^k>>>3)+a[p-7]+((l<<15|l>>>17)^(l<<13|l>>>19)^l>>>10)+a[p-16]}k=q+((h<<26|h>>>6)^(h<<21|h>>>11)^(h<<7|h>>>25))+(h&m^~h&n)+r[p]+a[p];l=((e<<30|e>>>2)^(e<<19|e>>>13)^(e<<10|e>>>22))+(e&f^e&g^f&g);q=n;n=m;m=h;h=j+k|0;j=g;g=f;f=e;e=k+l|0}b[0]=b[0]+e|0;b[1]=b[1]+f|0;b[2]=b[2]+g|0;b[3]=b[3]+j|0;b[4]=b[4]+h|0;b[5]=b[5]+m|0;b[6]=b[6]+n|0;b[7]=b[7]+q|0},_doFinalize:function(){var a=this._data,d=a.words,b=8*this._nDataBytes,e=8*a.sigBytes;
949+
d[e>>>5]|=128<<24-e%32;d[(e+64>>>9<<4)+14]=h.floor(b/4294967296);d[(e+64>>>9<<4)+15]=b;a.sigBytes=4*d.length;this._process();return this._hash},clone:function(){var a=q.clone.call(this);a._hash=this._hash.clone();return a}});s.SHA256=q._createHelper(f);s.HmacSHA256=q._createHmacHelper(f)})(Math);
950+
(function(){var h=CryptoJS,s=h.enc.Utf8;h.algo.HMAC=h.lib.Base.extend({init:function(f,g){f=this._hasher=new f.init;"string"==typeof g&&(g=s.parse(g));var h=f.blockSize,m=4*h;g.sigBytes>m&&(g=f.finalize(g));g.clamp();for(var r=this._oKey=g.clone(),l=this._iKey=g.clone(),k=r.words,n=l.words,j=0;j<h;j++)k[j]^=1549556828,n[j]^=909522486;r.sigBytes=l.sigBytes=m;this.reset()},reset:function(){var f=this._hasher;f.reset();f.update(this._iKey)},update:function(f){this._hasher.update(f);return this},finalize:function(f){var g=
951+
this._hasher;f=g.finalize(f);g.reset();return g.finalize(this._oKey.clone().concat(f))}})})();

src/xapi-launch.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,25 @@ function xAPILaunch(cb, terminate_on_unload)
103103
var launchToken = getQueryVariable("xAPILaunchKey");
104104
var launchEndpoint = getQueryVariable("xAPILaunchService");
105105
var encrypted = getQueryVariable("encrypted");
106-
if (!encrypted)
106+
if (encrypted)
107107
{
108108
//here, we'd have to implement decryption for the data. This makes little sense in a client side only course
109109
}
110+
111+
xAPILaunch.terminate = function(message)
112+
{
113+
var launch = new URL(launchEndpoint);
114+
launch.pathname += "launch/" + launchToken + "/terminate";
115+
var xhr2 = new XMLHttpRequest();
116+
xhr2.withCredentials = true;
117+
xhr2.crossDomain = true;
118+
119+
xhr2.open('POST', launch.toString(), false);
120+
xhr2.setRequestHeader("Content-type" , "application/json");
121+
xhr2.send(JSON.stringify({"code":0,"description": message ||"User closed content"}));
122+
123+
}
124+
110125
if (!launchToken || !launchEndpoint)
111126
return cb("invalid launch parameters");
112127
var launch = new URL(launchEndpoint);
@@ -138,15 +153,7 @@ function xAPILaunch(cb, terminate_on_unload)
138153
{
139154
if (!terminate_on_unload)
140155
return;
141-
var launch = new URL(launchEndpoint);
142-
launch.pathname += "launch/" + launchToken + "/terminate";
143-
var xhr2 = new XMLHttpRequest();
144-
xhr2.withCredentials = true;
145-
xhr2.crossDomain = true;
146-
147-
xhr2.open('POST', launch.toString(), false);
148-
xhr2.setRequestHeader("Content-type" , "application/json");
149-
xhr2.send('{"code":0,"description":"User closed content"}');
156+
xAPILaunch.terminate("User closed content")
150157
}
151158
var wrapper = new ADL.XAPIWrapper.constructor();
152159
wrapper.changeConfig(conf);

src/xapiwrapper.js

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ function toSHA1(text){
4040
else
4141
return Crypto.util.bytesToHex( Crypto.SHA1(text,{asBytes:true}) );
4242
}
43+
function toSHA256(text){
44+
if(CryptoJS && CryptoJS.SHA256)
45+
return CryptoJS.SHA256(text).toString();
46+
47+
}
4348

4449
// check if string or object is date, if it is, return date object
4550
// feburary 31st == march 3rd in this solution
@@ -69,6 +74,24 @@ function isDate(date) {
6974

7075
(function(ADL){
7176
log.debug = false;
77+
78+
function getByteLen(normal_val) {
79+
// Force string type
80+
normal_val = String(normal_val);
81+
82+
var byteLen = 0;
83+
for (var i = 0; i < normal_val.length; i++) {
84+
var c = normal_val.charCodeAt(i);
85+
byteLen += c < (1 << 7) ? 1 :
86+
c < (1 << 11) ? 2 :
87+
c < (1 << 16) ? 3 :
88+
c < (1 << 21) ? 4 :
89+
c < (1 << 26) ? 5 :
90+
c < (1 << 31) ? 6 : Number.NaN;
91+
}
92+
return byteLen;
93+
}
94+
7295
/*
7396
* Config object used w/ url params to configure the lrs object
7497
* change these to match your lrs
@@ -108,6 +131,9 @@ function isDate(date) {
108131
*/
109132
XAPIWrapper = function(config, verifyxapiversion)
110133
{
134+
135+
136+
111137
this.lrs = getLRSObject(config || {});
112138
if (this.lrs.user && this.lrs.password)
113139
updateAuth(this.lrs, this.lrs.user, this.lrs.password);
@@ -278,7 +304,7 @@ function isDate(date) {
278304
* ADL.XAPIWrapper.log("[" + obj.id + "]: " + resp.status + " - " + resp.statusText);});
279305
* >> [4edfe763-8b84-41f1-a355-78b7601a6fe8]: 204 - NO CONTENT
280306
*/
281-
XAPIWrapper.prototype.sendStatement = function(stmt, callback)
307+
XAPIWrapper.prototype.sendStatement = function(stmt, callback, attachments)
282308
{
283309
if (this.testConfig())
284310
{
@@ -293,14 +319,84 @@ function isDate(date) {
293319
id = ADL.ruuid();
294320
stmt['id'] = id;
295321
}
322+
323+
var payload = JSON.stringify(stmt);
324+
var extraHeaders = null;
325+
if(attachments && attachments.length > 0)
326+
{
327+
extraHeaders = {}
328+
payload = this.buildMultipartPost(stmt,attachments,extraHeaders)
329+
}
296330
var resp = ADL.XHR_request(this.lrs, this.lrs.endpoint+"statements",
297-
"POST", JSON.stringify(stmt), this.lrs.auth, callback, {"id":id},null,false,this.withCredentials);
331+
"POST", payload, this.lrs.auth, callback, {"id":id},null,extraHeaders,this.withCredentials);
298332
if (!callback)
299333
return {"xhr":resp,
300334
"id" :id};
301335
}
302336
};
337+
/*
338+
* Build the post body to include the multipart boundries, edit the statement to include the attachment types
339+
* extraHeaders should be an object. It will have the multipart boundary value set
340+
* attachments should be an array of objects of the type
341+
* {
342+
type:"signature" || {
343+
usageType : URI,
344+
display: Language-map
345+
description: Language-map
346+
},
347+
value : a UTF8 string containing the binary data of the attachment. For string values, this can just be the JS string.
348+
}
349+
*/
350+
XAPIWrapper.prototype.buildMultipartPost = function(statement,attachments,extraHeaders)
351+
{
352+
statement.attachments = [];
353+
for(var i =0; i < attachments.length; i++)
354+
{
355+
//replace the term 'signature' with the hard coded definition for a signature attachment
356+
if(attachments[i].type == "signature")
357+
{
358+
attachments[i].type = {
359+
"usageType": "http://adlnet.gov/expapi/attachments/signature",
360+
"display": {
361+
"en-US": "A JWT signature"
362+
},
363+
"description": {
364+
"en-US": "A signature proving the statement was not modified"
365+
},
366+
"contentType": "application/octet-stream"
367+
}
368+
}
369+
370+
//compute the length and the sha2 of the attachment
371+
attachments[i].type.length = attachments[i].value.length;
372+
attachments[i].type.sha2 = toSHA256(attachments[i].value);
373+
374+
//attach the attachment metadata to the statement
375+
statement.attachments.push(attachments[i].type)
376+
}
377+
378+
var body = "";
379+
var CRLF = "\r\n";
380+
var boundary = (Math.random()+' ').substring(2,10)+(Math.random()+' ').substring(2,10);
381+
382+
extraHeaders["Content-Type"] = "multipart/mixed; boundary=" + boundary;
303383

384+
body += CRLF + '--' + boundary + CRLF + 'Content-Type:application/json' + CRLF + "Content-Disposition: form-data; name=\"statement\"" + CRLF + CRLF;
385+
body += JSON.stringify(statement);
386+
387+
for(var i in attachments)
388+
{
389+
390+
body += CRLF + '--' + boundary + CRLF + 'X-Experience-API-Hash:' + attachments[i].type.sha2 + CRLF + "Content-Type:application/octet-stream" + CRLF + "Content-Transfer-Encoding: binary" + CRLF + CRLF
391+
body += attachments[i].value;
392+
}
393+
body += CRLF + "--" + boundary + "--" + CRLF
394+
395+
396+
397+
398+
return body;
399+
}
304400
/*
305401
* Send a list of statements to the LRS.
306402
* @param {array} stmtArray the list of statement objects to send

test/testAttachments.html

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<html>
2+
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Mocha Tests</title>
6+
<link href="./libs/mocha/2.2.5/mocha.css" rel="stylesheet" />
7+
<script src="./libs/jquery/2.1.4/jquery.min.js"></script>
8+
<script>
9+
localStorage.clear();
10+
</script>
11+
</head>
12+
13+
<body>
14+
15+
16+
<!-- <script src="../dist/xapiwrapper.min.js"></script>-->
17+
<script type="text/javascript" src="../lib/cryptojs_v3.1.2.js"></script>
18+
<script type="text/javascript" src="../src/verbs.js"></script>
19+
<script type="text/javascript" src="../src/xapistatement.js"></script>
20+
<script type="text/javascript" src="../src/xapiwrapper.js"></script>
21+
22+
<script type="text/javascript" src="../examples/stmtBank.js"></script>
23+
24+
<script src="../src/xapi-util.js" data-cover></script>
25+
26+
<script>
27+
var conf = {
28+
"endpoint" : "https://lrs.adlnet.gov/xapi/",
29+
"user" : "tom",
30+
"password" : "1234",
31+
};
32+
ADL.XAPIWrapper.changeConfig(conf);
33+
34+
var statement = {"actor":{"mbox":"mailto:[email protected]"}, "verb":{"id":"http://verb.com/do1"}, "object":{"id":"http://from.tom/act1", "objectType":"Activity", "definition":{"name":{"en-US": "soccer", "fr": "football", "de": "foossball"}}}};
35+
36+
37+
var attachmentMetadata = {
38+
"usageType": "http://adlnet.gov/expapi/attachments/asdf",
39+
"display":
40+
{
41+
"en-US": "asdfasdf"
42+
},
43+
"description":
44+
{
45+
"en-US": "asdfasdfasd"
46+
},
47+
"contentType": "application/octet-stream"
48+
}
49+
var attachment = {
50+
value: "this is a simple string attachment",
51+
type:attachmentMetadata
52+
}
53+
54+
55+
ADL.XAPIWrapper.sendStatement(statement,null,[attachment]);
56+
57+
</script>
58+
</body>
59+
60+
</html>

0 commit comments

Comments
 (0)