Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix saving tokens #4

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions backend/src/strategies/github-token/github-token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class GithubTokenStrategyService extends Strategy {
override async getSyncDataForLoginData(
loginData: UserLoginData,
): Promise<{ token: string | null; [key: string]: any }> {
return { token: loginData.data["accessToken"] ?? null };
return { token: loginData.data["token"] ?? null };
}

override getImsUserTemplatedValuesForLoginData(loginData: UserLoginData): object {
Expand Down Expand Up @@ -123,6 +123,7 @@ export class GithubTokenStrategyService extends Strategy {
username: string;
displayName: string;
email: string;
token: string;
} | null> {
const graphqlQuery = `
{
Expand Down Expand Up @@ -155,6 +156,7 @@ export class GithubTokenStrategyService extends Strategy {
username: userData.login,
displayName: userData.name,
email: userData.email,
token
};
}

Expand Down Expand Up @@ -184,15 +186,15 @@ export class GithubTokenStrategyService extends Strategy {

override async handleAction(loginData: UserLoginData, name: string, data: Record<string, any>): Promise<void> {
if (name === "update-token") {
const accessToken = data["token"];
const userLoginData = await this.getUserData(accessToken, await loginData.strategyInstance);
const token = data["token"];
const userLoginData = await this.getUserData(token, await loginData.strategyInstance);
if (userLoginData == null) {
throw new HttpException("Token invalid", HttpStatus.BAD_REQUEST);
}
if (loginData.data["github_id"] !== userLoginData.github_id) {
throw new HttpException("Token does not match the user", HttpStatus.BAD_REQUEST);
}
loginData.data["accessToken"] = accessToken;
loginData.data["token"] = token;
this.loginDataService.save(loginData);
} else {
throw new HttpException("Unknown action", HttpStatus.BAD_REQUEST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class JiraTokenCloudStrategyService extends Strategy {
override async getSyncDataForLoginData(
loginData: UserLoginData,
): Promise<{ token: string | null; [key: string]: any }> {
return { token: loginData.data["apiToken"] ?? null, type: "PAT" };
return { token: loginData.data["token"] ?? null, type: "PAT" };
}

override getImsUserTemplatedValuesForLoginData(loginData: UserLoginData): object {
Expand Down Expand Up @@ -131,6 +131,7 @@ export class JiraTokenCloudStrategyService extends Strategy {
username: string;
displayName: string;
email: string;
token: string;
} | null> {
const response = await fetch(
new URL("/rest/api/2/myself", strategyInstance.instanceConfig["imsTemplatedFieldsFilter"]["root-url"]),
Expand All @@ -154,6 +155,7 @@ export class JiraTokenCloudStrategyService extends Strategy {
username: "",
displayName: userData.displayName,
email,
token
};
}

Expand Down Expand Up @@ -184,16 +186,16 @@ export class JiraTokenCloudStrategyService extends Strategy {

override async handleAction(loginData: UserLoginData, name: string, data: Record<string, any>): Promise<void> {
if (name === "update-token") {
const apiToken = data["token"];
const token = data["token"];
const email = data["email"] || loginData.data["email"];
const userLoginData = await this.getUserData(apiToken, email, await loginData.strategyInstance);
const userLoginData = await this.getUserData(token, email, await loginData.strategyInstance);
if (userLoginData == null) {
throw new HttpException("Token invalid", HttpStatus.BAD_REQUEST);
}
if (loginData.data["jira_id"] !== userLoginData.jira_id) {
throw new HttpException("Token does not match the user", HttpStatus.BAD_REQUEST);
}
loginData.data["apiToken"] = apiToken;
loginData.data["token"] = token;
loginData.data["email"] = email;
this.loginDataService.save(loginData);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class JiraTokenDatacenterStrategyService extends Strategy {
override async getSyncDataForLoginData(
loginData: UserLoginData,
): Promise<{ token: string | null; [key: string]: any }> {
return { token: loginData.data["apiToken"] ?? null, type: "PAT" };
return { token: loginData.data["token"] ?? null, type: "PAT" };
}

override getImsUserTemplatedValuesForLoginData(loginData: UserLoginData): object {
Expand Down Expand Up @@ -119,6 +119,7 @@ export class JiraTokenDatacenterStrategyService extends Strategy {
username: string;
displayName: string;
email?: string;
token: string;
} | null> {
const response = await fetch(
new URL("/rest/api/2/myself", strategyInstance.instanceConfig["imsTemplatedFieldsFilter"]["root-url"]),
Expand All @@ -142,6 +143,7 @@ export class JiraTokenDatacenterStrategyService extends Strategy {
username: userData.name,
displayName: userData.displayName,
email: userData.emailAddress,
token
};
}

Expand Down Expand Up @@ -171,15 +173,15 @@ export class JiraTokenDatacenterStrategyService extends Strategy {

override async handleAction(loginData: UserLoginData, name: string, data: Record<string, any>): Promise<void> {
if (name === "update-token") {
const apiToken = data["token"];
const userLoginData = await this.getUserData(apiToken, await loginData.strategyInstance);
const token = data["token"];
const userLoginData = await this.getUserData(token, await loginData.strategyInstance);
if (userLoginData == null) {
throw new HttpException("Token invalid", HttpStatus.BAD_REQUEST);
}
if (loginData.data["jira_id"] !== userLoginData.jira_id) {
throw new HttpException("Token does not match the user", HttpStatus.BAD_REQUEST);
}
loginData.data["apiToken"] = apiToken;
loginData.data["token"] = token;
this.loginDataService.save(loginData);
} else {
throw new HttpException("Unknown action", HttpStatus.BAD_REQUEST);
Expand Down