Skip to content

Commit

Permalink
[XMPPStream] Standardize naming on Facebook accessToken.
Browse files Browse the repository at this point in the history
Add initWithFacebookAppId:
Add hostName check to prevent wait on SRV record timeout.
Fix spacing.
  • Loading branch information
Eric Chamberlain committed Oct 8, 2011
1 parent fe52d5c commit 9ee8125
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 22 deletions.
12 changes: 9 additions & 3 deletions Core/XMPPStream.h
Expand Up @@ -62,7 +62,7 @@ typedef enum XMPPStreamErrorCode XMPPStreamErrorCode;
UInt16 hostPort;

NSString *tempPassword;
BOOL isAuthToken;
BOOL isAccessToken;

NSString *appId;

Expand Down Expand Up @@ -106,6 +106,12 @@ typedef enum XMPPStreamErrorCode XMPPStreamErrorCode;
**/
- (id)initP2PFrom:(XMPPJID *)myJID;

/**
* Facebook Chat X-FACEBOOK-PLATFORM SASL authentication initialization.
* This is a convienence init method to help configure Facebook Chat.
**/
- (id)initWithFacebookAppId:(NSString *)fbAppId;

/**
* XMPPStream uses a multicast delegate.
* This allows one to add multiple delegates to a single XMPPStream instance,
Expand Down Expand Up @@ -414,7 +420,7 @@ typedef enum XMPPStreamErrorCode XMPPStreamErrorCode;
/**
* Authentication.
*
* The authenticateWithPassword:error: and authenticateWithFacebookAuthToken:error: methods are asynchronous.
* The authenticateWithPassword:error: and authenticateWithFacebookAccessToken:error: methods are asynchronous.
* Each will return immediately, and the delegate methods are used to determine success.
* See the xmppStreamDidAuthenticate: and xmppStream:didNotAuthenticate: methods.
*
Expand All @@ -437,7 +443,7 @@ typedef enum XMPPStreamErrorCode XMPPStreamErrorCode;
- (BOOL)supportsXFacebookPlatformAuthentication;
- (BOOL)supportsDeprecatedPlainAuthentication;
- (BOOL)supportsDeprecatedDigestAuthentication;
- (BOOL)authenticateWithFacebookAuthToken:(NSString *)authToken error:(NSError **)errPtr;
- (BOOL)authenticateWithFacebookAccessToken:(NSString *)accessToken error:(NSError **)errPtr;
- (BOOL)authenticateWithPassword:(NSString *)password error:(NSError **)errPtr;
- (BOOL)authenticateAnonymously:(NSError **)errPtr;

Expand Down
66 changes: 47 additions & 19 deletions Core/XMPPStream.m
Expand Up @@ -45,6 +45,9 @@
NSString *const XMPPStreamErrorDomain = @"XMPPStreamErrorDomain";
NSString *const XMPPStreamDidChangeMyJIDNotification = @"XMPPStreamDidChangeMyJID";

static NSString *const XMPPFacebookChatHostName = @"chat.facebook.com";


enum XMPPStreamFlags
{
kP2PInitiator = 1 << 0, // If set, we are the P2P initializer
Expand Down Expand Up @@ -170,6 +173,22 @@ - (id)initP2PFrom:(XMPPJID *)jid
return self;
}


/**
* Facebook Chat X-FACEBOOK-PLATFORM SASL authentication initialization.
* This is a convienence init method to help configure Facebook Chat.
**/
- (id)initWithFacebookAppId:(NSString *)fbAppId
{
if ((self = [self init]))
{
self.appId = fbAppId;
self.myJID = [XMPPJID jidWithString:XMPPFacebookChatHostName];
self.hostName = XMPPFacebookChatHostName;
}
return self;
}

/**
* Standard deallocation method.
* Every object variable declared in the header file should be released here.
Expand Down Expand Up @@ -813,6 +832,14 @@ - (BOOL)connect:(NSError **)errPtr

// Notify delegates
[multicastDelegate xmppStreamWillConnect:self];

// Check for Facebook Chat and set hostName if not set.
// As of October 8, 2011, Facebook doesn't have their XMPP SRV records configured properly
// and we have to wait for the SRV timeout before trying the A record.
if ([hostName length] == 0 && [myJID.domain isEqualToString:XMPPFacebookChatHostName])
{
self.hostName = XMPPFacebookChatHostName;
}

if ([hostName length] == 0)
{
Expand Down Expand Up @@ -1757,7 +1784,7 @@ - (BOOL)supportsDeprecatedDigestAuthentication
* This method attempts to connect to the Facebook Chat servers
* using the Facebook OAuth token returned by the Facebook OAuth 2.0 authentication process.
**/
- (BOOL)authenticateWithFacebookAuthToken:(NSString *)authToken error:(NSError **)errPtr
- (BOOL)authenticateWithFacebookAccessToken:(NSString *)accessToken error:(NSError **)errPtr
{
XMPPLogTrace();

Expand All @@ -1782,11 +1809,11 @@ - (BOOL)authenticateWithFacebookAuthToken:(NSString *)authToken error:(NSError *
}

// Save authentication information
isAuthToken = YES;
isAccessToken = YES;
[tempPassword release];
tempPassword = [authToken copy];
tempPassword = [accessToken copy];

result = [self authenticateWithPassword:authToken error:&err];
result = [self authenticateWithPassword:accessToken error:&err];

[err retain];
[pool drain];
Expand All @@ -1809,7 +1836,7 @@ - (BOOL)authenticateWithFacebookAuthToken:(NSString *)authToken error:(NSError *
/**
* This method attempts to sign-in to the server using the configured myJID and given password.
* This method also works with Facebook Chat and the Facebook user's password, but authenticating
* with authenticateWithFacebookAuthToken:error: is preferred by Facebook.
* with authenticateWithFacebookAccessToken:error: is preferred by Facebook.
**/
- (BOOL)authenticateWithPassword:(NSString *)password error:(NSError **)errPtr
{
Expand Down Expand Up @@ -1849,19 +1876,20 @@ - (BOOL)authenticateWithPassword:(NSString *)password error:(NSError **)errPtr
return_from_block;
}

// Facebook authentication
if (isAuthToken)
{
[self sendSASLRequestForMechanism:@"X-FACEBOOK-PLATFORM"];
// Facebook authentication
if (isAccessToken)
{
[self sendSASLRequestForMechanism:@"X-FACEBOOK-PLATFORM"];

// Update state
state = STATE_XMPP_AUTH_1;
} else if ([self supportsDigestMD5Authentication])
}
else if ([self supportsDigestMD5Authentication])
{
[self sendSASLRequestForMechanism:@"DIGEST-MD5"];
[self sendSASLRequestForMechanism:@"DIGEST-MD5"];

// Save authentication information
isAuthToken = NO;
isAccessToken = NO;
[tempPassword release];
tempPassword = [password copy];

Expand Down Expand Up @@ -2023,7 +2051,7 @@ - (BOOL)authenticateAnonymously:(NSError **)errPtr
withTimeout:TIMEOUT_XMPP_WRITE
tag:TAG_XMPP_WRITE_STREAM];

isAuthToken = NO;
isAccessToken = NO;

// Update state
state = STATE_XMPP_AUTH_3;
Expand Down Expand Up @@ -2856,7 +2884,7 @@ - (void)handleRegistration:(NSXMLElement *)response
}

/**
* After the authenticateWithPassword:error: or authenticateWithFacebookAuthToken:error: methods are invoked, an
* After the authenticateWithPassword:error: or authenticateWithFacebookAccessToken:error: methods are invoked, an
* authentication message is sent to the server.
* If the server supports digest-md5 sasl authentication, it is used. Otherwise plain sasl authentication is used,
* assuming the server supports it.
Expand All @@ -2871,7 +2899,7 @@ - (void)handleAuth1:(NSXMLElement *)response

XMPPLogTrace();

if ([self supportsDigestMD5Authentication] || isAuthToken)
if ([self supportsDigestMD5Authentication] || isAccessToken)
{
// We're expecting a challenge response
// If we get anything else we can safely assume it's the equivalent of a failure response
Expand All @@ -2888,11 +2916,11 @@ - (void)handleAuth1:(NSXMLElement *)response
// We'll release this object at the end of this else block
id<XMPPSASLAuthentication> auth = nil;

if (isAuthToken)
if (isAccessToken)
{
auth = [[XMPPXFacebookPlatformAuthentication alloc] initWithChallenge:response
appId:self.appId
authToken:tempPassword];
accessToken:tempPassword];

// Update state
state = STATE_XMPP_AUTH_3;
Expand Down Expand Up @@ -2943,7 +2971,7 @@ - (void)handleAuth1:(NSXMLElement *)response

// Release unneeded resources
[auth release];
isAuthToken = NO;
isAccessToken = NO;
[tempPassword release]; tempPassword = nil;
}
}
Expand Down Expand Up @@ -3437,7 +3465,7 @@ - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
parser = nil;

// Clear any saved authentication information
isAuthToken = NO;
isAccessToken = NO;
[tempPassword release]; tempPassword = nil;

// Clear stored elements
Expand Down

0 comments on commit 9ee8125

Please sign in to comment.