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

How could I modify the strategy so that it has the ability to vary the content-type beyond application/x-www-form-urlencoded #176

Open
CakeCrusher opened this issue Aug 19, 2023 · 0 comments

Comments

@CakeCrusher
Copy link

CakeCrusher commented Aug 19, 2023

Here is everything I have tried: https://chat.openai.com/share/cda16915-b97e-48de-836b-62501ab93041

Content-Type is decided in the oauth package here https://github.com/ciaranj/node-oauth/blob/0749d671f04b684ca255d6ff5340ae3efe711d9a/lib/oauth2.js#L191

The goal is to give the third party I'm authenticating, the ability to indicate content-type of they their tokenURL,, therefore changing the way the request is made.

The most promising solution thus far has been something along the lines of this (it did not work):

import { OAuth2Strategy as OriginalOAuth2Strategy } from 'passport-oauth2';

class DynamicContentTypeOAuth2Strategy extends OriginalOAuth2Strategy {
  contentType: string;

  constructor(options: any, verify: any) {
    super(options, verify);
    this.contentType = options.contentType || 'application/x-www-form-urlencoded';
  }

  getOAuthAccessToken(code: string, params: any, callback: any) {
    let post_data;
    if (this.contentType === 'application/json') {
      post_data = JSON.stringify({
        ...params,
        code: code,
      });
    } else {
      post_data = new URLSearchParams({
        ...params,
        code: code,
      }).toString();
    }

    const post_headers = {
      'Content-Type': this.contentType,
      'Content-Length': Buffer.byteLength(post_data),
    };

    this._oauth2._request(
      'POST',
      this._oauth2._getAccessTokenUrl(),
      post_headers,
      post_data,
      null,
      (err, data, response) => {
        if (err) return callback(err);
        let results;
        try {
          results = JSON.parse(data);
        } catch (e) {
          return callback(e);
        }
        const accessToken = results.access_token;
        const refreshToken = results.refresh_token;
        delete results.refresh_token;
        callback(null, accessToken, refreshToken, results);
      }
    );
  }
}
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

No branches or pull requests

1 participant