Mercurial > prosody-modules
comparison mod_rest/example/prosody_oauth.py @ 5650:0eb2d5ea2428
merge
| author | Stephen Paul Weber <singpolyma@singpolyma.net> |
|---|---|
| date | Sat, 06 May 2023 19:40:23 -0500 |
| parents | 0e5a37f55440 |
| children | 9a4556a13cc7 |
comparison
equal
deleted
inserted
replaced
| 5649:2c69577b28c2 | 5650:0eb2d5ea2428 |
|---|---|
| 1 from oauthlib.oauth2 import LegacyApplicationClient | |
| 2 from requests_oauthlib import OAuth2Session | 1 from requests_oauthlib import OAuth2Session |
| 3 | 2 import requests |
| 4 | |
| 5 class ProsodyRestClient(LegacyApplicationClient): | |
| 6 pass | |
| 7 | 3 |
| 8 | 4 |
| 9 class ProsodyRestSession(OAuth2Session): | 5 class ProsodyRestSession(OAuth2Session): |
| 10 def __init__(self, base_url=None, token_url=None, rest_url=None, *args, **kwargs): | 6 def __init__( |
| 11 if base_url and not token_url: | 7 self, base_url, client_name, client_uri, redirect_uri, *args, **kwargs |
| 12 token_url = base_url + "/oauth2/token" | 8 ): |
| 13 if base_url and not rest_url: | 9 self.base_url = base_url |
| 14 rest_url = base_url + "/rest" | 10 discovery_url = base_url + "/.well-known/oauth-authorization-server" |
| 15 self._prosody_rest_url = rest_url | |
| 16 self._prosody_token_url = token_url | |
| 17 | 11 |
| 18 super().__init__(client=ProsodyRestClient(*args, **kwargs)) | 12 meta = requests.get(discovery_url).json() |
| 13 reg = requests.post( | |
| 14 meta["registration_endpoint"], | |
| 15 json={ | |
| 16 "client_name": client_name, | |
| 17 "client_uri": client_uri, | |
| 18 "redirect_uris": [redirect_uri], | |
| 19 }, | |
| 20 ).json() | |
| 21 | |
| 22 super().__init__(client_id=reg["client_id"], *args, **kwargs) | |
| 23 | |
| 24 self.meta = meta | |
| 25 self.client_secret = reg["client_secret"] | |
| 26 self.client_id = reg["client_id"] | |
| 27 | |
| 28 def authorization_url(self, *args, **kwargs): | |
| 29 return super().authorization_url( | |
| 30 self.meta["authorization_endpoint"], *args, **kwargs | |
| 31 ) | |
| 19 | 32 |
| 20 def fetch_token(self, *args, **kwargs): | 33 def fetch_token(self, *args, **kwargs): |
| 21 return super().fetch_token(token_url=self._prosody_token_url, *args, **kwargs) | 34 return super().fetch_token( |
| 35 token_url=self.meta["token_endpoint"], | |
| 36 client_secret=self.client_secret, | |
| 37 *args, | |
| 38 **kwargs | |
| 39 ) | |
| 22 | 40 |
| 23 def xmpp(self, json=None, *args, **kwargs): | 41 def xmpp(self, json=None, *args, **kwargs): |
| 24 return self.post(self._prosody_rest_url, json=json, *args, **kwargs) | 42 return self.post(self.base_url + "/rest", json=json, *args, **kwargs) |
| 25 | 43 |
| 26 | 44 |
| 27 if __name__ == "__main__": | 45 if __name__ == "__main__": |
| 28 # Example usage | 46 # Example usage |
| 29 | 47 |
| 30 # from prosody_oauth import ProsodyRestSession | 48 # from prosody_oauth import ProsodyRestSession |
| 31 from getpass import getpass | 49 from getpass import getpass |
| 32 | 50 |
| 33 p = ProsodyRestSession(base_url=input("Base URL: "), client_id="app") | 51 p = ProsodyRestSession( |
| 34 | 52 input("Base URL: "), |
| 35 p.fetch_token(username=input("XMPP Address: "), password=getpass("Password: ")) | 53 "Prosody mod_rest OAuth 2 example", |
| 54 "https://modules.prosody.im/mod_rest", | |
| 55 "urn:ietf:wg:oauth:2.0:oob", | |
| 56 ) | |
| 57 | |
| 58 print("Open the following URL in a browser and login:") | |
| 59 print(p.authorization_url()[0]) | |
| 60 | |
| 61 p.fetch_token(code=getpass("Paste Authorization code: ")) | |
| 36 | 62 |
| 37 print(p.xmpp(json={"disco": True, "to": "jabber.org"}).json()) | 63 print(p.xmpp(json={"disco": True, "to": "jabber.org"}).json()) |
