Skip to content

Commit

Permalink
server/oauth: store usernames
Browse files Browse the repository at this point in the history
Store usernames for new signups, and update usernames on oauth refreshes
  • Loading branch information
zegl committed Feb 5, 2024
1 parent 0c74ec5 commit eb339d4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
33 changes: 33 additions & 0 deletions server/migrations/versions/2024-02-05_oauth_username.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""oauth.username
Revision ID: 57d1a318f8e5
Revises: bedd8fdf9f8c
Create Date: 2024-02-05 11:14:59.053973
"""
import sqlalchemy as sa
from alembic import op

# Polar Custom Imports
from polar.kit.extensions.sqlalchemy import PostgresUUID

# revision identifiers, used by Alembic.
revision = "57d1a318f8e5"
down_revision = "bedd8fdf9f8c"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"oauth_accounts",
sa.Column("account_username", sa.String(length=320), nullable=True),
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("oauth_accounts", "account_username")
# ### end Alembic commands ###
3 changes: 2 additions & 1 deletion server/polar/integrations/discord/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ async def discord_user_callback(
try:
await discord_user_service.create_oauth_account(session, auth.user, data)
except ResourceAlreadyExists:
pass
existing = await discord_user_service.get_oauth_account(session, auth.user)
await discord_user_service.update_user_info(session, existing)

redirect_to = get_safe_return_url(state["return_to"])
return RedirectResponse(redirect_to, 303)
Expand Down
21 changes: 21 additions & 0 deletions server/polar/integrations/discord/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ async def create_oauth_account(

account_id = data["id"]
account_email = data["email"]
account_username = data["username"]

oauth_account = OAuthAccount(
platform=OAuthPlatform.discord,
Expand All @@ -54,6 +55,7 @@ async def create_oauth_account(
refresh_token=oauth2_token_data["refresh_token"],
account_id=account_id,
account_email=account_email,
account_username=account_username,
user=user,
)
session.add(oauth_account)
Expand All @@ -68,6 +70,25 @@ async def create_oauth_account(

return oauth_account

async def update_user_info(
self,
session: AsyncSession,
oauth_account: OAuthAccount,
) -> OAuthAccount:
client = DiscordClient("Bearer", oauth_account.access_token)
data = await client.get_me()

account_email = data["email"]
account_username = data["username"]

oauth_account.account_email = account_email
oauth_account.account_username = account_username

await oauth_account.save(session)
await session.commit()

return oauth_account

async def get_oauth_account(
self, session: AsyncSession, user: User
) -> OAuthAccount:
Expand Down
4 changes: 4 additions & 0 deletions server/polar/integrations/github/service/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ async def signup(
refresh_token=tokens.refresh_token,
account_id=str(github_user.id),
account_email=email,
account_username=github_user.login,
)
],
)
Expand Down Expand Up @@ -169,13 +170,15 @@ async def login(
platform=OAuthPlatform.github,
account_id=str(github_user.id),
account_email=email,
account_username=github_user.login,
user=user,
)

oauth_account.access_token = tokens.access_token
oauth_account.expires_at = tokens.expires_at
oauth_account.refresh_token = tokens.refresh_token
oauth_account.account_email = email
oauth_account.account_username = github_user.login
await oauth_account.save(session)

log.info(
Expand Down Expand Up @@ -308,6 +311,7 @@ async def link_existing_user(
oauth_account.expires_at = tokens.expires_at
oauth_account.refresh_token = tokens.refresh_token
oauth_account.account_email = email
oauth_account.account_username = github_user.login
await oauth_account.save(session)

# Update User profile
Expand Down
5 changes: 5 additions & 0 deletions server/polar/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class OAuthAccount(RecordModel):
)
account_id: Mapped[str] = mapped_column(String(320), nullable=False)
account_email: Mapped[str] = mapped_column(String(320), nullable=False)

# Usernames are not always unique across a platform. Usernames on GitHub are unique.
# Discords usernames are not.
account_username: Mapped[str] = mapped_column(String(320), nullable=True)

user_id: Mapped[UUID] = mapped_column(
PostgresUUID,
ForeignKey("users.id", ondelete="cascade"),
Expand Down

0 comments on commit eb339d4

Please sign in to comment.