-
Notifications
You must be signed in to change notification settings - Fork 53
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 #7 from MycroftAI/dev
Merging 2018.2 changes into master
- Loading branch information
Showing
87 changed files
with
1,402 additions
and
1,196 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,17 +1,41 @@ | ||
from flask import Flask | ||
from flask import Flask, request | ||
from flask_restful import Api | ||
|
||
from .authorize import AuthorizeAntisocialView | ||
from .endpoints import ( | ||
AuthenticateAntisocialEndpoint, | ||
SocialLoginTokensEndpoint, | ||
AuthorizeFacebookEndpoint, | ||
AuthorizeGithubEndpoint, | ||
AuthorizeGoogleEndpoint, | ||
LogoutEndpoint | ||
) | ||
from .config import get_config_location | ||
from .logout import LogoutView | ||
|
||
BASE_URL = '/api/auth/' | ||
# Initialize the Flask application and the Flask Restful API | ||
login = Flask(__name__) | ||
login.config.from_object(get_config_location()) | ||
login_api = Api(login, catch_all_404s=True) | ||
|
||
antisocial_view_url = BASE_URL + 'antisocial' | ||
login_api.add_resource(AuthorizeAntisocialView, antisocial_view_url) | ||
# Define the endpoints | ||
login_api.add_resource(AuthenticateAntisocialEndpoint, '/api/antisocial') | ||
login_api.add_resource(AuthorizeFacebookEndpoint, '/api/social/facebook') | ||
login_api.add_resource(AuthorizeGithubEndpoint, '/api/social/github') | ||
login_api.add_resource(AuthorizeGoogleEndpoint, '/api/social/google') | ||
login_api.add_resource(SocialLoginTokensEndpoint, '/api/social/tokens') | ||
login_api.add_resource(LogoutEndpoint, '/api/logout') | ||
|
||
|
||
def add_cors_headers(response): | ||
"""Allow any application to logout""" | ||
# if 'logout' in request.url: | ||
response.headers['Access-Control-Allow-Origin'] = '*' | ||
if request.method == 'OPTIONS': | ||
response.headers['Access-Control-Allow-Methods'] = ( | ||
'DELETE, GET, POST, PUT' | ||
) | ||
headers = request.headers.get('Access-Control-Request-Headers') | ||
if headers: | ||
response.headers['Access-Control-Allow-Headers'] = headers | ||
return response | ||
|
||
|
||
logout_view_url = BASE_URL + 'logout' | ||
login_api.add_resource(LogoutView, logout_view_url) | ||
login.after_request(add_cors_headers) |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from .authenticate_antisocial import AuthenticateAntisocialEndpoint | ||
from .social_login_tokens import SocialLoginTokensEndpoint | ||
from .facebook import AuthorizeFacebookEndpoint | ||
from .github import AuthorizeGithubEndpoint | ||
from .google import AuthorizeGoogleEndpoint | ||
from .logout import LogoutEndpoint |
54 changes: 54 additions & 0 deletions
54
login/backend/v1/login-api/login_api/endpoints/authenticate_antisocial.py
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from http import HTTPStatus | ||
import json | ||
from time import time | ||
|
||
import requests as service_request | ||
|
||
from selene_util.api import SeleneEndpoint, APIError | ||
from selene_util.auth import encode_auth_token, THIRTY_DAYS | ||
|
||
|
||
class AuthenticateAntisocialEndpoint(SeleneEndpoint): | ||
""" | ||
User Login Resource | ||
""" | ||
def __init__(self): | ||
super(AuthenticateAntisocialEndpoint, self).__init__() | ||
self.response_status_code = HTTPStatus.OK | ||
self.tartarus_token = None | ||
self.users_uuid = None | ||
|
||
def get(self): | ||
try: | ||
self._authenticate_credentials() | ||
except APIError: | ||
pass | ||
else: | ||
self._build_response() | ||
|
||
return self.response | ||
|
||
def _authenticate_credentials(self): | ||
basic_credentials = self.request.headers['authorization'] | ||
service_request_headers = {'Authorization': basic_credentials} | ||
auth_service_response = service_request.get( | ||
self.config['TARTARUS_BASE_URL'] + '/auth/login', | ||
headers=service_request_headers | ||
) | ||
self._check_for_service_errors(auth_service_response) | ||
auth_service_response_content = json.loads( | ||
auth_service_response.content | ||
) | ||
self.users_uuid = auth_service_response_content['uuid'] | ||
self.tartarus_token = auth_service_response_content['accessToken'] | ||
|
||
def _build_response(self): | ||
self.selene_token = encode_auth_token( | ||
self.config['SECRET_KEY'], self.users_uuid | ||
) | ||
response_data = dict( | ||
expiration=time() + THIRTY_DAYS, | ||
seleneToken=self.selene_token, | ||
tartarusToken=self.tartarus_token, | ||
) | ||
self.response = (response_data, HTTPStatus.OK) |
37 changes: 37 additions & 0 deletions
37
login/backend/v1/login-api/login_api/endpoints/authenticate_social.py
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from http import HTTPStatus | ||
|
||
from selene_util.api import SeleneEndpoint | ||
from selene_util.auth import encode_auth_token, THIRTY_DAYS | ||
from time import time | ||
import json | ||
|
||
class AuthenticateSocialEndpoint(SeleneEndpoint): | ||
def __init__(self): | ||
super(AuthenticateSocialEndpoint, self).__init__() | ||
self.response_status_code = HTTPStatus.OK | ||
self.tartarus_token = None | ||
self.users_uuid = None | ||
|
||
def get(self): | ||
self._get_tartarus_token() | ||
self._build_front_end_response() | ||
return self.response | ||
|
||
def _get_tartarus_token(self): | ||
args = self.request.args | ||
if "data" in args: | ||
self.tartarus_token = args['data'] | ||
token_json = json.loads(self.tartarus_token) | ||
self.users_uuid = token_json["uuid"] | ||
|
||
def _build_front_end_response(self): | ||
self.selene_token = encode_auth_token( | ||
self.config['SECRET_KEY'], self.users_uuid | ||
) | ||
|
||
response_data = dict( | ||
expiration=time() + THIRTY_DAYS, | ||
seleneToken=self.selene_token, | ||
tartarusToken=self.tartarus_token, | ||
) | ||
self.response = (response_data, HTTPStatus.OK) |
17 changes: 17 additions & 0 deletions
17
login/backend/v1/login-api/login_api/endpoints/facebook.py
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"""Endpoint for single sign on through Facebook""" | ||
from flask import redirect | ||
|
||
from selene_util.api import SeleneEndpoint | ||
|
||
|
||
class AuthorizeFacebookEndpoint(SeleneEndpoint): | ||
def get(self): | ||
"""Call a Tartarus endpoint that will redirect to Facebook login.""" | ||
tartarus_auth_endpoint = ( | ||
'{tartarus_url}/social/auth/facebook' | ||
'?clientUri={login_url}&path=/social/login'.format( | ||
tartarus_url=self.config['TARTARUS_BASE_URL'], | ||
login_url=self.config['LOGIN_BASE_URL'] | ||
) | ||
) | ||
return redirect(tartarus_auth_endpoint) |
Oops, something went wrong.