Skip to content

Commit

Permalink
Merge pull request #6 from Hexaville/prorosum-http-client
Browse files Browse the repository at this point in the history
replace http client from URLSession to Prorsum.HTTPClient
  • Loading branch information
noppoMan committed Jul 13, 2017
2 parents 9dbb01b + f7d1bbd commit 843990d
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 89 deletions.
78 changes: 46 additions & 32 deletions Sources/HexavilleAuth/OAuth/OAuth1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ public enum OAuth1Error: Error {
extension OAuth1Error: CustomStringConvertible {
public var description: String {
switch self {
case .couldNotGenerateSignature:
return "couldNotGenerateSignature"

case .invalidAuthrozeURL(let url):
return "invalidAuthrozeURL: \(url)"

case .missingRequiredParameters(let param):
return "missingRequiredParameter: \(param)"

case .accessTokenIsMissingInSession:
return "accessTokenIsMissingInSession"

case .verifyFailed(let req, let res):
return stringify(code: "verifyFailed", request: req, response: res)

Expand All @@ -31,9 +43,6 @@ extension OAuth1Error: CustomStringConvertible {

case .failedToGetRequestToken(let req, let res):
return stringify(code: "failedToGetRequestToken", request: req, response: res)

default:
return "\(self)"
}
}

Expand Down Expand Up @@ -123,22 +132,23 @@ public class OAuth1 {

params["oauth_signature"] = sig

var urlRequest = URLRequest(url: URL(string: requestTokenUrl)!)
urlRequest.httpMethod = "POST"

urlRequest.addValue(OAuth1.oAuthAuthorizationString(fromParameters: params, withAllowedCharacters: withAllowedCharacters), forHTTPHeaderField: "Authorization")

let (response, data) = try URLSession.shared.resumeSync(with: urlRequest)
let authorizationValue = OAuth1.oAuthAuthorizationString(fromParameters: params, withAllowedCharacters: withAllowedCharacters)

let bodyDictionary = OAuth1.parse(bodyData: data)
let request = Request(
method: .post,
url: URL(string: requestTokenUrl)!,
headers: ["Authorization": authorizationValue]
)
let client = try HTTPClient(url: request.url)
try client.open()
let response = try client.request(request)

guard (200..<300).contains(response.statusCode) else {
throw OAuth1Error.failedToGetRequestToken(
urlRequest.transform(),
response.transform(withBodyData: data)
)
throw OAuth1Error.failedToGetRequestToken(request, response)
}

let bodyDictionary = OAuth1.parse(bodyData: response.body.asData())

guard let oauthToken = bodyDictionary["oauth_token"] else {
throw OAuth1Error.missingRequiredParameters("oauth_token")
}
Expand Down Expand Up @@ -189,21 +199,23 @@ public class OAuth1 {

params["oauth_signature"] = sig

var urlRequest = URLRequest(url: URL(string: verifyURL)!)
urlRequest.httpMethod = "GET"
let authrozationString = OAuth1.oAuthAuthorizationString(fromParameters: params, withAllowedCharacters: withAllowedCharacters)

urlRequest.addValue(OAuth1.oAuthAuthorizationString(fromParameters: params, withAllowedCharacters: withAllowedCharacters), forHTTPHeaderField: "Authorization")
let request = Request(
method: .get,
url: URL(string: verifyURL)!,
headers: ["Authorization": authrozationString]
)

let (response, data) = try URLSession.shared.resumeSync(with: urlRequest)
let client = try HTTPClient(url: request.url)
try client.open()
let response = try client.request(request)

guard (200..<300).contains(response.statusCode) else {
throw OAuth1Error.verifyFailed(
urlRequest.transform(),
response.transform(withBodyData: data)
)
throw OAuth1Error.verifyFailed(request, response)
}

return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] ?? [:]
return try JSONSerialization.jsonObject(with: response.body.asData(), options: []) as? [String: Any] ?? [:]
}

public func getAccessToken(request: Request, requestToken: RequestToken) throws -> Credential {
Expand Down Expand Up @@ -239,21 +251,23 @@ public class OAuth1 {

params["oauth_signature"] = sig

var urlRequest = URLRequest(url: URL(string: urlString)!)
urlRequest.httpMethod = "POST"
let authrozationString = OAuth1.oAuthAuthorizationString(fromParameters: params, withAllowedCharacters: withAllowedCharacters)

urlRequest.addValue(OAuth1.oAuthAuthorizationString(fromParameters: params, withAllowedCharacters: withAllowedCharacters), forHTTPHeaderField: "Authorization")
let request = Request(
method: .post,
url: URL(string: urlString)!,
headers: ["Authorization": authrozationString]
)

let (response, data) = try URLSession.shared.resumeSync(with: urlRequest)
let client = try HTTPClient(url: request.url)
try client.open()
let response = try client.request(request)

guard (200..<300).contains(response.statusCode) else {
throw OAuth1Error.failedToGetAccessToken(
urlRequest.transform(),
response.transform(withBodyData: data)
)
throw OAuth1Error.failedToGetAccessToken(request, response)
}

return try Credential(withDictionary: OAuth1.parse(bodyData: data))
return try Credential(withDictionary: OAuth1.parse(bodyData: response.body.asData()))
}
}

Expand Down
22 changes: 14 additions & 8 deletions Sources/HexavilleAuth/OAuth/OAuth2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,26 @@ public class OAuth2 {
"redirect_uri=\(self.callbackURL.absoluteURL()!.absoluteString)"
]

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpBody = body.joined(separator: "&").data
let request = Request(
method: .post,
url: url,
headers: [
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded"
],
body: body.joined(separator: "&").data
)

let (response, bodyData) = try URLSession.shared.resumeSync(with: request)
let client = try HTTPClient(url: request.url)
try client.open()
let response = try client.request(request)

guard (200..<300).contains(response.statusCode) else {
throw HexavilleAuthError.responseError(response.transform(withBodyData: bodyData))
throw HexavilleAuthError.responseError(response)
}

do {
let bodyDictionary = try JSONSerialization.jsonObject(with: bodyData, options: []) as! [String: Any]
let bodyDictionary = try JSONSerialization.jsonObject(with: response.body.asData(), options: []) as! [String: Any]
return try Credential(withDictionary: bodyDictionary)
} catch {
throw error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,21 @@ public struct FacebookAuthorizationProvider: OAuth2AuthorizationProvidable {

public func authorize(request: Request) throws -> (Credential, LoginUser) {
let credential = try self.getAccessToken(request: request)
let url = URL(string: "https://graph.facebook.com/me?fields=id,name,email,picture,gender&access_token=\(credential.accessToken)")!
let (response, data) = try URLSession.shared.resumeSync(with: URLRequest(url: url))

let request = Request(
method: .get,
url: URL(string: "https://graph.facebook.com/me?fields=id,name,email,picture,gender&access_token=\(credential.accessToken)")!
)

let client = try HTTPClient(url: request.url)
try client.open()
let response = try client.request(request)

guard (200..<300).contains(response.statusCode) else {
throw HexavilleAuthError.responseError(response.transform(withBodyData: data))
throw HexavilleAuthError.responseError(response)
}

guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else {
guard let json = try JSONSerialization.jsonObject(with: response.body.asData(), options: []) as? [String: Any] else {
throw FacebookAuthorizationProviderError.bodyShouldBeAJSON
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,21 @@ public struct GithubAuthorizationProvider: OAuth2AuthorizationProvidable {

public func authorize(request: Request) throws -> (Credential, LoginUser) {
let credential = try self.getAccessToken(request: request)
let url = URL(string: "https://api.github.com/user?access_token=\(credential.accessToken)")!
let (response, data) = try URLSession.shared.resumeSync(with: URLRequest(url: url))

let request = Request(
method: .get,
url: URL(string: "https://api.github.com/user?access_token=\(credential.accessToken)")!
)

let client = try HTTPClient(url: request.url)
try client.open()
let response = try client.request(request)

guard (200..<300).contains(response.statusCode) else {
throw HexavilleAuthError.responseError(response.transform(withBodyData: data))
throw HexavilleAuthError.responseError(response)
}

guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else {
guard let json = try JSONSerialization.jsonObject(with: response.body.asData(), options: []) as? [String: Any] else {
throw GithubAuthorizationProviderError.bodyShouldBeAJSON
}

Expand Down
41 changes: 0 additions & 41 deletions Sources/HexavilleAuth/URLSession.swift

This file was deleted.

0 comments on commit 843990d

Please sign in to comment.