Elgg  Version 1.9
security.js
Go to the documentation of this file.
1 
4 elgg.provide('elgg.security.token');
5 
6 elgg.security.tokenRefreshFailed = false;
7 
8 elgg.security.tokenRefreshTimer = null;
9 
16 elgg.security.setToken = function(json) {
17  //update the convenience object
18  elgg.security.token = json;
19 
20  //also update all forms
21  $('[name=__elgg_ts]').val(json.__elgg_ts);
22  $('[name=__elgg_token]').val(json.__elgg_token);
23 
24  // also update all links that contain tokens and time stamps
25  $('[href*="__elgg_ts"][href*="__elgg_token"]').each(function() {
26  this.href = this.href
27  .replace(/__elgg_ts=\d*/, '__elgg_ts=' + json.__elgg_ts)
28  .replace(/__elgg_token=[0-9a-f]*/, '__elgg_token=' + json.__elgg_token);
29  });
30 };
31 
37 elgg.security.refreshToken = function() {
38  elgg.getJSON('refresh_token', function(data) {
39  if (data && data.__elgg_ts && data.__elgg_token) {
40  elgg.security.setToken(data);
41  if (elgg.is_logged_in() && data.logged_in === false) {
42  elgg.session.user = null;
43  elgg.register_error(elgg.echo('session_expired'));
44  }
45  }
46  });
47 };
48 
49 
57 elgg.security.addToken = function(data) {
58 
59  // 'http://example.com?data=sofar'
60  if (elgg.isString(data)) {
61  // is this a full URL, relative URL, or just the query string?
62  var parts = elgg.parse_url(data),
63  args = {},
64  base = '';
65 
66  if (parts['host'] === undefined) {
67  if (data.indexOf('?') === 0) {
68  // query string
69  base = '?';
70  args = elgg.parse_str(parts['query']);
71  }
72  } else {
73  // full or relative URL
74 
75  if (parts['query'] !== undefined) {
76  // with query string
77  args = elgg.parse_str(parts['query']);
78  }
79  var split = data.split('?');
80  base = split[0] + '?';
81  }
82  args["__elgg_ts"] = elgg.security.token.__elgg_ts;
83  args["__elgg_token"] = elgg.security.token.__elgg_token;
84 
85  return base + jQuery.param(args);
86  }
87 
88  // no input! acts like a getter
89  if (elgg.isUndefined(data)) {
90  return elgg.security.token;
91  }
92 
93  // {...}
94  if (elgg.isPlainObject(data)) {
95  return elgg.extend(data, elgg.security.token);
96  }
97 
98  // oops, don't recognize that!
99  throw new TypeError("elgg.security.addToken not implemented for " + (typeof data) + "s");
100 };
101 
102 elgg.security.init = function() {
103  // elgg.security.interval is set in the js/elgg PHP view.
104  elgg.security.tokenRefreshTimer = setInterval(elgg.security.refreshToken, elgg.security.interval);
105 };
106 
107 elgg.register_hook_handler('boot', 'system', elgg.security.init);
elgg
Definition: install.js:23
a
Definition: admin.php:97