Skip to content

Commit

Permalink
Merge pull request #8 from Hexaville/callbackurl-extends
Browse files Browse the repository at this point in the history
add blockForCallbackURLQueryParams label to Oauth1 to extend callback…
  • Loading branch information
noppoMan committed Jul 19, 2017
2 parents 8684bb6 + 5b7141b commit 5a8419c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Sources/HexavilleAuth/HexavilleAuth+Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ extension HexavilleAuth {
switch type {
case .oauth1(let provider):
router.use(.get, provider.path) { request, context in
let requestToken = try provider.getRequestToken()
let queryItems: [URLQueryItem] = provider.oauth.blockForCallbackURLQueryParams?(request) ?? []
let requestToken = try provider.getRequestToken(withCallbackURLQueryItems: queryItems)
context.session?["hexaville.oauth_token_secret"] = requestToken.oauthTokenSecret
context.session?["hexaville.oauth_token"] = requestToken.oauthToken
let location = try provider.createAuthorizeURL(requestToken: requestToken).absoluteString
Expand Down
8 changes: 5 additions & 3 deletions Sources/HexavilleAuth/OAuth/OAuth1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,27 @@ public class OAuth1 {
let authorizeUrl: String
let accessTokenUrl: String
let callbackURL: CallbackURL
let blockForCallbackURLQueryParams: ((Request) -> [URLQueryItem])?
let withAllowedCharacters: CharacterSet

public init(consumerKey: String, consumerSecret: String, requestTokenUrl: String, authorizeUrl: String, accessTokenUrl: String, callbackURL: CallbackURL, withAllowedCharacters: CharacterSet = CharacterSet.alphanumerics) {
public init(consumerKey: String, consumerSecret: String, requestTokenUrl: String, authorizeUrl: String, accessTokenUrl: String, callbackURL: CallbackURL, blockForCallbackURLQueryParams: ((Request) -> [URLQueryItem])? = nil, withAllowedCharacters: CharacterSet = CharacterSet.alphanumerics) {
self.consumerKey = consumerKey
self.consumerSecret = consumerSecret
self.requestTokenUrl = requestTokenUrl
self.authorizeUrl = authorizeUrl
self.accessTokenUrl = accessTokenUrl
self.callbackURL = callbackURL
self.blockForCallbackURLQueryParams = blockForCallbackURLQueryParams
self.withAllowedCharacters = withAllowedCharacters
}

private func dictionary2Query(_ dict: [String: String]) -> String {
return dict.map({ "\($0.key)=\($0.value)" }).joined(separator: "&")
}

public func getRequestToken() throws -> RequestToken {
public func getRequestToken(withCallbackURLQueryItems queryItems: [URLQueryItem]) throws -> RequestToken {
var params = [
"oauth_callback": callbackURL.absoluteURL()!.absoluteString,
"oauth_callback": callbackURL.absoluteURL(withQueryItems: queryItems)!.absoluteString,
"oauth_consumer_key": consumerKey,
"oauth_nonce": OAuth1.generateNonce(),
"oauth_signature_method": "HMAC-SHA1",
Expand Down
11 changes: 11 additions & 0 deletions Sources/HexavilleAuth/Providers/CallbackURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@ public struct CallbackURL {
public func absoluteURL() -> URL? {
return URL(string: "\(baseURL)\(path)")
}

public func absoluteURL(withQueryItems queryItems: [URLQueryItem]) -> URL? {
guard let url = absoluteURL() else { return nil }
if queryItems.count > 0 {
let additionalQuery = queryItems.filter({ $0.value != nil }).map({ "\($0.name)=\($0.value!)" }).joined(separator: "&")
let separator = url.queryItems.count == 0 ? "?" : "&"
return URL(string: url.absoluteString+separator+additionalQuery)
}

return url
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public struct TwitterAuthorizationProvider: OAuth1AuthorizationProvidable {

public let callback: RespodWithCredential

public init(path: String, consumerKey: String, consumerSecret: String, callbackURL: CallbackURL, scope: String, callback: @escaping RespodWithCredential) {
public init(path: String, consumerKey: String, consumerSecret: String, callbackURL: CallbackURL, blockForCallbackURLQueryParams: ((Request) -> [URLQueryItem])? = nil, scope: String, callback: @escaping RespodWithCredential) {
self.path = path

self.oauth = OAuth1(
Expand All @@ -35,6 +35,7 @@ public struct TwitterAuthorizationProvider: OAuth1AuthorizationProvidable {
authorizeUrl: "https://api.twitter.com/oauth/authenticate",
accessTokenUrl: "https://api.twitter.com/oauth/access_token",
callbackURL: callbackURL,
blockForCallbackURLQueryParams: blockForCallbackURLQueryParams,
withAllowedCharacters: .twitterQueryAllowed
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ public protocol OAuth1AuthorizationProvidable {
var path: String { get }
var oauth: OAuth1 { get }
var callback: RespodWithCredential { get }
init(path: String, consumerKey: String, consumerSecret: String, callbackURL: CallbackURL, scope: String, callback: @escaping RespodWithCredential)
func getRequestToken() throws -> RequestToken
init(path: String, consumerKey: String, consumerSecret: String, callbackURL: CallbackURL, blockForCallbackURLQueryParams: ((Request) -> [URLQueryItem])?, scope: String, callback: @escaping RespodWithCredential)
func getRequestToken(withCallbackURLQueryItems queryItems: [URLQueryItem]) throws -> RequestToken
func createAuthorizeURL(requestToken: RequestToken) throws -> URL
func getAccessToken(request: Request, requestToken: RequestToken) throws -> Credential
func authorize(request: Request, requestToken: RequestToken) throws -> (Credential, LoginUser)
}

extension OAuth1AuthorizationProvidable {
public func getRequestToken() throws -> RequestToken {
return try oauth.getRequestToken()
public func getRequestToken(withCallbackURLQueryItems queryItems: [URLQueryItem]) throws -> RequestToken {
return try oauth.getRequestToken(withCallbackURLQueryItems: queryItems)
}

public func createAuthorizeURL(requestToken: RequestToken) throws -> URL {
Expand Down

0 comments on commit 5a8419c

Please sign in to comment.