简体   繁体   English

如何在我的应用程序中使用 Discord OAuth2 进行用户管理和身份验证?

[英]How do I use Discord OAuth2 for user management and authentication in my application?

I'm building an application where I want to be able to create and authenticate users using Discord and OAuth2.我正在构建一个应用程序,我希望能够使用 Discord 和 OAuth2 创建和验证用户。 The reasons are:原因是:

  • The application can be considered a "companion" app to a Discord community I am running, and该应用程序可以被视为我正在运行的 Discord 社区的“伴侣”应用程序,并且
  • I don't want the users or myself to have to deal with usernames and passwords我不希望用户或我自己必须处理用户名和密码

The application consists of a client desktop application and backend services.该应用程序由客户端桌面应用程序和后端服务组成。 I have a fairly basic understanding on how I authorize the user with Discord:我对如何使用 Discord 授权用户有相当基本的了解:

  1. Client application goes to backend endpoint /oauth/login and the user is redirected to the Discord app approval page客户端应用程序转到后端端点/oauth/login并且用户被重定向到 Discord 应用程序批准页面
  2. The user confirms and is redirected to the backend callback /oauth/callback with a code that can be used to fetch a pair of access and refresh tokens.用户确认并被重定向到后端回调/oauth/callback ,其代码可用于获取一对访问和刷新令牌。

Frankly, from this point I am kind of stumped on how the rest of the authentication should work.坦率地说,从这一点来看,我对身份验证的 rest 应该如何工作感到困惑。 I assume at least the following:我至少假设以下几点:

  • I need to create a user entry in my database with at least an UID (for simplicity the same as the one for the user in Discord), the access and refresh token pair.我需要在我的数据库中创建一个用户条目,其中至少有一个 UID(为简单起见,与 Discord 中的用户相同)、访问和刷新令牌对。 If user is already created, update the database with the new tokens.如果已创建用户,请使用新令牌更新数据库。
  • Whenever the application needs user information from Discord it should use the access token.每当应用程序需要来自 Discord 的用户信息时,它应该使用访问令牌。 If it has expired, exchange the refresh token with Discord to get a new token pair.如果已过期,则将刷新令牌与 Discord 交换以获得新的令牌对。

But now what?但现在呢? This only authenticates the user against Discord.这仅针对 Discord 对用户进行身份验证。 I want to leverage the fact that the user is authenticated with Discord to be authenticated to my application.我想利用用户通过 Discord 进行身份验证这一事实对我的应用程序进行身份验证。 Here are some general questions I have:以下是我的一些一般性问题:

  • Do I make a new token for the user to use for subsequent requests to my backend endpoints?我是否为用户创建一个新令牌以用于对我的后端端点的后续请求? Or do I return the Discord access token to the desktop client?还是将 Discord 访问令牌返回给桌面客户端?
  • What do I do when the token expires?令牌过期了怎么办? Do I also need a "exchange" endpoint for the desktop client to refresh the token (possibly that just forwards to Discord to get a new token, depending on the answer to my previous question).我是否还需要桌面客户端的“交换”端点来刷新令牌(可能只是转发到 Discord 以获取新令牌,具体取决于我之前问题的答案)。

This all feels like it should be very basic, but I am out of my comfort zone here and need some help to be unblocked.这一切都感觉应该是非常基本的,但我在这里超出了我的舒适区,需要一些帮助才能畅通无阻。

Thanks for reading!谢谢阅读!

Your own application should effectively have its own session system.您自己的应用程序应该有效地拥有自己的 session 系统。

The easiest is likely to just use HttpOnly cookie-based sessions, which something like a Redis store (or Memory store if this is a toy project).最简单的可能只是使用基于 HttpOnly cookie 的会话,类似于 Redis 存储(如果这是一个玩具项目,则为 Memory 存储)。

The session data on the server should contain information on which user is currently logged in. You should probably store the discord access and refresh token in a database.服务器上的 session 数据应包含有关当前登录的用户的信息。您可能应该将 discord 访问和刷新令牌存储在数据库中。

The simplest way to deal with refreshing, is to simply call their refresh token endpoint as soon as you get a 401 response.处理刷新的最简单方法是在收到401响应后立即调用其刷新令牌端点。 If discord provides information on how long access tokens are valid, you could also preemptively refresh instead of only doing this when you get the 401 .如果 discord 提供有关访问令牌有效时间的信息,您也可以抢先刷新而不是仅在获得401时才这样做。 Your server does the refreshing, you don't need an endpoint for this.您的服务器进行刷新,您不需要端点。

Generally I can recommend that your server handles all interactions with the discord API, and never your client.一般来说,我建议您的服务器处理与 discord API 的所有交互,而不是您的客户端。 (aside from the initial authorization step). (除了初始授权步骤)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM