def authenticate( cls, username, password, tenant_name, url, serialize_format="json", deserialize_format="json"): if url.endswith("v2.0") or url.endswith("v2.0/"): url = '{0}/tokens'.format(url) else: url = '{0}/v2.0/tokens'.format(url) client = AutoMarshallingHTTPClient( serialize_format, deserialize_format) #both are set because AutoMarshallingHTTPClient doesn't handle headers #correctly client.default_headers["Content-Type"] = "application/{0}".format( serialize_format) client.default_headers["Accept"] = "application/{0}".format( deserialize_format) request_entity = requests.Auth( username=username, password=password, tenant_name=tenant_name) r = client.request( "POST", url, request_entity=request_entity, response_entity_type=responses.AuthResponse) if not r.ok: raise Exception("Failed to authenticate") r.entity = responses.AuthResponse.deserialize( r.content, deserialize_format) if r.entity is None: raise Exception("Failed to parse Auth response Body") return r
def authenticate_v2( url, username=None, password=None, tenant_name=None, tenant_id=None, token=None, domain=None, serialize_format="json", deserialize_format="json"): url = '{0}/v2.0/tokens'.format(url) client = AutoMarshallingHTTPClient(serialize_format, deserialize_format) client.default_headers["Content-Type"] = "application/{0}".format( serialize_format) client.default_headers["Accept"] = "application/{0}".format( deserialize_format) kwargs = {} kwargs["tenant_name"] = tenant_name kwargs["tenant_id"] = tenant_id if password is not None: password_creds = v2.PasswordCredentials( username=username, password=password) request_entity = v2.Auth( tenant_name=tenant_name, tenant_id=tenant_id, password_creds=password_creds) r = client.request( "POST", url, request_entity=request_entity, response_entity_type=v2.AuthResponse) if not r.ok: raise Exception("Failed to authenticate") if r.entity is None: raise Exception("Failed to parse Auth response Body") return r
def authenticate_v3( url, username=None, password=None, user_id=None, domain_id=None, domain_name=None, token=None): url = '{0}/v3/auth/tokens'.format(url) client = AutoMarshallingHTTPClient("json", "json") client.default_headers["Content-Type"] = "application/json" client.default_headers["Accept"] = "application/json" if user_id is not None: domain = None username = None else: domain = v3.Domain(name=domain_name, id_=domain_id) password = v3.Password(user=v3.User( name=username, password=password, id_=user_id, domain=domain)) if token is not None: kwargs = {"token": v3.Token(id_=token), "methods": ["token"]} else: kwargs = {"password": password, "methods": ["password"]} request_entity = v3.Auth(identity=v3.Identity(**kwargs)) r = client.request( "POST", url, request_entity=request_entity) if not r.ok: raise Exception("Failed to authenticate") return r
def authenticate(cls, username, password, tenant_name, url, serialize_format="json", deserialize_format="json"): if url.endswith("v2.0") or url.endswith("v2.0/"): url = '{0}/tokens'.format(url) else: url = '{0}/v2.0/tokens'.format(url) client = AutoMarshallingHTTPClient(serialize_format, deserialize_format) #both are set because AutoMarshallingHTTPClient doesn't handle headers #correctly client.default_headers["Content-Type"] = "application/{0}".format( serialize_format) client.default_headers["Accept"] = "application/{0}".format( deserialize_format) request_entity = requests.Auth(username=username, password=password, tenant_name=tenant_name) r = client.request("POST", url, request_entity=request_entity, response_entity_type=responses.AuthResponse) if not r.ok: raise Exception("Failed to authenticate") r.entity = responses.AuthResponse.deserialize(r.content, deserialize_format) if r.entity is None: raise Exception("Failed to parse Auth response Body") return r