-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from happycube-timer/refactor/layout
Refactored services
- Loading branch information
Showing
6 changed files
with
82 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,60 @@ | ||
from happycube.service import BaseService | ||
from happycube.users.services import user_service | ||
from happycube.errors import HTTPError | ||
from happycube.users.models import User | ||
from flask import g, request | ||
import jwt | ||
|
||
|
||
class AuthService(object): | ||
def get_authenticated_user(login, password): | ||
user = User.first(name=login) | ||
if user and user.check_password(password): | ||
return user | ||
else: | ||
return None | ||
|
||
def get_authenticated_user(self, login, password): | ||
user = user_service.first(name=login) | ||
if user and user.check_password(password): | ||
return user | ||
else: | ||
return None | ||
|
||
def name_available(login): | ||
user = User.first(name=login) | ||
if user is None: | ||
return True | ||
else: | ||
return False | ||
|
||
def verify_jwt(self): | ||
"""Does the actual work of verifying the JWT data in the current request. | ||
""" | ||
auth = request.headers.get('Authorization', None) | ||
|
||
if auth is None: | ||
raise HTTPError(401, 'Authorization header was missing') | ||
def register_new_user(login, password): | ||
return User.create(name=login, password=password) | ||
|
||
segments = auth.split() | ||
|
||
if segments[0].lower() != 'bearer': | ||
raise HTTPError(401, 'Authorization header format is \'Bearer TOKEN\'') | ||
elif len(segments) == 1: | ||
raise HTTPError(401, 'Token missing') | ||
elif len(segments) > 2: | ||
raise HTTPError(401, 'Token invalid') | ||
def verify_jwt(): | ||
"""Does the actual work of verifying the JWT data in the current request. | ||
""" | ||
auth = request.headers.get('Authorization', None) | ||
|
||
try: | ||
payload = jwt.decode(segments[1], 'secret') # TODO: make secret key secret | ||
except jwt.ExpiredSignatureError: | ||
raise HTTPError(401, 'Token expired') | ||
except jwt.DecodeError: | ||
raise HTTPError(401, 'Token invalid') | ||
if auth is None: | ||
raise HTTPError(401, 'Authorization header was missing') | ||
|
||
try: | ||
g.user = user_service.get(payload.get('user_id')) | ||
except: | ||
raise HTTPError(401, 'User does not exist') | ||
segments = auth.split() | ||
|
||
return None | ||
if segments[0].lower() != 'bearer': | ||
raise HTTPError(401, 'Authorization header format is \'Bearer TOKEN\'') | ||
elif len(segments) == 1: | ||
raise HTTPError(401, 'Token missing') | ||
elif len(segments) > 2: | ||
raise HTTPError(401, 'Token invalid') | ||
|
||
try: | ||
payload = jwt.decode(segments[1], 'secret') # TODO: make secret key secret | ||
except jwt.ExpiredSignatureError: | ||
raise HTTPError(401, 'Token expired') | ||
except jwt.DecodeError: | ||
raise HTTPError(401, 'Token invalid') | ||
|
||
try: | ||
g.user = User.get(payload.get('user_id')) | ||
except: | ||
raise HTTPError(401, 'User does not exist') | ||
|
||
def issue_jwt(self, user): | ||
return jwt.encode({'user_id': user.id}, 'secret') | ||
return None | ||
|
||
|
||
auth_service = AuthService() | ||
def issue_jwt(user): | ||
return jwt.encode({'user_id': user.id}, 'secret') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
from happycube.service import BaseService | ||
from happycube.solves.models import Solve | ||
|
||
def get_all_solves(): | ||
return Solve.all() | ||
|
||
class SolveService(BaseService): | ||
__model__ = Solve | ||
|
||
solve_service = SolveService() | ||
def create_solve(payload): | ||
Solve.create(**payload) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.