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