Skip to content

Commit

Permalink
Introduced "authenticator" property on Tin, and the TinBasicAuthentic…
Browse files Browse the repository at this point in the history
…ator class (and it's accompagnion Tin+BasicAuthentication).

Basic authentication is now handled through the TinAuthenticator.
This opens the door for other authentication schemes.
  • Loading branch information
Inferis committed Feb 9, 2012
1 parent e583c73 commit 0140445
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 13 deletions.
13 changes: 11 additions & 2 deletions Tin.h
Expand Up @@ -6,7 +6,17 @@
// Copyright 2011 10to1. All rights reserved.
//

@class Tin;
@class TinResponse;
@class AFHTTPClient;

@protocol TinAuthenticator <NSObject>

@optional
- (void)tin:(Tin*)tin setOptionsOnClient:(AFHTTPClient *)client;
- (NSString*)tin:(Tin*)tin transformQuery:(NSString *)query;

@end

@interface Tin : NSObject

Expand All @@ -23,12 +33,11 @@
//
// Especially useful if you keep the instance somewhere handy.
@property (nonatomic, retain) NSString *baseURI;
@property (nonatomic, retain) NSString *username;
@property (nonatomic, retain) NSString *password;
@property (nonatomic, retain) NSString *contentType;
@property (nonatomic, retain) NSDictionary *headers;
@property (nonatomic, assign) NSTimeInterval timeoutSeconds;
@property (nonatomic, assign) BOOL debugOutput;
@property (nonatomic, retain) id<TinAuthenticator> authenticator;

+ (TinResponse *)get:(NSString *)url;
+ (TinResponse *)get:(NSString *)url query:(id)query;
Expand Down
28 changes: 23 additions & 5 deletions Tin.m
Expand Up @@ -44,8 +44,7 @@ - (void)performRequest:(NSString *)method withURL:(NSString *)urlString andQuery

@implementation Tin
@synthesize baseURI;
@synthesize password;
@synthesize username;
@synthesize authenticator = _authenticator;
@synthesize timeoutSeconds;
@synthesize contentType;
@synthesize headers;
Expand Down Expand Up @@ -199,6 +198,23 @@ + (TinResponse *)delete:(NSString *)url query:(id)aQuery body:(id)body {

#pragma mark - Instance Methods

#pragma mark - initialisation & dealloc

- (id)init {
if ((self = [super init])) {
}
return self;
}

- (void)dealloc {
self.authenticator = nil;
self.baseURI = nil;
self.contentType = nil;
self.headers = nil;

[super dealloc];
}

#pragma mark GET ASYNCHRONOUS

- (void)get:(NSString *)url success:(void(^)(TinResponse *response))callback {
Expand Down Expand Up @@ -351,6 +367,9 @@ - (void)performRequest:(NSString *)method withURL:(NSString *)urlString andQuery
if (body && self.contentType != nil && ![self.contentType isEqualToString:@""]) {
[_client setDefaultHeader:@"Content-Type" value:self.contentType];
}

if ([self.authenticator respondsToSelector:@selector(tin:transformQuery:)])
query = [self.authenticator tin:self transformQuery:query];

// Initialize request
NSString *_url = [self normalizeURL:urlString withQuery:query];
Expand Down Expand Up @@ -398,9 +417,8 @@ - (void)performRequest:(NSString *)method withURL:(NSString *)urlString andQuery

// Sets all specified options to the request
- (void)setOptionsOnClient:(AFHTTPClient *)client {
if (self.username && self.password && ![self.username isEqualToString:@""] && ![self.password isEqualToString:@""]) {
[client setAuthorizationHeaderWithUsername:username password:password];
}
if ([self.authenticator respondsToSelector:@selector(tin:setOptionsOnClient:)])
[self.authenticator tin:self setOptionsOnClient:client];

if (self.headers) {
[self.headers enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
Expand Down
24 changes: 18 additions & 6 deletions Tin.xcodeproj/project.pbxproj
Expand Up @@ -32,7 +32,9 @@
9DDB7EC2148E4E4100EE6E90 /* AFURLConnectionOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 9DDB7EB6148E4E4100EE6E90 /* AFURLConnectionOperation.m */; };
9DDB7EC3148E4E4100EE6E90 /* AFXMLRequestOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 9DDB7EB8148E4E4100EE6E90 /* AFXMLRequestOperation.m */; };
9DDB7EC4148E4E4100EE6E90 /* UIImageView+AFNetworking.m in Sources */ = {isa = PBXBuildFile; fileRef = 9DDB7EBA148E4E4100EE6E90 /* UIImageView+AFNetworking.m */; };
9DFFD284148F7DA200370EC9 /* TinFile.m in Sources */ = {isa = PBXBuildFile; fileRef = 9DFFD283148F7DA200370EC9 /* TinFile.m */; };
E2ECE00914E4718400CF91DB /* Tin+BasicAuthentication.m in Sources */ = {isa = PBXBuildFile; fileRef = E2ECE00414E4718400CF91DB /* Tin+BasicAuthentication.m */; };
E2ECE00A14E4718400CF91DB /* TinBasicAuthenticator.m in Sources */ = {isa = PBXBuildFile; fileRef = E2ECE00614E4718400CF91DB /* TinBasicAuthenticator.m */; };
E2ECE00B14E4718400CF91DB /* TinFile.m in Sources */ = {isa = PBXBuildFile; fileRef = E2ECE00814E4718400CF91DB /* TinFile.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -86,8 +88,12 @@
9DDB7EB8148E4E4100EE6E90 /* AFXMLRequestOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFXMLRequestOperation.m; sourceTree = "<group>"; };
9DDB7EB9148E4E4100EE6E90 /* UIImageView+AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImageView+AFNetworking.h"; sourceTree = "<group>"; };
9DDB7EBA148E4E4100EE6E90 /* UIImageView+AFNetworking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImageView+AFNetworking.m"; sourceTree = "<group>"; };
9DFFD282148F7DA200370EC9 /* TinFile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TinFile.h; path = ../../Projects/butane/Tin/TinFile.h; sourceTree = "<group>"; };
9DFFD283148F7DA200370EC9 /* TinFile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TinFile.m; path = ../../Projects/butane/Tin/TinFile.m; sourceTree = "<group>"; };
E2ECE00314E4718400CF91DB /* Tin+BasicAuthentication.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "Tin+BasicAuthentication.h"; path = "Tin/Tin+BasicAuthentication.h"; sourceTree = "<group>"; };
E2ECE00414E4718400CF91DB /* Tin+BasicAuthentication.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "Tin+BasicAuthentication.m"; path = "Tin/Tin+BasicAuthentication.m"; sourceTree = "<group>"; };
E2ECE00514E4718400CF91DB /* TinBasicAuthenticator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TinBasicAuthenticator.h; path = Tin/TinBasicAuthenticator.h; sourceTree = "<group>"; };
E2ECE00614E4718400CF91DB /* TinBasicAuthenticator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TinBasicAuthenticator.m; path = Tin/TinBasicAuthenticator.m; sourceTree = "<group>"; };
E2ECE00714E4718400CF91DB /* TinFile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TinFile.h; path = Tin/TinFile.h; sourceTree = "<group>"; };
E2ECE00814E4718400CF91DB /* TinFile.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TinFile.m; path = Tin/TinFile.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -166,12 +172,16 @@
0520609013A54EBC005072D0 /* Tin */ = {
isa = PBXGroup;
children = (
9DFFD282148F7DA200370EC9 /* TinFile.h */,
9DFFD283148F7DA200370EC9 /* TinFile.m */,
E2ECE00714E4718400CF91DB /* TinFile.h */,
E2ECE00814E4718400CF91DB /* TinFile.m */,
051423FF13A5508F00E5BD31 /* Tin+Extensions.h */,
0514240013A5508F00E5BD31 /* Tin+Extensions.m */,
055E1A4A13A66CC600C7D258 /* TinResponse.h */,
055E1A4B13A66CC600C7D258 /* TinResponse.m */,
E2ECE00314E4718400CF91DB /* Tin+BasicAuthentication.h */,
E2ECE00414E4718400CF91DB /* Tin+BasicAuthentication.m */,
E2ECE00514E4718400CF91DB /* TinBasicAuthenticator.h */,
E2ECE00614E4718400CF91DB /* TinBasicAuthenticator.m */,
);
name = Tin;
sourceTree = "<group>";
Expand Down Expand Up @@ -345,7 +355,9 @@
9DDB7EC3148E4E4100EE6E90 /* AFXMLRequestOperation.m in Sources */,
9DDB7EC4148E4E4100EE6E90 /* UIImageView+AFNetworking.m in Sources */,
9D85CD8B148E80B300DBEFAC /* JSONKit.m in Sources */,
9DFFD284148F7DA200370EC9 /* TinFile.m in Sources */,
E2ECE00914E4718400CF91DB /* Tin+BasicAuthentication.m in Sources */,
E2ECE00A14E4718400CF91DB /* TinBasicAuthenticator.m in Sources */,
E2ECE00B14E4718400CF91DB /* TinFile.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
15 changes: 15 additions & 0 deletions Tin/Tin+BasicAuthentication.h
@@ -0,0 +1,15 @@
//
// Tin+BasicAuthentication.h
// Faering
//
// Created by Tom Adriaenssen on 09/02/12.
// Copyright (c) 2012 Adriaenssen BVBA. All rights reserved.
//

#import "Tin.h"

@interface Tin (BasicAuthentication)

- (Tin*)authenticateWithUsername:(NSString*)username password:(NSString*)password;

@end
19 changes: 19 additions & 0 deletions Tin/Tin+BasicAuthentication.m
@@ -0,0 +1,19 @@
//
// Tin+BasicAuthentication.m
// Faering
//
// Created by Tom Adriaenssen on 09/02/12.
// Copyright (c) 2012 Adriaenssen BVBA. All rights reserved.
//

#import "Tin+BasicAuthentication.h"
#import "TinBasicAuthenticator.h"

@implementation Tin (BasicAuthentication)

- (Tin*)authenticateWithUsername:(NSString*)username password:(NSString*)password {
self.authenticator = [TinBasicAuthenticator basicAuthenticatorWithUsername:username password:password];
return self;
}

@end
21 changes: 21 additions & 0 deletions Tin/TinBasicAuthenticator.h
@@ -0,0 +1,21 @@
//
// TinBasicAuthenticator.h
// Faering
//
// Created by Tom Adriaenssen on 09/02/12.
// Copyright (c) 2012 Adriaenssen BVBA. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Tin.h"

@interface TinBasicAuthenticator : NSObject<TinAuthenticator>

@property (nonatomic, retain) NSString *username;
@property (nonatomic, retain) NSString *password;

- (id)initWithUsername:(NSString*)username password:(NSString*)password;

+ (TinBasicAuthenticator*)basicAuthenticatorWithUsername:(NSString*)username password:(NSString*)password;

@end
42 changes: 42 additions & 0 deletions Tin/TinBasicAuthenticator.m
@@ -0,0 +1,42 @@
//
// TinBasicAuthenticator.m
// Faering
//
// Created by Tom Adriaenssen on 09/02/12.
// Copyright (c) 2012 Adriaenssen BVBA. All rights reserved.
//

#import "TinBasicAuthenticator.h"
#import "AFHTTPClient.h"

@implementation TinBasicAuthenticator

@synthesize password = _password;
@synthesize username = _username;

- (id)initWithUsername:(NSString*)username password:(NSString*)password {
if ((self = [self init])) {
self.username = username;
self.password = password;
}
return self;
}

- (void)dealloc {
self.password = nil;
self.username = nil;

[super dealloc];
}

- (void)tin:(Tin *)tin setOptionsOnClient:(AFHTTPClient *)client {
if (self.username && self.password && ![self.username isEqualToString:@""] && ![self.password isEqualToString:@""]) {
[client setAuthorizationHeaderWithUsername:self.username password:self.password];
}
}

+ (TinBasicAuthenticator*)basicAuthenticatorWithUsername:(NSString*)username password:(NSString*)password {
return [[[TinBasicAuthenticator alloc] initWithUsername:username password:password] autorelease];
}

@end

0 comments on commit 0140445

Please sign in to comment.