Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Token authentication #6

Merged
merged 1 commit into from

2 participants

Jelle Vandebeeck Piet Jaspers
Jelle Vandebeeck
Collaborator

Implemented Basic authentication with a token

Piet Jaspers pjaspers merged commit 0b4ba26 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 2, 2012
  1. Jelle Vandebeeck
This page is out of date. Refresh to see the latest.
1  Tin/Tin+BasicAuthentication.h
View
@@ -8,5 +8,6 @@
@interface Tin (BasicAuthentication)
- (Tin*)authenticateWithUsername:(NSString*)username password:(NSString*)password;
+- (Tin*)authenticateWithToken:(NSString *)token;
@end
5 Tin/Tin+BasicAuthentication.m
View
@@ -13,4 +13,9 @@ - (Tin*)authenticateWithUsername:(NSString*)username password:(NSString*)passwor
return self;
}
+- (Tin*)authenticateWithToken:(NSString *)token {
+ self.authenticator = [TinBasicAuthenticator basicAuthenticatorWithToken:token];
+ return self;
+}
+
@end
4 Tin/TinBasicAuthenticator.h
View
@@ -7,12 +7,14 @@
#import "Tin.h"
@interface TinBasicAuthenticator : NSObject<TinAuthenticator>
-
+@property (nonatomic, retain) NSString *token;
@property (nonatomic, retain) NSString *username;
@property (nonatomic, retain) NSString *password;
- (id)initWithUsername:(NSString*)username password:(NSString*)password;
+- (id)initWithToken:(NSString*)token;
+ (TinBasicAuthenticator*)basicAuthenticatorWithUsername:(NSString*)username password:(NSString*)password;
++ (TinBasicAuthenticator*)basicAuthenticatorWithToken:(NSString *)token;
@end
17 Tin/TinBasicAuthenticator.m
View
@@ -7,7 +7,7 @@
#import "AFHTTPClient.h"
@implementation TinBasicAuthenticator
-
+@synthesize token = _token;
@synthesize password = _password;
@synthesize username = _username;
@@ -19,6 +19,13 @@ - (id)initWithUsername:(NSString*)username password:(NSString*)password {
return self;
}
+- (id)initWithToken:(NSString*)token {
+ if ((self = [self init])) {
+ self.token = token;
+ }
+ return self;
+}
+
- (void)dealloc {
self.password = nil;
self.username = nil;
@@ -27,7 +34,9 @@ - (void)dealloc {
}
- (NSString *)tin:(Tin *)tin applyAuthenticationOnClient:(AFHTTPClient *)client withMethod:(NSString*)method url:(NSString *)url query:(NSString *)query {
- if (self.username && self.password && ![self.username isEqualToString:@""] && ![self.password isEqualToString:@""]) {
+ if (self.token && ![self.token isEqualToString:@""]) {
+ [client setAuthorizationHeaderWithToken:self.token];
+ } else if (self.username && self.password && ![self.username isEqualToString:@""] && ![self.password isEqualToString:@""]) {
[client setAuthorizationHeaderWithUsername:self.username password:self.password];
}
return query;
@@ -37,4 +46,8 @@ + (TinBasicAuthenticator*)basicAuthenticatorWithUsername:(NSString*)username pas
return [[[TinBasicAuthenticator alloc] initWithUsername:username password:password] autorelease];
}
++ (TinBasicAuthenticator*)basicAuthenticatorWithToken:(NSString *)token {
+ return [[[TinBasicAuthenticator alloc] initWithToken:token] autorelease];
+}
+
@end
4 tests/TinAsyncTest.m
View
@@ -187,13 +187,13 @@ - (void)testErroredRequest {
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:10.0];
}
-- (void)testBasicAuthentication {
+- (void)testBasicPasswordAuthentication {
[self prepare];
Tin *tin = [[[Tin alloc] init] authenticateWithUsername:@"user" password:@"passwd"];
[tin get:@"http://httpbin.org/basic-auth/user/passwd" success:^(TinResponse *response) {
GHTestLog(@"Response: %@", response);
GHAssertNil(response.error, @"We should be authenticated");
- [self notify:kGHUnitWaitStatusSuccess forSelector:@selector(testBasicAuthentication)];
+ [self notify:kGHUnitWaitStatusSuccess forSelector:@selector(testBasicPasswordAuthentication)];
}];
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:10.0];
}
Something went wrong with that request. Please try again.