Permalink
Browse files

remove app engine dependency for logging in, use simperium authorizat…

…ion with api key
  • Loading branch information...
1 parent 5a86bd8 commit d47e3e596671ae0406bddf06827390791784568c @fredrocious fredrocious committed Mar 19, 2013
Showing with 34 additions and 21 deletions.
  1. +9 −7 NotationPrefsViewController.m
  2. +3 −1 SimplenoteSession.h
  3. +22 −13 SimplenoteSession.m
@@ -448,20 +448,22 @@ - (void)cancelLoginVerifier {
- (void)startLoginVerifier {
if (!loginVerifier && [[syncAccountField stringValue] length] && [[syncPasswordField stringValue] length]) {
- NSURL *loginURL = [SimplenoteSession servletURLWithPath:@"/api2/login" parameters:nil];
- loginVerifier = [[SyncResponseFetcher alloc] initWithURL:loginURL POSTData:
- [[[NSDictionary dictionaryWithObjectsAndKeys:
- [syncAccountField stringValue], @"email", [syncPasswordField stringValue], @"password", @"1", @"api", nil] URLEncodedString]
- dataUsingEncoding:NSUTF8StringEncoding] delegate:self];
+ NSURL *loginURL = [SimplenoteSession authURLWithPath:@"/authorize/" parameters:nil];
+ NSDictionary *headers = [NSDictionary dictionaryWithObject:kSimperiumAPIKey forKey:@"X-Simperium-API-Key"];
+ NSDictionary *login = [NSDictionary dictionaryWithObjectsAndKeys:
+ [syncAccountField stringValue], @"username", [syncPasswordField stringValue], @"password", nil];
+
+ loginVerifier = [[SyncResponseFetcher alloc] initWithURL:loginURL POSTData:[[login jsonStringValue] dataUsingEncoding:NSUTF8StringEncoding] headers:headers contentType:@"application/json" delegate:self];
+
[loginVerifier start];
[self setVerificationStatus:VERIFY_IN_PROGRESS withString:@""];
}
}
- (void)syncResponseFetcher:(SyncResponseFetcher*)fetcher receivedData:(NSData*)data returningError:(NSString*)errString {
- BOOL authFailed = errString && [fetcher statusCode] >= 400;
+ BOOL authFailed = errString || [fetcher statusCode] >= 400;
- [self setVerificationStatus:errString ? VERIFY_FAILED : VERIFY_SUCCESS withString:
+ [self setVerificationStatus:authFailed ? VERIFY_FAILED : VERIFY_SUCCESS withString:
authFailed ? NSLocalizedString(@"Incorrect login and password", @"sync status menu msg") : errString];
if (authFailed) {
View
@@ -29,6 +29,8 @@
extern NSString *SimplenoteServiceName;
extern NSString *SimplenoteSeparatorKey;
+extern NSString * const kSimperiumAPIKey;
+
@interface SimplenoteSession : NSObject <SyncServiceSession, NSCopying> {
NSString *emailAddress, *password, *simperiumToken;
@@ -68,7 +70,7 @@ extern NSString *SimplenoteSeparatorKey;
+ (NSString*)localizedServiceTitle;
+ (NSString*)serviceName;
+ (NSString*)nameOfKeyElement;
-+ (NSURL*)servletURLWithPath:(NSString*)path parameters:(NSDictionary*)params;
++ (NSURL*)authURLWithPath:(NSString*)path parameters:(NSDictionary*)params;
+ (NSURL*)simperiumURLWithPath:(NSString*)path parameters:(NSDictionary*)params;
+ (SCNetworkReachabilityRef)createReachabilityRefWithCallback:(SCNetworkReachabilityCallBack)callout target:(id)aTarget;
//+ (NSString*)localizedNetworkDiagnosticMessage;
View
@@ -38,6 +38,10 @@
NSString *SimplenoteSeparatorKey = @"SepStr";
#define kSimplenoteSessionIndexBatchSize 100
+// If you're building from source and want to sync with Simplenote, you can request your own API key
+// For now, please email: fred@simperium.com
+NSString * const kSimperiumAPIKey = @"your simplenote-simperium api key";
+
@implementation SimplenoteSession
static void SNReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkConnectionFlags flags, void * info);
@@ -54,17 +58,17 @@ + (NSString*)nameOfKeyElement {
return @"key";
}
-+ (NSURL*)servletURLWithPath:(NSString*)path parameters:(NSDictionary*)params {
++ (NSURL*)authURLWithPath:(NSString*)path parameters:(NSDictionary*)params {
NSAssert(path != nil, @"path is required");
- //path example: "/api2/index"
+ //path example: "/authorize"
NSString *queryStr = params ? [NSString stringWithFormat:@"?%@", [params URLEncodedString]] : @"";
- return [NSURL URLWithString:[NSString stringWithFormat:@"https://simple-note.appspot.com%@%@", path, queryStr]];
+ return [NSURL URLWithString:[NSString stringWithFormat:@"https://auth.simperium.com/1/chalk-bump-f49%@%@", path, queryStr]];
}
+ (NSURL*)simperiumURLWithPath:(NSString*)path parameters:(NSDictionary*)params {
NSAssert(path != nil, @"path is required");
- //path example: "/api2/index"
+ //path example: "/Note/index"
NSString *queryStr = params ? [NSString stringWithFormat:@"?%@", [params URLEncodedString]] : @"";
return [NSURL URLWithString:[NSString stringWithFormat:@"https://api.simperium.com/1/chalk-bump-f49%@%@", path, queryStr]];
@@ -73,7 +77,7 @@ + (NSURL*)simperiumURLWithPath:(NSString*)path parameters:(NSDictionary*)params
#if 0
+ (NSString*)localizedNetworkDiagnosticMessage {
- CFNetDiagnosticRef networkDiagnosticRef = CFNetDiagnosticCreateWithURL(kCFAllocatorDefault, (CFURLRef)[self servletURLWithPath:@"/" parameters:nil]);
+ CFNetDiagnosticRef networkDiagnosticRef = CFNetDiagnosticCreateWithURL(kCFAllocatorDefault, (CFURLRef)[self authURLWithPath:@"/" parameters:nil]);
if (networkDiagnosticRef) {
CFStringRef localizedDiagnosticString = NULL;
@@ -90,7 +94,7 @@ + (NSString*)localizedNetworkDiagnosticMessage {
+ (SCNetworkReachabilityRef)createReachabilityRefWithCallback:(SCNetworkReachabilityCallBack)callout target:(id)aTarget {
SCNetworkReachabilityRef reachableRef = NULL;
- if ((reachableRef = SCNetworkReachabilityCreateWithName(NULL, [[[SimplenoteSession servletURLWithPath:
+ if ((reachableRef = SCNetworkReachabilityCreateWithName(NULL, [[[SimplenoteSession authURLWithPath:
@"/" parameters:nil] host] UTF8String]))) {
SCNetworkReachabilityContext context = {0, aTarget, NULL, NULL, NULL};
if (SCNetworkReachabilitySetCallback(reachableRef, callout, &context)) {
@@ -292,11 +296,11 @@ - (SyncResponseFetcher*)loginFetcher {
//init fetcher for login method; credentials POSTed in body
if (!loginFetcher) {
- NSURL *loginURL = [SimplenoteSession servletURLWithPath:@"/api2/login" parameters:nil];
- loginFetcher = [[SyncResponseFetcher alloc] initWithURL:loginURL POSTData:
- [[[NSDictionary dictionaryWithObjectsAndKeys:
- emailAddress, @"email", password, @"password", @"1", @"api", nil] URLEncodedString]
- dataUsingEncoding:NSUTF8StringEncoding] delegate:self];
+ NSURL *loginURL = [SimplenoteSession authURLWithPath:@"/authorize/" parameters:nil];
+ NSDictionary *headers = [NSDictionary dictionaryWithObject:kSimperiumAPIKey forKey:@"X-Simperium-API-Key"];
+ NSDictionary *login = [NSDictionary dictionaryWithObjectsAndKeys:
+ emailAddress, @"username", password, @"password", nil];
+ loginFetcher = [[SyncResponseFetcher alloc] initWithURL:loginURL POSTData:[[login jsonStringValue] dataUsingEncoding:NSUTF8StringEncoding] headers:headers contentType:@"application/json" delegate:self];
}
return loginFetcher;
}
@@ -1082,9 +1086,14 @@ - (void)syncResponseFetcher:(SyncResponseFetcher*)fetcher receivedData:(NSData*)
NSUInteger i = 0;
if (fetcher == loginFetcher) {
- if ([[fetcher headers] objectForKey:@"Simperium-Token"]) {
+ @try {
+ responseDictionary = [NSDictionary dictionaryWithJSONString:bodyString];
+ } @catch (NSException *e) {
+ NSLog(@"Exception while parsing Simplenote user: %@", [e reason]);
+ }
+ if ([responseDictionary objectForKey:@"access_token"]) {
[simperiumToken autorelease];
- simperiumToken = [[[fetcher headers] objectForKey:@"Simperium-Token"] retain];
+ simperiumToken = [[responseDictionary objectForKey:@"access_token"] retain];
} else {
[self _stoppedWithErrorString:NSLocalizedString(@"No authorization token", @"Simplenote-specific error")];
}

0 comments on commit d47e3e5

Please sign in to comment.