Mercurial > prosody-modules
annotate mod_rest/example/prosody_oauth.py @ 5119:048e339706ba
mod_rest: Remove manual reference expansion in schema
This hack was originally added to reduce the number of definitions of
common attributes (type, to, from etc) and payloads (e.g. delay). This
predated pointers and references, and until now was needed because
parsing picked out the correct stanza kind from the schema, which broke
internal references.
Removing this hack paves the way for allowing the schema to be
configured or customized more easily.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 20 Dec 2022 21:48:28 +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()) |