Mercurial > prosody-modules
annotate mod_measure_active_users/mod_measure_active_users.lua @ 5193:2bb29ece216b
mod_http_oauth2: Implement stateless dynamic client registration
Replaces previous explicit registration that required either the
additional module mod_adhoc_oauth2_client or manually editing the
database. That method was enough to have something to test with, but
would not probably not scale easily.
Dynamic client registration allows creating clients on the fly, which
may be even easier in theory.
In order to not allow basically unauthenticated writes to the database,
we implement a stateless model here.
per_host_key := HMAC(config -> oauth2_registration_key, hostname)
client_id := JWT { client metadata } signed with per_host_key
client_secret := HMAC(per_host_key, client_id)
This should ensure everything we need to know is part of the client_id,
allowing redirects etc to be validated, and the client_secret can be
validated with only the client_id and the per_host_key.
A nonce injected into the client_id JWT should ensure nobody can submit
the same client metadata and retrieve the same client_secret
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 03 Mar 2023 21:14:19 +0100 |
parents | 1132f2888cd2 |
children | 34b46d157797 |
rev | line source |
---|---|
4774
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local store = module:open_store("lastlog2"); |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local measure_d1 = module:measure("active_users_1d", "amount"); |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local measure_d7 = module:measure("active_users_7d", "amount"); |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local measure_d30 = module:measure("active_users_30d", "amount"); |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 function update_calculations() |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 module:log("debug", "Calculating active users"); |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local host_user_sessions = prosody.hosts[module.host].sessions; |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local active_d1, active_d7, active_d30 = 0, 0, 0; |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local now = os.time(); |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 for username in store:users() do |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 if host_user_sessions[username] then |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 -- Active now |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 active_d1, active_d7, active_d30 = |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 active_d1 + 1, active_d7 + 1, active_d30 + 1; |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 else |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 local lastlog_data = store:get(username); |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 if lastlog_data then |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 -- Due to server restarts/crashes/etc. some events |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 -- may not always get recorded, so we'll just take the |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 -- latest as a sign of last activity |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 local last_active = math.max( |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 lastlog_data.login and lastlog_data.login.timestamp or 0, |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 lastlog_data.logout and lastlog_data.logout.timestamp or 0 |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 ); |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 if now - last_active < 86400 then |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 active_d1 = active_d1 + 1; |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 end |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 if now - last_active < 86400*7 then |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 active_d7 = active_d7 + 1; |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 end |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 if now - last_active < 86400*30 then |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 active_d30 = active_d30 + 1; |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 end |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 end |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 end |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 end |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 module:log("debug", "Active users (took %ds): %d (24h), %d (7d), %d (30d)", os.time()-now, active_d1, active_d7, active_d30); |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 measure_d1(active_d1); |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 measure_d7(active_d7); |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 measure_d30(active_d30); |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 return 3600 + (300*math.random()); |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 end |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 |
1132f2888cd2
mod_measure_active_users: Calculate active user counts over fixed time periods
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 module:add_timer(15, update_calculations); |