def setUp(self): """Create an authenticated session.""" # Specify the validity period for the session. start = datetime.now(tz=timezone('US/Eastern')) end = start + timedelta(seconds=36000) self.session = domain.Session( session_id='123-session-abc', start_time=start, end_time=end, user=domain.User( user_id='235678', email='*****@*****.**', username='******', name=domain.UserFullName('Jane', 'Bloggs', 'III'), profile=domain.UserProfile( affiliation='FSU', rank=3, country='de', default_category=domain.Category('astro-ph.GA'), submission_groups=['grp_physics'])), authorizations=domain.Authorizations( scopes=[ auth.scopes.CREATE_SUBMISSION, auth.scopes.EDIT_SUBMISSION, auth.scopes.VIEW_SUBMISSION ], endorsements=[ domain.Category('astro-ph.CO'), domain.Category('astro-ph.GA') ]))
def generate_client_token(client_id: str, owner_id: str, name: str, url: Optional[str] = None, description: Optional[str] = None, redirect_uri: Optional[str] = None, scope: str = DEFAULT_SCOPES, expires: int = 36000, endorsements: str = 'astro-ph.CO,astro-ph.GA', secret: str = DEFAULT_SECRET) -> None: # Specify the validity period for the session. start = datetime.now(tz=timezone('US/Eastern')) end = start + timedelta(seconds=expires) client = domain.Client(client_id=client_id, owner_id=owner_id, name=name, url=url, description=description, redirect_uri=redirect_uri) authorizations = domain.Authorizations( scopes=[domain.Scope(*s.split(':')) for s in scope.split()], endorsements=[ domain.Category(*cat.split('.', 1)) for cat in endorsements.split(',') ]) session = domain.Session(session_id=str(uuid.uuid4()), start_time=start, end_time=end, client=client, authorizations=authorizations) return auth.tokens.encode(session, secret)
def test_post_great(self, mock_legacy, mock_sessions, mock_users): """Form data are valid and check out.""" mock_users.exceptions.AuthenticationFailed = \ users.exceptions.AuthenticationFailed mock_sessions.exceptions.SessionCreationFailed = \ sessions.exceptions.SessionCreationFailed mock_legacy.exceptions.SessionCreationFailed = \ legacy.exceptions.SessionCreationFailed form_data = MultiDict({'username': '******', 'password': '******'}) ip = '123.45.67.89' next_page = '/foo' start_time = datetime.now(tz=EASTERN) user = domain.User(user_id=42, username='******', email='*****@*****.**', verified=True) auths = domain.Authorizations( classic=6, scopes=['public:read', 'submission:create']) mock_users.authenticate.return_value = user, auths c_session = domain.Session(session_id='barsession', user=user, start_time=start_time, authorizations=auths) c_cookie = 'bardata' mock_legacy.create.return_value = c_session mock_legacy.generate_cookie.return_value = c_cookie session = domain.Session( session_id='foosession', user=user, start_time=start_time, authorizations=domain.Authorizations( scopes=['public:read', 'submission:create'])) cookie = 'foodata' mock_sessions.create.return_value = session mock_sessions.generate_cookie.return_value = cookie data, status_code, header = login('POST', form_data, ip, next_page) self.assertEqual(status_code, status.HTTP_303_SEE_OTHER, "Redirects user to next page") self.assertEqual(header['Location'], next_page, "Redirects user to next page.") self.assertEqual(data['cookies']['session_cookie'], (cookie, None), "Session cookie is returned") self.assertEqual(data['cookies']['classic_cookie'], (c_cookie, None), "Classic session cookie is returned")
def test_post_not_verified(self, mock_legacy, mock_SessionStore, mock_users): """Form data are valid and check out.""" mock_users.exceptions.AuthenticationFailed = \ users.exceptions.AuthenticationFailed mock_legacy.exceptions.SessionCreationFailed = \ legacy.exceptions.SessionCreationFailed form_data = MultiDict({'username': '******', 'password': '******'}) ip = '123.45.67.89' next_page = '/foo' start_time = datetime.now(tz=UTC) user = domain.User(user_id=42, username='******', email='*****@*****.**', verified=False) auths = domain.Authorizations( classic=6, scopes=['public:read', 'submission:create']) mock_users.authenticate.return_value = user, auths c_session = domain.Session(session_id='barsession', user=user, start_time=start_time, authorizations=auths) c_cookie = 'bardata' mock_legacy.create.return_value = c_session mock_legacy.generate_cookie.return_value = c_cookie session = domain.Session( session_id='foosession', user=user, start_time=start_time, authorizations=domain.Authorizations( scopes=['public:read', 'submission:create'])) cookie = 'foodata' mock_SessionStore.current_session.return_value \ .create.return_value = session mock_SessionStore.current_session.return_value \ .generate_cookie.return_value = cookie data, status_code, header = login('POST', form_data, ip, next_page) self.assertEqual(status_code, status.HTTP_400_BAD_REQUEST, "Bad request error is returned")
def generate_token(user_id: str, email: str, username: str, first_name: str = 'Jane', last_name: str = 'Doe', suffix_name: str = 'IV', affiliation: str = 'Cornell University', rank: int = 3, country: str = 'us', default_category: str = 'astro-ph.GA', submission_groups: str = 'grp_physics', endorsements: Optional[str] = None, scope: str = DEFAULT_SCOPES) \ -> None: """Generate an auth token for dev/testing purposes.""" # Specify the validity period for the session. start = datetime.now(tz=timezone('US/Eastern')) end = start + timedelta(seconds=36000) # Make this as long as you want. if endorsements: these_endorsements = [ domain.Category(category) for category in endorsements.split(',') ] else: these_endorsements = [] session = domain.Session( session_id=str(uuid.uuid4()), start_time=start, end_time=end, user=domain.User( user_id=user_id, email=email, username=username, name=domain.UserFullName(first_name, last_name, suffix_name), profile=domain.UserProfile( affiliation=affiliation, rank=int(rank), country=country, default_category=domain.Category(default_category), submission_groups=submission_groups.split(','))), authorizations=domain.Authorizations( scopes=[domain.Scope(*s.split(':')) for s in scope.split()], endorsements=these_endorsements)) token = auth.tokens.encode(session, os.environ['JWT_SECRET']) click.echo(token)
def get_system_token(name: str, agent: Agent, scopes: List[str]) -> str: start = datetime.now(tz=UTC) end = start + timedelta(seconds=36000) if isinstance(agent, User): user = domain.User(username=agent.username, email=agent.email, user_id=agent.identifier, name=agent.name, verified=True) else: user = None session = domain.Session( session_id=str(uuid.uuid4()), start_time=datetime.now(), end_time=end, user=user, client=domain.Client(owner_id='system', client_id=name, name=name), authorizations=domain.Authorizations(scopes=scopes)) return auth.tokens.encode(session, get_application_config()['JWT_SECRET'])
def generate_token(user_id: str, email: str, username: str, first_name: str = 'Jane', last_name: str = 'Doe', suffix_name: str = 'IV', affiliation: str = 'Cornell University', rank: int = 3, country: str = 'us', default_category: str = 'astro-ph.GA', submission_groups: str = 'grp_physics', endorsements: str = 'astro-ph.CO,astro-ph.GA', scope: str = 'upload:read,upload:write,upload:admin') \ -> None: # Specify the validity period for the session. start = datetime.now(tz=UTC) end = start + timedelta(seconds=36000) # Make this as long as you want. # Create a user with endorsements in astro-ph.CO and .GA. session = domain.Session( session_id=str(uuid.uuid4()), start_time=start, end_time=end, user=domain.User( user_id=user_id, email=email, username=username, name=domain.UserFullName(first_name, last_name, suffix_name), profile=domain.UserProfile( affiliation=affiliation, rank=int(rank), country=country, default_category=domain.Category(default_category), submission_groups=submission_groups.split(',') ) ), authorizations=domain.Authorizations( scopes=[scope.split(',')], endorsements=[domain.Category(cat.split('.', 1)) for cat in endorsements.split(',')] ) ) token = auth.tokens.encode(session, app.config['JWT_SECRET']) click.echo(token)
def generate_token(user_id: str, email: str, username: str, first_name: str = 'Jane', last_name: str = 'Doe', suffix_name: str = 'IV', affiliation: str = 'Cornell University', rank: int = 3, country: str = 'us', default_category: domain.Category = (domain.Category( 'astro-ph', 'GA')), submission_groups: str = 'grp_physics', endorsements: List[domain.Category] = [], scope: List[domain.Scope] = []) -> None: """Generate an auth token for dev/testing purposes.""" # Specify the validity period for the session. start = datetime.now(tz=timezone('US/Eastern')) end = start + timedelta(seconds=36000) # Make this as long as you want. # Create a user with endorsements in astro-ph.CO and .GA. session = domain.Session( session_id=str(uuid.uuid4()), start_time=start, end_time=end, user=domain.User(user_id=user_id, email=email, username=username, name=domain.UserFullName(first_name, last_name, suffix_name), profile=domain.UserProfile( affiliation=affiliation, rank=int(rank), country=country, default_category=default_category, submission_groups=submission_groups.split(','))), authorizations=domain.Authorizations(scopes=scope, endorsements=endorsements)) token = auth.tokens.encode(session, os.environ['JWT_SECRET']) return token