From 28b74b09235953629f2bc12b805ca0879cf28446 Mon Sep 17 00:00:00 2001 From: Simon Jespersen <6494049+simjes@users.noreply.github.com> Date: Sat, 18 Nov 2023 12:29:40 +0100 Subject: [PATCH] fix callbackURL in DiscordRouter * add required scope to DiscordRouter * remove unsued scope property in DiscordCallbackBody * add useful links to Discord Readme --- Sources/ImperialDiscord/DiscordCallbackBody.swift | 2 -- Sources/ImperialDiscord/DiscordRouter.swift | 13 ++++++------- docs/Discord/README.md | 6 ++++++ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Sources/ImperialDiscord/DiscordCallbackBody.swift b/Sources/ImperialDiscord/DiscordCallbackBody.swift index 91f4217..5f4b5a1 100644 --- a/Sources/ImperialDiscord/DiscordCallbackBody.swift +++ b/Sources/ImperialDiscord/DiscordCallbackBody.swift @@ -8,7 +8,6 @@ struct DiscordCallbackBody: Content { let grantType: String let code: String let redirectUri: String - let scope: String enum CodingKeys: String, CodingKey { case clientId = "client_id" @@ -16,6 +15,5 @@ struct DiscordCallbackBody: Content { case grantType = "grant_type" case code case redirectUri = "redirect_uri" - case scope } } diff --git a/Sources/ImperialDiscord/DiscordRouter.swift b/Sources/ImperialDiscord/DiscordRouter.swift index 6932a86..4fbe3e3 100644 --- a/Sources/ImperialDiscord/DiscordRouter.swift +++ b/Sources/ImperialDiscord/DiscordRouter.swift @@ -1,13 +1,13 @@ -import Vapor import Foundation +import Vapor public class DiscordRouter: FederatedServiceRouter { - public static var baseURL: String = "https://discord.com/" public static var callbackURL: String = "callback" public let tokens: FederatedServiceTokens public let callbackCompletion: (Request, String) throws -> (EventLoopFuture) public var scope: [String] = [] + public var requiredScopes = ["identify"] public let callbackURL: String public let accessTokenURL: String = "\(DiscordRouter.baseURL.finished(with: "/"))api/oauth2/token" public let service: OAuthService = .discord @@ -20,6 +20,7 @@ public class DiscordRouter: FederatedServiceRouter { } public func authURL(_ request: Request) throws -> String { + let allScopes = scope + requiredScopes var components = URLComponents() components.scheme = "https" @@ -27,9 +28,9 @@ public class DiscordRouter: FederatedServiceRouter { components.path = "/api/oauth2/authorize" components.queryItems = [ clientIDItem, - .init(name: "redirect_uri", value: DiscordRouter.callbackURL), + .init(name: "redirect_uri", value: callbackURL), .init(name: "response_type", value: "code"), - scopeItem + .init(name: "scope", value: allScopes.joined(separator: " ")) ] guard let url = components.url else { @@ -45,9 +46,7 @@ public class DiscordRouter: FederatedServiceRouter { clientSecret: tokens.clientSecret, grantType: "authorization_code", code: code, - redirectUri: DiscordRouter.callbackURL, - scope: scope.joined(separator: " ") + redirectUri: callbackURL ) } - } diff --git a/docs/Discord/README.md b/docs/Discord/README.md index 940311a..1817234 100644 --- a/docs/Discord/README.md +++ b/docs/Discord/README.md @@ -15,3 +15,9 @@ You can use Discord with the `ImperialDiscord` package. This expects two environ Additionally you must set `DiscordRouter.callbackURL` to an valid Redirect URL you added in the Developer Portal. You can then register the OAuth provider like normal. + +### References + +Some potentially useful references: + +* [Discord OAuth2 scopes](https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes)