Skip to content

Commit

Permalink
Merge pull request #90 from auth0/bugfix-connection-scopes
Browse files Browse the repository at this point in the history
Fix `connection_scopes` issue
  • Loading branch information
hzalaz committed Mar 17, 2015
2 parents 9baf30e + 5f2e175 commit 3acf62f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ All notable changes to this project will be documented in this file.

##master

##Changed
- Fix how `connection_scopes` parameter was handled.

## 1.10.0 - 2015-03-16

###Added
- Property to hide statusBar in `A0Theme`.

Expand Down
15 changes: 5 additions & 10 deletions Lock/Tests/A0AuthParametersSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
@"google": @[],
@"linkedin": @[@"public_profile"],
};
[params setValue:@"facebook" forKey:A0ParameterConnection];
params.state = @"TEST";
params.device = @"Specta Test";
[params setValue:@"bar" forKey:@"foo"];
Expand All @@ -196,16 +197,10 @@
expect(dict[A0ParameterScope]).to.equal(@"openid profile offline_access");
});

it(@"should coalesce connection_scopes for FB in a NSString", ^{
expect(dict[A0ParameterConnectionScopes][@"facebook"]).to.equal(@"email,friends");
});

it(@"should coalesce connection_scopes for linkedin in a NSString", ^{
expect(dict[A0ParameterConnectionScopes][@"linkedin"]).to.equal(@"public_profile");
});

it(@"should skip connection_scopes for twitter", ^{
expect(dict[A0ParameterConnectionScopes][@"twitter"]).to.beNil();
it(@"should include specified scopes for connection", ^{
NSDictionary *payload = [params asAPIPayload];
expect(payload.allKeys).to.contain(@"connection_scope");
expect(payload[@"connection_scope"]).to.equal(@"email,friends");
});

it(@"should have state", ^{
Expand Down
13 changes: 8 additions & 5 deletions Pod/Classes/Core/A0AuthParameters.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,16 @@ + (instancetype)newWithScopes:(NSArray *)scopes {
}

- (NSDictionary *)asAPIPayload {
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:self.params];
params[A0ParameterScope] = ScopeValueFromNSArray(self.params[A0ParameterScope]);
NSMutableDictionary *params = self.params.mutableCopy;
[params removeObjectsForKeys:@[A0ParameterScope, A0ParameterConnectionScopes]];
NSMutableDictionary *payload = [NSMutableDictionary dictionaryWithDictionary:params];
payload[A0ParameterScope] = ScopeValueFromNSArray(self.params[A0ParameterScope]);
NSDictionary *connectionScopes = self.params[A0ParameterConnectionScopes];
if (connectionScopes.count > 0) {
params[A0ParameterConnectionScopes] = ConnectionScopeValuesFromNSDictionary(connectionScopes);
NSArray *connectionScope = connectionScopes[self.params[A0ParameterConnection]];
if (connectionScope) {
payload[@"connection_scope"] = [connectionScope componentsJoinedByString:@","];
}
return [NSDictionary dictionaryWithDictionary:params];
return payload;
}

- (NSArray *)scopes {
Expand Down
10 changes: 9 additions & 1 deletion Pod/Classes/Provider/Facebook/A0FacebookAuthenticator.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#import "A0Application.h"
#import "A0APIClient.h"
#import "A0IdentityProviderCredentials.h"
#import "A0AuthParameters.h"

#import <Facebook-iOS-SDK/FacebookSDK/Facebook.h>
#import <libextobjc/EXTScope.h>
Expand Down Expand Up @@ -92,7 +93,8 @@ -(void)authenticateWithParameters:(A0AuthParameters *)parameters success:(void (
[self executeAuthenticationWithCredentials:[[A0IdentityProviderCredentials alloc] initWithAccessToken:active.accessTokenData.accessToken] parameters:parameters success:success failure:failure];
} else {
@weakify(self);
[FBSession openActiveSessionWithReadPermissions:self.permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
NSArray *permissions = [self permissionsFromParameters:parameters];
[FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if (error) {
if (failure) {
A0LogError(@"Failed to open FB Session with error %@", error);
Expand Down Expand Up @@ -122,6 +124,12 @@ -(void)authenticateWithParameters:(A0AuthParameters *)parameters success:(void (

#pragma mark - Utility methods

- (NSArray *)permissionsFromParameters:(A0AuthParameters *)parameters {
NSArray *connectionScopes = parameters.connectionScopes[A0StrategyNameFacebook];
NSArray *permissions = connectionScopes.count > 0 ? connectionScopes : self.permissions;
A0LogDebug(@"Facebook Permissions %@", permissions);
return permissions;
}
- (void)executeAuthenticationWithCredentials:(A0IdentityProviderCredentials *)credentials
parameters:(A0AuthParameters *)parameters
success:(void(^)(A0UserProfile *, A0Token *))success
Expand Down
25 changes: 17 additions & 8 deletions Pod/Classes/Provider/GooglePlus/A0GooglePlusAuthenticator.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
#import "A0APIClient.h"
#import "A0IdentityProviderCredentials.h"
#import "A0Errors.h"
#import "A0AuthParameters.h"

@interface A0GooglePlusAuthenticator () <GPPSignInDelegate>
@property (copy, nonatomic) void (^successBlock)(A0UserProfile *, A0Token *);
@property (copy, nonatomic) void (^failureBlock)(NSError *);
@property (strong, nonatomic) A0AuthParameters *parameters;
@property (assign, nonatomic) BOOL authenticating;
@property (strong, nonatomic) NSArray *scopes;
@end

@implementation A0GooglePlusAuthenticator
Expand All @@ -48,15 +50,8 @@ - (instancetype)initWithClientId:(NSString *)clientId scopes:(NSArray *)scopes {
if (self) {
GPPSignIn *signIn = [GPPSignIn sharedInstance];
signIn.clientID = clientId;
NSMutableSet *scopeSet = [[NSMutableSet alloc] init];
[scopeSet addObject:kGTLAuthScopePlusLogin];
[scopeSet addObject:kGTLAuthScopePlusUserinfoEmail];
if (scopes) {
[scopeSet addObjectsFromArray:scopes];
}
signIn.scopes = [scopeSet allObjects];
self.scopes = [scopes copy];
signIn.delegate = self;
A0LogDebug(@"Initialised Google+ authenticator with scopes %@", signIn.scopes);
[self clearCallbacks];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
}
Expand Down Expand Up @@ -88,6 +83,7 @@ - (void)authenticateWithParameters:(A0AuthParameters *)parameters
self.failureBlock = failure;
self.parameters = parameters;
GPPSignIn *signIn = [GPPSignIn sharedInstance];
signIn.scopes = [self scopesFromParameters:parameters];
self.authenticating = YES;
[signIn authenticate];
A0LogVerbose(@"Starting Google+ Authentication...");
Expand Down Expand Up @@ -129,6 +125,19 @@ - (void)finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error

#pragma mark - Utility methods

- (NSArray *)scopesFromParameters:(A0AuthParameters *)parameters {
NSMutableSet *scopeSet = [[NSMutableSet alloc] init];
[scopeSet addObject:kGTLAuthScopePlusLogin];
[scopeSet addObject:kGTLAuthScopePlusUserinfoEmail];
NSArray *connectionScopes = parameters.connectionScopes[A0StrategyNameFacebook];
NSArray *scopes = connectionScopes.count > 0 ? connectionScopes : self.scopes;
if (scopes) {
[scopeSet addObjectsFromArray:scopes];
}
A0LogDebug(@"Google+ scopes %@", scopeSet);
return [scopeSet allObjects];
}

- (void)clearCallbacks {
self.failureBlock = ^(NSError *error) {};
self.successBlock = ^(A0UserProfile* profile, A0Token *token) {};
Expand Down

0 comments on commit 3acf62f

Please sign in to comment.