-
Notifications
You must be signed in to change notification settings - Fork 712
Genericize OAuth profiles #13477
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
Draft
quinnjr
wants to merge
6
commits into
rust-lang:main
Choose a base branch
from
quinnjr:feature/genericize-profiles
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Genericize OAuth profiles #13477
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cc22b48
refactor(oauth): introduce OAuthProvider trait and GitHubProvider impl
quinnjr d72832b
feat(oauth): add OAuth provider registry
quinnjr 39f40c3
refactor(oauth): route session login through OAuthProvider
quinnjr 3a352d8
test(oauth): add Docker integration test infrastructure
quinnjr 910efad
feat(oauth): add oauth_provider enum type
quinnjr 30855bb
feat(oauth): add primary_oauth_provider column to users
quinnjr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,3 +36,4 @@ src/schema.rs.orig | |
| /blob-report/ | ||
| /playwright/.cache/ | ||
|
|
||
| docs/superpowers/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # Image for running the crates.io server in integration tests. | ||
| # Debug build for fast compilation and full debug output. | ||
|
|
||
| ARG RUST_VERSION=1.94.1 | ||
|
|
||
| FROM rust:${RUST_VERSION} | ||
|
|
||
| RUN cargo install diesel_cli --version 2.3.7 --no-default-features --features postgres | ||
|
|
||
| WORKDIR /app | ||
| COPY . /app | ||
| RUN cargo build --bin server | ||
|
|
||
| RUN cp target/debug/server /usr/local/bin/crates-io-server | ||
|
|
||
| EXPOSE 8888 | ||
|
|
||
| RUN cat > /diesel.toml << 'TOML' | ||
| [print_schema] | ||
| file = "/dev/null" | ||
| TOML | ||
|
|
||
| RUN cat > /entrypoint.sh << 'EOF' | ||
| #!/bin/sh | ||
| set -e | ||
|
|
||
| # Use a minimal diesel config that skips schema regeneration -- | ||
| # the schema.rs is already baked into the binary at build time. | ||
| export DIESEL_CONFIG_FILE=/diesel.toml | ||
|
|
||
| until diesel migration run 2>&1; do | ||
| echo "waiting for postgres..." >&2 | ||
| sleep 2 | ||
| done | ||
|
|
||
| ./script/init-local-index.sh 2>/dev/null || true | ||
|
|
||
| exec crates-io-server | ||
| EOF | ||
|
|
||
| RUN chmod +x /entrypoint.sh | ||
| ENTRYPOINT ["/entrypoint.sh"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| use std::io::Write; | ||
| use std::str::FromStr; | ||
|
|
||
| use diesel::deserialize::{self, FromSql}; | ||
| use diesel::pg::{Pg, PgValue}; | ||
| use diesel::query_builder::QueryId; | ||
| use diesel::serialize::{self, IsNull, Output, ToSql}; | ||
|
|
||
| use crate::schema::sql_types::OauthProvider as OauthProviderSql; | ||
|
|
||
| // Diesel's `#[derive(SqlType)]` does not emit `QueryId`. Binding an | ||
| // `OAuthProviderId` value into a query path requires it, so we implement it | ||
| // here rather than patching generated schema.rs. | ||
| impl QueryId for OauthProviderSql { | ||
| type QueryId = OauthProviderSql; | ||
| const HAS_STATIC_QUERY_ID: bool = true; | ||
| } | ||
|
|
||
| /// Identifier for an OAuth provider that a `User` can be associated with. | ||
| /// | ||
| /// Maps to the `oauth_provider` Postgres enum type. The `OAuthProvider` | ||
| /// trait in the main crate represents provider *behavior*; this enum | ||
| /// represents provider *identity* (which provider a row refers to). | ||
| #[derive( | ||
| Debug, | ||
| Copy, | ||
| Clone, | ||
| PartialEq, | ||
| Eq, | ||
| Hash, | ||
| serde::Serialize, | ||
| diesel::FromSqlRow, | ||
| diesel::AsExpression, | ||
| )] | ||
| #[diesel(sql_type = OauthProviderSql)] | ||
| #[serde(rename_all = "snake_case")] | ||
| pub enum OAuthProviderId { | ||
| Github, | ||
| } | ||
|
|
||
| impl OAuthProviderId { | ||
| pub fn as_str(&self) -> &'static str { | ||
| match self { | ||
| OAuthProviderId::Github => "github", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl FromStr for OAuthProviderId { | ||
| type Err = UnknownOAuthProvider; | ||
|
|
||
| fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| match s { | ||
| "github" => Ok(OAuthProviderId::Github), | ||
| other => Err(UnknownOAuthProvider(other.to_string())), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, thiserror::Error)] | ||
| #[error("unknown oauth provider: {0}")] | ||
| pub struct UnknownOAuthProvider(pub String); | ||
|
|
||
| impl FromSql<OauthProviderSql, Pg> for OAuthProviderId { | ||
| fn from_sql(bytes: PgValue<'_>) -> deserialize::Result<Self> { | ||
| let s = std::str::from_utf8(bytes.as_bytes())?; | ||
| Ok(s.parse()?) | ||
| } | ||
| } | ||
|
|
||
| impl ToSql<OauthProviderSql, Pg> for OAuthProviderId { | ||
| fn to_sql(&self, out: &mut Output<'_, '_, Pg>) -> serialize::Result { | ||
| out.write_all(self.as_str().as_bytes())?; | ||
| Ok(IsNull::No) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn as_str_roundtrips_through_from_str() { | ||
| let s = OAuthProviderId::Github.as_str(); | ||
| let parsed: OAuthProviderId = s.parse().expect("as_str output must parse back"); | ||
| assert_eq!(parsed, OAuthProviderId::Github); | ||
| } | ||
|
|
||
| #[test] | ||
| fn from_str_rejects_unknown_provider() { | ||
| let err = "gitlab" | ||
| .parse::<OAuthProviderId>() | ||
| .expect_err("unknown provider must fail"); | ||
| assert_eq!(err.0, "gitlab"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn serde_serializes_to_snake_case() { | ||
| let s = serde_json::to_string(&OAuthProviderId::Github).unwrap(); | ||
| assert_eq!(s, "\"github\""); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again I don't use docker, but this looks significantly different than the existing docker file at https://github.com/rust-lang/crates.io/blob/main/backend.Dockerfile, could you explain why this one is different?
View changes since the review