From 5546169f1a3983bd7331079cb5ed96d40ae2efc1 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Tue, 5 Dec 2017 06:24:21 -0600 Subject: [PATCH 01/50] Created ImperialError.noExistingService error case --- Sources/Imperial/Errors/ImperialError.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sources/Imperial/Errors/ImperialError.swift b/Sources/Imperial/Errors/ImperialError.swift index eea11a5..0f6fda2 100644 --- a/Sources/Imperial/Errors/ImperialError.swift +++ b/Sources/Imperial/Errors/ImperialError.swift @@ -8,6 +8,9 @@ public enum ImperialError: Error, CustomStringConvertible { /// Thrown when no service is registered with a given name. case noServiceFound(String) + /// Thrown when no `FederatedSewrvice` type is found whgen creating a `Service` from JSON. + case noExistingService(String) + /// Thrown when we attempt to create a `FederatedCreatable` model and there is /// no JSON in the response from the the request to `dataUri`. case missingJSONFromResponse(String) @@ -23,6 +26,7 @@ public enum ImperialError: Error, CustomStringConvertible { switch self { case let .missingEnvVar(variable): return "Missing enviroment variable '\(variable)'" case let .noServiceFound(name): return "No service was found with the name '\(name)'" + case let .noExistingService(name): return "No service exists with the name '\(name)'" case let .missingJSONFromResponse(uri): return "Reponse returned from '\(uri)' does not contain JSON" case let .noServiceEndpoint(endpoint): return "Service does not have available endpoint for key '\(endpoint)'" case let .typeNotInitialized(type): return "No instence of type '\(type)' has been created" From 5a9edcde134bc4d119dbb111202318db23f75994 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Tue, 5 Dec 2017 06:25:18 -0600 Subject: [PATCH 02/50] Conformed Service struct to JSONConvertible protocol --- .../Services/Service/Service+JSON.swift | 38 +++++++++++++++++++ .../Services/{ => Service}/Service.swift | 0 2 files changed, 38 insertions(+) create mode 100644 Sources/Imperial/Services/Service/Service+JSON.swift rename Sources/Imperial/Services/{ => Service}/Service.swift (100%) diff --git a/Sources/Imperial/Services/Service/Service+JSON.swift b/Sources/Imperial/Services/Service/Service+JSON.swift new file mode 100644 index 0000000..474a2e1 --- /dev/null +++ b/Sources/Imperial/Services/Service/Service+JSON.swift @@ -0,0 +1,38 @@ +import JSON + +internal fileprivate(set) var federatedServices: [String: FederatedService.Type] = [ + : +] + +extension FederatedService { + public static func registerName() { + let key = String(describing: self) + federatedServices[key] = self + } + + public static var typeName: String { + return String(describing: self) + } +} + +extension Service: JSONConvertible { + public func makeJSON() throws -> JSON { + var json = JSON() + try json.set("name", self.name) + try json.set("token_prefix", self.tokenPrefix) + try json.set("endpoints", self.endpoints) + try json.set("model", federatedServices[self.model.typeName]) + return json + } + + public init(json: JSON) throws { + let key: String = try json.get("model") + + let name: String = try json.get("name") + let tokenPrefix: String = try json.get("token_prefix") + let endpoints: [String: String] = try json.get("endpoints") + let model: FederatedService.Type = try federatedServices[key] ?? ImperialError.noExistingService(key) + + self.init(name: name, prefix: tokenPrefix, model: model, endpoints: endpoints) + } +} diff --git a/Sources/Imperial/Services/Service.swift b/Sources/Imperial/Services/Service/Service.swift similarity index 100% rename from Sources/Imperial/Services/Service.swift rename to Sources/Imperial/Services/Service/Service.swift From 7fd581beb19487d6ea3e22b871eac077b38cf424 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Tue, 5 Dec 2017 06:26:31 -0600 Subject: [PATCH 03/50] Registered Google and GitHub services with Service type --- Sources/Imperial/Provider.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/Imperial/Provider.swift b/Sources/Imperial/Provider.swift index 51ae2ed..e06eb14 100644 --- a/Sources/Imperial/Provider.swift +++ b/Sources/Imperial/Provider.swift @@ -7,6 +7,8 @@ public class Provider: Vapor.Provider { public func boot(_ droplet: Droplet) throws { drop = droplet + Google.registerName() + GitHub.registerName() } public func boot(_ config: Config) throws { From ae58e1fdac6b27adaae9b48435ff2d72e0fb4c7d Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Wed, 6 Dec 2017 13:08:26 -0600 Subject: [PATCH 04/50] Moved service based errors to ServiceError enum --- Sources/Imperial/Errors/ImperialError.swift | 12 +---------- Sources/Imperial/Errors/ServiceError.swift | 20 +++++++++++++++++++ .../Imperial/Helpers/Request+Imperial.swift | 2 +- .../Services/Service/Service+JSON.swift | 2 +- .../Imperial/Services/Service/Service.swift | 2 +- 5 files changed, 24 insertions(+), 14 deletions(-) create mode 100644 Sources/Imperial/Errors/ServiceError.swift diff --git a/Sources/Imperial/Errors/ImperialError.swift b/Sources/Imperial/Errors/ImperialError.swift index 0f6fda2..c7fa4de 100644 --- a/Sources/Imperial/Errors/ImperialError.swift +++ b/Sources/Imperial/Errors/ImperialError.swift @@ -5,18 +5,11 @@ public enum ImperialError: Error, CustomStringConvertible { /// - warning: This error is never thrown; rather, the application will fatal error. case missingEnvVar(String) - /// Thrown when no service is registered with a given name. - case noServiceFound(String) - - /// Thrown when no `FederatedSewrvice` type is found whgen creating a `Service` from JSON. - case noExistingService(String) - /// Thrown when we attempt to create a `FederatedCreatable` model and there is /// no JSON in the response from the the request to `dataUri`. case missingJSONFromResponse(String) - /// Thrown when a `FederatedCreatable` type has a `serviceKey` that does not match any available endpoints in the service. - case noServiceEndpoint(String) + /// Thrown when `request.fetch` is called with a type that has not been run through `request.create`. case typeNotInitialized(String) @@ -25,10 +18,7 @@ public enum ImperialError: Error, CustomStringConvertible { public var description: String { switch self { case let .missingEnvVar(variable): return "Missing enviroment variable '\(variable)'" - case let .noServiceFound(name): return "No service was found with the name '\(name)'" - case let .noExistingService(name): return "No service exists with the name '\(name)'" case let .missingJSONFromResponse(uri): return "Reponse returned from '\(uri)' does not contain JSON" - case let .noServiceEndpoint(endpoint): return "Service does not have available endpoint for key '\(endpoint)'" case let .typeNotInitialized(type): return "No instence of type '\(type)' has been created" } } diff --git a/Sources/Imperial/Errors/ServiceError.swift b/Sources/Imperial/Errors/ServiceError.swift new file mode 100644 index 0000000..dfb6feb --- /dev/null +++ b/Sources/Imperial/Errors/ServiceError.swift @@ -0,0 +1,20 @@ +/// Represents an error that occurs during a service action. +public enum ServiceError: Error, CustomStringConvertible { + + /// Thrown when no service is registered with a given name. + case noServiceFound(String) + + /// Thrown when no `FederatedSewrvice` type is found whgen creating a `Service` from JSON. + case noExistingService(String) + + /// Thrown when a `FederatedCreatable` type has a `serviceKey` that does not match any available endpoints in the service. + case noServiceEndpoint(String) + + public var description: String { + switch self { + case let .noServiceFound(name): return "No service was found with the name '\(name)'" + case let .noExistingService(name): return "No service exists with the name '\(name)'" + case let .noServiceEndpoint(endpoint): return "Service does not have available endpoint for key '\(endpoint)'" + } + } +} diff --git a/Sources/Imperial/Helpers/Request+Imperial.swift b/Sources/Imperial/Helpers/Request+Imperial.swift index 5daa022..e9b0d76 100644 --- a/Sources/Imperial/Helpers/Request+Imperial.swift +++ b/Sources/Imperial/Helpers/Request+Imperial.swift @@ -10,7 +10,7 @@ extension Request { /// - Returns: An instance of the type passed in. /// - Throws: Errors from trying to get the access token from the request. func create(_ model: T.Type, with service: Service)throws -> T { - let uri = try service[model.serviceKey] ?? ImperialError.noServiceEndpoint(model.serviceKey) + let uri = try service[model.serviceKey] ?? ServiceError.noServiceEndpoint(model.serviceKey) let token = try service.tokenPrefix + self.getAccessToken() let noJson = ImperialError.missingJSONFromResponse(uri) diff --git a/Sources/Imperial/Services/Service/Service+JSON.swift b/Sources/Imperial/Services/Service/Service+JSON.swift index 474a2e1..b8ecab2 100644 --- a/Sources/Imperial/Services/Service/Service+JSON.swift +++ b/Sources/Imperial/Services/Service/Service+JSON.swift @@ -31,7 +31,7 @@ extension Service: JSONConvertible { let name: String = try json.get("name") let tokenPrefix: String = try json.get("token_prefix") let endpoints: [String: String] = try json.get("endpoints") - let model: FederatedService.Type = try federatedServices[key] ?? ImperialError.noExistingService(key) + let model: FederatedService.Type = try federatedServices[key] ?? ServiceError.noExistingService(key) self.init(name: name, prefix: tokenPrefix, model: model, endpoints: endpoints) } diff --git a/Sources/Imperial/Services/Service/Service.swift b/Sources/Imperial/Services/Service/Service.swift index 5a970c8..8e6a128 100644 --- a/Sources/Imperial/Services/Service/Service.swift +++ b/Sources/Imperial/Services/Service/Service.swift @@ -50,6 +50,6 @@ public struct Service { /// - Returns: The service that matches the name passed in. /// - Throws: `ImperialError.noServiceFound` if no service is found with the name passed in. public static func get(service name: String)throws -> Service { - return try services[name] ?? ImperialError.noServiceFound(name) + return try services[name] ?? ServiceError.noServiceFound(name) } } From 760d391254f7b1faffd84a6d62eae9a9586a4025 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Wed, 6 Dec 2017 13:40:31 -0600 Subject: [PATCH 05/50] Changed model key value to service name in Service.makeJSON method --- Sources/Imperial/Services/Service/Service+JSON.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Imperial/Services/Service/Service+JSON.swift b/Sources/Imperial/Services/Service/Service+JSON.swift index b8ecab2..eac2e08 100644 --- a/Sources/Imperial/Services/Service/Service+JSON.swift +++ b/Sources/Imperial/Services/Service/Service+JSON.swift @@ -6,7 +6,7 @@ internal fileprivate(set) var federatedServices: [String: FederatedService.Type] extension FederatedService { public static func registerName() { - let key = String(describing: self) + let key = Self.typeName federatedServices[key] = self } @@ -21,7 +21,7 @@ extension Service: JSONConvertible { try json.set("name", self.name) try json.set("token_prefix", self.tokenPrefix) try json.set("endpoints", self.endpoints) - try json.set("model", federatedServices[self.model.typeName]) + try json.set("model", self.model.typeName) return json } From cb778ab29c177c0cac1631981c0267cb83aa89b3 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Wed, 6 Dec 2017 13:53:59 -0600 Subject: [PATCH 06/50] Documented Service+JSON file --- .../Services/Service/Service+JSON.swift | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Sources/Imperial/Services/Service/Service+JSON.swift b/Sources/Imperial/Services/Service/Service+JSON.swift index eac2e08..e40d976 100644 --- a/Sources/Imperial/Services/Service/Service+JSON.swift +++ b/Sources/Imperial/Services/Service/Service+JSON.swift @@ -5,17 +5,27 @@ internal fileprivate(set) var federatedServices: [String: FederatedService.Type] ] extension FederatedService { + + /// Saves the `FederatedService` type + /// so it can be used as the `model` property in a service. public static func registerName() { let key = Self.typeName federatedServices[key] = self } + /// The description of the type as a `String`. public static var typeName: String { return String(describing: self) } } + extension Service: JSONConvertible { + + /// Creates a JSON representation of the `Service`. + /// + /// - Returns: The `Service` in JSON format. + /// - Throws: Errors that occur when setting JSON keys. public func makeJSON() throws -> JSON { var json = JSON() try json.set("name", self.name) @@ -25,6 +35,17 @@ extension Service: JSONConvertible { return json } + /// Creates a `Service` from a JSON object. + /// The JSON must contain the following keys: + /// - model: `String` + /// - name: `String` + /// - token_prefix: `String` + /// - endpoints: JSON object (`[String: String]`) + /// + /// - Parameter json: The JSON to create the `Service` with. + /// - Throws: + /// - `ServiceError.noExistingService`: No service is registered with the key of the `model` parameter. + /// - Errors from getting the values from JSON keys. public init(json: JSON) throws { let key: String = try json.get("model") From 9d5b3de83b5d2823bba0387a17815ffbb574bc8c Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Wed, 27 Dec 2017 17:29:26 -0600 Subject: [PATCH 07/50] Uninstalled FluentProvider package --- Package.resolved | 59 +++++++++++++----------------------------------- Package.swift | 5 ++-- 2 files changed, 18 insertions(+), 46 deletions(-) diff --git a/Package.resolved b/Package.resolved index a31ef76..9bd2b4b 100644 --- a/Package.resolved +++ b/Package.resolved @@ -33,8 +33,8 @@ "repositoryURL": "https://github.com/vapor/console.git", "state": { "branch": null, - "revision": "df9eb9a6afd03851abcb3d8204d04c368729776e", - "version": "2.3.0" + "revision": "78d27da2838aa2fb0354037e71a589583d166b41", + "version": "2.3.1" } }, { @@ -51,8 +51,8 @@ "repositoryURL": "https://github.com/vapor/crypto.git", "state": { "branch": null, - "revision": "bf4470b9da79024aab79c85de80374f6c29e3864", - "version": "2.1.1" + "revision": "946edc6642d6825982a2f52a268a8ba9bd520a3d", + "version": "2.1.2" } }, { @@ -60,8 +60,8 @@ "repositoryURL": "https://github.com/vapor/debugging.git", "state": { "branch": null, - "revision": "49c5e8f0a7cb5456a8f7c72c6cd9f1553e5885a8", - "version": "1.1.0" + "revision": "fc5a27d6eb236141dc24e5f14eedaa2e035ae7b3", + "version": "1.1.1" } }, { @@ -69,26 +69,8 @@ "repositoryURL": "https://github.com/vapor/engine.git", "state": { "branch": null, - "revision": "decf702d774ac630dfe0441ff76b4bb68257b77a", - "version": "2.2.1" - } - }, - { - "package": "Fluent", - "repositoryURL": "https://github.com/vapor/fluent.git", - "state": { - "branch": null, - "revision": "2c66e5c6c99dac19554e1cb8957e6de256009efc", - "version": "2.4.2" - } - }, - { - "package": "FluentProvider", - "repositoryURL": "https://github.com/vapor/fluent-provider.git", - "state": { - "branch": null, - "revision": "425d973f7cad7bfa987409b9e4a8659c14c6f0e0", - "version": "1.3.0" + "revision": "c10bff75382f8e1f9476e4a9ebc470e18842c844", + "version": "2.2.2" } }, { @@ -96,8 +78,8 @@ "repositoryURL": "https://github.com/vapor/json.git", "state": { "branch": null, - "revision": "735800d8f2e75ebe3be25559eb6a781f4666dcfc", - "version": "2.2.1" + "revision": "6c7ce40d30b322d45cc49a8f1d742609d3228eb7", + "version": "2.2.2" } }, { @@ -114,8 +96,8 @@ "repositoryURL": "https://github.com/vapor/node.git", "state": { "branch": null, - "revision": "642f357d08ec5aa335ae2e3c4633c72da7b5a0c4", - "version": "2.1.1" + "revision": "e8e21dfe88aa14be905d6e98d5333f666c2e1a78", + "version": "2.1.3" } }, { @@ -132,17 +114,8 @@ "repositoryURL": "https://github.com/vapor/routing.git", "state": { "branch": null, - "revision": "cb9d78aca2540c1a6b45b0ab43e5b0c50f29d216", - "version": "2.2.0" - } - }, - { - "package": "SQLite", - "repositoryURL": "https://github.com/vapor/sqlite.git", - "state": { - "branch": null, - "revision": "9aceb6a0d7b1698a557647493bd78b030dad468b", - "version": "2.3.1" + "revision": "e950f3d56fb2eab6a64dafcfdb89ac98866045b8", + "version": "2.2.1" } }, { @@ -159,8 +132,8 @@ "repositoryURL": "https://github.com/vapor/tls.git", "state": { "branch": null, - "revision": "6c6eedb6761cddc6b6c87142a27eec13fa1701ec", - "version": "2.1.1" + "revision": "02a47309249e69358aa3c28b5853897585d7a750", + "version": "2.1.2" } }, { diff --git a/Package.swift b/Package.swift index 3f9e587..c01c8ff 100644 --- a/Package.swift +++ b/Package.swift @@ -8,11 +8,10 @@ let package = Package( .library(name: "Imperial", targets: ["Imperial"]), ], dependencies: [ - .package(url: "https://github.com/vapor/vapor.git", .exact("2.4.0")), - .package(url: "https://github.com/vapor/fluent-provider.git", .exact("1.3.0")) + .package(url: "https://github.com/vapor/vapor.git", .exact("2.4.0")) ], targets: [ - .target(name: "Imperial", dependencies: ["Vapor", "FluentProvider"]), + .target(name: "Imperial", dependencies: ["Vapor"]), .testTarget(name: "ImperialTests", dependencies: ["Imperial"]), ] ) From 9285f17ee1e70392d6a3d1df0f238cfae0e6ab53 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Wed, 27 Dec 2017 17:31:40 -0600 Subject: [PATCH 08/50] Updated Vapor version to 3.0.0-beta --- Package.resolved | 132 +++++++++++------------------------------------ Package.swift | 2 +- 2 files changed, 31 insertions(+), 103 deletions(-) diff --git a/Package.resolved b/Package.resolved index 9bd2b4b..aeef163 100644 --- a/Package.resolved +++ b/Package.resolved @@ -2,147 +2,75 @@ "object": { "pins": [ { - "package": "BCrypt", - "repositoryURL": "https://github.com/vapor/bcrypt.git", + "package": "Async", + "repositoryURL": "https://github.com/vapor/async.git", "state": { - "branch": null, - "revision": "3ee4aca16ba6ebfb1ad48cc5fd4dfb163c6d6be8", - "version": "1.1.0" - } - }, - { - "package": "Bits", - "repositoryURL": "https://github.com/vapor/bits.git", - "state": { - "branch": null, - "revision": "c32f5e6ae2007dccd21a92b7e33eba842dd80d2f", - "version": "1.1.0" - } - }, - { - "package": "CTLS", - "repositoryURL": "https://github.com/vapor/ctls.git", - "state": { - "branch": null, - "revision": "fddec6a4643d6e85b6bb6dc54b1b5cdbabd395d2", - "version": "1.1.2" + "branch": "beta", + "revision": "e2dec3c2013d5c71e3ebace661344de7d1259b79", + "version": null } }, { "package": "Console", "repositoryURL": "https://github.com/vapor/console.git", "state": { - "branch": null, - "revision": "78d27da2838aa2fb0354037e71a589583d166b41", - "version": "2.3.1" + "branch": "beta", + "revision": "b61cb141669f75ec8d820942cb04637a058d508d", + "version": null } }, { "package": "Core", "repositoryURL": "https://github.com/vapor/core.git", "state": { - "branch": null, - "revision": "f9f3a585ab0ea5764b46d7a36d9c0d9d508b9c63", - "version": "2.2.0" + "branch": "beta", + "revision": "d960863febf2e26e32afd0f10539a4476f3636ef", + "version": null } }, { "package": "Crypto", "repositoryURL": "https://github.com/vapor/crypto.git", "state": { - "branch": null, - "revision": "946edc6642d6825982a2f52a268a8ba9bd520a3d", - "version": "2.1.2" - } - }, - { - "package": "Debugging", - "repositoryURL": "https://github.com/vapor/debugging.git", - "state": { - "branch": null, - "revision": "fc5a27d6eb236141dc24e5f14eedaa2e035ae7b3", - "version": "1.1.1" + "branch": "beta", + "revision": "b31cdb4ecc32b8f529a6027c1945f404c4a41c1e", + "version": null } }, { "package": "Engine", "repositoryURL": "https://github.com/vapor/engine.git", "state": { - "branch": null, - "revision": "c10bff75382f8e1f9476e4a9ebc470e18842c844", - "version": "2.2.2" - } - }, - { - "package": "JSON", - "repositoryURL": "https://github.com/vapor/json.git", - "state": { - "branch": null, - "revision": "6c7ce40d30b322d45cc49a8f1d742609d3228eb7", - "version": "2.2.2" - } - }, - { - "package": "Multipart", - "repositoryURL": "https://github.com/vapor/multipart.git", - "state": { - "branch": null, - "revision": "8e541b2e6fc64a3741eca2aa48ee2c3f23cbe17c", - "version": "2.1.1" - } - }, - { - "package": "Node", - "repositoryURL": "https://github.com/vapor/node.git", - "state": { - "branch": null, - "revision": "e8e21dfe88aa14be905d6e98d5333f666c2e1a78", - "version": "2.1.3" - } - }, - { - "package": "Random", - "repositoryURL": "https://github.com/vapor/random.git", - "state": { - "branch": null, - "revision": "d7c4397d125caba795d14d956efacfe2a27a63d0", - "version": "1.2.0" - } - }, - { - "package": "Routing", - "repositoryURL": "https://github.com/vapor/routing.git", - "state": { - "branch": null, - "revision": "e950f3d56fb2eab6a64dafcfdb89ac98866045b8", - "version": "2.2.1" + "branch": "beta", + "revision": "2009aee5a9641760b37e39a10e5fe7a948afb0c9", + "version": null } }, { - "package": "Sockets", - "repositoryURL": "https://github.com/vapor/sockets.git", + "package": "Leaf", + "repositoryURL": "https://github.com/vapor/leaf.git", "state": { - "branch": null, - "revision": "70d14c0e223257176f5ef69a595f7cad5de7a88b", - "version": "2.2.1" + "branch": "beta", + "revision": "bb995939b257c5f24fdeb6f044708f8217f750b4", + "version": null } }, { - "package": "TLS", - "repositoryURL": "https://github.com/vapor/tls.git", + "package": "Service", + "repositoryURL": "https://github.com/vapor/service.git", "state": { - "branch": null, - "revision": "02a47309249e69358aa3c28b5853897585d7a750", - "version": "2.1.2" + "branch": "beta", + "revision": "bfd6821603f6c0ad42feeefefcaafc1ab59cac0f", + "version": null } }, { "package": "Vapor", "repositoryURL": "https://github.com/vapor/vapor.git", "state": { - "branch": null, - "revision": "63768e7f56e58dbfa4288e16ad2e4003bfd8dcde", - "version": "2.4.0" + "branch": "beta", + "revision": "376b6573cb460dde4a3bc02da63799659967f761", + "version": null } } ] diff --git a/Package.swift b/Package.swift index c01c8ff..e57a4e2 100644 --- a/Package.swift +++ b/Package.swift @@ -8,7 +8,7 @@ let package = Package( .library(name: "Imperial", targets: ["Imperial"]), ], dependencies: [ - .package(url: "https://github.com/vapor/vapor.git", .exact("2.4.0")) + .package(url: "https://github.com/vapor/vapor.git", .branch("beta")) ], targets: [ .target(name: "Imperial", dependencies: ["Vapor"]), From 28cbad71140230f8662caabd7dfd82288edaa351 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Thu, 28 Dec 2017 17:33:54 -0600 Subject: [PATCH 09/50] Replaced use of ResponseRepresentable with Future --- Sources/Imperial/Routing/FederatedServiceRouter.swift | 10 +++++----- Sources/Imperial/Services/FederatedService.swift | 5 +++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Sources/Imperial/Routing/FederatedServiceRouter.swift b/Sources/Imperial/Routing/FederatedServiceRouter.swift index 5986408..0a78a24 100644 --- a/Sources/Imperial/Routing/FederatedServiceRouter.swift +++ b/Sources/Imperial/Routing/FederatedServiceRouter.swift @@ -10,7 +10,7 @@ public protocol FederatedServiceRouter { /// The callback that is fired after the access token is fetched from the OAuth provider. /// The response that is returned from this callback is also returned from the callback route. - var callbackCompletion: (String) -> (ResponseRepresentable) { get } + var callbackCompletion: (String) -> (Future) { get } /// The scopes to get permission for when getting the access token. /// Usage of this property varies by provider. @@ -32,7 +32,7 @@ public protocol FederatedServiceRouter { /// - callback: The callback URL that the OAuth provider will redirect to after authenticating the user. /// - completion: The completion handler that will be fired at the end of the `callback` route. The access token is passed into it. /// - Throws: Any errors that could occur in the implementation. - init(callback: String, completion: @escaping (String) -> (ResponseRepresentable))throws + init(callback: String, completion: @escaping (String) -> (Future))throws /// Configures the `authenticate` and `callback` routes with the droplet. @@ -48,18 +48,18 @@ public protocol FederatedServiceRouter { /// - Parameter request: The request from the browser. /// - Returns: A response that, by default, redirects the user to `authURL`. /// - Throws: N/A - func authenticate(_ request: Request)throws -> ResponseRepresentable + func authenticate(_ request: Request)throws -> Future /// The route that the OAuth provider calls when the user has benn authenticated. /// /// - Parameter request: The request from the OAuth provider. /// - Returns: A response that should redirect the user back to the app. /// - Throws: An errors that occur in the implementation code. - func callback(_ request: Request)throws -> ResponseRepresentable + func callback(_ request: Request)throws -> Future } extension FederatedServiceRouter { - public func authenticate(_ request: Request)throws -> ResponseRepresentable { + public func authenticate(_ request: Request)throws -> Future { return Response(redirect: authURL) } diff --git a/Sources/Imperial/Services/FederatedService.swift b/Sources/Imperial/Services/FederatedService.swift index b77df4e..1c12a83 100644 --- a/Sources/Imperial/Services/FederatedService.swift +++ b/Sources/Imperial/Services/FederatedService.swift @@ -1,4 +1,5 @@ -import HTTP +import Vapor +import Async /** Represents a connection to an OAuth provider to get an access token for authenticating a user. @@ -41,5 +42,5 @@ public protocol FederatedService { /// - scope: The scopes to send to the provider to request access to. /// - completion: The completion handler that will fire at the end of the callback route. The access token is passed into the callback and the response that is returned will be returned from the callback route. This will usually be a redirect back to the app. /// - Throws: Any errors that occur in the implementation. - init(authenticate: String, callback: String, scope: [String], completion: @escaping (String) -> (ResponseRepresentable))throws + init(authenticate: String, callback: String, scope: [String], completion: @escaping (String) -> (Future))throws } From e1adf1300356476092aab23efc73fdf70b86a5ee Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Fri, 29 Dec 2017 07:02:49 -0600 Subject: [PATCH 10/50] Updated ImperialMiddleware for Vapor 3 --- Sources/Imperial/Middleware/ImperialMiddleware.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Imperial/Middleware/ImperialMiddleware.swift b/Sources/Imperial/Middleware/ImperialMiddleware.swift index a5dff3b..a00519d 100644 --- a/Sources/Imperial/Middleware/ImperialMiddleware.swift +++ b/Sources/Imperial/Middleware/ImperialMiddleware.swift @@ -15,7 +15,7 @@ public class ImperialMiddleware: Middleware { /// Checks that the request contains an access token. If it does, let the request through. If not, redirect the user to the `redirectPath`. /// If the `redirectPath` is `nil`, then throw the error from getting the access token (Abort.unauthorized). - public func respond(to request: Request, chainingTo next: Responder) throws -> Response { + public func respond(to request: Request, chainingTo next: Responder) throws -> Future { do { _ = try request.getAccessToken() return try next.respond(to: request) @@ -23,7 +23,7 @@ public class ImperialMiddleware: Middleware { guard let redirect = redirectPath else { throw error } - return Response(redirect: redirect) + return Future(request.redirect(to: redirect)) } catch let error { throw error } From 159dc1a0cf7ea06053078af44bc369c04ec62c52 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Wed, 17 Jan 2018 17:26:03 -0600 Subject: [PATCH 11/50] Updated Package resolve --- Package.resolved | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Package.resolved b/Package.resolved index aeef163..da0d55b 100644 --- a/Package.resolved +++ b/Package.resolved @@ -37,6 +37,15 @@ "version": null } }, + { + "package": "DatabaseKit", + "repositoryURL": "https://github.com/vapor/database-kit.git", + "state": { + "branch": "beta", + "revision": "c1dd27e0dd2fe01b5dd4389e8c0f59d31c9ca1f0", + "version": null + } + }, { "package": "Engine", "repositoryURL": "https://github.com/vapor/engine.git", @@ -64,6 +73,15 @@ "version": null } }, + { + "package": "TemplateKit", + "repositoryURL": "https://github.com/vapor/template-kit.git", + "state": { + "branch": "beta", + "revision": "fe10f22e41865cfeb97d9e747d82d1cc5fb21111", + "version": null + } + }, { "package": "Vapor", "repositoryURL": "https://github.com/vapor/vapor.git", From 5e5dac9735fa50e80c188d75ad3ff4953c0dfece Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Wed, 17 Jan 2018 17:26:06 -0600 Subject: [PATCH 12/50] Updated redirect response for FederatedServiceRouter.authenticate route method --- Sources/Imperial/Routing/FederatedServiceRouter.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Imperial/Routing/FederatedServiceRouter.swift b/Sources/Imperial/Routing/FederatedServiceRouter.swift index 0a78a24..7efc5f8 100644 --- a/Sources/Imperial/Routing/FederatedServiceRouter.swift +++ b/Sources/Imperial/Routing/FederatedServiceRouter.swift @@ -60,7 +60,7 @@ public protocol FederatedServiceRouter { extension FederatedServiceRouter { public func authenticate(_ request: Request)throws -> Future { - return Response(redirect: authURL) + return Future(request.redirect(to: authURL)) } public func configureRoutes(withAuthURL authURL: String) throws { From 75d691ecff0e82ce3a1c21ac255b630365aba2f3 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Thu, 18 Jan 2018 17:05:56 -0600 Subject: [PATCH 13/50] Replaced use of ResponseRepresentable with Future in Google and GitHub routers --- Sources/Imperial/Services/GitHub/GitHubRouter.swift | 7 +++---- Sources/Imperial/Services/Google/GoogleRouter.swift | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Sources/Imperial/Services/GitHub/GitHubRouter.swift b/Sources/Imperial/Services/GitHub/GitHubRouter.swift index 5b3531d..549128e 100644 --- a/Sources/Imperial/Services/GitHub/GitHubRouter.swift +++ b/Sources/Imperial/Services/GitHub/GitHubRouter.swift @@ -1,9 +1,8 @@ import Vapor -import Sessions public class GitHubRouter: FederatedServiceRouter { public let tokens: FederatedServiceTokens - public let callbackCompletion: (String) -> (ResponseRepresentable) + public let callbackCompletion: (String) -> (Future) public var scope: [String] = [] public let callbackURL: String public let accessTokenURL: String = "https://github.com/login/oauth/access_token" @@ -13,13 +12,13 @@ public class GitHubRouter: FederatedServiceRouter { "client_id=\(self.tokens.clientID)" } - public required init(callback: String, completion: @escaping (String) -> (ResponseRepresentable)) throws { + public required init(callback: String, completion: @escaping (String) -> (Future)) throws { self.tokens = try GitHubAuth() self.callbackURL = callback self.callbackCompletion = completion } - public func callback(_ request: Request)throws -> ResponseRepresentable { + public func callback(_ request: Request)throws -> Future { let code: String if let queryCode: String = try request.query?.get("code") { code = queryCode diff --git a/Sources/Imperial/Services/Google/GoogleRouter.swift b/Sources/Imperial/Services/Google/GoogleRouter.swift index 3c72f7d..74409b5 100644 --- a/Sources/Imperial/Services/Google/GoogleRouter.swift +++ b/Sources/Imperial/Services/Google/GoogleRouter.swift @@ -1,10 +1,9 @@ import Vapor import HTTP -import Sessions public class GoogleRouter: FederatedServiceRouter { public let tokens: FederatedServiceTokens - public let callbackCompletion: (String) -> (ResponseRepresentable) + public let callbackCompletion: (String) -> (Future) public var scope: [String] = [] public let callbackURL: String public let accessTokenURL: String = "https://www.googleapis.com/oauth2/v4/token" @@ -16,13 +15,13 @@ public class GoogleRouter: FederatedServiceRouter { "response_type=code" } - public required init(callback: String, completion: @escaping (String) -> (ResponseRepresentable)) throws { + public required init(callback: String, completion: @escaping (String) -> (Future)) throws { self.tokens = try GoogleAuth() self.callbackURL = callback self.callbackCompletion = completion } - public func callback(_ request: Request)throws -> ResponseRepresentable { + public func callback(_ request: Request)throws -> Future { let code: String if let queryCode: String = try request.query?.get("code") { code = queryCode From 224425f4c44a5c89a236d18107e8bcbae619ed38 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Thu, 18 Jan 2018 17:12:07 -0600 Subject: [PATCH 14/50] Replaced use of ResponseRepresentable with Future in Google and GitHub services --- Sources/Imperial/Services/GitHub/GitHub.swift | 4 ++-- Sources/Imperial/Services/Google/Google.swift | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/Imperial/Services/GitHub/GitHub.swift b/Sources/Imperial/Services/GitHub/GitHub.swift index 5303e14..dc513cf 100644 --- a/Sources/Imperial/Services/GitHub/GitHub.swift +++ b/Sources/Imperial/Services/GitHub/GitHub.swift @@ -1,11 +1,11 @@ -import HTTP +import Vapor public class GitHub: FederatedService { public var tokens: FederatedServiceTokens public var router: FederatedServiceRouter @discardableResult - public required init(authenticate: String, callback: String, scope: [String] = [], completion: @escaping (String) -> (ResponseRepresentable)) throws { + public required init(authenticate: String, callback: String, scope: [String] = [], completion: @escaping (String) -> (Future)) throws { self.router = try GitHubRouter(callback: callback, completion: completion) self.tokens = self.router.tokens diff --git a/Sources/Imperial/Services/Google/Google.swift b/Sources/Imperial/Services/Google/Google.swift index b5a3d5f..3cf9778 100644 --- a/Sources/Imperial/Services/Google/Google.swift +++ b/Sources/Imperial/Services/Google/Google.swift @@ -1,11 +1,11 @@ -import HTTP +import Vapor public class Google: FederatedService { public var tokens: FederatedServiceTokens public var router: FederatedServiceRouter @discardableResult - public required init(authenticate: String, callback: String, scope: [String] = [], completion: @escaping (String) -> (ResponseRepresentable)) throws { + public required init(authenticate: String, callback: String, scope: [String] = [], completion: @escaping (String) -> (Future)) throws { self.router = try GoogleRouter(callback: callback, completion: completion) self.tokens = self.router.tokens From 8d33cfe590b865422caf2be4412da9085515cb0f Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Thu, 18 Jan 2018 17:13:33 -0600 Subject: [PATCH 15/50] Removed bad imports --- .../Imperial/Helpers/Sessions+Imperial.swift | 1 - .../Imperial/Model/FederatedCreatable.swift | 2 - Sources/Imperial/Provider.swift | 62 +++++++++++-------- .../Routing/FederatedServiceRouter.swift | 1 - .../Services/Service/Service+JSON.swift | 2 - 5 files changed, 37 insertions(+), 31 deletions(-) diff --git a/Sources/Imperial/Helpers/Sessions+Imperial.swift b/Sources/Imperial/Helpers/Sessions+Imperial.swift index 5078fdc..bdeb13b 100644 --- a/Sources/Imperial/Helpers/Sessions+Imperial.swift +++ b/Sources/Imperial/Helpers/Sessions+Imperial.swift @@ -1,5 +1,4 @@ import Vapor -import Sessions extension Request { diff --git a/Sources/Imperial/Model/FederatedCreatable.swift b/Sources/Imperial/Model/FederatedCreatable.swift index 1279c57..b47753c 100644 --- a/Sources/Imperial/Model/FederatedCreatable.swift +++ b/Sources/Imperial/Model/FederatedCreatable.swift @@ -1,5 +1,3 @@ -import JSON - /// Defines a type that can be created with federated login data. /// This type is used as a parameter in the `request.fetch` method public protocol FederatedCreatable { diff --git a/Sources/Imperial/Provider.swift b/Sources/Imperial/Provider.swift index e06eb14..25d82bd 100644 --- a/Sources/Imperial/Provider.swift +++ b/Sources/Imperial/Provider.swift @@ -1,36 +1,48 @@ import Vapor -internal fileprivate(set) var drop: Droplet! - public class Provider: Vapor.Provider { - public static var repositoryName: String = "Imperial" - public func boot(_ droplet: Droplet) throws { - drop = droplet - Google.registerName() - GitHub.registerName() - } + public static var repositoryName: String = "Imperial" - public func boot(_ config: Config) throws { - guard let imperial = config["imperial"]?.object else { - return - } - - if let ghID = imperial["github_client_id"]?.string, - let ghSecret = imperial["github_client_secret"]?.string { - ImperialConfig.gitHubID = ghID - ImperialConfig.gitHubSecret = ghSecret - } - - if let googleID = imperial["google_client_id"]?.string, - let googleSecret = imperial["google_client_secret"]?.string { - ImperialConfig.googleID = googleID - ImperialConfig.googleSecret = googleSecret + /// Register all services provided by the provider here. + public func register(_ services: inout Services) throws { + services.register(isSingleton: true) { (container) in + return ServiceStorage() } } - public func beforeRun(_ droplet: Droplet) throws {} - public required init(config: Config) throws {} + /// Called after the container has initialized. + public func boot(_ worker: Container) throws { + + } +// public static var repositoryName: String = "Imperial" +// +// public func boot(_ droplet: Droplet) throws { +// drop = droplet +// Google.registerName() +// GitHub.registerName() +// } +// +// public func boot(_ config: Config) throws { +// guard let imperial = config["imperial"]?.object else { +// return +// } +// +// if let ghID = imperial["github_client_id"]?.string, +// let ghSecret = imperial["github_client_secret"]?.string { +// ImperialConfig.gitHubID = ghID +// ImperialConfig.gitHubSecret = ghSecret +// } +// +// if let googleID = imperial["google_client_id"]?.string, +// let googleSecret = imperial["google_client_secret"]?.string { +// ImperialConfig.googleID = googleID +// ImperialConfig.googleSecret = googleSecret +// } +// } +// +// public func beforeRun(_ droplet: Droplet) throws {} +// public required init(config: Config) throws {} } internal struct ImperialConfig { diff --git a/Sources/Imperial/Routing/FederatedServiceRouter.swift b/Sources/Imperial/Routing/FederatedServiceRouter.swift index 7efc5f8..9ae9f81 100644 --- a/Sources/Imperial/Routing/FederatedServiceRouter.swift +++ b/Sources/Imperial/Routing/FederatedServiceRouter.swift @@ -1,5 +1,4 @@ import Vapor -import URI /// Defines a type that implements the routing to get an access token from an OAuth provider. /// See implementations in the `Services/(Google|GitHub)/$0Router.swift` files diff --git a/Sources/Imperial/Services/Service/Service+JSON.swift b/Sources/Imperial/Services/Service/Service+JSON.swift index e40d976..cbf54df 100644 --- a/Sources/Imperial/Services/Service/Service+JSON.swift +++ b/Sources/Imperial/Services/Service/Service+JSON.swift @@ -1,5 +1,3 @@ -import JSON - internal fileprivate(set) var federatedServices: [String: FederatedService.Type] = [ : ] From 8930f95922ac81ef0fc5ad8f0f2fc54f2d08b4d9 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Thu, 18 Jan 2018 17:23:51 -0600 Subject: [PATCH 16/50] Updated provider for Vapor 3 --- Sources/Imperial/Provider.swift | 31 ++----------------------------- 1 file changed, 2 insertions(+), 29 deletions(-) diff --git a/Sources/Imperial/Provider.swift b/Sources/Imperial/Provider.swift index 25d82bd..cf01200 100644 --- a/Sources/Imperial/Provider.swift +++ b/Sources/Imperial/Provider.swift @@ -13,36 +13,9 @@ public class Provider: Vapor.Provider { /// Called after the container has initialized. public func boot(_ worker: Container) throws { - + Google.registerName() + GitHub.registerName() } -// public static var repositoryName: String = "Imperial" -// -// public func boot(_ droplet: Droplet) throws { -// drop = droplet -// Google.registerName() -// GitHub.registerName() -// } -// -// public func boot(_ config: Config) throws { -// guard let imperial = config["imperial"]?.object else { -// return -// } -// -// if let ghID = imperial["github_client_id"]?.string, -// let ghSecret = imperial["github_client_secret"]?.string { -// ImperialConfig.gitHubID = ghID -// ImperialConfig.gitHubSecret = ghSecret -// } -// -// if let googleID = imperial["google_client_id"]?.string, -// let googleSecret = imperial["google_client_secret"]?.string { -// ImperialConfig.googleID = googleID -// ImperialConfig.googleSecret = googleSecret -// } -// } -// -// public func beforeRun(_ droplet: Droplet) throws {} -// public required init(config: Config) throws {} } internal struct ImperialConfig { From 3364185a7c74908914cb49ae61056deb8020554f Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Thu, 18 Jan 2018 17:25:05 -0600 Subject: [PATCH 17/50] Renamed Service to ImperialService to prevent type conflicts --- Sources/Imperial/Helpers/Request+Imperial.swift | 2 +- Sources/Imperial/Model/FederatedCreatable.swift | 2 +- Sources/Imperial/Services/GitHub/GitHub.swift | 2 +- Sources/Imperial/Services/GitHub/GitHubRouter.swift | 2 +- Sources/Imperial/Services/GitHub/Service+GitHub.swift | 4 ++-- Sources/Imperial/Services/Google/Google.swift | 2 +- Sources/Imperial/Services/Google/GoogleRouter.swift | 2 +- Sources/Imperial/Services/Google/Service+Google.swift | 4 ++-- .../Service/{Service.swift => ImperialService.swift} | 8 ++++---- Sources/Imperial/Services/Service/Service+JSON.swift | 2 +- 10 files changed, 15 insertions(+), 15 deletions(-) rename Sources/Imperial/Services/Service/{Service.swift => ImperialService.swift} (90%) diff --git a/Sources/Imperial/Helpers/Request+Imperial.swift b/Sources/Imperial/Helpers/Request+Imperial.swift index e9b0d76..2a6813c 100644 --- a/Sources/Imperial/Helpers/Request+Imperial.swift +++ b/Sources/Imperial/Helpers/Request+Imperial.swift @@ -9,7 +9,7 @@ extension Request { /// - service: The service to get the data from. /// - Returns: An instance of the type passed in. /// - Throws: Errors from trying to get the access token from the request. - func create(_ model: T.Type, with service: Service)throws -> T { + func create(_ model: T.Type, with service: ImperialService)throws -> T { let uri = try service[model.serviceKey] ?? ServiceError.noServiceEndpoint(model.serviceKey) let token = try service.tokenPrefix + self.getAccessToken() diff --git a/Sources/Imperial/Model/FederatedCreatable.swift b/Sources/Imperial/Model/FederatedCreatable.swift index b47753c..6fc899a 100644 --- a/Sources/Imperial/Model/FederatedCreatable.swift +++ b/Sources/Imperial/Model/FederatedCreatable.swift @@ -10,5 +10,5 @@ public protocol FederatedCreatable { /// - Parameter json: The JSON in the response from the `dataUri`. /// - Returns: An instence of the type that conforms to this protocol. /// - Throws: Any errors that could be thrown inside the method. - static func create(with json: JSON, `for` service: Service)throws -> Self + static func create(with json: JSON, `for` service: ImperialService)throws -> Self } diff --git a/Sources/Imperial/Services/GitHub/GitHub.swift b/Sources/Imperial/Services/GitHub/GitHub.swift index dc513cf..89d5c2c 100644 --- a/Sources/Imperial/Services/GitHub/GitHub.swift +++ b/Sources/Imperial/Services/GitHub/GitHub.swift @@ -12,6 +12,6 @@ public class GitHub: FederatedService { self.router.scope = scope try self.router.configureRoutes(withAuthURL: authenticate) - Service.register(.github) + ImperialService.register(.github) } } diff --git a/Sources/Imperial/Services/GitHub/GitHubRouter.swift b/Sources/Imperial/Services/GitHub/GitHubRouter.swift index 549128e..3ccc7d0 100644 --- a/Sources/Imperial/Services/GitHub/GitHubRouter.swift +++ b/Sources/Imperial/Services/GitHub/GitHubRouter.swift @@ -47,7 +47,7 @@ public class GitHubRouter: FederatedServiceRouter { let session = try request.assertSession() try session.data.set("access_token", accessToken) - try session.data.set("access_token_service", Service.github) + try session.data.set("access_token_service", ImperialService.github) return callbackCompletion(accessToken) } diff --git a/Sources/Imperial/Services/GitHub/Service+GitHub.swift b/Sources/Imperial/Services/GitHub/Service+GitHub.swift index 37d03e6..9b28c64 100644 --- a/Sources/Imperial/Services/GitHub/Service+GitHub.swift +++ b/Sources/Imperial/Services/GitHub/Service+GitHub.swift @@ -1,5 +1,5 @@ -extension Service { - public static let github = Service.init( +extension ImperialService { + public static let github = ImperialService.init( name: "github", model: GitHub.self, endpoints: [ diff --git a/Sources/Imperial/Services/Google/Google.swift b/Sources/Imperial/Services/Google/Google.swift index 3cf9778..752f846 100644 --- a/Sources/Imperial/Services/Google/Google.swift +++ b/Sources/Imperial/Services/Google/Google.swift @@ -12,6 +12,6 @@ public class Google: FederatedService { self.router.scope = scope try self.router.configureRoutes(withAuthURL: authenticate) - Service.register(.google) + ImperialService.register(.google) } } diff --git a/Sources/Imperial/Services/Google/GoogleRouter.swift b/Sources/Imperial/Services/Google/GoogleRouter.swift index 74409b5..1e84f2c 100644 --- a/Sources/Imperial/Services/Google/GoogleRouter.swift +++ b/Sources/Imperial/Services/Google/GoogleRouter.swift @@ -48,7 +48,7 @@ public class GoogleRouter: FederatedServiceRouter { let session = try request.assertSession() try session.data.set("access_token", accessToken) - try session.data.set("access_token_service", Service.google) + try session.data.set("access_token_service", ImperialService.google) return callbackCompletion(accessToken) } diff --git a/Sources/Imperial/Services/Google/Service+Google.swift b/Sources/Imperial/Services/Google/Service+Google.swift index f468b4a..1074d14 100644 --- a/Sources/Imperial/Services/Google/Service+Google.swift +++ b/Sources/Imperial/Services/Google/Service+Google.swift @@ -1,5 +1,5 @@ -extension Service { - public static let google = Service.init( +extension ImperialService { + public static let google = ImperialService.init( name: "google", model: Google.self, endpoints: [:] diff --git a/Sources/Imperial/Services/Service/Service.swift b/Sources/Imperial/Services/Service/ImperialService.swift similarity index 90% rename from Sources/Imperial/Services/Service/Service.swift rename to Sources/Imperial/Services/Service/ImperialService.swift index 8e6a128..4ad0548 100644 --- a/Sources/Imperial/Services/Service/Service.swift +++ b/Sources/Imperial/Services/Service/ImperialService.swift @@ -1,9 +1,9 @@ /// The services that are available for use in the application. /// Services are added and fecthed with the `Service.register` and `.get` static methods. -fileprivate var services: [String: Service] = [:] +fileprivate var services: [String: ImperialService] = [:] /// Represents a service that interacts with an OAuth provider. -public struct Service { +public struct ImperialService { /// The name of the service, i.e. "google", "github", etc. public let name: String @@ -40,7 +40,7 @@ public struct Service { /// Registers a service as available for use. /// /// - Parameter service: The service to register. - internal static func register(_ service: Service) { + internal static func register(_ service: ImperialService) { services[service.name] = service } @@ -49,7 +49,7 @@ public struct Service { /// - Parameter name: The name of the service to fetch. /// - Returns: The service that matches the name passed in. /// - Throws: `ImperialError.noServiceFound` if no service is found with the name passed in. - public static func get(service name: String)throws -> Service { + public static func get(service name: String)throws -> ImperialService { return try services[name] ?? ServiceError.noServiceFound(name) } } diff --git a/Sources/Imperial/Services/Service/Service+JSON.swift b/Sources/Imperial/Services/Service/Service+JSON.swift index cbf54df..5010b7c 100644 --- a/Sources/Imperial/Services/Service/Service+JSON.swift +++ b/Sources/Imperial/Services/Service/Service+JSON.swift @@ -18,7 +18,7 @@ extension FederatedService { } -extension Service: JSONConvertible { +extension ImperialService: JSONConvertible { /// Creates a JSON representation of the `Service`. /// From f80ed692255572d3df356499849fbb8e9a40d276 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Thu, 18 Jan 2018 17:25:37 -0600 Subject: [PATCH 18/50] Removed ImperialConfig struct --- Sources/Imperial/Provider.swift | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Sources/Imperial/Provider.swift b/Sources/Imperial/Provider.swift index cf01200..47935b1 100644 --- a/Sources/Imperial/Provider.swift +++ b/Sources/Imperial/Provider.swift @@ -17,10 +17,3 @@ public class Provider: Vapor.Provider { GitHub.registerName() } } - -internal struct ImperialConfig { - internal fileprivate(set) static var gitHubID: String? - internal fileprivate(set) static var gitHubSecret: String? - internal fileprivate(set) static var googleID: String? - internal fileprivate(set) static var googleSecret: String? -} From 5e0304fdd115a07f9808c65b86f0fe6531c84a39 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Thu, 18 Jan 2018 17:31:59 -0600 Subject: [PATCH 19/50] Updated packages --- Package.resolved | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/Package.resolved b/Package.resolved index da0d55b..145d36f 100644 --- a/Package.resolved +++ b/Package.resolved @@ -6,7 +6,7 @@ "repositoryURL": "https://github.com/vapor/async.git", "state": { "branch": "beta", - "revision": "e2dec3c2013d5c71e3ebace661344de7d1259b79", + "revision": "2cb57234c8fc75c02e718d0c4eb48b17006c046e", "version": null } }, @@ -15,7 +15,7 @@ "repositoryURL": "https://github.com/vapor/console.git", "state": { "branch": "beta", - "revision": "b61cb141669f75ec8d820942cb04637a058d508d", + "revision": "2e7266b7c8c08839ea548223579f7ece74905331", "version": null } }, @@ -24,7 +24,7 @@ "repositoryURL": "https://github.com/vapor/core.git", "state": { "branch": "beta", - "revision": "d960863febf2e26e32afd0f10539a4476f3636ef", + "revision": "a848fb34323fa194ba9255df6899dcf9291b00ff", "version": null } }, @@ -42,7 +42,7 @@ "repositoryURL": "https://github.com/vapor/database-kit.git", "state": { "branch": "beta", - "revision": "c1dd27e0dd2fe01b5dd4389e8c0f59d31c9ca1f0", + "revision": "0c8db254b0a06b3c819c5a637a317817febae39f", "version": null } }, @@ -51,16 +51,7 @@ "repositoryURL": "https://github.com/vapor/engine.git", "state": { "branch": "beta", - "revision": "2009aee5a9641760b37e39a10e5fe7a948afb0c9", - "version": null - } - }, - { - "package": "Leaf", - "repositoryURL": "https://github.com/vapor/leaf.git", - "state": { - "branch": "beta", - "revision": "bb995939b257c5f24fdeb6f044708f8217f750b4", + "revision": "e755c415fa5f8570d4b806c7abdf16541f28213f", "version": null } }, @@ -69,7 +60,7 @@ "repositoryURL": "https://github.com/vapor/service.git", "state": { "branch": "beta", - "revision": "bfd6821603f6c0ad42feeefefcaafc1ab59cac0f", + "revision": "ae18f072da312710506802e557dc552d3c68bd34", "version": null } }, @@ -87,7 +78,7 @@ "repositoryURL": "https://github.com/vapor/vapor.git", "state": { "branch": "beta", - "revision": "376b6573cb460dde4a3bc02da63799659967f761", + "revision": "a677a99f27d2f86258c276c77fd8450af584bea7", "version": null } } From 8ecf0dc3f258e813b4250fb6dcfa62b5ec1559aa Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Thu, 18 Jan 2018 17:33:25 -0600 Subject: [PATCH 20/50] Created ServiceStorage service for storing FederatedCreatable models --- .../Services/Service/ServiceStorage.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Sources/Imperial/Services/Service/ServiceStorage.swift diff --git a/Sources/Imperial/Services/Service/ServiceStorage.swift b/Sources/Imperial/Services/Service/ServiceStorage.swift new file mode 100644 index 0000000..8b735e2 --- /dev/null +++ b/Sources/Imperial/Services/Service/ServiceStorage.swift @@ -0,0 +1,16 @@ +import Service + +final class ServiceStorage: Service { + private(set) var model: FederatedCreatable! + + init() {} + + subscript (model: FederatedCreatable) -> FederatedCreatable { + get { + return self.model + } + set { + self.model = model + } + } +} From fd3d87627959383c82c766af672c41e8bd2b1924 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Fri, 19 Jan 2018 16:05:51 -0600 Subject: [PATCH 21/50] Replaced drop.get in Request.create with Request.get extension method --- Sources/Imperial/Helpers/Request+Imperial.swift | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Sources/Imperial/Helpers/Request+Imperial.swift b/Sources/Imperial/Helpers/Request+Imperial.swift index 2a6813c..639465c 100644 --- a/Sources/Imperial/Helpers/Request+Imperial.swift +++ b/Sources/Imperial/Helpers/Request+Imperial.swift @@ -2,6 +2,15 @@ import Vapor extension Request { + func get(url: String, headers: HTTPHeaders.Literal = [:], body: HTTPBody = HTTPBody(), mediaType: MediaType? = nil)throws -> Future { + let client = try self.make(HTTPClient.self) + var header: HTTPHeaders = HTTPHeaders() + header.append(headers) + var request = HTTPRequest(method: .get, uri: URI(url), headers: header, body: body) + request.mediaType = .urlEncodedForm + return client.send(request) + } + /// Creates an instance of a `FederatedCreatable` type from JSON fetched from an OAuth provider's API. /// /// - Parameters: @@ -15,7 +24,7 @@ extension Request { let token = try service.tokenPrefix + self.getAccessToken() let noJson = ImperialError.missingJSONFromResponse(uri) - let response = try drop.client.get(uri, [.authorization: token]) + let response = try self.get(url: uri, headers: [.authorization: token]) let new = try model.create(with: response.json ?? noJson, for: service) self.storage["imperial-\(model)"] = new @@ -30,7 +39,8 @@ extension Request { /// - Throws: /// - `ImperialError.typeNotInitialized`: If there is no value stored in the request for the type passed in. func fetch(_ model: T.Type)throws -> T { - if let new = self.storage["imperial-\(model)"] { + let cache = try self.privateContainer.make(ServiceStorage.self, for: Request.self) + if let new = cache["imperial-\(model)"] { return new as! T } throw ImperialError.typeNotInitialized("\(model)") From 6e6c0793de45a28b0dce63204d8fc26b9a3946e6 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Fri, 19 Jan 2018 16:50:20 -0600 Subject: [PATCH 22/50] Updated packages --- Package.resolved | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Package.resolved b/Package.resolved index 145d36f..262b33d 100644 --- a/Package.resolved +++ b/Package.resolved @@ -6,7 +6,7 @@ "repositoryURL": "https://github.com/vapor/async.git", "state": { "branch": "beta", - "revision": "2cb57234c8fc75c02e718d0c4eb48b17006c046e", + "revision": "fa7aa97c0476fe6da859e5fea35b3f027fcfc4ff", "version": null } }, @@ -42,7 +42,7 @@ "repositoryURL": "https://github.com/vapor/database-kit.git", "state": { "branch": "beta", - "revision": "0c8db254b0a06b3c819c5a637a317817febae39f", + "revision": "19af4c3911167a6e0ccf465003b52dea29717429", "version": null } }, @@ -51,7 +51,7 @@ "repositoryURL": "https://github.com/vapor/engine.git", "state": { "branch": "beta", - "revision": "e755c415fa5f8570d4b806c7abdf16541f28213f", + "revision": "dce3bbcd3bf9cca676bcbf41a849bc392ea50076", "version": null } }, @@ -69,7 +69,7 @@ "repositoryURL": "https://github.com/vapor/template-kit.git", "state": { "branch": "beta", - "revision": "fe10f22e41865cfeb97d9e747d82d1cc5fb21111", + "revision": "56990033c17006e7d1ae9c78abc3da1654d79fe6", "version": null } }, @@ -78,7 +78,7 @@ "repositoryURL": "https://github.com/vapor/vapor.git", "state": { "branch": "beta", - "revision": "a677a99f27d2f86258c276c77fd8450af584bea7", + "revision": "24e66402cd89ab039ff95f48dec87406e7865395", "version": null } } From 4cc109d53c7fef17838f0d748ba2a89b4f5d30f3 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Fri, 19 Jan 2018 16:51:07 -0600 Subject: [PATCH 23/50] Changed FederatedCreatable to use a Response to create a model instance --- Sources/Imperial/Model/FederatedCreatable.swift | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Sources/Imperial/Model/FederatedCreatable.swift b/Sources/Imperial/Model/FederatedCreatable.swift index 6fc899a..33e31e3 100644 --- a/Sources/Imperial/Model/FederatedCreatable.swift +++ b/Sources/Imperial/Model/FederatedCreatable.swift @@ -1,14 +1,22 @@ +import Vapor + /// Defines a type that can be created with federated login data. /// This type is used as a parameter in the `request.fetch` method -public protocol FederatedCreatable { +public protocol FederatedCreatable: Codable { /// The key for the service's endpoint to use when `request.create` is called with the implimenting type. static var serviceKey: String { get } /// Creates an instance of the model with JSON. /// - /// - Parameter json: The JSON in the response from the `dataUri`. + /// - Parameter response: The JSON in the response from the `dataUri`. /// - Returns: An instence of the type that conforms to this protocol. /// - Throws: Any errors that could be thrown inside the method. - static func create(with json: JSON, `for` service: ImperialService)throws -> Self + static func create(from response: Response)throws -> Future +} + +extension FederatedCreatable { + static func create(from response: Response)throws -> Future { + return try response.content.decode(Self.self) + } } From d35001cc32f0fe8b9d96fb62ff6300123ac0ce6d Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Fri, 19 Jan 2018 16:51:43 -0600 Subject: [PATCH 24/50] Changed Request.get to return type Future --- Sources/Imperial/Helpers/Request+Imperial.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/Imperial/Helpers/Request+Imperial.swift b/Sources/Imperial/Helpers/Request+Imperial.swift index 639465c..0e402d2 100644 --- a/Sources/Imperial/Helpers/Request+Imperial.swift +++ b/Sources/Imperial/Helpers/Request+Imperial.swift @@ -2,13 +2,15 @@ import Vapor extension Request { - func get(url: String, headers: HTTPHeaders.Literal = [:], body: HTTPBody = HTTPBody(), mediaType: MediaType? = nil)throws -> Future { + func get(url: String, headers: HTTPHeaders.Literal = [:], body: HTTPBody = HTTPBody(), mediaType: MediaType? = nil)throws -> Future { let client = try self.make(HTTPClient.self) var header: HTTPHeaders = HTTPHeaders() header.append(headers) var request = HTTPRequest(method: .get, uri: URI(url), headers: header, body: body) request.mediaType = .urlEncodedForm - return client.send(request) + return client.send(request).map(to: Response.self, { (res) in + return Response(http: res, using: self.superContainer) + }) } /// Creates an instance of a `FederatedCreatable` type from JSON fetched from an OAuth provider's API. From 952ed83389d901f339a64ffa46d3eb1c6c453326 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Fri, 19 Jan 2018 16:57:24 -0600 Subject: [PATCH 25/50] Updated Request.create's use of futures and sessions --- Sources/Imperial/Helpers/Request+Imperial.swift | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Sources/Imperial/Helpers/Request+Imperial.swift b/Sources/Imperial/Helpers/Request+Imperial.swift index 0e402d2..aa4d1b8 100644 --- a/Sources/Imperial/Helpers/Request+Imperial.swift +++ b/Sources/Imperial/Helpers/Request+Imperial.swift @@ -20,17 +20,18 @@ extension Request { /// - service: The service to get the data from. /// - Returns: An instance of the type passed in. /// - Throws: Errors from trying to get the access token from the request. - func create(_ model: T.Type, with service: ImperialService)throws -> T { + func create(_ model: T.Type, with service: ImperialService)throws -> Future { let uri = try service[model.serviceKey] ?? ServiceError.noServiceEndpoint(model.serviceKey) let token = try service.tokenPrefix + self.getAccessToken() - let noJson = ImperialError.missingJSONFromResponse(uri) - let response = try self.get(url: uri, headers: [.authorization: token]) - let new = try model.create(with: response.json ?? noJson, for: service) - - self.storage["imperial-\(model)"] = new - return new + return try self.get(url: uri, headers: [.authorization: token]).flatMap(to: T.self, { (response) -> Future in + return try model.create(from: response) + }).map(to: T.self, { (instance) -> T in + let session = try self.session() + session.data.storage["imperial-\(model)"] = instance + return instance + }) } /// Gets an instance of a `FederatedCreatable` type that is stored in the request. From 21cc2ffd4640508187d2e8dd9809463fb8f6e885 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Fri, 19 Jan 2018 16:59:53 -0600 Subject: [PATCH 26/50] Replaced custom cache with sessions in Request.fetch extension method --- Sources/Imperial/Helpers/Request+Imperial.swift | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Sources/Imperial/Helpers/Request+Imperial.swift b/Sources/Imperial/Helpers/Request+Imperial.swift index aa4d1b8..a7c0474 100644 --- a/Sources/Imperial/Helpers/Request+Imperial.swift +++ b/Sources/Imperial/Helpers/Request+Imperial.swift @@ -42,10 +42,11 @@ extension Request { /// - Throws: /// - `ImperialError.typeNotInitialized`: If there is no value stored in the request for the type passed in. func fetch(_ model: T.Type)throws -> T { - let cache = try self.privateContainer.make(ServiceStorage.self, for: Request.self) - if let new = cache["imperial-\(model)"] { - return new as! T + let session = try self.session() + guard let fetched = session.data.storage["imperial-\(model)"], + let typed = fetched as? T else { + throw ImperialError.typeNotInitialized("\(model)") } - throw ImperialError.typeNotInitialized("\(model)") + return typed } } From b25b87c9b9e90cbb28621cd8a4b0bd07bd775116 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Fri, 19 Jan 2018 17:03:01 -0600 Subject: [PATCH 27/50] Removed Node syntax from Session.getAccessToken --- Sources/Imperial/Helpers/Sessions+Imperial.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Imperial/Helpers/Sessions+Imperial.swift b/Sources/Imperial/Helpers/Sessions+Imperial.swift index bdeb13b..7673b5d 100644 --- a/Sources/Imperial/Helpers/Sessions+Imperial.swift +++ b/Sources/Imperial/Helpers/Sessions+Imperial.swift @@ -20,7 +20,7 @@ extension Session { /// - Returns: The access token stored with the `access_token` key. /// - Throws: `Abort.unauthorized` if no access token exists.m public func getAccessToken()throws -> String { - guard let token = self.data["access_token"]?.string else { + guard let token = self["access_token"] else { throw Abort(.unauthorized, reason: "User currently not authenticated") } return token From dbebcc3b8b57ebd5384241650d29d6911325c410 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Fri, 19 Jan 2018 17:03:38 -0600 Subject: [PATCH 28/50] replaced .assertSession() with .session() in Request.getAccessToken --- Sources/Imperial/Helpers/Sessions+Imperial.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Imperial/Helpers/Sessions+Imperial.swift b/Sources/Imperial/Helpers/Sessions+Imperial.swift index 7673b5d..dc54f7a 100644 --- a/Sources/Imperial/Helpers/Sessions+Imperial.swift +++ b/Sources/Imperial/Helpers/Sessions+Imperial.swift @@ -9,7 +9,7 @@ extension Request { /// - `Abort.unauthorized` if no access token exists. /// - `SessionsError.notConfigured` if session middlware is not configured yet. public func getAccessToken()throws -> String { - return try self.assertSession().getAccessToken() + return try self.session().getAccessToken() } } From d5aafb3609706f059d4cb3a61452a02074656fc1 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Fri, 19 Jan 2018 17:04:21 -0600 Subject: [PATCH 29/50] Removed ServiceStorage registration from Provider.register --- Sources/Imperial/Provider.swift | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Sources/Imperial/Provider.swift b/Sources/Imperial/Provider.swift index 47935b1..2a09d1e 100644 --- a/Sources/Imperial/Provider.swift +++ b/Sources/Imperial/Provider.swift @@ -5,11 +5,7 @@ public class Provider: Vapor.Provider { public static var repositoryName: String = "Imperial" /// Register all services provided by the provider here. - public func register(_ services: inout Services) throws { - services.register(isSingleton: true) { (container) in - return ServiceStorage() - } - } + public func register(_ services: inout Services) throws {} /// Called after the container has initialized. public func boot(_ worker: Container) throws { From 2a99944328a73eb825bc9a199a3d258da465a3d5 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Fri, 19 Jan 2018 17:15:51 -0600 Subject: [PATCH 30/50] Updated FederatedServiceRouter.callbackURL parsing --- Sources/Imperial/Routing/FederatedServiceRouter.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Imperial/Routing/FederatedServiceRouter.swift b/Sources/Imperial/Routing/FederatedServiceRouter.swift index 9ae9f81..3d311d4 100644 --- a/Sources/Imperial/Routing/FederatedServiceRouter.swift +++ b/Sources/Imperial/Routing/FederatedServiceRouter.swift @@ -63,7 +63,7 @@ extension FederatedServiceRouter { } public func configureRoutes(withAuthURL authURL: String) throws { - var callbackPath = URIParser().parse(bytes: callbackURL.bytes).path + var callbackPath = URI(callbackURL).path callbackPath = callbackPath != "/" ? callbackPath : callbackURL drop.get(callbackPath, handler: callback) From 3aa04d8be16850c73cb4f7a954444e27593332f3 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 12:09:31 -0600 Subject: [PATCH 31/50] Deleted ServiceStorage type --- .../Services/Service/ServiceStorage.swift | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 Sources/Imperial/Services/Service/ServiceStorage.swift diff --git a/Sources/Imperial/Services/Service/ServiceStorage.swift b/Sources/Imperial/Services/Service/ServiceStorage.swift deleted file mode 100644 index 8b735e2..0000000 --- a/Sources/Imperial/Services/Service/ServiceStorage.swift +++ /dev/null @@ -1,16 +0,0 @@ -import Service - -final class ServiceStorage: Service { - private(set) var model: FederatedCreatable! - - init() {} - - subscript (model: FederatedCreatable) -> FederatedCreatable { - get { - return self.model - } - set { - self.model = model - } - } -} From 2e120798d83d27c34c9bbf1fedc7f47998e9ae97 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 12:09:42 -0600 Subject: [PATCH 32/50] Created global router --- Sources/Imperial/Provider.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sources/Imperial/Provider.swift b/Sources/Imperial/Provider.swift index 2a09d1e..8466c22 100644 --- a/Sources/Imperial/Provider.swift +++ b/Sources/Imperial/Provider.swift @@ -1,5 +1,7 @@ import Vapor +internal fileprivate(set) var routeer: Router! + public class Provider: Vapor.Provider { public static var repositoryName: String = "Imperial" @@ -9,6 +11,8 @@ public class Provider: Vapor.Provider { /// Called after the container has initialized. public func boot(_ worker: Container) throws { + routeer = try worker.make(Router.self, for: Container.self) + Google.registerName() GitHub.registerName() } From 7f67c4032bb1bdea14c00e746c70f39e9e365e47 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 12:13:28 -0600 Subject: [PATCH 33/50] Replaced use of droplet and string paths in FederatedServiceRouter.configureRoutes with router and PathCompnents --- Sources/Imperial/Routing/FederatedServiceRouter.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Imperial/Routing/FederatedServiceRouter.swift b/Sources/Imperial/Routing/FederatedServiceRouter.swift index 3d311d4..1add0c0 100644 --- a/Sources/Imperial/Routing/FederatedServiceRouter.swift +++ b/Sources/Imperial/Routing/FederatedServiceRouter.swift @@ -66,7 +66,7 @@ extension FederatedServiceRouter { var callbackPath = URI(callbackURL).path callbackPath = callbackPath != "/" ? callbackPath : callbackURL - drop.get(callbackPath, handler: callback) - drop.get(authURL, handler: authenticate) + routeer.get(callbackPath.makePathComponent(), use: callback) + routeer.get(authURL.makePathComponent(), use: authenticate) } } From 6bdf4a384fc26281ac43586a05dba0f190517daa Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 12:15:24 -0600 Subject: [PATCH 34/50] Replaced ImperialService.JSONConvertible conformance to Content protocol conformance --- .../Services/Service/ImperialService.swift | 4 +- .../Services/Service/Service+JSON.swift | 39 ------------------- 2 files changed, 3 insertions(+), 40 deletions(-) diff --git a/Sources/Imperial/Services/Service/ImperialService.swift b/Sources/Imperial/Services/Service/ImperialService.swift index 4ad0548..17e6cab 100644 --- a/Sources/Imperial/Services/Service/ImperialService.swift +++ b/Sources/Imperial/Services/Service/ImperialService.swift @@ -1,9 +1,11 @@ +import Vapor + /// The services that are available for use in the application. /// Services are added and fecthed with the `Service.register` and `.get` static methods. fileprivate var services: [String: ImperialService] = [:] /// Represents a service that interacts with an OAuth provider. -public struct ImperialService { +public struct ImperialService: Content { /// The name of the service, i.e. "google", "github", etc. public let name: String diff --git a/Sources/Imperial/Services/Service/Service+JSON.swift b/Sources/Imperial/Services/Service/Service+JSON.swift index 5010b7c..a892cb4 100644 --- a/Sources/Imperial/Services/Service/Service+JSON.swift +++ b/Sources/Imperial/Services/Service/Service+JSON.swift @@ -16,42 +16,3 @@ extension FederatedService { return String(describing: self) } } - - -extension ImperialService: JSONConvertible { - - /// Creates a JSON representation of the `Service`. - /// - /// - Returns: The `Service` in JSON format. - /// - Throws: Errors that occur when setting JSON keys. - public func makeJSON() throws -> JSON { - var json = JSON() - try json.set("name", self.name) - try json.set("token_prefix", self.tokenPrefix) - try json.set("endpoints", self.endpoints) - try json.set("model", self.model.typeName) - return json - } - - /// Creates a `Service` from a JSON object. - /// The JSON must contain the following keys: - /// - model: `String` - /// - name: `String` - /// - token_prefix: `String` - /// - endpoints: JSON object (`[String: String]`) - /// - /// - Parameter json: The JSON to create the `Service` with. - /// - Throws: - /// - `ServiceError.noExistingService`: No service is registered with the key of the `model` parameter. - /// - Errors from getting the values from JSON keys. - public init(json: JSON) throws { - let key: String = try json.get("model") - - let name: String = try json.get("name") - let tokenPrefix: String = try json.get("token_prefix") - let endpoints: [String: String] = try json.get("endpoints") - let model: FederatedService.Type = try federatedServices[key] ?? ServiceError.noExistingService(key) - - self.init(name: name, prefix: tokenPrefix, model: model, endpoints: endpoints) - } -} From 6338fe284b83197bf156f559bf6714dbeed35976 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 12:21:34 -0600 Subject: [PATCH 35/50] Removed use of ImperialConfig from GitHubAuth.init --- .../Imperial/Services/GitHub/GitHubAuth.swift | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/Sources/Imperial/Services/GitHub/GitHubAuth.swift b/Sources/Imperial/Services/GitHub/GitHubAuth.swift index 4bb6cb9..30915ae 100644 --- a/Sources/Imperial/Services/GitHub/GitHubAuth.swift +++ b/Sources/Imperial/Services/GitHub/GitHubAuth.swift @@ -10,22 +10,7 @@ public class GitHubAuth: FederatedServiceTokens { let idError = ImperialError.missingEnvVar(idEnvKey) let secretError = ImperialError.missingEnvVar(secretEnvKey) - do { - guard let id = ImperialConfig.gitHubID else { - throw idError - } - self.clientID = id - } catch { - self.clientID = try Env.get(idEnvKey).value(or: idError) - } - - do { - guard let secret = ImperialConfig.gitHubSecret else { - throw secretError - } - self.clientSecret = secret - } catch { - self.clientSecret = try Env.get(secretEnvKey).value(or: secretError) - } + self.clientID = try Environment.get(idEnvKey) ?? idError + self.clientSecret = try Environment.get(secretEnvKey) ?? secretError } } From 408b5515678f029ceffe4de4630ae75c8dec28c4 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 12:21:54 -0600 Subject: [PATCH 36/50] Removed use of ImperialConfig from GoogleAuth.init --- .../Imperial/Services/Google/GoogleAuth.swift | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/Sources/Imperial/Services/Google/GoogleAuth.swift b/Sources/Imperial/Services/Google/GoogleAuth.swift index 56ca1fc..ddede76 100644 --- a/Sources/Imperial/Services/Google/GoogleAuth.swift +++ b/Sources/Imperial/Services/Google/GoogleAuth.swift @@ -10,22 +10,7 @@ public class GoogleAuth: FederatedServiceTokens { let idError = ImperialError.missingEnvVar(idEnvKey) let secretError = ImperialError.missingEnvVar(secretEnvKey) - do { - guard let id = ImperialConfig.googleID else { - throw idError - } - self.clientID = id - } catch { - self.clientID = try Env.get(idEnvKey).value(or: idError) - } - - do { - guard let secret = ImperialConfig.googleSecret else { - throw secretError - } - self.clientSecret = secret - } catch { - self.clientSecret = try Env.get(secretEnvKey).value(or: secretError) - } + self.clientID = try Environment.get(idEnvKey) ?? idError + self.clientSecret = try Environment.get(secretEnvKey) ?? secretError } } From c11c5ee4da9740e05e0c1ec7e4b61a327e32de47 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 12:58:34 -0600 Subject: [PATCH 37/50] Changed Request.get to .send with custom HTTP method --- Sources/Imperial/Helpers/Request+Imperial.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/Imperial/Helpers/Request+Imperial.swift b/Sources/Imperial/Helpers/Request+Imperial.swift index a7c0474..eb1d48c 100644 --- a/Sources/Imperial/Helpers/Request+Imperial.swift +++ b/Sources/Imperial/Helpers/Request+Imperial.swift @@ -2,12 +2,12 @@ import Vapor extension Request { - func get(url: String, headers: HTTPHeaders.Literal = [:], body: HTTPBody = HTTPBody(), mediaType: MediaType? = nil)throws -> Future { + func send(method: HTTPMethod = .get, url: String, headers: HTTPHeaders.Literal = [:], body: HTTPBody = HTTPBody(), mediaType: MediaType? = nil)throws -> Future { let client = try self.make(HTTPClient.self) var header: HTTPHeaders = HTTPHeaders() header.append(headers) - var request = HTTPRequest(method: .get, uri: URI(url), headers: header, body: body) - request.mediaType = .urlEncodedForm + var request = HTTPRequest(method: method, uri: URI(url), headers: header, body: body) + request.mediaType = mediaType ?? .urlEncodedForm return client.send(request).map(to: Response.self, { (res) in return Response(http: res, using: self.superContainer) }) @@ -25,7 +25,7 @@ extension Request { let token = try service.tokenPrefix + self.getAccessToken() - return try self.get(url: uri, headers: [.authorization: token]).flatMap(to: T.self, { (response) -> Future in + return try self.send(url: uri, headers: [.authorization: token]).flatMap(to: T.self, { (response) -> Future in return try model.create(from: response) }).map(to: T.self, { (instance) -> T in let session = try self.session() From b7e259c00714cb5dc7be3591797699f792c806d0 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 12:58:57 -0600 Subject: [PATCH 38/50] Rewrote GitHubRouter.callback for Vapor 3 --- .../Services/GitHub/GitHubRouter.swift | 40 ++++++++----------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/Sources/Imperial/Services/GitHub/GitHubRouter.swift b/Sources/Imperial/Services/GitHub/GitHubRouter.swift index 3ccc7d0..426b04a 100644 --- a/Sources/Imperial/Services/GitHub/GitHubRouter.swift +++ b/Sources/Imperial/Services/GitHub/GitHubRouter.swift @@ -1,4 +1,5 @@ import Vapor +import Foundation public class GitHubRouter: FederatedServiceRouter { public let tokens: FederatedServiceTokens @@ -20,35 +21,28 @@ public class GitHubRouter: FederatedServiceRouter { public func callback(_ request: Request)throws -> Future { let code: String - if let queryCode: String = try request.query?.get("code") { + if let queryCode: String = try request.query.get(at: "code") { code = queryCode - } else if let error: String = try request.query?.get("error") { + } else if let error: String = try request.query.get(at: "error") { throw Abort(.badRequest, reason: error) } else { throw Abort(.badRequest, reason: "Missing 'code' key in URL query") } - let req = Request(method: .post, uri: accessTokenURL) - req.formURLEncoded = [ - "client_id": .string(self.tokens.clientID), - "client_secret": .string(self.tokens.clientSecret), - "code": .string(code) - ] + let bodyData = NSKeyedArchiver.archivedData(withRootObject: [ + "client_id": self.tokens.clientID, + "client_secret": self.tokens.clientSecret, + "code": code + ]) - let response = try drop.client.respond(to: req) - - guard let body = response.body.bytes else { - throw Abort(.internalServerError, reason: "Unable to get body from access token response") - } - - guard let accessToken: String = try Node(formURLEncoded: body, allowEmptyValues: false).get("access_token") else { - throw Abort(.internalServerError, reason: "Unable to get access token from response body") - } - - let session = try request.assertSession() - try session.data.set("access_token", accessToken) - try session.data.set("access_token_service", ImperialService.github) - - return callbackCompletion(accessToken) + return try request.send(url: accessTokenURL, body: HTTPBody(bodyData)).flatMap(to: String.self, { (response) in + return response.content.get(String.self, at: ["access_token"]) + }).map(to: ResponseEncodable.self, { (accessToken) in + let session = try request.session() + session.data.storage["access_token"] = accessToken + session.data.storage["access_token_service"] = ImperialService.github + + return self.callbackCompletion(accessToken) + }) } } From 2d6578d17a2f8a6440dc4fd11feeccdf6a2b7a7d Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 13:01:52 -0600 Subject: [PATCH 39/50] Rewrote GoogleRouter.callback for Vapor 3 --- .../Services/Google/GoogleRouter.swift | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/Sources/Imperial/Services/Google/GoogleRouter.swift b/Sources/Imperial/Services/Google/GoogleRouter.swift index 1e84f2c..9213dc8 100644 --- a/Sources/Imperial/Services/Google/GoogleRouter.swift +++ b/Sources/Imperial/Services/Google/GoogleRouter.swift @@ -1,5 +1,5 @@ import Vapor -import HTTP +import Foundation public class GoogleRouter: FederatedServiceRouter { public let tokens: FederatedServiceTokens @@ -23,33 +23,30 @@ public class GoogleRouter: FederatedServiceRouter { public func callback(_ request: Request)throws -> Future { let code: String - if let queryCode: String = try request.query?.get("code") { + if let queryCode: String = try request.query.get(at: "code") { code = queryCode - } else if let error: String = try request.query?.get("error") { + } else if let error: String = try request.query.get(at: "error") { throw Abort(.badRequest, reason: error) } else { throw Abort(.badRequest, reason: "Missing 'code' key in URL query") } - let req = Request(method: .post, uri: accessTokenURL) - req.formURLEncoded = [ - "code": .string(code), - "client_id": .string(self.tokens.clientID), - "client_secret": .string(self.tokens.clientSecret), - "grant_type": .string("authorization_code"), - "redirect_uri": .string(self.callbackURL) - ] - let response = try drop.client.respond(to: req) + let bodyData = NSKeyedArchiver.archivedData(withRootObject: [ + "code": code, + "client_id": self.tokens.clientID, + "client_secret": self.tokens.clientSecret, + "grant_type": "authorization_code", + "redirect_uri": self.callbackURL + ]) - guard let body = response.body.bytes, - let accessToken: String = try JSON(bytes: body).get("access_token") else { - throw Abort(.badRequest, reason: "Missing JSON from response body") - } - - let session = try request.assertSession() - try session.data.set("access_token", accessToken) - try session.data.set("access_token_service", ImperialService.google) - - return callbackCompletion(accessToken) + return try request.send(url: accessTokenURL, body: HTTPBody(bodyData)).flatMap(to: String.self, { (response) in + return response.content.get(String.self, at: ["access_token"]) + }).map(to: ResponseEncodable.self, { (accessToken) in + let session = try request.session() + session.data.storage["access_token"] = accessToken + session.data.storage["access_token_service"] = ImperialService.google + + return self.callbackCompletion(accessToken) + }) } } From 30e76e9e0b5d12231a720817d22b267cedf85b79 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 14:29:21 -0600 Subject: [PATCH 40/50] Removed .model property from ImperialService struct --- Sources/Imperial/Services/GitHub/Service+GitHub.swift | 1 - Sources/Imperial/Services/Google/Service+Google.swift | 1 - Sources/Imperial/Services/Service/ImperialService.swift | 6 +----- 3 files changed, 1 insertion(+), 7 deletions(-) diff --git a/Sources/Imperial/Services/GitHub/Service+GitHub.swift b/Sources/Imperial/Services/GitHub/Service+GitHub.swift index 9b28c64..2712f6b 100644 --- a/Sources/Imperial/Services/GitHub/Service+GitHub.swift +++ b/Sources/Imperial/Services/GitHub/Service+GitHub.swift @@ -1,7 +1,6 @@ extension ImperialService { public static let github = ImperialService.init( name: "github", - model: GitHub.self, endpoints: [ "user": "https://api.github.com/user" ] diff --git a/Sources/Imperial/Services/Google/Service+Google.swift b/Sources/Imperial/Services/Google/Service+Google.swift index 1074d14..f1d70c3 100644 --- a/Sources/Imperial/Services/Google/Service+Google.swift +++ b/Sources/Imperial/Services/Google/Service+Google.swift @@ -1,7 +1,6 @@ extension ImperialService { public static let google = ImperialService.init( name: "google", - model: Google.self, endpoints: [:] ) } diff --git a/Sources/Imperial/Services/Service/ImperialService.swift b/Sources/Imperial/Services/Service/ImperialService.swift index 17e6cab..c574ebe 100644 --- a/Sources/Imperial/Services/Service/ImperialService.swift +++ b/Sources/Imperial/Services/Service/ImperialService.swift @@ -16,9 +16,6 @@ public struct ImperialService: Content { /// The endpoints for the provider's API to use for initializing `FederatedCreatable` types public let endpoints: [String: String] - /// The service model that is used for interacting the the named OAuth provider. - public let model: FederatedService.Type - /// Creates an instance of a service. /// This is is usually done by creating an extension and a static property. /// @@ -27,10 +24,9 @@ public struct ImperialService: Content { /// - prefix: The prefix for the access token when it is used in a authoriazation header. /// - uri: The URI used to get data to initialize a `FederatedCreatable` type. /// - model: The model that works with the service. - public init(name: String, prefix: String? = nil, model: FederatedService.Type, endpoints: [String: String]) { + public init(name: String, prefix: String? = nil, endpoints: [String: String]) { self.name = name self.tokenPrefix = prefix ?? "Bearer " - self.model = model self.endpoints = endpoints } From a10c7a9965de803b29b68b6358d86cefb165774c Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 14:29:36 -0600 Subject: [PATCH 41/50] Conformed ImperialService to Codable --- Sources/Imperial/Services/Service/ImperialService.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Imperial/Services/Service/ImperialService.swift b/Sources/Imperial/Services/Service/ImperialService.swift index c574ebe..621c3a2 100644 --- a/Sources/Imperial/Services/Service/ImperialService.swift +++ b/Sources/Imperial/Services/Service/ImperialService.swift @@ -5,7 +5,7 @@ import Vapor fileprivate var services: [String: ImperialService] = [:] /// Represents a service that interacts with an OAuth provider. -public struct ImperialService: Content { +public struct ImperialService: Codable, Content { /// The name of the service, i.e. "google", "github", etc. public let name: String From 8a476fc3f38c5d4e39bc58b436658a3ef780ae1c Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 14:30:26 -0600 Subject: [PATCH 42/50] Renamed ImperialService to OAuthService --- Sources/Imperial/Helpers/Request+Imperial.swift | 2 +- Sources/Imperial/Services/GitHub/GitHub.swift | 2 +- Sources/Imperial/Services/GitHub/GitHubRouter.swift | 2 +- Sources/Imperial/Services/GitHub/Service+GitHub.swift | 4 ++-- Sources/Imperial/Services/Google/Google.swift | 2 +- Sources/Imperial/Services/Google/GoogleRouter.swift | 2 +- Sources/Imperial/Services/Google/Service+Google.swift | 4 ++-- .../Service/{ImperialService.swift => OAuthService.swift} | 8 ++++---- 8 files changed, 13 insertions(+), 13 deletions(-) rename Sources/Imperial/Services/Service/{ImperialService.swift => OAuthService.swift} (88%) diff --git a/Sources/Imperial/Helpers/Request+Imperial.swift b/Sources/Imperial/Helpers/Request+Imperial.swift index eb1d48c..0e369a0 100644 --- a/Sources/Imperial/Helpers/Request+Imperial.swift +++ b/Sources/Imperial/Helpers/Request+Imperial.swift @@ -20,7 +20,7 @@ extension Request { /// - service: The service to get the data from. /// - Returns: An instance of the type passed in. /// - Throws: Errors from trying to get the access token from the request. - func create(_ model: T.Type, with service: ImperialService)throws -> Future { + func create(_ model: T.Type, with service: OAuthService)throws -> Future { let uri = try service[model.serviceKey] ?? ServiceError.noServiceEndpoint(model.serviceKey) let token = try service.tokenPrefix + self.getAccessToken() diff --git a/Sources/Imperial/Services/GitHub/GitHub.swift b/Sources/Imperial/Services/GitHub/GitHub.swift index 89d5c2c..9ba9a43 100644 --- a/Sources/Imperial/Services/GitHub/GitHub.swift +++ b/Sources/Imperial/Services/GitHub/GitHub.swift @@ -12,6 +12,6 @@ public class GitHub: FederatedService { self.router.scope = scope try self.router.configureRoutes(withAuthURL: authenticate) - ImperialService.register(.github) + OAuthService.register(.github) } } diff --git a/Sources/Imperial/Services/GitHub/GitHubRouter.swift b/Sources/Imperial/Services/GitHub/GitHubRouter.swift index 426b04a..8a55a5f 100644 --- a/Sources/Imperial/Services/GitHub/GitHubRouter.swift +++ b/Sources/Imperial/Services/GitHub/GitHubRouter.swift @@ -40,7 +40,7 @@ public class GitHubRouter: FederatedServiceRouter { }).map(to: ResponseEncodable.self, { (accessToken) in let session = try request.session() session.data.storage["access_token"] = accessToken - session.data.storage["access_token_service"] = ImperialService.github + session.data.storage["access_token_service"] = OAuthService.github return self.callbackCompletion(accessToken) }) diff --git a/Sources/Imperial/Services/GitHub/Service+GitHub.swift b/Sources/Imperial/Services/GitHub/Service+GitHub.swift index 2712f6b..d681120 100644 --- a/Sources/Imperial/Services/GitHub/Service+GitHub.swift +++ b/Sources/Imperial/Services/GitHub/Service+GitHub.swift @@ -1,5 +1,5 @@ -extension ImperialService { - public static let github = ImperialService.init( +extension OAuthService { + public static let github = OAuthService.init( name: "github", endpoints: [ "user": "https://api.github.com/user" diff --git a/Sources/Imperial/Services/Google/Google.swift b/Sources/Imperial/Services/Google/Google.swift index 752f846..687eb06 100644 --- a/Sources/Imperial/Services/Google/Google.swift +++ b/Sources/Imperial/Services/Google/Google.swift @@ -12,6 +12,6 @@ public class Google: FederatedService { self.router.scope = scope try self.router.configureRoutes(withAuthURL: authenticate) - ImperialService.register(.google) + OAuthService.register(.google) } } diff --git a/Sources/Imperial/Services/Google/GoogleRouter.swift b/Sources/Imperial/Services/Google/GoogleRouter.swift index 9213dc8..388d013 100644 --- a/Sources/Imperial/Services/Google/GoogleRouter.swift +++ b/Sources/Imperial/Services/Google/GoogleRouter.swift @@ -44,7 +44,7 @@ public class GoogleRouter: FederatedServiceRouter { }).map(to: ResponseEncodable.self, { (accessToken) in let session = try request.session() session.data.storage["access_token"] = accessToken - session.data.storage["access_token_service"] = ImperialService.google + session.data.storage["access_token_service"] = OAuthService.google return self.callbackCompletion(accessToken) }) diff --git a/Sources/Imperial/Services/Google/Service+Google.swift b/Sources/Imperial/Services/Google/Service+Google.swift index f1d70c3..625ea9b 100644 --- a/Sources/Imperial/Services/Google/Service+Google.swift +++ b/Sources/Imperial/Services/Google/Service+Google.swift @@ -1,5 +1,5 @@ -extension ImperialService { - public static let google = ImperialService.init( +extension OAuthService { + public static let google = OAuthService.init( name: "google", endpoints: [:] ) diff --git a/Sources/Imperial/Services/Service/ImperialService.swift b/Sources/Imperial/Services/Service/OAuthService.swift similarity index 88% rename from Sources/Imperial/Services/Service/ImperialService.swift rename to Sources/Imperial/Services/Service/OAuthService.swift index 621c3a2..cec7561 100644 --- a/Sources/Imperial/Services/Service/ImperialService.swift +++ b/Sources/Imperial/Services/Service/OAuthService.swift @@ -2,10 +2,10 @@ import Vapor /// The services that are available for use in the application. /// Services are added and fecthed with the `Service.register` and `.get` static methods. -fileprivate var services: [String: ImperialService] = [:] +fileprivate var services: [String: OAuthService] = [:] /// Represents a service that interacts with an OAuth provider. -public struct ImperialService: Codable, Content { +public struct OAuthService: Codable, Content { /// The name of the service, i.e. "google", "github", etc. public let name: String @@ -38,7 +38,7 @@ public struct ImperialService: Codable, Content { /// Registers a service as available for use. /// /// - Parameter service: The service to register. - internal static func register(_ service: ImperialService) { + internal static func register(_ service: OAuthService) { services[service.name] = service } @@ -47,7 +47,7 @@ public struct ImperialService: Codable, Content { /// - Parameter name: The name of the service to fetch. /// - Returns: The service that matches the name passed in. /// - Throws: `ImperialError.noServiceFound` if no service is found with the name passed in. - public static func get(service name: String)throws -> ImperialService { + public static func get(service name: String)throws -> OAuthService { return try services[name] ?? ServiceError.noServiceFound(name) } } From ebe42a4af67bd3395f899e4237b1ab726d44ae37 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 14:34:08 -0600 Subject: [PATCH 43/50] Removed FederatedService name registration --- Sources/Imperial/Provider.swift | 3 --- .../Services/Service/Service+JSON.swift | 18 ------------------ 2 files changed, 21 deletions(-) delete mode 100644 Sources/Imperial/Services/Service/Service+JSON.swift diff --git a/Sources/Imperial/Provider.swift b/Sources/Imperial/Provider.swift index 8466c22..914dadd 100644 --- a/Sources/Imperial/Provider.swift +++ b/Sources/Imperial/Provider.swift @@ -12,8 +12,5 @@ public class Provider: Vapor.Provider { /// Called after the container has initialized. public func boot(_ worker: Container) throws { routeer = try worker.make(Router.self, for: Container.self) - - Google.registerName() - GitHub.registerName() } } diff --git a/Sources/Imperial/Services/Service/Service+JSON.swift b/Sources/Imperial/Services/Service/Service+JSON.swift deleted file mode 100644 index a892cb4..0000000 --- a/Sources/Imperial/Services/Service/Service+JSON.swift +++ /dev/null @@ -1,18 +0,0 @@ -internal fileprivate(set) var federatedServices: [String: FederatedService.Type] = [ - : -] - -extension FederatedService { - - /// Saves the `FederatedService` type - /// so it can be used as the `model` property in a service. - public static func registerName() { - let key = Self.typeName - federatedServices[key] = self - } - - /// The description of the type as a `String`. - public static var typeName: String { - return String(describing: self) - } -} From 8f6c2c04496810d409f09a6b0361bf8fab85dfb3 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 14:36:22 -0600 Subject: [PATCH 44/50] Fixed Typo in global fileprivate services variable --- Sources/Imperial/Services/Service/OAuthService.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Imperial/Services/Service/OAuthService.swift b/Sources/Imperial/Services/Service/OAuthService.swift index cec7561..d075dc0 100644 --- a/Sources/Imperial/Services/Service/OAuthService.swift +++ b/Sources/Imperial/Services/Service/OAuthService.swift @@ -1,7 +1,7 @@ import Vapor /// The services that are available for use in the application. -/// Services are added and fecthed with the `Service.register` and `.get` static methods. +/// Services are added and fetched with the `Service.register` and `.get` static methods. fileprivate var services: [String: OAuthService] = [:] /// Represents a service that interacts with an OAuth provider. From ddb72fe521f9ef3d46958ec739db93a6084caec7 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 14:39:04 -0600 Subject: [PATCH 45/50] Renamed global routeer variable to router --- Sources/Imperial/Provider.swift | 4 ++-- Sources/Imperial/Routing/FederatedServiceRouter.swift | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/Imperial/Provider.swift b/Sources/Imperial/Provider.swift index 914dadd..3dd9c78 100644 --- a/Sources/Imperial/Provider.swift +++ b/Sources/Imperial/Provider.swift @@ -1,6 +1,6 @@ import Vapor -internal fileprivate(set) var routeer: Router! +internal fileprivate(set) var router: Router! public class Provider: Vapor.Provider { @@ -11,6 +11,6 @@ public class Provider: Vapor.Provider { /// Called after the container has initialized. public func boot(_ worker: Container) throws { - routeer = try worker.make(Router.self, for: Container.self) + router = try worker.make(Router.self, for: Container.self) } } diff --git a/Sources/Imperial/Routing/FederatedServiceRouter.swift b/Sources/Imperial/Routing/FederatedServiceRouter.swift index 1add0c0..b19c2f0 100644 --- a/Sources/Imperial/Routing/FederatedServiceRouter.swift +++ b/Sources/Imperial/Routing/FederatedServiceRouter.swift @@ -66,7 +66,7 @@ extension FederatedServiceRouter { var callbackPath = URI(callbackURL).path callbackPath = callbackPath != "/" ? callbackPath : callbackURL - routeer.get(callbackPath.makePathComponent(), use: callback) - routeer.get(authURL.makePathComponent(), use: authenticate) + router.get(callbackPath.makePathComponent(), use: callback) + router.get(authURL.makePathComponent(), use: authenticate) } } From 2f01faf4c48c6a3488d0813f62c1ce8d72783cac Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 14:53:02 -0600 Subject: [PATCH 46/50] Copied Vapor CircleCI config --- .circleci/config.yml | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 .circleci/config.yml diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..ef69509 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,37 @@ +version: 2 + +jobs: + macos: + macos: + xcode: "9.0" + steps: + - run: brew install vapor/tap/vapor + - checkout + - run: swift build + - run: swift test + + linux-3: + docker: + - image: swift:3.1.1 + steps: + - run: apt-get install -yq libssl-dev + - checkout + - run: swift build + - run: swift test + + linux-4: + docker: + - image: swift:4.0.2 + steps: + - run: apt-get install -yq libssl-dev + - checkout + - run: swift build + - run: swift test + + workflows: + version: 2 + tests: + jobs: + - macos + - linux-3 + - linux-4 From 76f4ebaa21f684c45007e245ef99429b23cab3bc Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 14:55:14 -0600 Subject: [PATCH 47/50] Created empty test --- Tests/ImperialTests/ImperialTests.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Tests/ImperialTests/ImperialTests.swift b/Tests/ImperialTests/ImperialTests.swift index e2d6ae6..9ae6498 100644 --- a/Tests/ImperialTests/ImperialTests.swift +++ b/Tests/ImperialTests/ImperialTests.swift @@ -2,6 +2,9 @@ import XCTest @testable import Imperial class ImperialTests: XCTestCase { + func testExists() {} + static var allTests: [(String, (ImperialTests) -> () -> ())] = [ + ("testExists", testExists) ] } From 8f5d02aaef1b3b1599741e22012d87af26db3a3f Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 14:57:16 -0600 Subject: [PATCH 48/50] Fixed formatting for circleci workflows object --- .circleci/config.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index ef69509..ece8203 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -28,10 +28,10 @@ jobs: - run: swift build - run: swift test - workflows: - version: 2 - tests: - jobs: - - macos - - linux-3 - - linux-4 +workflows: + version: 2 + tests: + jobs: + - macos + - linux-3 + - linux-4 From d0395da6d68da62c967d8c01b73c1b739330cf37 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 14:59:35 -0600 Subject: [PATCH 49/50] Placed workflows.jobs list under workflows.tests --- .circleci/config.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index ece8203..48d620a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -31,7 +31,7 @@ jobs: workflows: version: 2 tests: - jobs: - - macos - - linux-3 - - linux-4 + jobs: + - macos + - linux-3 + - linux-4 From 05f0ca25d8ea3bfa3f2f56ee01774a0e1d3d7527 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Sat, 20 Jan 2018 15:02:32 -0600 Subject: [PATCH 50/50] Removed linux 3 build --- .circleci/config.yml | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 48d620a..9e3c1ec 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -10,18 +10,9 @@ jobs: - run: swift build - run: swift test - linux-3: + linux: docker: - - image: swift:3.1.1 - steps: - - run: apt-get install -yq libssl-dev - - checkout - - run: swift build - - run: swift test - - linux-4: - docker: - - image: swift:4.0.2 + - image: swift:4.0 steps: - run: apt-get install -yq libssl-dev - checkout @@ -33,5 +24,4 @@ workflows: tests: jobs: - macos - - linux-3 - - linux-4 + - linux