Commit 9f5700da authored by Thomas Phil's avatar Thomas Phil

commit before continuing RM-45

parent d2e009c0
......@@ -12,11 +12,9 @@ class RunRestServerCommand(Command):
def handle(self):
config = strongr.core.Core.config()
host = config.restdomain.host
port = int(config.restdomain.port)
backend = config.restdomain.backend.strip().lower()
self.info("Starting server on {}:{} using {}".format(host, port, backend))
self.info("Starting server using {}".format(backend))
wsgi_query_factory = RestDomain.wsgiQueryFactory()
wsgi_query_bus = RestDomain.wsgiService().getQueryBus()
......
import strongr.core
import strongr.core.domain.restdomain
from flask_oauthlib.provider import OAuth2Provider
from datetime import datetime, timedelta
core = strongr.core.getCore()
restDomain = core.domains().restDomain()
from flask import make_response, jsonify, g
oauth2Service = restDomain.oauth2Service()
oauth2CommandFactory = restDomain.oauth2CommandFactory()
oauth2CommandBus = oauth2Service.getCommandBus()
from strongr.restdomain.model.oauth2 import Token
from strongr.core.gateways import Gateways
oauth2QueryFactory = restDomain.oauth2QueryFactory()
oauth2QueryBus = oauth2Service.getQueryBus()
core = strongr.core.Core
rest_domain = strongr.core.domain.restdomain.RestDomain
oauth2_service = rest_domain.oauth2Service()
oauth2_command_factory = rest_domain.oauth2CommandFactory()
oauth2_command_bus = oauth2_service.getCommandBus()
oauth2_query_factory = rest_domain.oauth2QueryFactory()
oauth2_query_bus = oauth2_service.getQueryBus()
def bind_oauth2(app):
oauth = OAuth2Provider(app)
@oauth.clientgetter
def get_client(client_id):
return oauth2QueryBus.handle(oauth2QueryFactory.newRetrieveClient(client_id))
print('get_client')
return oauth2_query_bus.handle(oauth2_query_factory.newRetrieveClient(client_id))
@oauth.grantgetter
def get_grant(client_id, code):
return oauth2QueryBus.handle(oauth2QueryFactory.newRetrieveGrant(client_id, code))
print('get_grant')
return oauth2_query_bus.handle(oauth2_query_factory.newRetrieveGrant(client_id, code))
@oauth.tokengetter
def get_token(access_token=None, refresh_token=None):
print('get_token')
if access_token:
return oauth2QueryBus.handle(oauth2QueryFactory.newRetrieveTokenByAccessToken(access_token))
return oauth2_query_bus.handle(oauth2_query_factory.newRetrieveTokenByAccessToken(access_token))
if refresh_token:
return oauth2QueryBus.handle(oauth2QueryFactory.newRetrieveTokenByRefreshToken(refresh_token))
return oauth2_query_bus.handle(oauth2_query_factory.newRetrieveTokenByRefreshToken(refresh_token))
return None
@oauth.grantsetter
def set_grant(client_id, code, request, *args, **kwargs):
print('set_grant')
expires = datetime.utcnow() + timedelta(seconds=600)
oauth2CommandBus.handle(oauth2CommandFactory.newAppendGrant(
oauth2_command_bus.handle(oauth2_command_factory.newAppendGrant(
client_id=client_id,
code=code['code'],
redirect_uri=request.redirect_uri,
......@@ -46,30 +56,31 @@ def bind_oauth2(app):
@oauth.tokensetter
def set_token(token, request, *args, **kwargs):
print('set_token')
# In real project, a token is unique bound to user and client.
# Which means, you don't need to create a token every time.
tok = Token(**token)
tok.user_id = request.user.id
tok.client_id = request.client.client_id
db.session.add(tok)
db.session.commit()
session = Gateways.sqlalchemy_session()
session.add(tok)
session.commit()
#@oauth.usergetter
#def get_user(username, password, *args, **kwargs):
@oauth.usergetter
def get_user(username, password, *args, **kwargs):
# # This is optional, if you don't need password credential
# # there is no need to implement this method
print('get_user')
# return User.query.filter_by(username=username).first()
return oauth
def oauth2Routes(app):
@app.route('/oauth/authorize', methods=['GET', 'POST'])
@oauth.authorize_handler
def authorize(*args, **kwargs):
def authorize(request, *args, **kwargs):
print('authorize_request')
# NOTICE: for real project, you need to require login
if request.method == 'GET':
#if request.method == 'GET':
# render a page for user to confirm the authorization
return render_template('confirm.html')
# return render_template('confirm.html')
if request.method == 'HEAD':
# if HEAD is supported properly, request parameters like
......@@ -84,13 +95,18 @@ def oauth2Routes(app):
@app.route('/oauth/token', methods=['POST', 'GET'])
@oauth.token_handler
def access_token():
print('access_token')
return {}
@app.route('/oauth/revoke', methods=['POST'])
@oauth.revoke_handler
def revoke_token():
print('revoke_token')
pass
@oauth.invalid_response
def require_oauth_invalid(req):
print('require_oauth_invalid')
return jsonify(message=req.error_message), 401
return oauth
class RetrieveClientHandler:
def __call__(self, query):
pass
from pprint import pprint
pprint(query.__dict__)
from .user import User
from .token import Token
from .grant import Grant
from .client import Client
import strongr.core.gateways as gateways
from sqlalchemy import Column, ForeignKey, Integer, String, Enum, DateTime, func, LargeBinary, Text
from sqlalchemy.orm import relationship, synonym
from strongr.schedulerdomain.model import JobState
Base = gateways.Gateways.sqlalchemy_base()
class Client(Base):
__tablename__ = 'oauth_client'
name = Column(String(40))
client_id = Column(String(40), primary_key=True)
client_secret = Column(String(55), unique=True, index=True, nullable=False)
client_type = Column(String(20), default='public')
_redirect_uris = Column(Text)
default_scope = Column(Text, default='')
@property
def user(self):
#return User.query.get(1)
# this should link to the user table
pass
@property
def redirect_uris(self):
if self._redirect_uris:
return self._redirect_uris.split()
return []
@property
def default_redirect_uri(self):
return self.redirect_uris[0]
@property
def default_scopes(self):
if self.default_scope:
return self.default_scope.split()
return []
@property
def allowed_grant_types(self):
return ['authorization_code', 'password', 'client_credentials',
'refresh_token']
import strongr.core.gateways as gateways
from sqlalchemy import Column, ForeignKey, Integer, String, Enum, DateTime, func, LargeBinary, Text
from sqlalchemy.orm import relationship, synonym
from strongr.schedulerdomain.model import JobState
Base = gateways.Gateways.sqlalchemy_base()
class Grant(Base):
__tablename__ = 'oauth_grant'
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey('user.id', ondelete='CASCADE'))
user = relationship('User')
client_id = Column(String(40), ForeignKey('client.client_id', ondelete='CASCADE'), nullable=False)
client = relationship('Client')
code = Column(String(255), index=True, nullable=False)
redirect_uri = Column(String(255))
scope = Column(Text)
expires = Column(DateTime)
@property
def scopes(self):
if self.scope:
return self.scope.split()
return None
import strongr.core.gateways as gateways
from sqlalchemy import Column, ForeignKey, Integer, String, Enum, DateTime, func, LargeBinary, Text
from sqlalchemy.orm import relationship, synonym
from datetime import datetime, timedelta
from strongr.schedulerdomain.model import JobState
Base = gateways.Gateways.sqlalchemy_base()
class Token(Base):
__tablename__ = 'oauth_token'
id = Column(Integer, primary_key=True)
client_id = Column(
String(40), ForeignKey('client.client_id', ondelete='CASCADE'),
nullable=False,
)
user_id = Column(
Integer, ForeignKey('user.id', ondelete='CASCADE')
)
user = relationship('User')
client = relationship('Client')
token_type = Column(String(40))
access_token = Column(String(255))
refresh_token = Column(String(255))
expires = Column(DateTime)
scope = Column(Text)
def __init__(self, **kwargs):
expires_in = kwargs.pop('expires_in', None)
if expires_in is not None:
self.expires = datetime.utcnow() + timedelta(seconds=expires_in)
for k, v in kwargs.items():
setattr(self, k, v)
@property
def scopes(self):
if self.scope:
return self.scope.split()
return []
import strongr.core.gateways as gateways
from sqlalchemy import Column, ForeignKey, Integer, String, Enum, DateTime, func, LargeBinary, Text
from sqlalchemy.orm import relationship, synonym
from datetime import datetime, timedelta
from strongr.schedulerdomain.model import JobState
Base = gateways.Gateways.sqlalchemy_base()
class User(Base):
__tablename__ = 'oauth_user'
id = Column(Integer, primary_key=True)
client_id = Column(String(40), ForeignKey('client.client_id', ondelete='CASCADE'), nullable=False)
user_id = Column(Integer, ForeignKey('user.id', ondelete='CASCADE'))
user = relationship('User')
client = relationship('Client')
token_type = Column(String(40))
access_token = Column(String(255))
refresh_token = Column(String(255))
expires = Column(DateTime)
scope = Column(Text)
def __init__(self, **kwargs):
expires_in = kwargs.pop('expires_in', None)
if expires_in is not None:
self.expires = datetime.utcnow() + timedelta(seconds=expires_in)
for k, v in kwargs.items():
setattr(self, k, v)
@property
def scopes(self):
if self.scope:
return self.scope.split()
return []
......@@ -13,6 +13,10 @@ class Oauth2Service(AbstractService):
_command_bus = None
_query_bus = None
def register_models(self):
import strongr.restdomain.model
# importing alone is enough for registration
def getCommandBus(self):
if self._command_bus is None:
self._command_bus = self._make_default_commandbus({
......
......@@ -7,6 +7,9 @@ class WsgiService(AbstractService):
_command_bus = None
_query_bus = None
def register_models(self):
pass
def getCommandBus(self):
if self._command_bus is None:
self._command_bus = self._make_default_commandbus({
......
import zlib
import strongr.core.gateways as gateways
from sqlalchemy import Column, ForeignKey, Integer, String, Enum, DateTime, func, LargeBinary, Text
from sqlalchemy.orm import relationship, synonym
......
......@@ -29,7 +29,7 @@ class SchedulerService(AbstractService):
_query_bus = None
def register_models(self):
import strongr.schedulerdomain.model as model
import strongr.schedulerdomain.model
# importing alone is enough for registration
def getCommandBus(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment