mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-23 08:58:26 +00:00
* multi org changes * Initial changes * changes * manage sso page * Multi-organization changes * Multi organization changes * multi-org changes * multi-org changes * multi-org changes * multi-org fixes * env variables app.json changes * multi-org-fix * user invitation token fix * multi-org group permission fix * multi-org app privilege * google oauth fix * Remove enable signup for form login * Multi organization fixes * multi-org user invite flow changes * multi-org sign up fix * rebase and multi-org fixes * revert testing logs * test logs revert * migration changes * migration file fix * error message changes * git login for private email fix * dropdown fix * test cases * e2e test cases added * test cases fix * documentation changes * testcases fix * testcases added * replace findOne with findOneOrFail * accept invite testcases * login page fixes * added encrypted tag * review comments * migration fixes * improvements * manage sso loading fix * review comments * migration file changes * new organization creation bug fix * added e2e testcases * added testcases * Update data_sources.controller.ts
57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import got from 'got';
|
|
import UserResponse from './models/user_response';
|
|
|
|
@Injectable()
|
|
export class GitOAuthService {
|
|
constructor(private readonly configService: ConfigService) {}
|
|
private readonly authUrl = 'https://github.com/login/oauth/access_token';
|
|
private readonly getUserUrl = 'https://api.github.com/user';
|
|
private readonly getUserEmailUrl = 'https://api.github.com/user/emails';
|
|
|
|
async #getUserDetails({ access_token }: AuthResponse): Promise<UserResponse> {
|
|
const response: any = await got(this.getUserUrl, {
|
|
method: 'get',
|
|
headers: { Accept: 'application/json', Authorization: `token ${access_token}` },
|
|
}).json();
|
|
|
|
const { name } = response;
|
|
let { email } = response;
|
|
const words = name?.split(' ');
|
|
const firstName = words?.[0] || '';
|
|
const lastName = words?.length > 1 ? words[words.length - 1] : '';
|
|
|
|
if (!email) {
|
|
// email visibility not set to public
|
|
email = await this.#getEmailId(access_token);
|
|
}
|
|
|
|
return { userSSOId: access_token, firstName, lastName, email, sso: 'git' };
|
|
}
|
|
|
|
async #getEmailId(access_token: string) {
|
|
const response: any = await got(this.getUserEmailUrl, {
|
|
method: 'get',
|
|
headers: { Accept: 'application/json', Authorization: `token ${access_token}` },
|
|
}).json();
|
|
|
|
return response?.find((emails) => emails.primary)?.email;
|
|
}
|
|
|
|
async signIn(code: string, configs: any): Promise<any> {
|
|
const response: any = await got(this.authUrl, {
|
|
method: 'post',
|
|
headers: { Accept: 'application/json' },
|
|
json: { client_id: configs.clientId, client_secret: configs.clientSecret, code },
|
|
}).json();
|
|
|
|
return await this.#getUserDetails(response);
|
|
}
|
|
}
|
|
|
|
interface AuthResponse {
|
|
access_token: string;
|
|
scope?: string;
|
|
token_type?: string;
|
|
}
|