Skip to content

Commit aa738fe

Browse files
committed
setup graph_auth app and test project 💥
1 parent 96092fd commit aa738fe

37 files changed

+1275
-11
lines changed

ReadMe.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1+
# Overview
2+
This a replacement for [Django GraphQL Auth](https://github.com/PedroBern/django-graphql-auth) that works with latest Django and Graphene
13

24

35
## TODO
4-
- [] Add Decorators
6+
- [ ] Update ReadMe
7+
- [ ] Add Documentation
8+
- [ ] Test usage of `EMAIL_ASYNC_TASK` to send emails Asynchronously
9+
- [ ] Add a Notify Admin Aysnc function that sends emails to admins when Exceptions occur
10+
- [ ] Write Tests
11+
- [ ] Package the app for Pypi

graph_auth/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__version__ = "0.3.16"
2+
3+
default_app_config = "graph_auth.apps.GraphQLAuthConfig"

graph_auth/admin.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.contrib import admin
2+
3+
from .models import UserStatus
4+
5+
admin.site.register(UserStatus)

graph_auth/apps.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.apps import AppConfig
2+
3+
4+
class GraphQLAuthConfig(AppConfig):
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "graph_auth"
7+
verbose_name = "GraphQL Auth"

graph_auth/constants.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from django.utils.translation import gettext as _
2+
3+
4+
class Messages:
5+
INVALID_PASSWORD = [{"message": _("Invalid password."), "code": "invalid_password"}]
6+
UNAUTHENTICATED = [{"message": _("Unauthenticated."), "code": "unauthenticated"}]
7+
INVALID_TOKEN = [{"message": _("Invalid token."), "code": "invalid_token"}]
8+
EXPIRED_TOKEN = [{"message": _("Expired token."), "code": "expired_token"}]
9+
ALREADY_VERIFIED = [
10+
{"message": _("Account already verified."), "code": "already_verified"}
11+
]
12+
EMAIL_FAIL = [{"message": _("Failed to send email."), "code": "email_fail"}]
13+
INVALID_CREDENTIALS = [
14+
{
15+
"message": _("Please, enter valid credentials."),
16+
"code": "invalid_credentials",
17+
}
18+
]
19+
NOT_VERIFIED = [
20+
{"message": _("Please verify your account."), "code": "not_verified"}
21+
]
22+
NOT_VERIFIED_PASSWORD_RESET = [
23+
{
24+
"message": _("Verify your account. A new verification email was sent."),
25+
"code": "not_verified",
26+
}
27+
]
28+
EMAIL_IN_USE = [
29+
{"message": _("A user with that email already exists."), "code": "unique_email"}
30+
]
31+
USERNAME_IN_USE = [
32+
{"message": _("A user with that username already exists."), "code": "unique_username"}
33+
]
34+
SECONDARY_EMAIL_REQUIRED = [
35+
{
36+
"message": _("You need to setup a secondary email to proceed."),
37+
"code": "secondary_email_required",
38+
}
39+
]
40+
PASSWORD_ALREADY_SET = [
41+
{
42+
"message": _("Password already set for account."),
43+
"code": "password_already_set",
44+
}
45+
]
46+
SERVER_ERROR = [{"message": _("Server error."), "code": "500"}]
47+
48+
49+
class TokenAction(object):
50+
ACTIVATION = "activation"
51+
PASSWORD_RESET = "password_reset"
52+
ACTIVATION_SECONDARY_EMAIL = "activation_secondary_email"
53+
PASSWORD_SET = "password_set"

graph_auth/exceptions.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from django.utils.translation import gettext as _
2+
3+
4+
class GraphQLAuthError(Exception):
5+
default_message = None
6+
7+
def __init__(self, message=None):
8+
if message is None:
9+
message = self.default_message
10+
11+
super().__init__(message)
12+
13+
14+
class UserAlreadyVerified(GraphQLAuthError):
15+
default_message = _("User already verified.")
16+
17+
18+
class InvalidCredentials(GraphQLAuthError):
19+
default_message = _("Invalid credentials.")
20+
21+
22+
class UserNotVerified(GraphQLAuthError):
23+
default_message = _("User is not verified.")
24+
25+
26+
class EmailAlreadyInUse(GraphQLAuthError):
27+
default_message = _("This email is already in use.")
28+
29+
30+
class TokenScopeError(GraphQLAuthError):
31+
default_message = _("This token if for something else.")
32+
33+
34+
class PasswordAlreadySetError(GraphQLAuthError):
35+
default_message = _("Password already set for account.")
36+
37+
38+
class WrongUsage(GraphQLAuthError):
39+
"""
40+
internal exception
41+
"""
42+
43+
default_message = _("Wrong usage, check your code!.")

graph_auth/graphql/base.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import graphene
2+
3+
from ..type import ExpectedErrorType
4+
5+
# TODO: if for custom was here
6+
OutputErrorType = ExpectedErrorType
7+
8+
9+
class BaseAuthMutation(graphene.Mutation):
10+
success = graphene.Boolean(default_value=True)
11+
errors = graphene.Field(OutputErrorType)
12+
13+
class Meta:
14+
abstract = True
15+
16+
@classmethod
17+
def mutate(cls, root, info, **data):
18+
return cls.perform_mutation(root, info, **data)
19+
20+
@classmethod
21+
def perform_mutation(cls, root, info, **data):
22+
pass
23+
24+
@classmethod
25+
def success_response(cls):
26+
"""Return a success response."""
27+
return cls(**{"errors": []})
28+
29+
@classmethod
30+
def unsuccess_response(cls):
31+
"""Return a success response."""
32+
return cls(**{"errors": ["That did not go Well"]})
33+
34+
35+
class BaseRegister(BaseAuthMutation):
36+
37+
"""
38+
# how do I pass the arguments to the mutation as it is
39+
# without defining and input variable so that the mutation
40+
# can be called like this:
41+
register(
42+
email: String!
43+
username: String!
44+
password1: String!
45+
password2: String!
46+
): Register
47+
"""
48+
49+
class Arguments:
50+
email = graphene.String(required=True, description="Email")
51+
username = graphene.String(required=True, description="Username")
52+
password = graphene.String(required=True, description="Password")
53+
54+
class Meta:
55+
abstract = True
56+
57+
58+
class VerifyAccountBase(BaseAuthMutation):
59+
class Arguments:
60+
token = graphene.String(required=True, description="Token")
61+
62+
class Meta:
63+
abstract = True
64+
65+
66+
class ResendActivationEmailBase(BaseAuthMutation):
67+
class Arguments:
68+
email = graphene.String(required=True, description="Email")
69+
70+
class Meta:
71+
abstract = True
72+
73+
74+
class SendPasswordResetEmailBase(BaseAuthMutation):
75+
class Arguments:
76+
email = graphene.String(required=True, description="Email")
77+
78+
class Meta:
79+
abstract = True
80+
81+
82+
class PasswordResetBase(BaseAuthMutation):
83+
class Arguments:
84+
token = graphene.String(required=True, description="Token")
85+
newPassword = graphene.String(required=True, description="NewPassword")
86+
87+
class Meta:
88+
abstract = True
89+
90+
91+
class PasswordSetBase(BaseAuthMutation):
92+
class Arguments:
93+
token = graphene.String(required=True, description="Token")
94+
newPassword = graphene.String(required=True, description="NewPassword")
95+
96+
class Meta:
97+
abstract = True

graph_auth/graphql/inputs.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import graphene
2+
3+
4+
class EmailInput(graphene.InputObjectType):
5+
email = graphene.String(required=True, description="Email")
6+
7+
8+
class RegisterInput(EmailInput):
9+
username = graphene.String(required=True, description="Username")
10+
password = graphene.String(required=True, description="Password")
11+
12+
13+
class PasswordResetInput(graphene.InputObjectType):
14+
token = graphene.String(required=True, description="Token")
15+
newPassword = graphene.String(required=True, description="NewPassword")

0 commit comments

Comments
 (0)