Skip to content

Commit a3b0f04

Browse files
committed
Ajax demo app: proper authentication, explicitely request json
1 parent fae6435 commit a3b0f04

2 files changed

Lines changed: 142 additions & 11 deletions

File tree

Client/Base64.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
var Base64 = {
2+
// private property
3+
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
4+
5+
// public method for encoding
6+
encode : function (input) {
7+
var output = "";
8+
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
9+
var i = 0;
10+
11+
input = Base64._utf8_encode(input);
12+
13+
while (i < input.length) {
14+
15+
chr1 = input.charCodeAt(i++);
16+
chr2 = input.charCodeAt(i++);
17+
chr3 = input.charCodeAt(i++);
18+
19+
enc1 = chr1 >> 2;
20+
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
21+
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
22+
enc4 = chr3 & 63;
23+
24+
if (isNaN(chr2)) {
25+
enc3 = enc4 = 64;
26+
} else if (isNaN(chr3)) {
27+
enc4 = 64;
28+
}
29+
30+
output = output +
31+
Base64._keyStr.charAt(enc1) + Base64._keyStr.charAt(enc2) +
32+
Base64._keyStr.charAt(enc3) + Base64._keyStr.charAt(enc4);
33+
34+
}
35+
36+
return output;
37+
},
38+
39+
// public method for decoding
40+
decode : function (input) {
41+
var output = "";
42+
var chr1, chr2, chr3;
43+
var enc1, enc2, enc3, enc4;
44+
var i = 0;
45+
46+
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
47+
48+
while (i < input.length) {
49+
50+
enc1 = Base64._keyStr.indexOf(input.charAt(i++));
51+
enc2 = Base64._keyStr.indexOf(input.charAt(i++));
52+
enc3 = Base64._keyStr.indexOf(input.charAt(i++));
53+
enc4 = Base64._keyStr.indexOf(input.charAt(i++));
54+
55+
chr1 = (enc1 << 2) | (enc2 >> 4);
56+
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
57+
chr3 = ((enc3 & 3) << 6) | enc4;
58+
59+
output = output + String.fromCharCode(chr1);
60+
61+
if (enc3 != 64) {
62+
output = output + String.fromCharCode(chr2);
63+
}
64+
if (enc4 != 64) {
65+
output = output + String.fromCharCode(chr3);
66+
}
67+
68+
}
69+
70+
output = Base64._utf8_decode(output);
71+
72+
return output;
73+
74+
},
75+
76+
// private method for UTF-8 encoding
77+
_utf8_encode : function (string) {
78+
string = string.replace(/\r\n/g,"\n");
79+
var utftext = "";
80+
81+
for (var n = 0; n < string.length; n++) {
82+
83+
var c = string.charCodeAt(n);
84+
85+
if (c < 128) {
86+
utftext += String.fromCharCode(c);
87+
}
88+
else if((c > 127) && (c < 2048)) {
89+
utftext += String.fromCharCode((c >> 6) | 192);
90+
utftext += String.fromCharCode((c & 63) | 128);
91+
}
92+
else {
93+
utftext += String.fromCharCode((c >> 12) | 224);
94+
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
95+
utftext += String.fromCharCode((c & 63) | 128);
96+
}
97+
98+
}
99+
100+
return utftext;
101+
},
102+
103+
// private method for UTF-8 decoding
104+
_utf8_decode : function (utftext) {
105+
var string = "";
106+
var i = 0;
107+
var c = c1 = c2 = 0;
108+
109+
while ( i < utftext.length ) {
110+
111+
c = utftext.charCodeAt(i);
112+
113+
if (c < 128) {
114+
string += String.fromCharCode(c);
115+
i++;
116+
}
117+
else if((c > 191) && (c < 224)) {
118+
c2 = utftext.charCodeAt(i+1);
119+
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
120+
i += 2;
121+
}
122+
else {
123+
c2 = utftext.charCodeAt(i+1);
124+
c3 = utftext.charCodeAt(i+2);
125+
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
126+
i += 3;
127+
}
128+
129+
}
130+
return string;
131+
}
132+
}

Client/index_served.html

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,29 @@
3636
<script src="Three.js"></script>
3737
<script src="RequestAnimationFrame.js"></script>
3838
<script src="ThreeJsViewer.js"></script>
39+
<script src="Base64.js"></script>
3940
<script>
41+
var viewer;
4042
function revisionUpdate(poid) {
4143
$('#roids').empty();
42-
$.ajax({url: '/rest/getAllRevisionsOfProject', data: 'poid='+poid, success: function(data){
44+
$.ajax({url: '/rest/getAllRevisionsOfProject', data: 'poid='+poid, dataType: 'json', success: function(data){
4345
var select = $('#roids');
4446
$.each(data.sRevision, function(i, revision){
4547
$('<option />').val(revision.oid).text(revision.id).appendTo(select);
4648
});
47-
}});
49+
}});
4850
}
4951
function projectUpdate() {
5052
$('#revision').show();
5153
$('#poids').empty();
52-
$.ajax({url: '/rest/getAllProjects', success: function(data) {
54+
$.ajax({url: '/rest/getAllProjects', dataType: 'json', success: function(data) {
5355
var select = $('#poids');
5456
$.each( data.sProject, function(i, project) {
5557
$('<option />').val(project.oid).text(project.name).appendTo(select);
5658
});
5759
revisionUpdate(select.val());
5860
}});
5961
}
60-
var viewer
6162
$(document).ready(function() {
6263
viewer = new ThreeJsViewer();
6364
viewer.init($('#viewerContainer'));
@@ -76,13 +77,11 @@
7677
};
7778
viewer.animate();
7879
$('#login').submit(function() {
79-
$.ajax({
80-
url: '/rest/login',
81-
username: $('#login input[name=username]').val(),
82-
password: $('#login input[name=password]').val(),
83-
data: $('#login').serialize(),
84-
success: projectUpdate
85-
});
80+
var username = $('#login input[name=username]').val();
81+
var password = $('#login input[name=password]').val();
82+
var auth = Base64.encode(username+':'+password);
83+
$.ajax({url: '/rest/ping', headers: {'Authorization': auth}});
84+
projectUpdate();
8685
return false;
8786
});
8887
$('#poids').change(function() {

0 commit comments

Comments
 (0)