Skip to content

Commit

Permalink
Merge pull request #7 from MycroftAI/dev
Browse files Browse the repository at this point in the history
Merging 2018.2 changes into master
  • Loading branch information
chrisveilleux authored Oct 9, 2018
2 parents bf132bf + b2e9e89 commit 42d1d18
Show file tree
Hide file tree
Showing 87 changed files with 1,402 additions and 1,196 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*.launch
.settings/
*.sublime-workspace
__pycache__/

# IDE - VSCode
.vscode/*
Expand All @@ -37,3 +38,5 @@ testem.log
# System Files
.DS_Store
Thumbs.db


2 changes: 1 addition & 1 deletion login/backend/v1/login-api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# The selene-shared parent image contains all the common Docker configs for
# all Selene apps and services see the "shared" directory in this repository.
FROM selene-shared:latest
FROM docker.mycroft.ai/selene-shared:latest
LABEL description="Run the API for the Mycroft login screen"

# Use pipenv to install the package's dependencies in the container
Expand Down
3 changes: 2 additions & 1 deletion login/backend/v1/login-api/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ requests = "*"
pyjwt = "*"
flask-restful = "*"
certifi = "*"
gunicorn = "*"
uwsgi = "*"

[dev-packages]
selene-util = {path = "./../../../../shared"}

[requires]
python_version = "3.7"
30 changes: 17 additions & 13 deletions login/backend/v1/login-api/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 33 additions & 9 deletions login/backend/v1/login-api/login_api/api.py
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)
76 changes: 0 additions & 76 deletions login/backend/v1/login-api/login_api/authorize.py

This file was deleted.

16 changes: 13 additions & 3 deletions login/backend/v1/login-api/login_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,30 @@ class LoginConfigException(Exception):
class BaseConfig:
"""Base configuration."""
DEBUG = False
LOGIN_BASE_URL = os.environ['LOGIN_BASE_URL']
SECRET_KEY = os.environ['JWT_SECRET']
SELENE_BASE_URL = os.environ['SELENE_BASE_URL']
TARTARUS_BASE_URL = os.environ['TARTARUS_BASE_URL']


class DevelopmentConfig(BaseConfig):
"""Development configuration."""
DEBUG = True
TARTARUS_BASE_URL = 'https://api-test.mycroft.ai/v1'


class TestConfig(BaseConfig):
pass


class ProdConfig(BaseConfig):
pass


def get_config_location():
environment_configs = dict(
dev='login_api.config.DevelopmentConfig',
# test=TestConfig,
# prod=ProdConfig
test=TestConfig,
prod=ProdConfig
)

try:
Expand Down
6 changes: 6 additions & 0 deletions login/backend/v1/login-api/login_api/endpoints/__init__.py
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
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)
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 login/backend/v1/login-api/login_api/endpoints/facebook.py
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)
Loading

0 comments on commit 42d1d18

Please sign in to comment.