-
-
Notifications
You must be signed in to change notification settings - Fork 327
feat: Add support for exchanging oauth code for access token #780
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
05950c3
374cdf4
c56462c
7acda1d
b14d9bb
c0b5dad
ca9de41
70201d5
f3223da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| //! Authentication related types and functions. | ||
|
|
||
| use crate::models::AppId; | ||
| use crate::Result; | ||
| use crate::{models::AppId, Octocrab}; | ||
| use either::Either; | ||
| use jsonwebtoken::{Algorithm, EncodingKey, Header}; | ||
| use secrecy::{ExposeSecret, SecretString}; | ||
|
|
@@ -258,6 +258,58 @@ impl DeviceCodes { | |
| } | ||
| } | ||
|
|
||
| #[derive(serde::Serialize)] | ||
| pub struct ExchangeWebFlowCodeBuilder<'octo, 'client_id, 'code, 'client_secret, 'redirect_uri> { | ||
|
||
| #[serde(skip)] | ||
| crab: &'octo Octocrab, | ||
| client_id: &'client_id str, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| code: Option<&'code str>, | ||
| client_secret: &'client_secret str, | ||
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| redirect_uri: Option<&'redirect_uri str>, | ||
| } | ||
|
|
||
| impl<'octo, 'client_id, 'code, 'client_secret, 'redirect_uri> | ||
| ExchangeWebFlowCodeBuilder<'octo, 'client_id, 'code, 'client_secret, 'redirect_uri> | ||
| { | ||
| pub fn new( | ||
| crab: &'octo Octocrab, | ||
| client_id: &'client_id SecretString, | ||
| client_secret: &'client_secret SecretString, | ||
| ) -> Self { | ||
| Self { | ||
| crab, | ||
| client_id: client_id.expose_secret(), | ||
| code: None, | ||
| client_secret: client_secret.expose_secret(), | ||
| redirect_uri: None, | ||
| } | ||
| } | ||
|
|
||
| /// Set the `code` for exchange web flow code request to be created. | ||
| pub fn code(mut self, code: &'code str) -> Self { | ||
| self.code = Some(code); | ||
| self | ||
| } | ||
|
|
||
| /// Set the `redirect_uri` for exchange web flow code request to be created. | ||
| pub fn redirect_uri(mut self, redirect_uri: &'redirect_uri str) -> Self { | ||
| self.redirect_uri = Some(redirect_uri); | ||
| self | ||
| } | ||
|
|
||
| /// Sends the actual request. | ||
| /// Exchange a code for a user access token | ||
| /// | ||
| /// see: https://docs.github.com/en/developers/apps/identifying-and-authorizing-users-for-github-apps | ||
| /// | ||
| pub async fn send(self) -> crate::Result<OAuth> { | ||
| let route = "/login/oauth/access_token"; | ||
| self.crab.post(route, Some(&self)).await | ||
| } | ||
| } | ||
|
|
||
| /// See https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps#input-parameters | ||
| #[derive(Serialize)] | ||
| struct DeviceFlow<'a> { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| mod mock_error; | ||
|
|
||
| use mock_error::setup_error_handler; | ||
| use octocrab::{ | ||
| auth::{self, ExchangeWebFlowCodeBuilder}, | ||
| Octocrab, | ||
| }; | ||
| use secrecy::SecretString; | ||
| use serde_json::json; | ||
| use wiremock::{ | ||
| matchers::{method, path}, | ||
| Mock, MockServer, ResponseTemplate, | ||
| }; | ||
|
|
||
| async fn setup_post_api(template: ResponseTemplate) -> MockServer { | ||
| let mock_server = MockServer::start().await; | ||
|
|
||
| Mock::given(method("POST")) | ||
| .and(path(format!("/login/oauth/access_token"))) | ||
| .respond_with(template.clone()) | ||
| .mount(&mock_server) | ||
| .await; | ||
|
|
||
| setup_error_handler( | ||
| &mock_server, | ||
| &format!("POST on /login/oauth/access_token was not received"), | ||
| ) | ||
| .await; | ||
| mock_server | ||
| } | ||
|
|
||
| fn setup_octocrab(uri: &str) -> Octocrab { | ||
| Octocrab::builder().base_uri(uri).unwrap().build().unwrap() | ||
| } | ||
|
|
||
| const CLIENT_SECRET: &str = "some_secret"; | ||
| const CLIENT_ID: &str = "some_client_id"; | ||
| const CODE: &str = "a_code"; | ||
| const REDIRECT_URI: &str = "https://yourapp/auth/callback-example"; | ||
|
|
||
| #[tokio::test] | ||
| async fn should_return_oauth_response() { | ||
| let expected_response = json!({ | ||
| "access_token":"gho_16C7e42F292c6912E7710c838347Ae178B4a", | ||
| "scope":"repo,gist", | ||
| "token_type":"bearer" | ||
| } | ||
| ); | ||
| let template = ResponseTemplate::new(201).set_body_json(expected_response); | ||
| let mock_server = setup_post_api(template).await; | ||
| let client = setup_octocrab(&mock_server.uri()); | ||
|
|
||
| let result = auth::ExchangeWebFlowCodeBuilder::new( | ||
| &client, | ||
| &SecretString::from(CLIENT_ID), | ||
| &SecretString::from(CLIENT_SECRET), | ||
| ) | ||
| .code(CODE) | ||
| .redirect_uri(REDIRECT_URI.to_owned()) | ||
| .send() | ||
| .await; | ||
|
|
||
| assert!(result.is_ok()); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn should_fail_when_receving_a_server_error() { | ||
| let template = ResponseTemplate::new(500); | ||
| let mock_server = setup_post_api(template).await; | ||
| let client = setup_octocrab(&mock_server.uri()); | ||
|
|
||
| let result = auth::ExchangeWebFlowCodeBuilder::new( | ||
| &client, | ||
| &SecretString::from(CLIENT_ID), | ||
| &SecretString::from(CLIENT_SECRET), | ||
| ) | ||
| .send() | ||
| .await; | ||
|
|
||
| assert!(result.is_err()); | ||
| } |
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.
Similar to DeviceFlow below, leave a comment linking to the input parameters. https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/authorizing-oauth-apps#2-users-are-redirected-back-to-your-site-by-github is the one I found
Edit: or maybe https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app#using-the-web-application-flow-to-generate-a-user-access-token
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.
Added the second one, thanks!