Skip to content

Commit

Permalink
Merge pull request #24 from tookey-io/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
alerdenisov authored Jan 14, 2024
2 parents 7acff99 + 9a11042 commit eb87487
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 26 deletions.
13 changes: 10 additions & 3 deletions apps/api/src/secrets/secrets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ export class SecretsController {
.type('html')
.type('text/html')
.send(
`<script>if(window.opener){window.opener.postMessage({ 'code': '${encodeURIComponent(
code,
)}' },'*')}</script> <html>Redirect succuesfully, this window should close now</html>`,
`
<script>
if (window.opener) {
window.opener.postMessage({ 'code': '${encodeURIComponent(code)}' },'*')
} else {
window.localStorage.setItem('__oauth_code', params['code']);
window.close();
}
</script>
<html>Redirect succuesfully, this window should close now</html>`,
);
}

Expand Down
34 changes: 19 additions & 15 deletions apps/api/src/secrets/secrets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { mergeWith } from 'lodash';
import { ConfigService } from '@nestjs/config';
import { AppConfiguration } from 'apps/app/src/app.config';
import axios from 'axios';

@Injectable()
export class SecretsService {
Expand All @@ -22,15 +23,15 @@ export class SecretsService {
) {}

async refresh(appConnection: RefreshConnectionDto) {
const { clientSecret } = await this.getSecretById(appConnection.clientId);
const { clientSecret, redirectUrl, contentType } = await this.getSecretById(appConnection.clientId);
const body: Record<string, string> = {
grant_type: 'refresh_token',
refresh_token: appConnection.refreshToken,
redirect_uri: this.config.getOrThrow('publicUrl') + '/api/secrets/redirect',
redirect_uri: redirectUrl || this.config.getOrThrow('publicUrl') + '/api/secrets/redirect',
};

const headers: Record<string, string> = {
'content-type': 'application/x-www-form-urlencoded',
'content-type': contentType || 'application/x-www-form-urlencoded',
accept: 'application/json',
};

Expand All @@ -50,14 +51,12 @@ export class SecretsService {

const claimed_at = Math.round(Date.now() / 1000);
const response = await firstValueFrom(
this.httpService.post(appConnection.tokenUrl, new URLSearchParams(body), {
this.httpService.post(appConnection.tokenUrl, contentType === 'application/json' ? body : new URLSearchParams(body), {
headers,
timeout: 10000,
}),
).then((r) => r.data);

console.log('response from google', response);

return {
...response,
claimed_at,
Expand All @@ -73,18 +72,20 @@ export class SecretsService {
}

async claim(dto: ClaimConnectionDto) {
const { clientSecret } = await this.getSecretById(dto.clientId);
const { clientSecret, redirectUrl, contentType } = await this.getSecretById(dto.clientId);
console.log('claim request', dto, clientSecret, redirectUrl, contentType)
const redirect_uri = redirectUrl || this.config.getOrThrow('publicUrl') + '/api/secrets/redirect';
const body: Record<string, string> = {
redirect_uri: this.config.getOrThrow('publicUrl') + '/api/secrets/redirect',
grant_type: 'authorization_code',
redirect_uri,
code: dto.code,
};

if (dto.codeVerifier) {
body.code_verifier = dto.codeVerifier;
}
const headers: Record<string, string> = {
'content-type': 'application/x-www-form-urlencoded',
'content-type': contentType || 'application/x-www-form-urlencoded',
accept: 'application/json',
};
const authorizationMethod = dto.authorizationMethod || AuthorizationMethod.BODY;
Expand All @@ -100,13 +101,16 @@ export class SecretsService {
throw new Error(`Unknown authorization method: ${authorizationMethod}`);
}
const claimed_at = Math.round(Date.now() / 1000);
const response = await firstValueFrom(
this.httpService.post(dto.tokenUrl, new URLSearchParams(body), {

console.log(contentType, body, dto, headers);
const response = await axios.post(dto.tokenUrl, contentType === 'application/json' ? body : new URLSearchParams(body), {
headers,
}),
).then((r) => r.data);

console.log('response from google', response);
})
.then((r) => r.data)
.catch((e) => {
console.error(e);
throw e;
});

return {
...response,
Expand Down
5 changes: 5 additions & 0 deletions libs/database/src/entities/secrets.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export class SecretEntity extends MetaEntity {
@Column({ nullable: false })
clientId: string;

@Column({ nullable: true })
redirectUrl: string;

@Column({ nullable: true })
contentType: string;
}

@CustomRepository(SecretEntity)
Expand Down
14 changes: 14 additions & 0 deletions libs/database/src/migrations/1705086423012-secrets-redirects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class SecretsRedirects1705086423012 implements MigrationInterface {
name = 'SecretsRedirects1705086423012'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "secret_entity" ADD "redirectUrl" character varying`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "secret_entity" DROP COLUMN "redirectUrl"`);
}

}
14 changes: 14 additions & 0 deletions libs/database/src/migrations/1705089802726-secrets-content-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class SecretsContentType1705089802726 implements MigrationInterface {
name = 'SecretsContentType1705089802726'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "secret_entity" ADD "contentType" character varying`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "secret_entity" DROP COLUMN "contentType"`);
}

}
4 changes: 3 additions & 1 deletion libs/database/src/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ export * from './1697435937767-user-google';
export * from './1697737348969-email';
export * from './1697743613593-roles';
export * from './1699113772938-secrets';
export * from './1701893634169-pieces';
export * from './1701893634169-pieces';
export * from './1705086423012-secrets-redirects';
export * from './1705089802726-secrets-content-type';
25 changes: 18 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@nestjs/typeorm": "^9.0.1",
"@nestjs/websockets": "^9.2.1",
"@tookey-io/libtss-ethereum": "^1.2.4",
"axios": "^1.6.5",
"bcrypt": "^5.1.0",
"bull": "^4.10.2",
"cache-manager": "^4.1.0",
Expand Down

0 comments on commit eb87487

Please sign in to comment.