Skip to content

Commit

Permalink
Add support custom profile url
Browse files Browse the repository at this point in the history
  • Loading branch information
MiraiSubject committed Apr 13, 2022
1 parent 9465486 commit dee0345
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
8 changes: 7 additions & 1 deletion example/login/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ export default class Server {
const strat: OsuStrategy = new OsuStrategy({
clientID,
clientSecret,
// If you want to read a specific mode from the user instead of
// the default game mode, change this to match the appropriate game mode
userProfileUrl: 'https://osu.ppy.sh/api/v2/me/osu',
callbackURL: callbackUrl
}, (_accessToken: string, _refreshToken: string, profile: any, cb: any) => cb(null, profile));
}, (_accessToken: string, _refreshToken: string, profile: any, cb: any) => {
console.log(profile);
return cb(null, profile);
});

passport.use(strat);

Expand Down
25 changes: 6 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import OAuth2Strategy, { InternalOAuthError } from 'passport-oauth2';
import { OutgoingHttpHeaders } from 'http';
import passport from 'passport';

export default class OsuStrategy extends OAuth2Strategy {
private userProfileUrl: string;

constructor(options: StrategyOptionsWithRequest, verify: OAuth2Strategy.VerifyFunctionWithRequest)
constructor(options: StrategyOptions, verify: OAuth2Strategy.VerifyFunction)
Expand All @@ -12,15 +12,16 @@ export default class OsuStrategy extends OAuth2Strategy {
options.authorizationURL = options.authorizationURL || 'https://osu.ppy.sh/oauth/authorize';
options.tokenURL = options.tokenURL || 'https://osu.ppy.sh/oauth/token';
options.scopeSeparator = options.scopeSeparator || ' ';

super(options, verify);

this.userProfileUrl = options.userProfileUrl || 'https://osu.ppy.sh/api/v2/me';
this._oauth2.useAuthorizationHeaderforGET(true);
this.name = "osu";
}

userProfile(accessToken: string, done: (err?: Error | null, profile?: PassportProfile) => void): void {
this._oauth2.get('https://osu.ppy.sh/api/v2/me', accessToken, (err, body) => {
this._oauth2.get(this.userProfileUrl, accessToken, (err, body) => {
if (err || body instanceof Buffer || body === undefined)
return done(new InternalOAuthError('Failed to fetch the user profile.', err))

Expand All @@ -41,20 +42,6 @@ export default class OsuStrategy extends OAuth2Strategy {
}
}

export interface StrategyOptions extends passport.AuthenticateOptions {
clientID: string;
clientSecret: string;
callbackURL: string;

scope?: string[];
userAgent?: string;

authorizationURL?: string;
tokenURL?: string;
scopeSeparator?: string;
customHeaders?: OutgoingHttpHeaders;
}

export type OAuth2StrategyOptionsWithoutRequiredURLs = Pick<
OAuth2Strategy._StrategyOptionsBase,
Exclude<keyof OAuth2Strategy._StrategyOptionsBase, 'authorizationURL' | 'tokenURL'>
Expand All @@ -67,10 +54,10 @@ export interface _StrategyOptionsBase extends OAuth2StrategyOptionsWithoutRequir

scope?: string[];
userAgent?: string;
state?: string;

authorizationURL?: string;
tokenURL?: string;
userProfileUrl?: string;
scopeSeparator?: string;
customHeaders?: OutgoingHttpHeaders;
}
Expand All @@ -80,7 +67,7 @@ export interface StrategyOptions extends _StrategyOptionsBase {
* @deprecated this property is not required anymore. This will be deleted in 6 months.
*/
type?: 'StrategyOptions'
passReqToCallback?: false;
passReqToCallback?: false | undefined;
}

export interface StrategyOptionsWithRequest extends _StrategyOptionsBase {
Expand Down

0 comments on commit dee0345

Please sign in to comment.