[SSO] Add an example of how to authorize an app with Google using OAuth2
authorAlexander Kanavin <alex.kanavin@gmail.com>
Mon, 27 Oct 2014 14:52:27 +0000 (16:52 +0200)
committerAlexander Kanavin <alex.kanavin@gmail.com>
Thu, 30 Oct 2014 15:48:09 +0000 (17:48 +0200)
examples/index.html
examples/sso-advanced.html [moved from examples/sso.html with 100% similarity]
examples/sso-google-oauth.html [new file with mode: 0644]

index 3a78732..c095167 100644 (file)
@@ -33,7 +33,8 @@ div.block {
 <a href="notification.html"><div class="block">notification</div></a>
 <a href="power.html"><div class="block">power</div></a>
 <a href="speech.html"><div class="block">speech</div></a>
-<a href="sso.html"><div class="block">SSO</div></a>
+<a href="sso-google-oauth.html"><div class="block">SSO OAuth example</div></a>
+<a href="sso-advanced.html"><div class="block">SSO advanced example</div></a>
 <a href="system_info.html"><div class="block">system info</div></a>
 <a href="system_setting.html"><div class="block">system setting</div></a>
 <a href="time.html"><div class="block">time</div></a>
similarity index 100%
rename from examples/sso.html
rename to examples/sso-advanced.html
diff --git a/examples/sso-google-oauth.html b/examples/sso-google-oauth.html
new file mode 100644 (file)
index 0000000..a6a5579
--- /dev/null
@@ -0,0 +1,188 @@
+<html>
+<head>
+<title>SSO example</title>
+</head>
+<body>
+<style type="text/css">
+  .topleftcorner {
+    position:relative;
+    width: 50%;
+  }
+  .toprightcorner {
+    position:absolute;
+    top:0;
+    right:0;
+    width: 50%;
+    height:100%
+  }
+  .topcenter {
+    position: absolute;
+    width: 100%;
+    left: 100%;
+    top: 0%;
+    height:100%
+  }
+</style>
+<h3>SSO Google OAuth client example</h3>
+<div id='serviceContainer' class="topleftcorner">
+<table id="service" border="5" width="100%">
+  <tr>
+    <th colspan="2">
+      <H3><BR>Identity management</H3>
+    </th>
+  </tr>
+    <th>API</th>
+    <th>Arguments</th>
+  <tr><td><button onclick='queryIdentities()'>List Identities</button></td>
+  </tr>
+  <tr>
+    <td><button onclick='createIdentity()'>Create Identity</button></td>
+    <td><form>Identity name:
+    <input type="text" name="cident_caption" id="cident_caption"></form>
+  </tr>
+  <tr>
+    <td><button onclick='removeIdentity()'>Remove Identity</button></td>
+    <td><form>Identity id:
+    <input type="number" name="remove_identity_id" id="remove_identity_id"></form>
+  </tr>
+</table>
+
+<table id="session" border="5" width="100%">
+  <tr>
+    <th colspan="2">
+      <H3><BR>Authentication session</H3>
+    </th>
+  </tr>
+    <th>API</th>
+    <th>Arguments</th>
+  <tr><td><button onclick='getAccessToken()'>Get access token</button></td>
+    <td><form>Identity id:
+    <input type="number" name="get_token_identity_id" id="get_token_identity_id">
+    </form>
+  </tr>
+</table>
+
+</div>
+<div id='resultContainer' class="toprightcorner">
+<form name='form_out'>
+  <textarea name="form_text" id="form_text" style="width:100%; height:100%"></textarea>
+</form>
+</div>
+
+<script>
+
+function _logData(data) {
+  document.form_out.form_text.value = data + '\n\n' + document.form_out.form_text.value;
+}
+
+function queryIdentities() {
+  var res = tizen.sso.authService.queryIdentities({});
+  if (res != null) {
+    res.then(onQueryIdentitiesComplete, function(err) {
+      _logData('QueryIdentities failed: ' + err);});
+  } else {
+    _logData('identities not found');
+  }
+}
+
+function onQueryIdentitiesComplete(result) {
+  _logData('QueryIdentities successful: ' + JSON.stringify(result));
+}
+
+function createIdentity() {
+  var info = {
+    'caption': document.getElementById('cident_caption').value,
+    'realms': ['google.com'],
+    'accessControlList': [{"method": "oauth", "mechanisms": ["*"]}]
+  };
+  var res = tizen.sso.authService.createIdentity(info);
+  if (res.syncOpErrorMsg != null) {
+    _logData('Identity creation FAILED with error: ' + res.asyncOpErrorMsg);
+    return;
+  }
+  
+  res.identity.store().then(function(msg) {
+    _logData('Identity store succeeded with resp: ' + JSON.stringify(msg));},
+    function(err) {_logData('Identity store failed: ' + err);});
+}
+
+function removeIdentity() {
+  var id = parseInt(document.getElementById('remove_identity_id').value);
+  if (isNaN(id)) {
+    _logData('Invalid id');
+    return;
+  }
+  var res = tizen.sso.authService.getIdentity(id);
+  if (res != null) {
+    res.then(onGetIdentityForRemovalComplete, function(err) {
+      _logData('GetIdentity failed: ' + err);});
+  } else {
+    _logData('Identity not found with the specified id');
+  }
+}
+
+function onGetIdentityForRemovalComplete(identobj) {
+  identobj.remove().then(onIdentityRemoved,
+    function(err) {_logData('remove failed: ' + err);});
+}
+
+function onIdentityRemoved(ident) {
+  _logData('Identity has been removed');
+}
+
+function getAccessToken() {
+  var id = parseInt(document.getElementById('get_token_identity_id').value);
+  if (isNaN(id)) {
+    _logData('Invalid id');
+    return;
+  }
+  var res = tizen.sso.authService.getIdentity(id);
+  if (res != null) {
+    res.then(onGetIdentityForAccessTokenComplete, function(err) {
+      _logData('GetIdentity failed: ' + err);});
+  } else {
+    _logData('Identity not found with the specified id');
+  }
+}
+
+function onGetIdentityForAccessTokenComplete(identobj) {
+  identobj.startSession('oauth').then(onSessionStarted,
+    function(err) {_logData('starting session failed: ' + err);});
+}
+
+function onSessionStarted(session) {
+    // the client id and secret are specific to this example app
+    // if you develop something based on this example, get your own
+    // client id and secret at https://console.developers.google.com/project
+    //
+    // The meaning of these parameters is explained at
+    // http://gsignon-docs.accounts-sso.googlecode.com/git/gsignond-plugin-oauth/GSignondOauthPlugin.html
+    var session_data = {
+      'ClientId': '988702115901-hter2d0t0vso6vc65ielc3t2vd13lvum.apps.googleusercontent.com',
+      'ClientSecret': 'G0nyBM2q0jEdAq5vpFjPkt9W',
+      'UiPolicy': 0,
+      'ResponseType': 'code',
+      'AuthHost': 'accounts.google.com',
+      'AuthPath': '/o/oauth2/auth',
+      'RedirectUri': 'http://localhost:9004',
+      'Scope': 'profile',
+      'ForceClientAuthViaRequestBody': true,
+      'TokenHost': 'accounts.google.com',
+      'TokenPath': '/o/oauth2/token'
+    };
+
+    session.challenge("oauth2", session_data).then(onSessionResponse,
+    function(err) {_logData('session challenge failed: ' + err);});
+}
+
+function onSessionResponse(response) {
+    _logData('AccessToken: ' + response.AccessToken + '\nDuration: ' +
+        response.Duration + '\nRefreshToken: ' + response.RefreshToken +
+        '\nScope: ' + response.Scope + '\nTimestamp: ' + response.Timestamp +
+        '\nTokenType: ' + response.TokenType);
+}
+
+</script>
+
+</body>
+</html>