Saving oauth connections

This commit is contained in:
Gabriel Rosa
2022-08-23 15:42:33 -03:00
parent 0f079a7b22
commit 195fdae0e6
8 changed files with 120 additions and 28 deletions
+49
View File
@@ -0,0 +1,49 @@
import { Injectable } from '@nestjs/common';
import { ConnectionDto } from '../connection/dtos/connection';
import jwt from 'jsonwebtoken';
import { PartialType } from '@nestjs/swagger';
class PartialConnection extends PartialType(ConnectionDto) {}
@Injectable()
export class OauthService {
generateStateFromRequest(req, type): string {
const plugin: string = req.route.path
.replace('/oauth/', '')
.replace('/callback', '');
const { query } = req;
const {
name,
description,
connector_id,
connector_name,
image_url,
user_id,
customer_id,
customer_name,
} = query;
const properties = { plugin, credentials_type: 'oauth' };
for (const key in query) {
if (key.includes('properties.')) {
const propertyKey = key.split('.')[1];
properties[propertyKey] = query[key];
}
}
const newConnection: PartialConnection = {
name,
description,
connector_id,
connector_name,
image_url,
user_id,
customer_id,
customer_name,
type,
properties,
};
return jwt.sign({ ...newConnection }, process.env.JWT_PRIVATE_KEY);
}
}