From 1889f3248ae83cc773a5443c6ba0f62834d73b7b Mon Sep 17 00:00:00 2001 From: Nate the great Date: Thu, 29 Mar 2018 04:52:29 -0400 Subject: Oauth (#45) * Creating a OAUTH login for the site. We still need to have a way to fill in credentials however. Signed-off-by: Zwork101 * How ya like me now Travis? Signed-off-by: Zwork101 * Fix slight error in database insertion * Revert "Fix slight error in database insertion" (wrong branch) This reverts commit 9ac6cbb * Don't have snekchek working, but I'll try again. Signed-off-by: Zwork101 * Please enter the commit message for your changes. Lines starting * Adding Oauth2 login to site. * Add prefix * Add prefix * This never happened * Flipping Travis I still can't get snekchek to work locally. * Added a whole bunch of stuff, ready to be used. Signed-off-by: Zwork101 * Making sessions more secure, and future safe. Signed-off-by: Zwork101 * Adding some quick stuff Signed-off-by: Zwork101 * Appease the flake8 gods Signed-off-by: Zwork101 * Appease the flake8 gods Signed-off-by: Zwork101 * Whoops Signed-off-by: Zwork101 * Add comments, fix user_data function. Signed-off-by: Zwork101 * Whooops, forgot to flake :/ Signed-off-by: Zwork101 * Make it look nicer, thanks Aperture. Signed-off-by: Zwork101 * Fixing login issues and added button * Add a OauthMixin to allow for easy data access. Clean stuff up. Signed-off-by: Zwork101 * Fix a test, and use self.assertEqual rather then self.assertEquals! Signed-off-by: Zwork101 * Please don't ask how that happened. Signed-off-by: Zwork101 * Added some tests, moved a bunch of stuff around. Mainly cleaned stuff up. Signed-off-by: Zwork101 * Add a ton of tests, try to please the coverall gods :D, moved some code into a function for testing. Signed-off-by: Zwork101 * Just some stupid stuff I missed. Signed-off-by: Zwork101 * Fix an issue with the test, and add docs Signed-off-by: Zwork101 * Remove pointless function. (join_user) Signed-off-by: Zwork101 * Fix test consistency --- pysite/mixins.py | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'pysite/mixins.py') diff --git a/pysite/mixins.py b/pysite/mixins.py index 059f871d..5b1a780f 100644 --- a/pysite/mixins.py +++ b/pysite/mixins.py @@ -6,7 +6,7 @@ from _weakref import ref from pysite.database import RethinkDB -class DBMixin(): +class DBMixin: """ Mixin for classes that make use of RethinkDB. It can automatically create a table with the specified primary key using the attributes set at class-level. @@ -59,3 +59,49 @@ class DBMixin(): @property def db(self) -> RethinkDB: return self._db() + + +class OauthMixin: + """ + Mixin for the classes that need access to a logged in user's information. This class should be used + to grant route's access to user information, such as name, email, id, ect. + + There will almost never be a need for someone to inherit this, as BaseView does that for you. + + This class will add 3 properties to your route: + + * logged_in (bool): True if user is registered with the site, False else wise. + + * user_data (dict): A dict that looks like this: + + { + "user_id": Their discord ID, + "username": Their discord username (without discriminator), + "discriminator": Their discord discriminator, + "email": Their email, in which is connected to discord + } + + user_data returns None, if the user isn't logged in. + + * oauth (OauthBackend): The instance of pysite.oauth.OauthBackend, connected to the RouteManager. + """ + + @classmethod + def setup(cls: "OauthMixin", manager: "pysite.route_manager.RouteManager", blueprint: Blueprint): + + if hasattr(super(), "setup"): + super().setup(manager, blueprint) # pragma: no cover + + cls._oauth = ref(manager.oauth_backend) + + @property + def logged_in(self) -> bool: + return self.user_data is not None + + @property + def user_data(self) -> dict: + return self.oauth.user_data() + + @property + def oauth(self): + return self._oauth() -- cgit v1.2.3