Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mattiapitossi
Copy link

closes #638

/// 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 get_access_token(
Copy link
Owner

Choose a reason for hiding this comment

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

Is there a reason this is a free function as opposed to a impl method?

Copy link
Author

Choose a reason for hiding this comment

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

Hi @XAMPPRocky, TBH I didn't see any value in adding an additional structure as it was done for the device flow. In that case, it's useful as the params are received from another REST API. But if you see any case in which I'm not considering I can change this to be a method

Copy link
Owner

Choose a reason for hiding this comment

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

How does this look in octokit.js? We try to follow similar conventions when possible.

Copy link
Author

@mattiapitossi mattiapitossi Jun 29, 2025

Choose a reason for hiding this comment

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

Ok, it seems that in octokit is defined as a struct:

export type ExchangeWebFlowCodeGitHubAppOptions = {
  clientType: "github-app";
  clientId: string;
  clientSecret: string;
  code: string;
  redirectUrl?: string;
  request?: RequestInterface;
};

https://github.com/octokit/oauth-methods.js/blob/main/src/exchange-web-flow-code.ts

Also the redirectUri seems to be opt.

I'll update the MR and replicate this behavior

Copy link
Owner

Choose a reason for hiding this comment

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

Sorry I meant how does it look to get the access token? Like what does the actual method call look like if you used octokit?

Copy link
Author

@mattiapitossi mattiapitossi Jun 30, 2025

Choose a reason for hiding this comment

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

There are two options (GitHub App is the recommended one):

export async function exchangeWebFlowCode(
  options: ExchangeWebFlowCodeOAuthAppOptions,
): Promise<ExchangeWebFlowCodeOAuthAppResponse>;

export async function exchangeWebFlowCode(
  options: ExchangeWebFlowCodeGitHubAppOptions,
): Promise<ExchangeWebFlowCodeGitHubAppResponse>;

export async function exchangeWebFlowCode(
  options:
    | ExchangeWebFlowCodeOAuthAppOptions
    | ExchangeWebFlowCodeGitHubAppOptions,
): Promise<any> {..}

https://github.com/octokit/oauth-methods.js/blob/main/src/exchange-web-flow-code.ts

Copy link
Contributor

Choose a reason for hiding this comment

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

My two cents - the new method should follow the pattern of other post methods and return a Builder to set optional parameters, e.g. redirect_uri. This allows the API to evolve forward without breaking existing usages and is pleasant to use with optional parameters. https://docs.rs/octocrab/latest/octocrab/repos/releases/struct.CreateReleaseBuilder.html, for example

@mattiapitossi mattiapitossi requested a review from XAMPPRocky June 28, 2025 09:02
Comment on lines +338 to +339
// Strongly recommended. The URL in your application where users will be sent after authorization.
redirect_uri: &'a str,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think redirect_uri is only strongly recommended when directing the user to https://github.com/login/oauth/access_token to login. When calling https://github.com/login/oauth/access_token, redirect_uri is unused afaict and the documentation doesn't mention strongly recommended.

I suggest making this parameter optional.

https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app#generating-a-user-access-token-when-a-user-installs-your-app

/// 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 get_access_token(
Copy link
Contributor

Choose a reason for hiding this comment

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

My two cents - the new method should follow the pattern of other post methods and return a Builder to set optional parameters, e.g. redirect_uri. This allows the API to evolve forward without breaking existing usages and is pleasant to use with optional parameters. https://docs.rs/octocrab/latest/octocrab/repos/releases/struct.CreateReleaseBuilder.html, for example

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.

Support procuring a user access token from an oauth code
3 participants