From 0e764a2b3e2aed345750b87f4a77bee345598c69 Mon Sep 17 00:00:00 2001
From: Satsuki Yanagi <17376330+u1-liquid@users.noreply.github.com>
Date: Fri, 3 May 2019 18:38:19 +0900
Subject: [PATCH] Fix external service authentication (#4846)

---
 migration/1556746559567-UserProfile.ts | 23 +++++++++++++++++++++++
 src/models/entities/user-profile.ts    | 12 ++++++------
 src/server/api/service/discord.ts      |  8 ++------
 src/server/api/service/github.ts       |  8 ++------
 src/server/api/service/twitter.ts      |  8 ++------
 5 files changed, 35 insertions(+), 24 deletions(-)
 create mode 100644 migration/1556746559567-UserProfile.ts

diff --git a/migration/1556746559567-UserProfile.ts b/migration/1556746559567-UserProfile.ts
new file mode 100644
index 0000000000..719f8e1ae0
--- /dev/null
+++ b/migration/1556746559567-UserProfile.ts
@@ -0,0 +1,23 @@
+import {MigrationInterface, QueryRunner} from "typeorm";
+
+export class UserProfile1556746559567 implements MigrationInterface {
+
+    public async up(queryRunner: QueryRunner): Promise<any> {
+        await queryRunner.query(`UPDATE "user_profile" SET github = FALSE`);
+        await queryRunner.query(`ALTER TABLE "user_profile" DROP COLUMN "githubId"`);
+        await queryRunner.query(`ALTER TABLE "user_profile" ADD COLUMN "githubId" VARCHAR(64)`);
+        await queryRunner.query(`UPDATE "user_profile" SET discord = FALSE`);
+        await queryRunner.query(`ALTER TABLE "user_profile" DROP COLUMN "discordExpiresDate"`);
+        await queryRunner.query(`ALTER TABLE "user_profile" ADD COLUMN "discordExpiresDate" VARCHAR(64)`);
+    }
+
+    public async down(queryRunner: QueryRunner): Promise<any> {
+        await queryRunner.query(`UPDATE "user_profile" SET github = FALSE`);
+        await queryRunner.query(`ALTER TABLE "user_profile" DROP COLUMN "githubId"`);
+        await queryRunner.query(`ALTER TABLE "user_profile" ADD COLUMN "githubId" INTEGER`);
+        await queryRunner.query(`UPDATE "user_profile" SET discord = FALSE`);
+        await queryRunner.query(`ALTER TABLE "user_profile" DROP COLUMN "discordExpiresDate"`);
+        await queryRunner.query(`ALTER TABLE "user_profile" ADD COLUMN "discordExpiresDate" INTEGER`);
+    }
+
+}
diff --git a/src/models/entities/user-profile.ts b/src/models/entities/user-profile.ts
index a2d7b8d2c2..16e5d5b9e0 100644
--- a/src/models/entities/user-profile.ts
+++ b/src/models/entities/user-profile.ts
@@ -144,10 +144,10 @@ export class UserProfile {
 	})
 	public githubAccessToken: string | null;
 
-	@Column('integer', {
-		nullable: true, default: null,
+	@Column('varchar', {
+		length: 64, nullable: true, default: null,
 	})
-	public githubId: number | null;
+	public githubId: string | null;
 
 	@Column('varchar', {
 		length: 64, nullable: true, default: null,
@@ -169,10 +169,10 @@ export class UserProfile {
 	})
 	public discordRefreshToken: string | null;
 
-	@Column('integer', {
-		nullable: true, default: null,
+	@Column('varchar', {
+		length: 64, nullable: true, default: null,
 	})
-	public discordExpiresDate: number | null;
+	public discordExpiresDate: string | null;
 
 	@Column('varchar', {
 		length: 64, nullable: true, default: null,
diff --git a/src/server/api/service/discord.ts b/src/server/api/service/discord.ts
index ac4d9a5601..29da17dd9e 100644
--- a/src/server/api/service/discord.ts
+++ b/src/server/api/service/discord.ts
@@ -203,12 +203,8 @@ router.get('/dc/cb', async ctx => {
 		}
 
 		const profile = await UserProfiles.createQueryBuilder()
-			.where('discord @> :discord', {
-				discord: {
-					id: id,
-				},
-			})
-			.andWhere('userHost IS NULL')
+			.where('"discordId" = :id', { id: id })
+			.andWhere('"userHost" IS NULL')
 			.getOne();
 
 		if (profile == null) {
diff --git a/src/server/api/service/github.ts b/src/server/api/service/github.ts
index 4f287406d1..de1e15f079 100644
--- a/src/server/api/service/github.ts
+++ b/src/server/api/service/github.ts
@@ -193,12 +193,8 @@ router.get('/gh/cb', async ctx => {
 		}
 
 		const link = await UserProfiles.createQueryBuilder()
-			.where('github @> :github', {
-				github: {
-					id: id,
-				},
-			})
-			.andWhere('userHost IS NULL')
+			.where('"githubId" = :id', { id: id })
+			.andWhere('"userHost" IS NULL')
 			.getOne();
 
 		if (link == null) {
diff --git a/src/server/api/service/twitter.ts b/src/server/api/service/twitter.ts
index 3a5800f00b..55369d0aee 100644
--- a/src/server/api/service/twitter.ts
+++ b/src/server/api/service/twitter.ts
@@ -141,12 +141,8 @@ router.get('/tw/cb', async ctx => {
 		const result = await twAuth!.done(JSON.parse(twCtx), ctx.query.oauth_verifier);
 
 		const link = await UserProfiles.createQueryBuilder()
-			.where('twitter @> :twitter', {
-				twitter: {
-					userId: result.userId,
-				},
-			})
-			.andWhere('userHost IS NULL')
+			.where('"twitterUserId" = :id', { id: result.userId })
+			.andWhere('"userHost" IS NULL')
 			.getOne();
 
 		if (link == null) {