Mercurial > prosody-modules
annotate mod_rest/example/prosody_oauth.py @ 4974:807007913f67
mod_log_json: Prefer native Lua table.pack over Prosody util.table one
Prosody is removing support for Lua 5.1, which was the reason for
util.table.pack to exist in the first place, since Lua 5.2+ provides
table.pack. In prosody rev 5eaf77114fdb everything was switched over to
use table.pack, opening the door for removing util.table.pack at some
point. This change here is to prepare for that future eventuality.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 11 Jul 2022 20:08:41 +0200 |
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()) |