From 7b86214e496978db8d6a18858c613fd13eff5e31 Mon Sep 17 00:00:00 2001 From: Francesco Paolo Severino Date: Sun, 10 Nov 2024 17:31:29 +0100 Subject: [PATCH] Remove old Microsoft service --- .../Services/Microsoft/Microsoft.swift | 24 ------ .../Services/Microsoft/MicrosoftAuth.swift | 16 ---- .../Microsoft/MicrosoftCallbackBody.swift | 21 ----- .../Services/Microsoft/MicrosoftRouter.swift | 81 ------------------- .../Microsoft/Service+Microsoft.swift | 6 -- 5 files changed, 148 deletions(-) delete mode 100644 Sources/Imperial/Services/Microsoft/Microsoft.swift delete mode 100644 Sources/Imperial/Services/Microsoft/MicrosoftAuth.swift delete mode 100644 Sources/Imperial/Services/Microsoft/MicrosoftCallbackBody.swift delete mode 100644 Sources/Imperial/Services/Microsoft/MicrosoftRouter.swift delete mode 100644 Sources/Imperial/Services/Microsoft/Service+Microsoft.swift diff --git a/Sources/Imperial/Services/Microsoft/Microsoft.swift b/Sources/Imperial/Services/Microsoft/Microsoft.swift deleted file mode 100644 index 2279eaa2..00000000 --- a/Sources/Imperial/Services/Microsoft/Microsoft.swift +++ /dev/null @@ -1,24 +0,0 @@ -import Vapor - -public class Microsoft: FederatedService { - public var tokens: FederatedServiceTokens - public var router: FederatedServiceRouter - - @discardableResult - public required init( - router: Router, - authenticate: String, - authenticateCallback: ((Request)throws -> (Future))?, - callback: String, - scope: [String] = [], - completion: @escaping (Request, String)throws -> (Future) - ) throws { - self.router = try MicrosoftRouter(callback: callback, completion: completion) - self.tokens = self.router.tokens - - self.router.scope = scope - try self.router.configureRoutes(withAuthURL: authenticate, authenticateCallback: authenticateCallback, on: router) - - OAuthService.register(.microsoft) - } -} diff --git a/Sources/Imperial/Services/Microsoft/MicrosoftAuth.swift b/Sources/Imperial/Services/Microsoft/MicrosoftAuth.swift deleted file mode 100644 index 45d41b0c..00000000 --- a/Sources/Imperial/Services/Microsoft/MicrosoftAuth.swift +++ /dev/null @@ -1,16 +0,0 @@ -import Vapor - -public class MicrosoftAuth: FederatedServiceTokens { - public static var idEnvKey: String = "MICROSOFT_CLIENT_ID" - public static var secretEnvKey: String = "MICROSOFT_CLIENT_SECRET" - public var clientID: String - public var clientSecret: String - - public required init() throws { - let idError = ImperialError.missingEnvVar(MicrosoftAuth.idEnvKey) - let secretError = ImperialError.missingEnvVar(MicrosoftAuth.secretEnvKey) - - self.clientID = try Environment.get(MicrosoftAuth.idEnvKey).value(or: idError) - self.clientSecret = try Environment.get(MicrosoftAuth.secretEnvKey).value(or: secretError) - } -} diff --git a/Sources/Imperial/Services/Microsoft/MicrosoftCallbackBody.swift b/Sources/Imperial/Services/Microsoft/MicrosoftCallbackBody.swift deleted file mode 100644 index 4b6ec696..00000000 --- a/Sources/Imperial/Services/Microsoft/MicrosoftCallbackBody.swift +++ /dev/null @@ -1,21 +0,0 @@ -import Vapor - -struct MicrosoftCallbackBody: Content { - let code: String - let clientId: String - let clientSecret: String - let redirectURI: String - let scope: String - let grantType: String = "authorization_code" - - static var defaultContentType: MediaType = .urlEncodedForm - - enum CodingKeys: String, CodingKey { - case code = "code" - case clientId = "client_id" - case clientSecret = "client_secret" - case redirectURI = "redirect_uri" - case grantType = "grant_type" - case scope = "scope" - } -} diff --git a/Sources/Imperial/Services/Microsoft/MicrosoftRouter.swift b/Sources/Imperial/Services/Microsoft/MicrosoftRouter.swift deleted file mode 100644 index 2e6ec451..00000000 --- a/Sources/Imperial/Services/Microsoft/MicrosoftRouter.swift +++ /dev/null @@ -1,81 +0,0 @@ -import Vapor -import Foundation - -public class MicrosoftRouter: FederatedServiceRouter { - public static var tenantIDEnvKey: String = "MICROSOFT_TENANT_ID" - - public let tokens: FederatedServiceTokens - public let callbackCompletion: (Request, String)throws -> (Future) - 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) - ) throws { - self.tokens = try MicrosoftAuth() - self.callbackURL = callback - self.callbackCompletion = completion - } - - public func authURL(_ request: Request) throws -> String { - return "https://login.microsoftonline.com/\(self.tenantID)/oauth2/v2.0/authorize?" - + "client_id=\(self.tokens.clientID)&" - + "response_type=code&" - + "redirect_uri=\(self.callbackURL)&" - + "response_mode=query&" - + "scope=\(scope.joined(separator: "%20"))&" - + "prompt=consent" - } - - public func fetchToken(from request: Request)throws -> Future { - let code: String - - if let queryCode: String = try request.query.get(at: "code") { - code = queryCode - } else if let error: String = try request.query.get(at: "error_description") { - throw Abort(.badRequest, reason: error) - } else { - throw Abort(.badRequest, reason: "Missing 'code' key in URL query") - } - - let body = MicrosoftCallbackBody( - code: code, - clientId: self.tokens.clientID, - clientSecret: self.tokens.clientSecret, - redirectURI: self.callbackURL, - scope: scope.joined(separator: "%20") - ) - - 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 - return response.content.get(String.self, at: ["access_token"]) - } - } - - public func callback(_ request: Request)throws -> Future { - return try self.fetchToken(from: request).flatMap(to: ResponseEncodable.self) { accessToken in - let session = try request.session() - - session.setAccessToken(accessToken) - try session.set("access_token_service", to: OAuthService.microsoft) - - return try self.callbackCompletion(request, accessToken) - }.flatMap(to: Response.self) { response in - return try response.encode(for: request) - } - } -} diff --git a/Sources/Imperial/Services/Microsoft/Service+Microsoft.swift b/Sources/Imperial/Services/Microsoft/Service+Microsoft.swift deleted file mode 100644 index 9341deef..00000000 --- a/Sources/Imperial/Services/Microsoft/Service+Microsoft.swift +++ /dev/null @@ -1,6 +0,0 @@ -extension OAuthService { - public static let microsoft = OAuthService.init( - name: "microsoft", - endpoints: [:] - ) -}