From 098a80df510538b3fe61f2496369496baed2fe0d Mon Sep 17 00:00:00 2001 From: noppoman Date: Wed, 31 May 2017 13:20:46 +0900 Subject: [PATCH] validate authorize url --- Sources/HexavilleAuth/HexaviileAuth.swift | 4 ++-- Sources/HexavilleAuth/OAuth/OAuth2.swift | 14 ++++++++++---- .../Providers/OAuth2AuthentitionProvidable.swift | 4 ++-- Sources/HexavilleAuthExample/main.swift | 4 ++++ 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Sources/HexavilleAuth/HexaviileAuth.swift b/Sources/HexavilleAuth/HexaviileAuth.swift index 5fcedaa..311de75 100644 --- a/Sources/HexavilleAuth/HexaviileAuth.swift +++ b/Sources/HexavilleAuth/HexaviileAuth.swift @@ -20,13 +20,13 @@ public struct HexavilleAuth: Middleware { public func respond(to request: Request, context: ApplicationContext) throws -> Chainer { let currentPath = request.path ?? "/" - + for provider in providers { if provider.path == currentPath { let response = Response( status: .found, headers: [ - "Location": provider.createAuthorizeURL().absoluteString + "Location": try provider.createAuthorizeURL().absoluteString ] ) return .respond(to: response) diff --git a/Sources/HexavilleAuth/OAuth/OAuth2.swift b/Sources/HexavilleAuth/OAuth/OAuth2.swift index f750517..de50a03 100644 --- a/Sources/HexavilleAuth/OAuth/OAuth2.swift +++ b/Sources/HexavilleAuth/OAuth/OAuth2.swift @@ -9,6 +9,10 @@ import Foundation import HexavilleFramework +public enum OAuth2Error: Error { + case invalidAuthrozeURL(String) +} + public class OAuth2 { let consumerKey: String let consumerSecret: String @@ -32,7 +36,7 @@ public class OAuth2 { return dict.map({ "\($0.key)=\($0.value)" }).joined(separator: "&") } - public func createAuthorizeURL() -> URL { + public func createAuthorizeURL() throws -> URL { let params = [ "client_id": consumerKey, "redirect_uri": callbackURL, @@ -42,7 +46,11 @@ public class OAuth2 { let queryString = dictionary2Query(params) - return URL(string: "\(authorizeURL)?\(queryString)")! + guard let url = URL(string: "\(authorizeURL)?\(queryString)") else { + throw OAuth2Error.invalidAuthrozeURL("\(authorizeURL)?\(queryString)") + } + + return url } public func getAccessToken(request: Request) throws -> Credential { @@ -60,8 +68,6 @@ public class OAuth2 { "redirect_uri=\(self.callbackURL)" ] - print(body) - let client = try HTTPClient(url: url) try client.open() let response = try client.request( diff --git a/Sources/HexavilleAuth/Providers/OAuth2AuthentitionProvidable.swift b/Sources/HexavilleAuth/Providers/OAuth2AuthentitionProvidable.swift index 3c6c92f..f5c1e39 100644 --- a/Sources/HexavilleAuth/Providers/OAuth2AuthentitionProvidable.swift +++ b/Sources/HexavilleAuth/Providers/OAuth2AuthentitionProvidable.swift @@ -20,8 +20,8 @@ public protocol OAuth2AuthentitionProvidable { } extension OAuth2AuthentitionProvidable { - public func createAuthorizeURL() -> URL { - return oauth.createAuthorizeURL() + public func createAuthorizeURL() throws -> URL { + return try oauth.createAuthorizeURL() } public func getAccessToken(request: Request) throws -> Credential { diff --git a/Sources/HexavilleAuthExample/main.swift b/Sources/HexavilleAuthExample/main.swift index 3e1e469..5eb2a4c 100644 --- a/Sources/HexavilleAuthExample/main.swift +++ b/Sources/HexavilleAuthExample/main.swift @@ -65,6 +65,10 @@ app.use(middleware) let router = Router() +router.use(.get, "/") { _ in + return Response(body: "Welcome to Hexaville Auth") +} + app.use(router) app.catch { error in