Skip to content

Commit

Permalink
Passing along response parameters to the caller
Browse files Browse the repository at this point in the history
  • Loading branch information
Sander Nieuwenhuizen committed May 31, 2015
1 parent e4d4945 commit 647a3ab
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions OAuthSwift/OAuth2Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class OAuth2Swift: NSObject {
static let appOnlyAuthenticationErrorCode = 1
}

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


Expand All @@ -53,22 +53,22 @@ public class OAuth2Swift: NSObject {
notification in
NSNotificationCenter.defaultCenter().removeObserver(self.observer!)
let url = notification.userInfo![CallbackNotification.optionsURLKey] as! NSURL
var parameters: Dictionary<String, String> = Dictionary()
if ((url.query) != nil){
parameters = url.query!.parametersFromQueryString()
var responseParameters: Dictionary<String, String> = Dictionary()
if let query = url.query {
responseParameters = query.parametersFromQueryString()
}
if ((url.fragment) != nil && url.fragment!.isEmpty == false) {
parameters = url.fragment!.parametersFromQueryString()
responseParameters = url.fragment!.parametersFromQueryString()
}
if (parameters["access_token"] != nil){
self.client.credential.oauth_token = parameters["access_token"]!
success(credential: self.client.credential, response: nil)
if let accessToken = responseParameters["access_token"] {
self.client.credential.oauth_token = accessToken
success(credential: self.client.credential, response: nil, parameters: responseParameters)
}
if (parameters["code"] != nil){
self.postOAuthAccessTokenWithRequestTokenByCode(parameters["code"]!.stringByRemovingPercentEncoding!,
if let code = responseParameters["code"] {
self.postOAuthAccessTokenWithRequestTokenByCode(code.stringByRemovingPercentEncoding!,
callbackURL:callbackURL,
success: { credential, response in
success(credential: credential, response: response)
success: { credential, response, responseParameters in
success(credential: credential, response: response, parameters: responseParameters)
}, failure: failure)

}
Expand Down Expand Up @@ -106,17 +106,20 @@ public class OAuth2Swift: NSObject {
self.client.post(self.access_token_url!, parameters: parameters, success: {
data, response in
var responseJSON: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil)
var accessToken = ""
if let parameters:NSDictionary = responseJSON as? NSDictionary{
accessToken = parameters["access_token"] as! String

let responseParameters: NSDictionary

if responseJSON != nil {
responseParameters = responseJSON as! NSDictionary
} else {
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding) as String!
let parameters = responseString.parametersFromQueryString()
accessToken = parameters["access_token"]!
responseParameters = responseString.parametersFromQueryString()
}

let accessToken = responseParameters["access_token"] as! String
self.client.credential.oauth_token = accessToken
self.client.credential.oauth2 = true
success(credential: self.client.credential, response: response)
success(credential: self.client.credential, response: response, parameters: responseParameters)
}, failure: failure)
}

Expand Down

0 comments on commit 647a3ab

Please sign in to comment.