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

Add mechanism to allow bot to authenticate and to get tokens on user's behalf #192

Merged
merged 6 commits into from
Jun 12, 2024

Conversation

johnpooch
Copy link
Collaborator

No description provided.

@johnpooch johnpooch requested a review from zond June 12, 2024 14:39
- The API is now available on your machine at `localhost:8080`
- The Admin server is now available on your machine at `localhost:8000`
- **Note** to get the Discord bot auth to work, you need to send a curl request
after the service is initialized: `curl -XPOST http://localhost:8080/_configure -d '{"DiscordBotCredentials": {"Username": "<username>", "Password": "<password>"}}'`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need both username and password? We won't have multiple bots with different purposes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not. I was just following Basic auth standard. I don't think it matters much either way

@@ -417,6 +461,88 @@ func getOAuth2Config(ctx context.Context, r *http.Request) (*oauth2.Config, erro
}, nil
}

func handleGetTokenForDiscordUser(w ResponseWriter, r Request) error {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just a convenience function for superusers, or?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what allows the bot to make requests on behalf of a user. Here's the flow

  • Bot gets command from user
  • Bot gets bot-token from Diplicity (using basic auth)
  • Bot gets user-token from Diplicity (using this endpoint - bot is a super user)
  • Bot makes subsequent requests on behalf of user by using user-token

Copy link
Owner

@zond zond Jun 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you use 3 steps (bot authenticates to become superuser, bot gets user token as superuser, bot acts as user) where you could use 2 steps (bot gets user token by using bot authentication, bot acts as user).

I mean, do other superusers need this 'act as user' power? Not so far, I think.

Also, I hope you cash the first 2 steps (or first step in my case) for a few minutes in the bot service, so you don't have to make 2 requests for every operation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think you're right. But maybe this is a little cleaner. Now the bot has a superuser token that it can use for other things in the future if needed. For example, when it's responding to an async event like the game ending for example, we might want it to then do something, but it doesn't have a user ID to get a token for, so it's nice that it can just operate as a user in its own right. For example, suppose we wanted the bot to delete a game when the game ends (unlikely).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will cache the responses later for sure. But for now I'm just fetching them again each time. Should be a straight forward optimization later.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the superuser access isn't something that's generally used, and it shouldn't be a part of business-as-usual. Currently it's only used when I find strange bugs in the database and need to create a new endpoint that fixes that, that should never be called by mistake or by anyone else.

@zond
Copy link
Owner

zond commented Jun 12, 2024

Looks good in general, I think.

@johnpooch johnpooch merged commit 35081c1 into master Jun 12, 2024
1 check failed
@johnpooch johnpooch deleted the johnpooch/bot-auth branch June 12, 2024 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants