def post(user_id): logger.debug(f"get token string: {request.json}.") from RDS import Token token = Util.getTokenObject(request.json) logger.debug(f"parsed token: {token}.") code = 200 try: user = utility.storage.getUser(user_id) except UserNotExistsError as e: user = User(user_id) try: utility.storage.addTokenToUser(token, user) except UserHasTokenAlreadyError as e: abort(409, description=str(e)) except UserNotExistsError as e: # only force adding, if user not exists, otherwise it also overwrites existing tokens. utility.storage.addTokenToUser(token, user, Force=True) code = 201 return jsonify({"success": True}), code
def getAllServicesForUser(self, user: User) -> list: """ Returns a `list` for all services which the user has registered a token for. """ response = requests.get( f"{self.address}/user/{user.username}/token", verify=(os.environ.get("VERIFY_SSL", "True") == "True"), ) data = response.json() # TODO: adjust to oai spec services = [] try: for index, l in enumerate(data["list"]): token = Util.getTokenObject(l) services.append({ "id": index, "servicename": token.servicename, "access_token": token.access_token, "projects": self.getProjectsForToken(token), "implements": token._service.implements, "informations": self.getInformations(token.service) }) except Exception as e: logger.error(e) raise UserNotFoundError(user) return services
def getTokenForServiceFromUser(self, service: BaseService, user: User) -> Token: """ Returns the token from type Token (struct: servicename: str, access_token: str) for given service from given user. Raise ServiceNotExistsError, if no token for service was found. """ response = requests.get( f"{self.address}/user/{user.username}/token/{service.servicename}", verify=(os.environ.get("VERIFY_SSL", "True") == "True"), ) data = response.json() while type(data) is not dict: data = json.loads(data) if response.status_code != 200: if "error" in data: if data["error"] == "TokenNotExistsError": raise TokenNotFoundError(Token(user, service, "NOT_USED")) if data["error"] == "UserNotExistsError": raise UserNotFoundError(user) if data["error"] == "ServiceNotExistsError": raise ServiceNotFoundError(service) raise Exception(data) token = Util.getTokenObject(data) if isinstance(token.service, OAuth2Service): token.service._client_secret = "" if isinstance(token, OAuth2Token): token._refresh_token = "" return token
def test_init_objects(self): self.assertEqual(Util.getServiceObject(json.dumps(self.oauthservice1)), self.oauthservice1) svc1 = LoginService("MusterService", ["fileStorage"]) self.assertEqual(Util.getServiceObject(json.dumps(svc1)), svc1) self.assertNotEqual( Util.getServiceObject(json.dumps(svc1)).__class__, self.oauthservice1.__class__) self.assertEqual(Util.getUserObject(json.dumps(self.user1)), self.user1) self.assertEqual(Util.getTokenObject(json.dumps(self.token1)), self.token1)