def login(client, credentials):
if not IUsernamePassword.providedBy(credentials):<EOL><INDENT>raise UnhandledCredentials()<EOL><DEDENT>d = client.callRemote(<EOL>PasswordLogin, username=credentials.username)<EOL>def cbChallenge(response):<EOL><INDENT>args = PasswordChallengeResponse.determineFrom(<EOL>response['<STR_LIT>'], credentials.password)<EOL>d = client.callRemote(PasswordChallengeResponse, **args)<EOL>return d.addCallback(lambda ignored: client)<EOL><DEDENT>d.addCallback(cbChallenge)<EOL>return d<EOL>
Authenticate using the given L{AMP} instance.  The protocol must be
connected to a server with responders for L{PasswordLogin} and
L{PasswordChallengeResponse}.

@param client: A connected L{AMP} instance which will be used to issue
    authentication commands.

@param credentials: An object providing L{IUsernamePassword} which will
    be used to authenticate this connection to the server.

@return: A L{Deferred} which fires when authentication has succeeded or
    which fails with L{UnauthorizedLogin} if the server rejects the
    authentication attempt.