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

fix callbackURL in DiscordRouter #95

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
2 changes: 0 additions & 2 deletions Sources/ImperialDiscord/DiscordCallbackBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ struct DiscordCallbackBody: Content {
let grantType: String
let code: String
let redirectUri: String
let scope: String

enum CodingKeys: String, CodingKey {
case clientId = "client_id"
case clientSecret = "client_secret"
case grantType = "grant_type"
case code
case redirectUri = "redirect_uri"
case scope
}
}
13 changes: 6 additions & 7 deletions Sources/ImperialDiscord/DiscordRouter.swift
Original file line number Diff line number Diff line change
@@ -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<ResponseEncodable>)
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
Expand All @@ -20,16 +20,17 @@ public class DiscordRouter: FederatedServiceRouter {
}

public func authURL(_ request: Request) throws -> String {
let allScopes = scope + requiredScopes

var components = URLComponents()
components.scheme = "https"
components.host = "discord.com"
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 {
Expand All @@ -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
)
}

}
6 changes: 6 additions & 0 deletions docs/Discord/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)