New strategies

This commit is contained in:
Gabriel Rosa
2022-06-24 11:08:38 -03:00
parent e439823319
commit 07f123e528
5 changed files with 121 additions and 0 deletions
+41
View File
@@ -29,6 +29,7 @@
"jsonwebtoken": "^8.5.1",
"jwk-to-pem": "^2.0.5",
"passport": "^0.6.0",
"passport-facebook": "^3.0.0",
"passport-google-oauth20": "^2.0.0",
"passport-hubspot-oauth2": "^1.0.3",
"reflect-metadata": "^0.1.13",
@@ -45,6 +46,7 @@
"@types/jsonwebtoken": "^8.5.8",
"@types/jwk-to-pem": "^2.0.1",
"@types/node": "^16.11.41",
"@types/passport-facebook": "^2.1.11",
"@types/passport-google-oauth20": "^2.0.11",
"@types/passport-oauth2": "^1.4.11",
"@types/supertest": "^2.0.12",
@@ -3036,6 +3038,16 @@
"@types/express": "*"
}
},
"node_modules/@types/passport-facebook": {
"version": "2.1.11",
"resolved": "https://registry.npmjs.org/@types/passport-facebook/-/passport-facebook-2.1.11.tgz",
"integrity": "sha512-YsZUZryJvHt2E/WdR22FlnCi9ckMwkGR+FwCcXq830rOvruhXJHJtKSXSD+Kn4phBsrEpqoP4xX6fFuEodk20A==",
"dev": true,
"dependencies": {
"@types/express": "*",
"@types/passport": "*"
}
},
"node_modules/@types/passport-google-oauth20": {
"version": "2.0.11",
"resolved": "https://registry.npmjs.org/@types/passport-google-oauth20/-/passport-google-oauth20-2.0.11.tgz",
@@ -8185,6 +8197,17 @@
"url": "https://github.com/sponsors/jaredhanson"
}
},
"node_modules/passport-facebook": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/passport-facebook/-/passport-facebook-3.0.0.tgz",
"integrity": "sha512-K/qNzuFsFISYAyC1Nma4qgY/12V3RSLFdFVsPKXiKZt434wOvthFW1p7zKa1iQihQMRhaWorVE1o3Vi1o+ZgeQ==",
"dependencies": {
"passport-oauth2": "1.x.x"
},
"engines": {
"node": ">= 0.4.0"
}
},
"node_modules/passport-google-oauth20": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/passport-google-oauth20/-/passport-google-oauth20-2.0.0.tgz",
@@ -12658,6 +12681,16 @@
"@types/express": "*"
}
},
"@types/passport-facebook": {
"version": "2.1.11",
"resolved": "https://registry.npmjs.org/@types/passport-facebook/-/passport-facebook-2.1.11.tgz",
"integrity": "sha512-YsZUZryJvHt2E/WdR22FlnCi9ckMwkGR+FwCcXq830rOvruhXJHJtKSXSD+Kn4phBsrEpqoP4xX6fFuEodk20A==",
"dev": true,
"requires": {
"@types/express": "*",
"@types/passport": "*"
}
},
"@types/passport-google-oauth20": {
"version": "2.0.11",
"resolved": "https://registry.npmjs.org/@types/passport-google-oauth20/-/passport-google-oauth20-2.0.11.tgz",
@@ -16590,6 +16623,14 @@
"utils-merge": "^1.0.1"
}
},
"passport-facebook": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/passport-facebook/-/passport-facebook-3.0.0.tgz",
"integrity": "sha512-K/qNzuFsFISYAyC1Nma4qgY/12V3RSLFdFVsPKXiKZt434wOvthFW1p7zKa1iQihQMRhaWorVE1o3Vi1o+ZgeQ==",
"requires": {
"passport-oauth2": "1.x.x"
}
},
"passport-google-oauth20": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/passport-google-oauth20/-/passport-google-oauth20-2.0.0.tgz",
+2
View File
@@ -44,6 +44,7 @@
"jsonwebtoken": "^8.5.1",
"jwk-to-pem": "^2.0.5",
"passport": "^0.6.0",
"passport-facebook": "^3.0.0",
"passport-google-oauth20": "^2.0.0",
"passport-hubspot-oauth2": "^1.0.3",
"reflect-metadata": "^0.1.13",
@@ -63,6 +64,7 @@
"@types/jsonwebtoken": "^8.5.8",
"@types/jwk-to-pem": "^2.0.1",
"@types/node": "^16.11.41",
"@types/passport-facebook": "^2.1.11",
"@types/passport-google-oauth20": "^2.0.11",
"@types/passport-oauth2": "^1.4.11",
"@types/supertest": "^2.0.12",
@@ -0,0 +1,39 @@
import {
Profile,
Strategy,
StrategyOptionWithRequest,
VerifyFunction,
} from 'passport-facebook';
import { PassportStrategy } from '@nestjs/passport';
import { Inject, Injectable } from '@nestjs/common';
import { OauthSecrets } from 'src/utils/OauthSecrets';
@Injectable()
export class FacebookStrategy extends PassportStrategy(Strategy) {
constructor(
@Inject('OAUTH_SECRETS')
private readonly oauthSecrets: OauthSecrets,
) {
const options: StrategyOptionWithRequest = {
clientID: oauthSecrets.google.client_id,
clientSecret: oauthSecrets.google.client_secret,
callbackURL: oauthSecrets.google.redirect_uri,
passReqToCallback: true,
};
const verify: VerifyFunction = (
accessToken: string,
refreshToken: string,
profile: Profile,
done: (error: any, user?: any, info?: any) => void,
) => done(null, profile, { accessToken, refreshToken });
super(options, verify);
}
authenticate(req, options) {
const { customer_id } = req.query;
options.state = customer_id;
options.scope = 'ads_management ads_read';
super.authenticate(req, options);
}
}
@@ -0,0 +1,39 @@
import {
Profile,
Strategy,
StrategyOptionsWithRequest,
VerifyCallback,
} from 'passport-google-oauth20';
import { PassportStrategy } from '@nestjs/passport';
import { Inject, Injectable } from '@nestjs/common';
import { OauthSecrets } from 'src/utils/OauthSecrets';
@Injectable()
export class GoogleStrategy extends PassportStrategy(Strategy) {
constructor(
@Inject('OAUTH_SECRETS')
private readonly oauthSecrets: OauthSecrets,
) {
const options: StrategyOptionsWithRequest = {
clientID: oauthSecrets.google.client_id,
clientSecret: oauthSecrets.google.client_secret,
scope: 'drive.metadata.readonly spreadsheets.readonly',
callbackURL: oauthSecrets.google.redirect_uri,
passReqToCallback: true,
};
const verify = (
accessToken: string,
refreshToken: string,
profile: Profile,
done: VerifyCallback,
) => done(null, profile, { accessToken, refreshToken });
super(options, verify);
}
authenticate(req, options) {
const { customer_id } = req.query;
options.state = customer_id;
super.authenticate(req, options);
}
}