Skip to content

Commit

Permalink
feat: custom redirect urls for app connections
Browse files Browse the repository at this point in the history
  • Loading branch information
alerdenisov committed Jan 12, 2024
1 parent 9dce366 commit da872dc
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 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
5 changes: 3 additions & 2 deletions apps/api/src/secrets/secrets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ export class SecretsService {
}

async claim(dto: ClaimConnectionDto) {
const { clientSecret } = await this.getSecretById(dto.clientId);
const { clientSecret, redirectUrl } = await this.getSecretById(dto.clientId);
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,
};

Expand Down
2 changes: 2 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,8 @@ export class SecretEntity extends MetaEntity {
@Column({ nullable: false })
clientId: string;

@Column({ nullable: true })
redirectUrl: 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"`);
}

}
3 changes: 2 additions & 1 deletion libs/database/src/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ 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';

0 comments on commit da872dc

Please sign in to comment.