def save_session(self, session: Session, user: User) -> None: with connect(self.__db_path) as db: c = db.cursor() OAuthSqlite.__insert_update_user(db, user.get_sub(), user.get_email()) c.execute("INSERT INTO session VALUES (?, ?)", (session.get_id(), str(session)))
def init_create_user(): user = User(None) print() print("address/key:", user.address) print() print("Keep it secret, keep it safe") return user
def __init__(self, *args, **kwargs): super(Application, self).__init__(*args, **kwargs) self.user = User('http://localhost:8888/user1') self.user.addErrorListener(self._user_error) self.user.addLoadedListener(self._update_user) self._update_user(self.user) self.user.get()
def test_new_user_properties(mocker): """ARRANGE""" expected_address = "fake_address" expected_network_id = "fake_network_id" mocker.patch('client.utils.PlugApiClient', MockApiClient) mocker.patch('client.utils.SqliteKeyManager', MockKeyManager) """ACT""" user = User(None) """ASSERT""" assert user.address == expected_address assert user.network_id == expected_network_id assert type(user.key_manager) is MockKeyManager assert type(user.client) is MockApiClient
def get_session(self, session_id: str) -> Union[Tuple[Session, User], None]: with connect(self.__db_path) as db: c = db.cursor() session_row = c.execute("SELECT detail FROM session WHERE id = ?", (session_id, )).fetchone() if session_row is None or 0 == len(session_row): return None else: session = Session(session_detail=json.loads(session_row[0])) user_row = c.execute("SELECT email FROM user WHERE sub = ?", (session.get_user_sub(), )).fetchone() if user_row is None or 0 == len(user_row): return None user = User(sub=session.get_user_sub(), email=user_row[0]) return session, user
class Application(SimplePanel): def __init__(self, *args, **kwargs): super(Application, self).__init__(*args, **kwargs) self.user = User('http://localhost:8888/user1') self.user.addErrorListener(self._user_error) self.user.addLoadedListener(self._update_user) self._update_user(self.user) self.user.get() def _update_user(self, user): if user.username is None: return user.maps.get() user.maps.addLoadedListener(self._update_user_maps) self._update_user_maps(self.user.maps) def _user_error(self, user, status, response): logging.error('Error loading user from %s: %s' % (user.get_resource_url(), status)) def _update_user_maps(self, maps): if maps.items is None: # Map list is not yet loaded return if len(maps.items) == 0: logging.error('User has no maps') return m = maps.items[0] m.addLoadedListener(self._update_map) self._update_map(m) def _update_map(self, m): if m.name is None: # Data is not yet loaded return m.layers.get() sp = HorizontalPanel(Size=('100%', '100%')) sidebar = Sidebar() sidebar.setLayersCollection(m.layers) sp.add(sidebar) sp.setCellWidth(sidebar, '25%') map_ = Map(Size=('100%', '100%')) map_.set_map(m) sp.add(map_) self.setWidget(sp)
def init_create_user(): user = User(None) print() print("address/key:", user.address) print() return user
def logout(self): """ Send REST call to logout from NEWT """ self.authentication = self.post(self.BASE_URL + '/logout') User.logout(self)
def user(self): return User(self._connector, self._api_prefix)
def redirect_uri_handler(): token_is_valid = False if 'state' not in session or session['state'] != request.args['state']: raise BadRequest('Missing or invalid state') if 'code' not in request.args: raise BadRequest('No code in response') try: token_data = _client.get_token(request.args['code']) if "error" in token_data: err_response = jsonify({ "success": False, "message": token_data["error"], "detail": "" if "error_description" not in token_data else token_data["error_description"], "status_code": 500 }) err_response.status_code = 500 return err_response except Exception as e: raise BadRequest('Could not fetch token(s): ' + str(e)) session.pop('state', None) # Store in basic server session, since flask session use cookie for storage user_session = Session() if 'access_token' in token_data: user_session.set_access_token(token_data['access_token']) if _jwt_validator and 'id_token' in token_data: # validate JWS; signature, aud and iss. # Token type, access token, ref-token and JWT if 0 == len(_config.get_issuer()): raise BadRequest('Could not validate token: no issuer configured') try: _jwt_validator.validate(token_data['id_token'], _config.get_issuer(), _config.get_client_id()) token_is_valid = True except JwtValidatorException as bs: raise BadRequest('Could not validate token: ' + str(bs)) except Exception as ve: raise BadRequest('Unexpected exception: ' + str(ve)) user_session.set_id_token(token_data['id_token']) if not token_is_valid: raise BadRequest('Forbidden', status_code=403) if 'refresh_token' in token_data: user_session.set_refresh_token(token_data['refresh_token']) user_info = _client.get_user_info(user_session.get_access_token()) if "email" not in user_info: user = User(email=None, sub=user_info["sub"]) else: user = User(email=user_info["email"], sub=user_info["sub"]) user_session.set_user_sub(user.get_sub()) _db.save_session(user_session, user) session['session_id'] = user_session.get_id() return redirect('/')