Permalink
Browse files

Add support for signing in with an authorization code

  • Loading branch information...
1 parent c3bcdc0 commit fd354c099415afa5559395d332facbae3802b569 @soffes soffes committed Jul 26, 2012
Showing with 33 additions and 1 deletion.
  1. +1 −0 CheddarKit/CDKHTTPClient.h
  2. +32 −1 CheddarKit/CDKHTTPClient.m
@@ -27,6 +27,7 @@ typedef void (^CDKHTTPClientFailure)(AFJSONRequestOperation *operation, NSError
// User
- (void)signInWithLogin:(NSString *)login password:(NSString *)password success:(CDKHTTPClientSuccess)success failure:(CDKHTTPClientFailure)failure;
+- (void)signInWithAuthorizationCode:(NSString *)code success:(CDKHTTPClientSuccess)success failure:(CDKHTTPClientFailure)failure;
- (void)signUpWithUsername:(NSString *)username email:(NSString *)email password:(NSString *)password success:(CDKHTTPClientSuccess)success failure:(CDKHTTPClientFailure)failure;
- (void)updateCurrentUserWithSuccess:(CDKHTTPClientSuccess)success failure:(CDKHTTPClientFailure)failure;
@@ -114,7 +114,7 @@ - (void)signInWithLogin:(NSString *)login password:(NSString *)password success:
nil];
[self setAuthorizationHeaderWithUsername:_clientID password:_clientSecret];
- [self postPath:@"oauth/access_token" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
+ [self postPath:@"/oauth/token" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
__weak NSManagedObjectContext *context = [CDKUser mainContext];
[context performBlockAndWait:^{
NSDictionary *dictionary = (NSDictionary *)responseObject;
@@ -138,6 +138,37 @@ - (void)signInWithLogin:(NSString *)login password:(NSString *)password success:
}
+- (void)signInWithAuthorizationCode:(NSString *)code success:(CDKHTTPClientSuccess)success failure:(CDKHTTPClientFailure)failure {
+ NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
+ code, @"code",
+ @"authorization_code", @"grant_type",
+ nil];
+
+ [self setAuthorizationHeaderWithUsername:_clientID password:_clientSecret];
+ [self postPath:@"/oauth/token" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
+ __weak NSManagedObjectContext *context = [CDKUser mainContext];
+ [context performBlockAndWait:^{
+ NSDictionary *dictionary = (NSDictionary *)responseObject;
+ CDKUser *user = [CDKUser objectWithDictionary:[dictionary objectForKey:@"user"]];
+ user.accessToken = [dictionary objectForKey:@"access_token"];
+ [user save];
+
+ [self changeUser:user];
+ [CDKUser setCurrentUser:user];
+ }];
+
+ if (success) {
+ success((AFJSONRequestOperation *)operation, responseObject);
+ }
+ } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
+ if (failure) {
+ failure((AFJSONRequestOperation *)operation, error);
+ }
+ }];
+ [self clearAuthorizationHeader];
+}
+
+
- (void)signUpWithUsername:(NSString *)username email:(NSString *)email password:(NSString *)password success:(CDKHTTPClientSuccess)success failure:(CDKHTTPClientFailure)failure {
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
username, @"user[username]",

0 comments on commit fd354c0

Please sign in to comment.