Skip to content

Commit

Permalink
fix: type-safe support for verify function with request (#44)
Browse files Browse the repository at this point in the history
* fix: verify function with request types

* chore: update changelog

* chore: release v1.2.4-beta.0

---------

Co-authored-by: GitHub Actions release job <[email protected]>
  • Loading branch information
janhalama and GitHub Actions release job authored Feb 6, 2024
1 parent 6001cbb commit ba5cf60
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 35 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

## 1.2.4-beta.0 - 2024-01-29
### Fixed
- Type-safe support for verify function with request

## 1.2.3 - 2023-01-13
### Added
- Examples for JavaScript and TypeScript usage (see [README](https://github.com/superfaceai/passport-twitter-oauth2/#examples))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@superfaceai/passport-twitter-oauth2",
"version": "1.2.3",
"version": "1.2.4-beta.0",
"description": "Twitter OAuth 2.0 authentication strategy for Passport.",
"source": "src/index.ts",
"main": "./dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type {
Profile,
ProfileWithMetaData,
StrategyOptions,
StrategyOptionWithRequest,
StrategyOptionsWithRequest,
AuthenticateOptions,
} from './models';
export { Strategy };
Expand Down
2 changes: 1 addition & 1 deletion src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type { Profile, ProfileWithMetaData } from './profile';
export type {
StrategyOptions,
StrategyOptionWithRequest,
StrategyOptionsWithRequest,
AuthenticateOptions,
} from './strategyOptions';
14 changes: 13 additions & 1 deletion src/models/strategyOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface StrategyOptions
/**
* @public
*/
export interface StrategyOptionWithRequest
export interface StrategyOptionsWithRequest
extends TwitterStrategyOptionsBase,
Omit<
PassportOAuth2StrategyOptionsWithRequest,
Expand All @@ -33,6 +33,18 @@ export interface StrategyOptionWithRequest
passReqToCallback: true;
}

export const isStrategyOptions = (
options: StrategyOptions | StrategyOptionsWithRequest
): options is StrategyOptions => {
return !options.passReqToCallback;
};

export const isStrategyOptionsWithRequest = (
options: StrategyOptions | StrategyOptionsWithRequest
): options is StrategyOptionsWithRequest => {
return options.passReqToCallback === true;
};

/**
* @public
*/
Expand Down
83 changes: 53 additions & 30 deletions src/strategy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { Strategy as OAuth2Strategy } from 'passport-oauth2';
import {
Strategy as OAuth2Strategy,
VerifyFunction,
VerifyFunctionWithRequest,
} from 'passport-oauth2';

import { mapUserProfile } from './mapUserProfile';
import { ProfileWithMetaData } from './models/profile';
import { StrategyOptions } from './models/strategyOptions';
import {
isStrategyOptions,
isStrategyOptionsWithRequest,
StrategyOptions,
StrategyOptionsWithRequest,
} from './models/strategyOptions';
import { TwitterError } from './models/twitterError';
import { TwitterUserInfoResponse } from './models/twitterUserInfo';

Expand Down Expand Up @@ -47,16 +56,41 @@ export class Strategy extends OAuth2Strategy {
* ));
* ```
*/
constructor(userOptions: StrategyOptions, verify: VerifyFunction);
constructor(
options: StrategyOptions,
verify: (
accessToken: string,
refreshToken: string,
profile: ProfileWithMetaData,
done: (error: Error | null, user?: Express.User) => void
) => void
userOptions: StrategyOptionsWithRequest,
verify: VerifyFunctionWithRequest
);
constructor(
userOptions: StrategyOptions | StrategyOptionsWithRequest,
verify: VerifyFunction | VerifyFunctionWithRequest
) {
const options = Strategy.buildStrategyOptions(userOptions);

if (isStrategyOptions(options)) {
super(options, verify as VerifyFunction);
} else if (isStrategyOptionsWithRequest(options)) {
super(options, verify as VerifyFunctionWithRequest);
} else {
throw Error('Strategy options not supported.');
}

this.name = 'twitter';
this._userProfileURL =
options.userProfileURL ||
'https://api.twitter.com/2/users/me?user.fields=profile_image_url,url';

let scope = options.scope || [];
if (!Array.isArray(scope)) {
scope = [scope];
}
options.scope = this.addDefaultScopes(scope, options);
}

static buildStrategyOptions(
userOptions: StrategyOptions | StrategyOptionsWithRequest
) {
options = options || {};
const options = userOptions || {};
options.sessionKey = options.sessionKey || 'oauth:twitter';
const authorizationURL =
options.authorizationURL || 'https://twitter.com/i/oauth2/authorize';
Expand Down Expand Up @@ -91,25 +125,11 @@ export class Strategy extends OAuth2Strategy {
options.customHeaders || {};
}

super(
{
...options,
authorizationURL,
tokenURL,
},
verify
);

this.name = 'twitter';
this._userProfileURL =
options.userProfileURL ||
'https://api.twitter.com/2/users/me?user.fields=profile_image_url,url';

let scope = options.scope || [];
if (!Array.isArray(scope)) {
scope = [scope];
}
options.scope = this.addDefaultScopes(scope, options);
return {
...options,
authorizationURL,
tokenURL,
};
}

/**
Expand Down Expand Up @@ -189,7 +209,10 @@ export class Strategy extends OAuth2Strategy {
});
}

addDefaultScopes(scopes: string[], options: StrategyOptions) {
addDefaultScopes(
scopes: string[],
options: StrategyOptions | StrategyOptionsWithRequest
) {
let skipUserProfile = false;
const skipUserProfileOption = options.skipUserProfile as unknown;

Expand Down

0 comments on commit ba5cf60

Please sign in to comment.