3 <title>SSO example</title>
6 <style type="text/css">
26 <div id='serviceContainer' class="topleftcorner">
27 <table id="service" border="5">
35 <tr><td><button onclick='queryMethods()'>Query Methods</button></td></tr>
36 <tr><td><button onclick='queryMechanisms()'>Query Mechanisms</button></td>
37 <td><form>Method: <input type="text" name="serv_method" id="serv_method"><br></form></td>
39 <tr><td><button onclick='queryIdentities()'>Query Identities</button></td>
40 <td><form>Filter(Input is json and keys can only be 'Type', 'Owner' or 'Caption'
41 e.g. {"Type":"WEB"} or {"Type":"APPLICATION"} or {"Type":"NETWORK"} or
42 {"Owner":{"sysContext":"*","appContext":"*"}} or {"Caption":"cap"}
43 <input type="text" name="serv_filter" id="serv_filter"><br></form></td>
45 <tr><td><button onclick='getIdentity()'>Get Identity</button></td>
46 <td><form>Id: <input type="text" name="serv_identityid" id="serv_identityid"><br></form></td>
48 <tr><td><button onclick='clearDB()'>Clear DB</button></td></tr>
51 <table id="creat_identity" border="5">
54 <H3><button onclick='onCreateIdentity()'><BR>Create Identity</button>
55 <button onclick='onUpdateIdentity()'><BR>Update Identity</button></H3>
58 <tr><td><form>Select Type: <select name="cident_type" id="cident_type">
59 <option value = "APP" >Application</option>
60 <option value = "WEB" selected >Web</option>
61 <option value = "NET" >Network</option>
62 </select></form></td></tr>
63 <tr><td><form>Username:
64 <input type="text" name="cident_username" id="cident_username"><br></form></td></tr>
65 <tr><td><form>Password:
66 <input type="text" name="cident_secret" id="cident_secret"><br></form></td></tr>
67 <tr><td><form>StoreSecret:
68 <input type="checkbox" name="cident_storesecret" id="cident_storesecret"><br></form></td></tr>
69 <tr><td><form>Caption:
70 <input type="text" name="cident_caption" id="cident_caption"><br></form></td></tr>
71 <tr><td><form>Realms(realm1,realm2,..):
72 <input type="text" name="cident_realms" id="cident_realms"><br></form></td></tr>
73 <tr><td><form>Owner(appcontext,syscontext):
74 <input type="text" name="cident_owner" id="cident_owner"><br></form></td></tr>
75 <tr><td><form>ACL(json: [{"secContext":{"sysContext":"*","appContext":"*"},
76 "method":"password","mechanisms":["password"]}]):
77 <input type="text" name="cident_acl" id="cident_acl"><br></form></td></tr>
79 <div id='resultContainer' class="topcenter">>
80 <form name='form_out'>
81 <textarea name="form_text" id="form_text" style="width:100%; height:100%"></textarea>
87 <div id='identityContainer' class="toprightcorner">
88 <table width="115%" id="identity" border="5">
92 <form>Selected Identity JSId:
93 <select name="ident_options" id="ident_options" onchange="onIdentityChanged(this)">
99 <tr><td><button onclick='startSession()'>Start Session</button></td>
100 <td><form>Method: <input type="text" name="ident_method" id="ident_method"><br></form></td>
102 <tr><td><button onclick='requestCredentialsUpdate()'>Update Credentials</button></td>
103 <td><form>Message: <input type="text" name="ident_message" id="ident_message"><br></form></td>
105 <tr><td><button onclick='store(null)'>Store</button></td>
107 <tr><td><button onclick='addReference()'>Add Reference</button></td>
108 <td><form>Reference: <input type="text" name="ident_addref" id="ident_addref"><br></form></td>
110 <tr><td><button onclick='removeReference()'>Remove Reference</button></td>
111 <td><form>Reference: <input type="text" name="ident_remref" id="ident_remref"><br></form></td>
113 <tr><td><button onclick='removeIdentity()'>Remove</button></td>
115 <tr><td><button onclick='signout()'>Signout</button></td>
119 <table id="session" border="5">
123 <form>Selected Session JSId: <select name="sess_options" id="sess_options"></select></form>
129 <tr><td><button onclick='queryAvailableMechanisms()'>QueryAvailableMechanisms</button></td>
130 <td><form>WantedMechanisms(mech1,mech2,..):
131 <input type="text" name="sess_mechs" id="sess_mechs"><br></form></td>
133 <tr><td><button onclick='challenge()'>Challenge</button></td>
134 <td><form>Mechanism: <input type="text" name="sess_mech" id="sess_mech"><br></form></td>
135 <td><form>SessionData (json: {"key1":"value1","key2":"value2",...}):
136 <input type="text" name="sess_data" id="sess_data"><br></form></td>
138 <tr><td><button onclick='cancel()'>Cancel</button></td>
145 function _logData(data) {
146 var old = document.form_out.form_text.value
147 document.form_out.form_text.value = data + '\n\n' + old;
150 function queryMethods() {
151 tizen.sso.authService.queryMethods().then(function(result) {
152 _logData('QueryMethods successful: ' + JSON.stringify(result));},
153 function (err) { document.form_out.form_text.value += "\n" + "QueryMethods failed: " + err;});
156 function queryMechanisms() {
157 var method = document.getElementById('serv_method').value;
158 var res = tizen.sso.authService.queryMechanisms(method);
160 res.then(function (result) {
161 _logData('QueryMechanisms successful: ' + JSON.stringify(result));},
162 function(err) {_logData('QueryMechanisms failed: ' + err);});
164 _logData('mechanisms not found');
168 function queryIdentities() {
169 var str = document.getElementById('serv_filter').value;
171 if (typeof str === 'string' && str.length > 0) {
172 filters = JSON.parse(str);
174 var res = tizen.sso.authService.queryIdentities(filters);
176 res.then(onQueryIdentitiesComplete, function(err) {
177 _logData('QueryIdentities failed: ' + err);});
179 _logData('identities not found');
183 function onQueryIdentitiesComplete(result) {
184 _logData('QueryIdentities successful: ' + JSON.stringify(result));
187 function getIdentity() {
188 var id = parseFloat(document.getElementById('serv_identityid').value);
190 _logData('Invalid id');
193 var res = tizen.sso.authService.getIdentity(id);
195 res.then(onGetIdentityComplete, function(err) {
196 _logData('GetIdentity failed: ' + err);});
198 _logData('Identity not found with the specified id');
202 function onGetIdentityComplete(result) {
203 _logData('GetIdentity successful: ' + JSON.stringify(result));
207 tizen.sso.authService.clear().then(function(result) {
208 _logData('Clear successful: ' + JSON.stringify(result));
209 clearSelect(document.getElementById("ident_options"));
210 clearSelect(document.getElementById("sess_options"));},
211 function(err) {_logData('Clear failed: ' + err);});
216 var str = document.getElementById('cident_owner').value;
217 if (typeof str === 'string' && str.length > 0) {
218 arr = str.split(",");
219 if (arr.length == 2) {
220 owner.sysContext = arr[0];
221 owner.appContext = arr[1];
225 str = document.getElementById('cident_realms').value;
226 if (typeof str === 'string' && str.length > 0) {
227 arr = str.split(",");
228 if (arr.length > 0) realms = arr;
231 str = document.getElementById('cident_acl').value;
232 if (typeof str === 'string' && str.length > 0) {
233 acl = JSON.parse(str);
236 'type': document.getElementById('cident_type').value,
237 'username': document.getElementById('cident_username').value,
238 'secret': document.getElementById('cident_secret').value,
239 'storeSecret': document.getElementById('cident_storesecret').checked,
240 'caption': document.getElementById('cident_caption').value,
243 'accessControlList': acl
248 function onCreateIdentity() {
249 var info = getInfo();
250 var res = tizen.sso.authService.createIdentity(info);
251 if (res.syncOpErrorMsg != null) {
252 _logData('Identity creation FAILED with error: ' + res.asyncOpErrorMsg);
256 identityAdded(res.identity);
257 _logData('Identity is added with jsid: ' + res.identity.jsid);
260 function onUpdateIdentity() {
261 var info = getInfo();
265 function identityAdded(ident) {
266 var select = document.getElementById("ident_options");
267 var option = document.createElement('option');
268 option.text = option.value = ident.jsid;
269 select.add(option, 0);
271 ident.onsignedout = function (ident) {
272 _logData('Identity with id ' + ident.info.id + ' is signedout and jsid is '
274 ident.onremoved = onIdentityRemoved;
277 function onIdentityRemoved(ident) {
278 _logData('Identity with id ' + ident.info.id + ' is removed and its jsid is '
280 if (document.getElementById('ident_options').value == ident.jsid) {
281 for (var i = 0; i < ident.sessions.length; i++) {
282 var sessionobj = ident.sessions[i];
283 sessionobj.removeEventListener('statechanged', onSessionStateChanged);
284 document.getElementById('sess_options').remove(i);
286 clearSelectOption(document.getElementById("ident_options"), ident.jsid);
290 function clearSelectOption(element, value) {
291 for (var i=0; i<element.length; i++) {
292 if (element.options[i].value == value)
297 function clearSelect(element) {
298 for (var i=0; i<element.length; i++) {
303 function onIdentityChanged(select) {
304 var jsid = document.getElementById("ident_options").value;
305 _logData('Identity selected with jsid: ' + jsid);
306 var identobj = tizen.sso.authService.getIdentityByJSId(jsid);
307 if (identobj == null) {
308 _logData('identity NOT found');
311 clearSelect(document.getElementById("sess_options"));
312 for (var i = 0; i < identobj.sessions.length; i++) {
313 var option = document.createElement('option');
314 option.text = option.value = identobj.sessions[i].jsid;
315 document.getElementById('sess_options').add(option, 0);
320 function startSession() {
321 var identobj = tizen.sso.authService.getIdentityByJSId(
322 document.getElementById('ident_options').value);
323 if (identobj == null) {
324 _logData('identity NOT selected/found. Please select/create an identity first');
327 _logData('identity found with JSId: ' + identobj.jsid);
328 var method = document.getElementById('ident_method').value;
330 var res = identobj.startSession(method);
332 res.then(onStartSessionComplete,
333 function(err) {_logData('startSession failed: ' + err);});
335 _logData('Session cannot be started');
339 function onStartSessionComplete(sessionobj) {
340 _logData('startSession successful: ' + JSON.stringify(sessionobj));
341 sessionobj.addEventListener('statechanged', onSessionStateChanged);
343 _logData('Session with jsid as ' + sessionobj.jsid + ' is added for identity with jsid '
344 + sessionobj.identityJSId);
345 var select = document.getElementById("ident_options");
346 if (select.value == sessionobj.identityJSId) {
347 var sess_select = document.getElementById("sess_options");
348 var option = document.createElement('option');
349 option.text = option.value = sessionobj.jsid;
350 sess_select.add(option, 0);
354 function onSessionStateChanged(event) {
355 var sessionobj = event.session;
356 _logData('Session with jsid ' + sessionobj.jsid + ' state has changed to '
357 + sessionobj.sessionState);
360 function requestCredentialsUpdate() {
361 var identobj = tizen.sso.authService.getIdentityByJSId(
362 document.getElementById('ident_options').value);
363 if (identobj == null) {
364 _logData('identity NOT selected/found. Please select/create an identity first');
367 _logData('identity found with JSId: ' + identobj.jsid);
368 var message = document.getElementById('ident_message').value;
370 var res = identobj.requestCredentialsUpdate(message);
372 res.then(function(msg) {document.form_out.form_text.value += "\n" +
373 "requestCredentialsUpdate succeeded";},
374 function(err) {_logData('requestCredentialsUpdate failed: ' + err);});
376 _logData('requestCredentialsUpdate failed');
380 function store(info) {
381 var identobj = tizen.sso.authService.getIdentityByJSId(
382 document.getElementById('ident_options').value);
383 if (identobj == null) {
384 _logData('identity NOT selected/found. Please select/create an identity first');
387 _logData('identity found with JSId: ' + identobj.jsid);
388 if (info != null) identobj.updateInfo(info);
390 identobj.store().then(function(msg) {
391 _logData('store succeeded with resp: ' + JSON.stringify(msg));},
392 function(err) {_logData('store failed: ' + err);});
395 function addReference() {
396 var identobj = tizen.sso.authService.getIdentityByJSId(
397 document.getElementById('ident_options').value);
398 if (identobj == null) {
399 _logData('identity NOT selected/found. Please select/create an identity first');
402 _logData('identity found with JSId: ' + identobj.jsid);
403 var reference = document.getElementById('ident_addref').value;
405 var res = identobj.addReference(reference);
407 res.then(function(msg) {_logData('addReference succeeded');},
408 function(err) {_logData('addReference failed: ' + err);});
410 _logData('addreference failed');
414 function removeReference() {
415 var identobj = tizen.sso.authService.getIdentityByJSId(
416 document.getElementById('ident_options').value);
417 if (identobj == null) {
418 _logData('identity NOT selected/found. Please select/create an identity first');
421 _logData('identity found with JSId: ' + identobj.jsid);
422 var reference = document.getElementById('ident_remref').value;
424 var res = identobj.removeReference(reference);
426 res.then(function(msg) {_logData('removeReference succeeded');},
427 function(err) {_logData('removeReference failed: ' + err);});
429 _logData('removeReference failed');
433 function removeIdentity() {
434 var identobj = tizen.sso.authService.getIdentityByJSId(
435 document.getElementById('ident_options').value);
436 if (identobj == null) {
437 _logData('identity NOT selected/found. Please select/create an identity first');
440 _logData('identity found with JSId: ' + identobj.jsid);
441 identobj.remove().then(onIdentityRemoved,
442 function(err) {_logData('remove failed: ' + err);});
446 var identobj = tizen.sso.authService.getIdentityByJSId(
447 document.getElementById('ident_options').value);
448 if (identobj == null) {
449 _logData('identity NOT selected/found. Please select/create an identity first');
452 _logData('identity found with JSId: ' + identobj.jsid);
453 identobj.signout().then(function(msg) {
454 _logData('signout succeeded');},
455 function(err) {_logData('signout failed: ' + err);});
458 //authsession interface
459 function queryAvailableMechanisms() {
460 var identobj = tizen.sso.authService.getIdentityByJSId(
461 document.getElementById('ident_options').value);
462 if (identobj == null) {
463 _logData('identity NOT selected/found. Please select/create an identity first');
466 _logData('identity found with JSId: ' + identobj.jsid);
467 var sessionobj = identobj.getSessionByJSId(document.getElementById('sess_options').value);
468 if (sessionobj == null) {
469 _logData('session NOT selected/found. Please select/create session first');
472 _logData('session found with JSId: ' + sessionobj.jsid);
474 var str = document.getElementById('sess_mechs').value;
475 var wantedMechs = [];
476 if (typeof str === 'string' && str.length > 0) {
477 arr = str.split(",");
478 if (arr.length > 0) wantedMechs = arr;
480 sessionobj.queryAvailableMechanisms(wantedMechs).then(function(msg) {
481 _logData('queryAvailableMechanisms succeeded with mechanisms:' + JSON.stringify(msg));},
482 function(err) {_logData('queryAvailableMechanisms failed: ' + err);});
485 function challenge() {
486 var identobj = tizen.sso.authService.getIdentityByJSId(
487 document.getElementById('ident_options').value);
488 if (identobj == null) {
489 _logData('identity NOT selected/found. Please select/create an identity first');
492 _logData('identity found with JSId: ' + identobj.jsid);
493 var sessionobj = identobj.getSessionByJSId(document.getElementById('sess_options').value);
494 if (sessionobj == null) {
495 _logData('session NOT selected/found. Please select/create session first');
498 _logData('session found with JSId: ' + sessionobj.jsid);
500 var mech = document.getElementById('sess_mech').value;
501 var sessionData = document.getElementById('sess_data').value;
503 sessionobj.challenge(mech, sessionData).then(function(msg) {
504 _logData('challenge succeeded with sessionData: ' + JSON.stringify(msg));},
505 function(err) {_logData('challenge failed: ' + err);});
509 var identobj = tizen.sso.authService.getIdentityByJSId(
510 document.getElementById('ident_options').value);
511 if (identobj == null) {
512 _logData('identity NOT selected/found. Please select/create an identity first');
515 _logData('identity found with JSId: ' + identobj.jsid);
516 var sessionobj = identobj.getSessionByJSId(document.getElementById('sess_options').value);
517 if (sessionobj == null) {
518 _logData('session NOT selected/found. Please select/create session first');
521 _logData('session found with JSId: ' + sessionobj.jsid);
523 sessionobj.removeEventListener('statechanged', onSessionStateChanged);
524 sessionobj.cancel().then(function(msg) {
525 _logData('cancel succeeded');},
526 function(err) {_logData('cancel failed: ' + err);});
527 clearSelectOption(document.getElementById('sess_options'), sessionobj.jsid);
530 window.onload = function() {
531 document.getElementById('cident_acl').value =
532 JSON.stringify([{"secContext":{"sysContext":"*","appContext":"*"},
533 "method":"password","mechanisms":["password"]}]);
534 document.getElementById('cident_owner').value = "*,*";
535 document.getElementById('serv_filter').value =
536 JSON.stringify({Type:"WEB",Owner:{"sysContext":"*","appContext":"*"},Caption:"cap"});