def _authenticate(self, validate, user_id, tenant_id=None): if tenant_id: duser = api.USER.get_by_tenant(user_id, tenant_id) if duser == None: raise fault.UnauthorizedFault("Unauthorized on this tenant") else: duser = api.USER.get(user_id) if duser == None: raise fault.UnauthorizedFault("Unauthorized") if not duser.enabled: raise fault.UserDisabledFault("Your account has been disabled") if not validate(duser): raise fault.UnauthorizedFault("Unauthorized") # use user's default tenant_id if one is not specified tenant_id = tenant_id or duser.tenant_id # check for an existing token dtoken = api.TOKEN.get_for_user_by_tenant(duser.id, tenant_id) if not dtoken or dtoken.expires < datetime.now(): # Create new token dtoken = models.Token() dtoken.id = str(uuid.uuid4()) dtoken.user_id = duser.id dtoken.tenant_id = tenant_id dtoken.expires = datetime.now() + timedelta(days=1) api.TOKEN.create(dtoken) return self.__get_auth_data(dtoken)
def add_credentials(user, type, key, secrete, tenant=None): obj = db_models.Token() obj.user_id = user obj.type = type obj.key = key obj.secret = secrete obj.tenant_id = tenant return db_api.CREDENTIALS.create(obj)
def add_token(token, user, tenant, expires): user = db_api.USER.get_by_name(name=user).id tenant = db_api.TENANT.get_by_name(name=tenant).id obj = db_models.Token() obj.id = token obj.user_id = user obj.tenant_id = tenant obj.expires = datetime.datetime.strptime(expires.replace("-", ""), "%Y%m%dT%H:%M") return db_api.TOKEN.create(obj)
def add_credentials(user, type, key, secrete, tenant=None): user = db_api.USER.get_by_name(user).id if tenant: tenant = db_api.TENANT.get_by_name(tenant).id obj = db_models.Token() obj.user_id = user obj.type = type obj.key = key obj.secret = secrete obj.tenant_id = tenant return db_api.CREDENTIALS.create(obj)
def create_token(self, user_id, token_id=None, tenant_id=None, expires=None): self.get_user(user_id) self.get_tenant(tenant_id) obj = models.Token() obj.user_id = user_id obj.tenant_id = tenant_id if token_id is not None: obj.id = token_id else: obj.id = uuid.uuid4().hex if expires is not None: obj.expires = self.str_to_datetime(expires) else: obj.expires = Command.get_datetime_tomorrow() return self.token_manager.create(obj)