Skip to content

Commit

Permalink
Merge pull request #10 from slash7/master
Browse files Browse the repository at this point in the history
Fix oauth problem when user denies access.
  • Loading branch information
dongri committed Dec 12, 2014
2 parents 414df61 + f0c406d commit df8fe80
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions OAuthSwift/OAuth1Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@
import Foundation
import UIKit

// OAuthSwift errors
public let OAuthSwiftErrorDomain = "oauthswift.error"

class OAuth1Swift {

var client: OAuthSwiftClient

var consumer_key: String
var consumer_secret: String
var request_token_url: String
var authorize_url: String
var access_token_url: String

var observer: AnyObject?

init(consumerKey: String, consumerSecret: String, requestTokenUrl: String, authorizeUrl: String, accessTokenUrl: String){
self.consumer_key = consumerKey
self.consumer_secret = consumerSecret
Expand All @@ -29,7 +32,7 @@ class OAuth1Swift {
self.access_token_url = accessTokenUrl
self.client = OAuthSwiftClient(consumerKey: consumerKey, consumerSecret: consumerSecret)
}

struct CallbackNotification {
static let notificationName = "OAuthSwiftCallbackNotificationName"
static let optionsURLKey = "OAuthSwiftCallbackNotificationOptionsURLKey"
Expand All @@ -39,28 +42,34 @@ class OAuth1Swift {
static let domain = "OAuthSwiftErrorDomain"
static let appOnlyAuthenticationErrorCode = 1
}

typealias TokenSuccessHandler = (credential: OAuthSwiftCredential, response: NSURLResponse) -> Void
typealias FailureHandler = (error: NSError) -> Void

// 0. Start
func authorizeWithCallbackURL(callbackURL: NSURL, success: TokenSuccessHandler, failure: ((error: NSError) -> Void)) {
self.postOAuthRequestTokenWithCallbackURL(callbackURL, success: {
credential, response in

self.observer = NSNotificationCenter.defaultCenter().addObserverForName(CallbackNotification.notificationName, object: nil, queue: NSOperationQueue.mainQueue(), usingBlock:{
notification in
//NSNotificationCenter.defaultCenter().removeObserver(self)
NSNotificationCenter.defaultCenter().removeObserver(self.observer!)
let url = notification.userInfo![CallbackNotification.optionsURLKey] as NSURL
let parameters = url.query!.parametersFromQueryString()
var credential: OAuthSwiftCredential = self.client.credential
self.client.credential.oauth_token = parameters["oauth_token"]!
self.client.credential.oauth_verifier = parameters["oauth_verifier"]!
self.postOAuthAccessTokenWithRequestToken({
credential, response in
success(credential: credential, response: response)
}, failure: failure)
if (parameters["oauth_token"] != nil && parameters["oauth_verifier"] != nil) {
var credential: OAuthSwiftCredential = self.client.credential
self.client.credential.oauth_token = parameters["oauth_token"]!
self.client.credential.oauth_verifier = parameters["oauth_verifier"]!
self.postOAuthAccessTokenWithRequestToken({
credential, response in
success(credential: credential, response: response)
}, failure: failure)
} else {
let userInfo = [NSLocalizedFailureReasonErrorKey: NSLocalizedString("Oauth problem.", comment: "")]
failure(error: NSError(domain: OAuthSwiftErrorDomain, code: -1, userInfo: userInfo))
return
}
})
// 2. Authorize
let queryURL = NSURL(string: self.authorize_url + "?oauth_token=\(credential.oauth_token)")
Expand Down

0 comments on commit df8fe80

Please sign in to comment.