passport-google-oidc-token
Version:
Google ID token authentication strategy for Passport.
106 lines (105 loc) • 3.32 kB
TypeScript
import { Request } from 'express';
import { OAuth2Client } from 'google-auth-library';
import { Profile as PassportProfile } from 'passport';
interface StrategyOptions {
clientID: string;
}
export interface Profile extends PassportProfile {
id: string;
username?: string;
name?: {
givenName: string;
middleName?: string;
familyName: string;
};
photos: {
value: string;
}[];
emails: {
value: string;
verified: boolean;
}[];
displayName: string;
_json: any;
}
export interface StrategyOptionsWithRequest extends StrategyOptions {
passReqToCallback: true;
}
declare type Info = {
message: string;
};
declare type DoneCallback = (error: Error | null, user: any | undefined, options: Info | undefined) => void;
declare type VerifyArgs = [
idToken: string,
profile: Profile,
doneCallback: DoneCallback
];
export declare type VerifyFunction = (...args: VerifyArgs) => void;
export declare type VerifyFunctionWithRequest = (req: Request, ...args: VerifyArgs) => void;
/**
* `GoogleOIDCTokenStrategy` constructor.
*
* The Google OIDC token strategy authenticates using the Google Auth Library
*
* Applications must supply a `verify` callback which accepts an `accessToken`,
* `refreshToken` and service-specific `profile`, and then calls the `cb`
* callback supplying a `user`, which should be set to `false` if the
* credentials are not valid. If an exception occurred, `err` should be set.
*
* @param {Object} options
* @param {Function} verify
* @example
* passport.use(new GoogleOIDCTokenStrategy(
* {
* clientID: '123456789',
* },
* (accessToken, refreshToken, profile, cb) => {
* User.findOrCreate({ googleId: profile.id }, cb);
* }
* );
*/
export default class GoogleOIDCTokenStrategy {
client: OAuth2Client;
clientId: string;
name: string;
_verify: VerifyFunction | VerifyFunctionWithRequest;
_passReqToCallback: boolean;
error: (err: Error | unknown) => void;
fail: (info: Info | undefined) => void;
success: (user: any, info: Info | undefined) => void;
constructor(options: StrategyOptionsWithRequest, verify: VerifyFunctionWithRequest);
constructor(options: StrategyOptions, verify: VerifyFunction);
/**
* Authenticate request using Google Auth Library
* @param {Object} req
*/
authenticate(req: Request): Promise<void>;
/**
* This method handles searhing the value of provided field in body, query, and header.
*
* @param {Object} req http request object
* @param {String} field
* @returns {String} field's value in body, query, or headers
*/
private lookup;
/**
* Parse profile.
*
* Parses user profiles as fetched from Google's OpenID Connect-compatible user
* info endpoint.
*
* The amount of detail in the profile varies based on the scopes granted by the
* user. The following scope values add additional data:
*
* `profile` - basic profile information
* `email` - email address
*
* References:
* - https://developers.google.com/identity/protocols/OpenIDConnect
*
* @param {object} payload
* @return {object}
*/
private static parseProfile;
}
export {};