Skip to content

Commit

Permalink
Add formatting and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Nov 20, 2024
1 parent f1fef48 commit c17faff
Show file tree
Hide file tree
Showing 65 changed files with 657 additions and 509 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ on:
jobs:
unit-tests:
uses: vapor/ci/.github/workflows/run-unit-tests.yml@main
with:
with_linting: true
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
70 changes: 70 additions & 0 deletions .swift-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"fileScopedDeclarationPrivacy": {
"accessLevel": "private"
},
"indentation": {
"spaces": 4
},
"indentConditionalCompilationBlocks": true,
"indentSwitchCaseLabels": false,
"lineBreakAroundMultilineExpressionChainComponents": false,
"lineBreakBeforeControlFlowKeywords": false,
"lineBreakBeforeEachArgument": false,
"lineBreakBeforeEachGenericRequirement": false,
"lineLength": 140,
"maximumBlankLines": 1,
"multiElementCollectionTrailingCommas": true,
"noAssignmentInExpressions": {
"allowedFunctions": [
"XCTAssertNoThrow"
]
},
"prioritizeKeepingFunctionOutputTogether": false,
"respectsExistingLineBreaks": true,
"rules": {
"AllPublicDeclarationsHaveDocumentation": false,
"AlwaysUseLiteralForEmptyCollectionInit": false,
"AlwaysUseLowerCamelCase": true,
"AmbiguousTrailingClosureOverload": true,
"BeginDocumentationCommentWithOneLineSummary": false,
"DoNotUseSemicolons": true,
"DontRepeatTypeInStaticProperties": true,
"FileScopedDeclarationPrivacy": true,
"FullyIndirectEnum": true,
"GroupNumericLiterals": true,
"IdentifiersMustBeASCII": true,
"NeverForceUnwrap": false,
"NeverUseForceTry": false,
"NeverUseImplicitlyUnwrappedOptionals": false,
"NoAccessLevelOnExtensionDeclaration": true,
"NoAssignmentInExpressions": true,
"NoBlockComments": true,
"NoCasesWithOnlyFallthrough": true,
"NoEmptyTrailingClosureParentheses": true,
"NoLabelsInCasePatterns": true,
"NoLeadingUnderscores": false,
"NoParensAroundConditions": true,
"NoPlaygroundLiterals": true,
"NoVoidReturnOnFunctionSignature": true,
"OmitExplicitReturns": false,
"OneCasePerLine": true,
"OneVariableDeclarationPerLine": true,
"OnlyOneTrailingClosureArgument": true,
"OrderedImports": true,
"ReplaceForEachWithForLoop": true,
"ReturnVoidInsteadOfEmptyTuple": true,
"TypeNamesShouldBeCapitalized": true,
"UseEarlyExits": false,
"UseExplicitNilCheckInConditions": true,
"UseLetInEveryBoundCaseVariable": true,
"UseShorthandTypeNames": true,
"UseSingleLinePropertyGetter": true,
"UseSynthesizedInitializer": true,
"UseTripleSlashForDocumentationComments": true,
"UseWhereClausesInForLoops": false,
"ValidateDocumentationComments": false
},
"spacesAroundRangeFormationOperators": false,
"tabWidth": 8,
"version": 1
}
36 changes: 19 additions & 17 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PackageDescription
let package = Package(
name: "Imperial",
platforms: [
.macOS(.v13)
.macOS(.v13)
],
products: [
.library(name: "ImperialCore", targets: ["ImperialCore"]),
Expand All @@ -18,23 +18,25 @@ let package = Package(
.library(name: "ImperialKeycloak", targets: ["ImperialCore", "ImperialKeycloak"]),
.library(name: "ImperialMicrosoft", targets: ["ImperialCore", "ImperialMicrosoft"]),
.library(name: "ImperialShopify", targets: ["ImperialCore", "ImperialShopify"]),
.library(name: "Imperial", targets: [
"ImperialCore",
"ImperialAuth0",
"ImperialDiscord",
"ImperialDropbox",
"ImperialFacebook",
"ImperialGitHub",
"ImperialGitlab",
"ImperialGoogle",
"ImperialKeycloak",
"ImperialMicrosoft",
"ImperialShopify"
]),
.library(
name: "Imperial",
targets: [
"ImperialCore",
"ImperialAuth0",
"ImperialDiscord",
"ImperialDropbox",
"ImperialFacebook",
"ImperialGitHub",
"ImperialGitlab",
"ImperialGoogle",
"ImperialKeycloak",
"ImperialMicrosoft",
"ImperialShopify",
]),
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),
.package(url: "https://github.com/vapor/jwt-kit.git", from: "5.0.0")
.package(url: "https://github.com/vapor/jwt-kit.git", from: "5.0.0"),
],
targets: [
.target(
Expand Down Expand Up @@ -78,6 +80,6 @@ let package = Package(

var swiftSettings: [SwiftSetting] {
[
.enableUpcomingFeature("ExistentialAny"),
.enableUpcomingFeature("ExistentialAny")
]
}
}
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) throws -> (Future<Void>))?,
callback: String,
scope: [String] = [],
completion: @escaping (Request, String)throws -> (Future<ResponseEncodable>)
completion: @escaping (Request, String) throws -> (Future<ResponseEncodable>)
) throws {
self.router = try DeviantArtRouter(callback: callback, completion: completion)
self.tokens = self.router.tokens
Expand Down
31 changes: 15 additions & 16 deletions Sources/Imperial/Services/DeviantArt/DeviantArtRouter.swift
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
import Vapor
import Foundation
import Vapor

public class DeviantArtRouter: FederatedServiceRouter {
public let tokens: FederatedServiceTokens
public let callbackCompletion: (Request, String)throws -> (Future<ResponseEncodable>)
public let callbackCompletion: (Request, String) throws -> (Future<ResponseEncodable>)
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) throws -> (Future<ResponseEncodable>)) throws {
self.tokens = try DeviantArtAuth()
self.callbackURL = callback
self.callbackCompletion = completion
}

public func authURL(_ request: Request) throws -> String {
let scope : String
let scope: String
if self.scope.count > 0 {
scope = "scope="+self.scope.joined(separator: " ")+"&"
scope = "scope=" + self.scope.joined(separator: " ") + "&"
} else {
scope = ""
}
return "https://www.deviantart.com/oauth2/authorize?" +
"client_id=\(self.tokens.clientID)&" +
"redirect_uri=\(self.callbackURL)&\(scope)" +
"response_type=code"
return "https://www.deviantart.com/oauth2/authorize?" + "client_id=\(self.tokens.clientID)&"
+ "redirect_uri=\(self.callbackURL)&\(scope)" + "response_type=code"
}

public func fetchToken(from request: Request)throws -> Future<String> {
public func fetchToken(from request: Request) throws -> Future<String> {
let code: String
if let queryCode: String = try request.query.get(at: "code") {
code = queryCode
Expand All @@ -37,7 +35,8 @@ public class DeviantArtRouter: FederatedServiceRouter {
throw Abort(.badRequest, reason: "Missing 'code' key in URL query")
}

let body = DeviantArtCallbackBody(code: code, clientId: self.tokens.clientID, clientSecret: self.tokens.clientSecret, redirectURI: self.callbackURL)
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")
Expand All @@ -49,15 +48,15 @@ public class DeviantArtRouter: FederatedServiceRouter {
let session = try request.session()

return response.content.get(String.self, at: ["refresh_token"])
.flatMap { refresh in
session.setRefreshToken(refresh)
.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> {
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()

Expand Down
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) throws -> (Future<Void>))?,
callback: String,
scope: [String] = [],
completion: @escaping (Request, String)throws -> (Future<ResponseEncodable>)
completion: @escaping (Request, String) throws -> (Future<ResponseEncodable>)
) throws {
self.router = try ImgurRouter(callback: callback, completion: completion)
self.tokens = self.router.tokens
Expand Down
22 changes: 10 additions & 12 deletions Sources/Imperial/Services/Imgur/ImgurRouter.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import Vapor
import Foundation
import Vapor

public class ImgurRouter: FederatedServiceRouter {
public let tokens: FederatedServiceTokens
public let callbackCompletion: (Request, String)throws -> (Future<ResponseEncodable>)
public let callbackCompletion: (Request, String) throws -> (Future<ResponseEncodable>)
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) throws -> (Future<ResponseEncodable>)) throws {
self.tokens = try ImgurAuth()
self.callbackURL = callback
self.callbackCompletion = completion
}

public func authURL(_ request: Request) throws -> String {
return "https://api.imgur.com/oauth2/authorize?" +
"client_id=\(self.tokens.clientID)&" +
"response_type=code"
return "https://api.imgur.com/oauth2/authorize?" + "client_id=\(self.tokens.clientID)&" + "response_type=code"
}

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

return response.content.get(String.self, at: ["refresh_token"])
.flatMap { refresh in
session.setRefreshToken(refresh)
.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> {
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()

Expand Down
4 changes: 2 additions & 2 deletions Sources/Imperial/Services/Mixcloud/Mixcloud.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public class Mixcloud: FederatedService {
public required init(
router: Router,
authenticate: String,
authenticateCallback: ((Request)throws -> (Future<Void>))?,
authenticateCallback: ((Request) throws -> (Future<Void>))?,
callback: String,
scope: [String] = [],
completion: @escaping (Request, String)throws -> (Future<ResponseEncodable>)
completion: @escaping (Request, String) throws -> (Future<ResponseEncodable>)
) throws {
self.router = try MixcloudRouter(callback: callback, completion: completion)
self.tokens = self.router.tokens
Expand Down
32 changes: 16 additions & 16 deletions Sources/Imperial/Services/Mixcloud/MixcloudRouter.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import Vapor
import Foundation
import Vapor

public class MixcloudRouter: FederatedServiceRouter {
public let tokens: FederatedServiceTokens
public let callbackCompletion: (Request, String)throws -> (Future<ResponseEncodable>)
public let callbackCompletion: (Request, String) throws -> (Future<ResponseEncodable>)
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) throws -> (Future<ResponseEncodable>)) throws {
self.tokens = try MixcloudAuth()
self.callbackURL = callback
self.callbackCompletion = completion
}

public func authURL(_ request: Request) throws -> String {
return "https://www.mixcloud.com/oauth/authorize?" +
"client_id=\(self.tokens.clientID)&" +
"redirect_uri=\(self.callbackURL)"
return "https://www.mixcloud.com/oauth/authorize?" + "client_id=\(self.tokens.clientID)&" + "redirect_uri=\(self.callbackURL)"
}

public func fetchToken(from request: Request)throws -> Future<String> {
public func fetchToken(from request: Request) throws -> Future<String> {
let code: String
if let queryCode: String = try request.query.get(at: "code") {
code = queryCode
Expand All @@ -30,17 +28,19 @@ public class MixcloudRouter: FederatedServiceRouter {
throw Abort(.badRequest, reason: "Missing 'code' key in URL query")
}

let body = MixcloudCallbackBody(code: code, clientId: self.tokens.clientID, clientSecret: self.tokens.clientSecret, redirectURI: self.callbackURL)
return try request
.client()
.get(self.accessTokenURL) { request in
try request.query.encode(body)
}.flatMap(to: String.self) { response in
return response.content.get(String.self, at: ["access_token"])
}
let body = MixcloudCallbackBody(
code: code, clientId: self.tokens.clientID, clientSecret: self.tokens.clientSecret, redirectURI: self.callbackURL)
return
try request
.client()
.get(self.accessTokenURL) { request in
try request.query.encode(body)
}.flatMap(to: String.self) { response in
return response.content.get(String.self, at: ["access_token"])
}
}

public func callback(_ request: Request)throws -> Future<Response> {
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()

Expand Down
6 changes: 3 additions & 3 deletions Sources/ImperialAuth0/Auth0.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Vapor
public class Auth0: FederatedService {
public var tokens: any FederatedServiceTokens
public var router: any FederatedServiceRouter

@discardableResult
public required init(
routes: some RoutesBuilder,
Expand All @@ -16,9 +16,9 @@ public class Auth0: FederatedService {
) throws {
self.router = try Auth0Router(callback: callback, scope: scope, completion: completion)
self.tokens = self.router.tokens

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

OAuthService.services[OAuthService.auth0.name] = .auth0
}
}
2 changes: 1 addition & 1 deletion Sources/ImperialAuth0/Auth0Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ final public class Auth0Auth: FederatedServiceTokens {
public let domain: String
public let clientID: String
public let clientSecret: String

public required init() throws {
guard let domain = Environment.get(Auth0Auth.domain) else {
throw ImperialError.missingEnvVar(Auth0Auth.domain)
Expand Down
Loading

0 comments on commit c17faff

Please sign in to comment.