Skip to content

Instantly share code, notes, and snippets.

@brandoncordell
Created August 10, 2012 00:43
Show Gist options
  • Save brandoncordell/3309674 to your computer and use it in GitHub Desktop.
Save brandoncordell/3309674 to your computer and use it in GitHub Desktop.
//
// BCOAuthController.h
// Bivy
//
// Created by Brandon Cordell on 8/1/12.
// Copyright (c) 2012 lead&rock. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <WebKit/WebKit.h>
#import "AFNetworking.h"
static const NSString *const kKeychainItemName = @"Basecamp Next OAuth2";
static NSString *const kOAuthClientID = @"8128a3859e0c15a0a7a08c54ef3f10ba2cada31b";
static NSString *const kOAuthClientSecret = @"c85168bdda9660833b576c6f7c785335b597a309";
static NSString *const oAuthAuthorizationBaseURL = @"https://launchpad.37signals.com/authorization/new";
static NSString *const oAuthTokenURL = @"https://launchpad.37signals.com/authorization/token";
static NSString *const oAuthRedirectURL = @"http://bivyapp.com/auth";
@interface BCOAuthController : NSWindowController
@property (strong) NSString *verificationCode;
@property (strong) NSString *authToken;
@property (weak) IBOutlet WebView *webView;
@property (weak) IBOutlet NSProgressIndicator *loadProgress;
- (void)visitOAuthAuthorizationURL;
@end
//
// BCOAuthController.m
// Bivy
//
// Created by Brandon Cordell on 8/1/12.
// Copyright (c) 2012 lead&rock. All rights reserved.
//
#import "BCOAuthController.h"
@implementation BCOAuthController
@synthesize webView, loadProgress, verificationCode;
- (void)visitOAuthAuthorizationURL
{
NSString *oAuthAuthorizationURL =
[NSString stringWithFormat:@"%@?client_id=%@&redirect_uri=%@&type=web_server",
oAuthAuthorizationBaseURL,
kOAuthClientID,
oAuthRedirectURL];
[webView setMainFrameURL:oAuthAuthorizationURL];
}
#pragma mark - WebFrameLoadDelgate Methods
- (void)webView:(WebView *)sender
didStartProvisionalLoadForFrame:(WebFrame *)frame {
[loadProgress startAnimation:nil];
}
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
[loadProgress stopAnimation:nil];
NSString *mainFrameURL = [sender mainFrameURL];
NSRange redirectRange = [mainFrameURL rangeOfString:oAuthRedirectURL];
NSRange basecampRange = [mainFrameURL rangeOfString:oAuthAuthorizationBaseURL];
if (redirectRange.length > 0 && basecampRange.length == 0) {
NSLog(@"%@", mainFrameURL);
NSString *accessDenied = [oAuthRedirectURL stringByAppendingString:@"/?error=access_denied"];
NSString *oAuthRedirectURLWithRequestParameter = [oAuthRedirectURL stringByAppendingString:@"/?code="];
if ([mainFrameURL isEqualToString:accessDenied]) {
[NSApp endSheet:self.window];
} else {
verificationCode =
[[sender mainFrameURL] stringByReplacingOccurrencesOfString:oAuthRedirectURLWithRequestParameter
withString:@""];
NSLog(@"%@", verificationCode);
[NSApp endSheet:self.window];
}
}
}
- (void)webView:(WebView *)sender
didFailProvisionalLoadWithError:(NSError *)error
forFrame:(WebFrame *)frame
{
NSLog(@"Error: %@", error);
}
- (void)webView:(WebView *)sender
didFailLoadWithError:(NSError *)error
forFrame:(WebFrame *)frame
{
NSLog(@"Error: %@", error);
}
#pragma mark - Object Methods
- (void)awakeFromNib {
[self visitOAuthAuthorizationURL];
}
@end
//
// BCAppDelegate.h
// Bivy
//
// Created by Brandon Cordell on 7/30/12.
// Copyright (c) 2012 lead&rock. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#import "INAppStoreWindow.h"
#import "BCBasecampAPIClient.h"
#import "BCOAuthController.h"
@interface BCAppDelegate : NSObject <NSApplicationDelegate>
@property (strong) BCOAuthController *oAuthController;
@property (assign) IBOutlet INAppStoreWindow *window;
@property (weak) IBOutlet NSView *titleView;
@property (weak) IBOutlet NSImageView *avatar;
- (IBAction)openSheet:(id)sender;
@end
//
// BCAppDelegate.m
// Bivy
//
// Created by Brandon Cordell on 7/30/12.
// Copyright (c) 2012 lead&rock. All rights reserved.
//
#import "BCAppDelegate.h"
@implementation BCAppDelegate
@synthesize window, titleView, avatar;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
self.window.collectionBehavior = NSWindowCollectionBehaviorFullScreenPrimary;
self.window.titleBarHeight = 45.0;
self.window.centerFullScreenButton = YES;
self.window.centerTrafficLightButtons = YES;
self.window.fullScreenButtonRightMargin = 7.0;
self.window.trafficLightButtonsLeftMargin = 7.0;
// self.titleView is a an IBOutlet to an NSView that has been configured in IB with everything you want in the title bar
self.titleView.frame = self.window.titleBarView.bounds;
self.titleView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[self.window.titleBarView addSubview:self.titleView];
NSImage *_avatar;
NSURL *avatarUrl = [NSURL URLWithString:@"http://asset0.37img.com/global/76fe2e59c667a1cb64a34bf0e6d711e5e4f3ead280d5aade4fff272a8b20011f847b16cbc2a1d7e8611345c0c755a0299fb8e6bb5cc7f2cbd8e04f4965f64beb4b63ca68a3a96aa64e7777cfb207d6d8/avatar.gif?r=3"];
_avatar = [[NSImage alloc] initWithContentsOfURL:avatarUrl];
[avatar setImage:_avatar];
}
- (IBAction)openSheet:(id)sender {
_oAuthController = [[BCOAuthController alloc]
initWithWindowNibName:@"OAuthSheetView"];
[NSApp beginSheet:[_oAuthController window]
modalForWindow:window
modalDelegate:self
didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
contextInfo:CFBridgingRetain([_oAuthController verificationCode])];
}
- (void)didEndSheet:(NSWindow *)aSheet
returnCode:(NSInteger)returnCode
contextInfo:(void *)contextInfo
{
NSLog(@"%@", CFBridgingRelease(contextInfo));
[aSheet orderOut:self];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment