def authUsernamePassword(username, password): # Authenticate using simple password service = DirectoryService() creds = UsernamePassword(username, password) try: id = yield service.requestAvatarId(creds) print("OK via UsernamePassword, avatarID: {id}".format(id=id)) print(" {name}".format(name=id.fullNames)) except UnauthorizedLogin: print("Via UsernamePassword, could not authenticate") print("") # Authenticate using Digest algorithm = "md5" # "md5-sess" cnonce = "/rrD6TqPA3lHRmg+fw/vyU6oWoQgzK7h9yWrsCmv/lE=" entity = "00000000000000000000000000000000" method = "GET" nc = "00000001" nonce = "128446648710842461101646794502" qop = None realm = "host.example.com" uri = "http://host.example.com" responseHash = calcResponse( calcHA1( algorithm.lower(), username, realm, password, nonce, cnonce ), algorithm.lower(), nonce, nc, cnonce, qop, method, uri, entity ) response = ( 'Digest username="******", uri="{uri}", response={hash}'.format( username=username, uri=uri, hash=responseHash ) ) fields = { "realm": realm, "nonce": nonce, "response": response, "algorithm": algorithm, } creds = DigestedCredentials(username, method, realm, fields) try: id = yield service.requestAvatarId(creds) print("OK via DigestedCredentials, avatarID: {id}".format(id=id)) print(" {name}".format(name=id.fullNames)) except UnauthorizedLogin: print("Via DigestedCredentials, could not authenticate")
def lookup(shortNames): service = DirectoryService() print( "Service = {service}\n" "Session = {service.session}\n" "Node = {service.node}\n" # "Local node = {service.localNode}\n" .format(service=service) ) print("-" * 80) for shortName in shortNames: print("Looking up short name: {0}".format(shortName)) record = yield service.recordWithShortName(service.recordType.user, shortName) if record: print(record.description()) continue matchExpression = MatchExpression( service.fieldName.shortNames, shortName, matchType=MatchType.equals, ) records = yield service.recordsFromExpression(matchExpression) for record in records: print(record.description()) compoundExpression = CompoundExpression( [ MatchExpression( service.fieldName.shortNames, shortName, matchType=MatchType.contains ), MatchExpression( service.fieldName.emailAddresses, shortName, matchType=MatchType.contains ), ], Operand.OR ) records = yield service.recordsFromExpression(compoundExpression) for record in records: print(record.description())
def setUp(self): self.service = DirectoryService()