Mercurial > prosody-modules
annotate mod_rest/example/prosody_oauth.py @ 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 | ccce785f53e1 |
children | 0e5a37f55440 |
rev | line source |
---|---|
4953
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 from oauthlib.oauth2 import LegacyApplicationClient |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 from requests_oauthlib import OAuth2Session |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 class ProsodyRestClient(LegacyApplicationClient): |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 pass |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 class ProsodyRestSession(OAuth2Session): |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 def __init__(self, base_url=None, token_url=None, rest_url=None, *args, **kwargs): |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 if base_url and not token_url: |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 token_url = base_url + "/oauth2/token" |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 if base_url and not rest_url: |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 rest_url = base_url + "/rest" |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 self._prosody_rest_url = rest_url |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 self._prosody_token_url = token_url |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 super().__init__(client=ProsodyRestClient(*args, **kwargs)) |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 def fetch_token(self, *args, **kwargs): |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 return super().fetch_token(token_url=self._prosody_token_url, *args, **kwargs) |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 def xmpp(self, json=None, *args, **kwargs): |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 return self.post(self._prosody_rest_url, json=json, *args, **kwargs) |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 if __name__ == "__main__": |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 # Example usage |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 # from prosody_oauth import ProsodyRestSession |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 from getpass import getpass |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 p = ProsodyRestSession(base_url=input("Base URL: "), client_id="app") |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 p.fetch_token(username=input("XMPP Address: "), password=getpass("Password: ")) |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 |
ccce785f53e1
mod_rest: Add an example OAuth client (needs mod_http_oauth2)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 print(p.xmpp(json={"disco": True, "to": "jabber.org"}).json()) |