- Add Social Login

- Fix Vari
This commit is contained in:
giuseppenuc committed 2016-03-17 12:30:53 +01:00
1 parent 22499a85d3
commit b0bcfad332
19 files changed
+899 -742

No files matched your search

@@ -27,6 +27,7 @@ NSString * const kAFOAuthClientCredentialsGrantType = @"client_credentials";
NSString * const kAFOAuthPasswordCredentialsGrantType = @"password";
NSString * const kAFOAuthCodeGrantType = @"authorization_code";
NSString * const kAFOAuthRefreshGrantType = @"refresh_token";
NSString * const kAFOAuthFacebookCredentialGrantType = @"provider_facebook_token";
NSString * const AFOAuth2ErrorDomain = @"com.alamofire.networking.oauth2.error";
@@ -35,9 +36,9 @@ static NSError * AFErrorFromRFC6749Section5_2Error(id object) {
if (![object valueForKey:@"error"] || [[object valueForKey:@"error"] isEqual:[NSNull null]]) {
return nil;
}
NSMutableDictionary *mutableUserInfo = [NSMutableDictionary dictionary];
NSString *description = nil;
if ([object valueForKey:@"error_description"]) {
description = [object valueForKey:@"error_description"];
@@ -54,15 +55,15 @@ static NSError * AFErrorFromRFC6749Section5_2Error(id object) {
description = NSLocalizedStringFromTable(@"The authorization grant type is not supported by the authorization server.", @"AFOAuth2Manager", @"invalid_request");
}
}
if (description) {
mutableUserInfo[NSLocalizedDescriptionKey] = description;
}
if ([object valueForKey:@"error_uri"]) {
mutableUserInfo[NSLocalizedRecoverySuggestionErrorKey] = [object valueForKey:@"error_uri"];
}
return [NSError errorWithDomain:AFOAuth2ErrorDomain code:-1 userInfo:mutableUserInfo];
}
@@ -101,19 +102,19 @@ static NSError * AFErrorFromRFC6749Section5_2Error(id object) {
NSParameterAssert(url);
NSParameterAssert(clientID);
NSParameterAssert(secret);
self = [super initWithBaseURL:url sessionConfiguration:configuration];
if (!self) {
return nil;
}
self.serviceProviderIdentifier = [self.baseURL host];
self.clientID = clientID;
self.secret = secret;
self.useHTTPBasicAuthentication = YES;
[self.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
return self;
}
@@ -121,7 +122,7 @@ static NSError * AFErrorFromRFC6749Section5_2Error(id object) {
- (void)setUseHTTPBasicAuthentication:(BOOL)useHTTPBasicAuthentication {
_useHTTPBasicAuthentication = useHTTPBasicAuthentication;
if (self.useHTTPBasicAuthentication) {
[self.requestSerializer setAuthorizationHeaderFieldWithUsername:self.clientID password:self.secret];
} else {
@@ -133,11 +134,11 @@ static NSError * AFErrorFromRFC6749Section5_2Error(id object) {
if (!secret) {
secret = @"";
}
_secret = secret;
}
#pragma mark -
#pragma mark -
- (NSURLSessionTask *)authenticateUsingOAuthWithURLString:(NSString *)URLString
username:(NSString *)username
@@ -147,31 +148,53 @@ static NSError * AFErrorFromRFC6749Section5_2Error(id object) {
failure:(void (^)(NSError * _Nonnull))failure {
NSParameterAssert(username);
NSParameterAssert(password);
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setValue:kAFOAuthPasswordCredentialsGrantType forKey:@"grant_type"];
[parameters setValue:username forKey:@"username"];
[parameters setValue:password forKey:@"password"];
if (scope) {
[parameters setValue:scope forKey:@"scope"];
}
return [self authenticateUsingOAuthWithURLString:URLString parameters:parameters success:success failure:failure];
}
- (NSURLSessionTask *)authenticateUsingFacebookOAuthWithURLString:(NSString *)URLString
facebookID:(NSString *)facebookid
facebookToken:(NSString *)facebookToken
scope:(NSString *)scope
success:(void (^)(AFOAuthCredential * _Nonnull))success
failure:(void (^)(NSError * _Nonnull))failure {
NSParameterAssert(facebookid);
NSParameterAssert(facebookToken);
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setValue:kAFOAuthFacebookCredentialGrantType forKey:@"grant_type"];
[parameters setValue:facebookid forKey:@"username"];
[parameters setValue:facebookToken forKey:@"password"];
if (scope) {
[parameters setValue:scope forKey:@"scope"];
}
return [self authenticateUsingOAuthWithURLString:URLString parameters:parameters success:success failure:failure];
}
- (NSURLSessionTask *)authenticateUsingOAuthWithURLString:(NSString *)URLString
scope:(NSString *)scope
success:(void (^)(AFOAuthCredential * _Nonnull))success
failure:(void (^)(NSError * _Nonnull))failure {
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setValue:kAFOAuthClientCredentialsGrantType forKey:@"grant_type"];
if (scope) {
[parameters setValue:scope forKey:@"scope"];
}
return [self authenticateUsingOAuthWithURLString:URLString parameters:parameters success:success failure:failure];
}
@@ -182,12 +205,12 @@ static NSError * AFErrorFromRFC6749Section5_2Error(id object) {
failure:(void (^)(NSError *error))failure
{
NSParameterAssert(refreshToken);
NSDictionary *parameters = @{
@"grant_type": kAFOAuthRefreshGrantType,
@"refresh_token": refreshToken
};
return [self authenticateUsingOAuthWithURLString:URLString parameters:parameters success:success failure:failure];
}
@@ -199,13 +222,13 @@ static NSError * AFErrorFromRFC6749Section5_2Error(id object) {
{
NSParameterAssert(code);
NSParameterAssert(uri);
NSDictionary *parameters = @{
@"grant_type": kAFOAuthCodeGrantType,
@"code": code,
@"redirect_uri": uri
};
return [self authenticateUsingOAuthWithURLString:URLString parameters:parameters success:success failure:failure];
}
@@ -220,7 +243,7 @@ static NSError * AFErrorFromRFC6749Section5_2Error(id object) {
mutableParameters[@"client_secret"] = self.secret;
}
parameters = [NSDictionary dictionaryWithDictionary:mutableParameters];
NSURLSessionTask *task;
task = [self POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (!responseObject) {
@@ -229,46 +252,46 @@ static NSError * AFErrorFromRFC6749Section5_2Error(id object) {
}
return;
}
if ([responseObject valueForKey:@"error"]) {
if (failure) {
failure(AFErrorFromRFC6749Section5_2Error(responseObject));
}
}
NSString *refreshToken = [responseObject valueForKey:@"refresh_token"];
if (!refreshToken || [refreshToken isEqual:[NSNull null]]) {
refreshToken = [parameters valueForKey:@"refresh_token"];
}
AFOAuthCredential *credential = [AFOAuthCredential credentialWithOAuthToken:[responseObject valueForKey:@"access_token"] tokenType:[responseObject valueForKey:@"token_type"]];
if (refreshToken) { // refreshToken is optional in the OAuth2 spec
[credential setRefreshToken:refreshToken];
}
// Expiration is optional, but recommended in the OAuth2 spec. It not provide, assume distantFuture === never expires
NSDate *expireDate = [NSDate distantFuture];
id expiresIn = [responseObject valueForKey:@"expires_in"];
if (expiresIn && ![expiresIn isEqual:[NSNull null]]) {
expireDate = [NSDate dateWithTimeIntervalSinceNow:[expiresIn doubleValue]];
}
if (expireDate) {
[credential setExpiration:expireDate];
}
if (success) {
success(credential);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
return task;
}