Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async/await #100

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Imperial/Services/DeviantArt/DeviantArt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public class DeviantArt: FederatedService {
public required init(
router: Router,
authenticate: String,
authenticateCallback: ((Request)throws -> (Future<Void>))?,
authenticateCallback: ((Request) async throws -> Void)?,
callback: String,
scope: [String] = [],
completion: @escaping (Request, String)throws -> (Future<ResponseEncodable>)
completion: @escaping (Request, String) async throws -> Response)
) throws {
self.router = try DeviantArtRouter(callback: callback, completion: completion)
self.tokens = self.router.tokens
Expand Down
50 changes: 24 additions & 26 deletions Sources/Imperial/Services/DeviantArt/DeviantArtRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import Foundation

public class DeviantArtRouter: FederatedServiceRouter {
public let tokens: FederatedServiceTokens
public let callbackCompletion: (Request, String)throws -> (Future<ResponseEncodable>)
public let callbackCompletion: (Request, String) async throws -> Response
public var scope: [String] = []
public var callbackURL: String
public let accessTokenURL: String = "https://www.deviantart.com/oauth2/token"

public required init(callback: String, completion: @escaping (Request, String)throws -> (Future<ResponseEncodable>)) throws {
public required init(callback: String, completion: @escaping (Request, String)async throws -> Response) throws {
self.tokens = try DeviantArtAuth()
self.callbackURL = callback
self.callbackCompletion = completion
Expand All @@ -27,7 +27,7 @@ public class DeviantArtRouter: FederatedServiceRouter {
"response_type=code"
}

public func fetchToken(from request: Request)throws -> Future<String> {
public func fetchToken(from request: Request) async throws -> String {
let code: String
if let queryCode: String = try request.query.get(at: "code") {
code = queryCode
Expand All @@ -38,35 +38,33 @@ public class DeviantArtRouter: FederatedServiceRouter {
}

let body = DeviantArtCallbackBody(code: code, clientId: self.tokens.clientID, clientSecret: self.tokens.clientSecret, redirectURI: self.callbackURL)
return try body.encode(using: request).flatMap(to: Response.self) { request in
guard let url = URL(string: self.accessTokenURL) else {
throw Abort(.internalServerError, reason: "Unable to convert String '\(self.accessTokenURL)' to URL")
}
request.http.method = .POST
request.http.url = url
return try request.make(Client.self).send(request)
}.flatMap(to: String.self) { response in
let session = try request.session()
let requestBody = try await body.encode(using: request)
guard let url = URL(string: self.accessTokenURL) else {
throw Abort(.internalServerError, reason: "Unable to convert String '\(self.accessTokenURL)' to URL")
}
requestBody.http.method = .POST
requestBody.http.url = url
let response = try await requestBody.make(Client.self).send(request)
let session = try request.session()

return response.content.get(String.self, at: ["refresh_token"])
.flatMap { refresh in
session.setRefreshToken(refresh)
return response.content.get(String.self, at: ["refresh_token"])
.flatMap { refresh in
session.setRefreshToken(refresh)

return response.content.get(String.self, at: ["access_token"])
}
return response.content.get(String.self, at: ["access_token"])
}

}

public func callback(_ request: Request)throws -> Future<Response> {
return try self.fetchToken(from: request).flatMap(to: ResponseEncodable.self) { accessToken in
let session = try request.session()
public func callback(_ request: Request) async throws -> Response {
let accessToken = try await self.fetchToken(from: request)
let session = try request.session()

session.setAccessToken(accessToken)
try session.set("access_token_service", to: OAuthService.deviantart)
session.setAccessToken(accessToken)
try session.set("access_token_service", to: OAuthService.deviantart)

return try self.callbackCompletion(request, accessToken)
}.flatMap(to: Response.self) { response in
return try response.encode(for: request)
}
let response = try await self.callbackCompletion(request, accessToken)
return try response.encode(for: request)

}
}
4 changes: 2 additions & 2 deletions Sources/Imperial/Services/Imgur/Imgur.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public class Imgur: FederatedService {
public required init(
router: Router,
authenticate: String,
authenticateCallback: ((Request)throws -> (Future<Void>))?,
authenticateCallback: ((Request) async throws -> Void?,
callback: String,
scope: [String] = [],
completion: @escaping (Request, String)throws -> (Future<ResponseEncodable>)
completion: @escaping (Request, String) async throws -> Response
) throws {
self.router = try ImgurRouter(callback: callback, completion: completion)
self.tokens = self.router.tokens
Expand Down
10 changes: 5 additions & 5 deletions Sources/Imperial/Services/Imgur/ImgurRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import Foundation

public class ImgurRouter: FederatedServiceRouter {
public let tokens: FederatedServiceTokens
public let callbackCompletion: (Request, String)throws -> (Future<ResponseEncodable>)
public let callbackCompletion: (Request, String) async throws -> Response
public var scope: [String] = []
public var callbackURL: String
public let accessTokenURL: String = "https://api.imgur.com/oauth2/token"

public required init(callback: String, completion: @escaping (Request, String)throws -> (Future<ResponseEncodable>)) throws {
public required init(callback: String, completion: @escaping (Request, String) async throws -> Response) throws {
self.tokens = try ImgurAuth()
self.callbackURL = callback
self.callbackCompletion = completion
Expand All @@ -20,7 +20,7 @@ public class ImgurRouter: FederatedServiceRouter {
"response_type=code"
}

public func fetchToken(from request: Request)throws -> Future<String> {
public func fetchToken(from request: Request) async throws -> String {
let code: String
if let queryCode: String = try request.query.get(at: "code") {
code = queryCode
Expand Down Expand Up @@ -50,8 +50,8 @@ public class ImgurRouter: FederatedServiceRouter {
}
}

public func callback(_ request: Request)throws -> Future<Response> {
return try self.fetchToken(from: request).flatMap(to: ResponseEncodable.self) { accessToken in
public func callback(_ request: Request) async throws -> Response {
return try self.fetchToken(from: request).flatMap(to: Response.self) { accessToken in
let session = try request.session()

session.setAccessToken(accessToken)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Imperial/Services/Microsoft/Microsoft.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public class Microsoft: FederatedService {
public required init(
router: Router,
authenticate: String,
authenticateCallback: ((Request)throws -> (Future<Void>))?,
authenticateCallback: ((Request) async throws -> Void)?,
callback: String,
scope: [String] = [],
completion: @escaping (Request, String)throws -> (Future<ResponseEncodable>)
completion: @escaping (Request, String) async throws -> Response
) throws {
self.router = try MicrosoftRouter(callback: callback, completion: completion)
self.tokens = self.router.tokens
Expand Down
10 changes: 5 additions & 5 deletions Sources/Imperial/Services/Microsoft/MicrosoftRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ public class MicrosoftRouter: FederatedServiceRouter {
public static var tenantIDEnvKey: String = "MICROSOFT_TENANT_ID"

public let tokens: FederatedServiceTokens
public let callbackCompletion: (Request, String)throws -> (Future<ResponseEncodable>)
public let callbackCompletion: (Request, String) async throws -> Response
public var scope: [String] = []
public let callbackURL: String
public var tenantID: String { Environment.get(MicrosoftRouter.tenantIDEnvKey) ?? "common" }
public var accessTokenURL: String { "https://login.microsoftonline.com/\(self.tenantID)/oauth2/v2.0/token" }

public required init(
callback: String,
completion: @escaping (Request, String) throws -> (Future<ResponseEncodable>)
completion: @escaping (Request, String) assync throws -> Response
) throws {
self.tokens = try MicrosoftAuth()
self.callbackURL = callback
Expand All @@ -30,7 +30,7 @@ public class MicrosoftRouter: FederatedServiceRouter {
+ "prompt=consent"
}

public func fetchToken(from request: Request)throws -> Future<String> {
public func fetchToken(from request: Request) async throws -> String {
let code: String

if let queryCode: String = try request.query.get(at: "code") {
Expand Down Expand Up @@ -66,8 +66,8 @@ public class MicrosoftRouter: FederatedServiceRouter {
}
}

public func callback(_ request: Request)throws -> Future<Response> {
return try self.fetchToken(from: request).flatMap(to: ResponseEncodable.self) { accessToken in
public func callback(_ request: Request) async throws -> Response {
return try self.fetchToken(from: request).flatMap(to: Response.self) { accessToken in
let session = try request.session()

session.setAccessToken(accessToken)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Imperial/Services/Mixcloud/Mixcloud.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Mixcloud: FederatedService {
authenticateCallback: ((Request)throws -> (Future<Void>))?,
callback: String,
scope: [String] = [],
completion: @escaping (Request, String)throws -> (Future<ResponseEncodable>)
completion: @escaping (Request, String) async throws -> Response
) throws {
self.router = try MixcloudRouter(callback: callback, completion: completion)
self.tokens = self.router.tokens
Expand Down
8 changes: 4 additions & 4 deletions Sources/Imperial/Services/Mixcloud/MixcloudRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import Foundation

public class MixcloudRouter: FederatedServiceRouter {
public let tokens: FederatedServiceTokens
public let callbackCompletion: (Request, String)throws -> (Future<ResponseEncodable>)
public let callbackCompletion: (Request, String) async throws -> Response
public var scope: [String] = []
public var callbackURL: String
public let accessTokenURL: String = "https://www.mixcloud.com/oauth/access_token"

public required init(callback: String, completion: @escaping (Request, String)throws -> (Future<ResponseEncodable>)) throws {
public required init(callback: String, completion: @escaping (Request, String) async throws -> Response) throws {
self.tokens = try MixcloudAuth()
self.callbackURL = callback
self.callbackCompletion = completion
Expand All @@ -20,7 +20,7 @@ public class MixcloudRouter: FederatedServiceRouter {
"redirect_uri=\(self.callbackURL)"
}

public func fetchToken(from request: Request)throws -> Future<String> {
public func fetchToken(from request: Request) async throws -> String {
let code: String
if let queryCode: String = try request.query.get(at: "code") {
code = queryCode
Expand All @@ -40,7 +40,7 @@ public class MixcloudRouter: FederatedServiceRouter {
}
}

public func callback(_ request: Request)throws -> Future<Response> {
public func callback(_ request: Request) async throws -> Response {
return try self.fetchToken(from: request).flatMap(to: ResponseEncodable.self) { accessToken in
let session = try request.session()

Expand Down
10 changes: 5 additions & 5 deletions Sources/ImperialAuth0/Auth0.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ public class Auth0: FederatedService {
public required init(
routes: RoutesBuilder,
authenticate: String,
authenticateCallback: ((Request) throws -> (EventLoopFuture<Void>))?,
authenticateCallback: ((Request) async throws -> Void)?,
callback: String,
scope: [String] = [],
completion: @escaping (Request, String) throws -> (EventLoopFuture<ResponseEncodable>)
) throws {
self.router = try Auth0Router(callback: callback, completion: completion)
completion: @escaping (Request, String) async throws -> Response
) async throws {
self.router = try await Auth0Router(callback: callback, completion: completion)
self.tokens = self.router.tokens

self.router.scope = scope
try self.router.configureRoutes(withAuthURL: authenticate, authenticateCallback: authenticateCallback, on: routes)
try await self.router.configureRoutes(withAuthURL: authenticate, authenticateCallback: authenticateCallback, on: routes)

OAuthService.register(.auth0)
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/ImperialAuth0/Auth0Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class Auth0Router: FederatedServiceRouter {

public let baseURL: String
public let tokens: FederatedServiceTokens
public let callbackCompletion: (Request, String) throws -> (EventLoopFuture<ResponseEncodable>)
public let callbackCompletion: (Request, String) async throws -> Response
public var scope: [String] = [ ]
public var requiredScopes = [ "openid" ]
public let callbackURL: String
Expand All @@ -17,7 +17,7 @@ public class Auth0Router: FederatedServiceRouter {
return self.baseURL.finished(with: "/") + path
}

public required init(callback: String, completion: @escaping (Request, String) throws -> (EventLoopFuture<ResponseEncodable>)) throws {
public required init(callback: String, completion: @escaping (Request, String) async throws -> Response) async throws {
let auth = try Auth0Auth()
self.tokens = auth
self.baseURL = "https://\(auth.domain)"
Expand Down Expand Up @@ -45,7 +45,7 @@ public class Auth0Router: FederatedServiceRouter {
return rtn
}

public func callbackBody(with code: String) -> ResponseEncodable {
public func callbackBody(with code: String) -> any Content {
Auth0CallbackBody(clientId: self.tokens.clientID,
clientSecret: self.tokens.clientSecret,
code: code,
Expand Down
Loading
Loading