You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ponymail.apache.org by hu...@apache.org on 2020/09/08 08:37:38 UTC

[incubator-ponymail-foal] 03/10: Add GitHub OAuth plugin

This is an automated email from the ASF dual-hosted git repository.

humbedooh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-ponymail-foal.git

commit 0cee3a70238867bd7da38925320875c2205721cb
Author: Daniel Gruno <hu...@apache.org>
AuthorDate: Tue Sep 8 10:27:07 2020 +0200

    Add GitHub OAuth plugin
---
 server/plugins/oauthGithub.py | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/server/plugins/oauthGithub.py b/server/plugins/oauthGithub.py
new file mode 100644
index 0000000..f1265d4
--- /dev/null
+++ b/server/plugins/oauthGithub.py
@@ -0,0 +1,28 @@
+"""
+    Github OAuth plugin.
+    To make this work, please set up an application at https://github.com/settings/applications/
+    copy the client ID and secret to your ponymail.yaml's oauth configuration, as such:
+    oauth:
+      github_client_id: abcdef123456
+      github_client_secret: bcfdgefa572564576
+"""
+
+import re
+import requests
+import plugins.server
+import typing
+
+
+async def process(formdata, session, server: plugins.server.BaseServer) -> typing.Optional[dict]:
+    formdata['client_id'] = server.config.oauth.github_client_id
+    formdata['client_secret'] = server.config.oauth.github_client_secret
+
+    rv = await server.runners.run(requests.post, 'https://github.com/login/oauth/access_token', data=formdata)
+    m = re.search(r"(access_token=[a-f0-9]+)", rv.text)
+
+    if m:
+        rv = await server.runners.run(requests.get, "https://api.github.com/user?%s" % m.group(1))
+        js = rv.json()
+        js["oauth_domain"] = "github.com"
+        return js
+    return None