mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-02-12 18:32:44 +00:00
fix: improve getToken
This commit is contained in:
@@ -17,6 +17,7 @@ const AnonymizedRepositorySchema = new Schema({
|
||||
accessToken: String,
|
||||
owner: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: "user",
|
||||
index: true,
|
||||
},
|
||||
conference: String,
|
||||
|
||||
@@ -17,6 +17,7 @@ import { downloadQueue, removeQueue } from "../queue";
|
||||
import RepositoryModel from "../database/repositories/repositories.model";
|
||||
import User from "../User";
|
||||
import { RepositoryStatus } from "../types";
|
||||
import { IUserDocument } from "../database/users/users.types";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -34,8 +35,18 @@ async function getTokenForAdmin(user: User, req: express.Request) {
|
||||
"source.accessToken": 1,
|
||||
owner: 1,
|
||||
}
|
||||
).exec();
|
||||
if (existingRepo && existingRepo.owner != user.id) {
|
||||
).populate({
|
||||
path: "owner",
|
||||
model: UserModel,
|
||||
});
|
||||
const user: IUserDocument = existingRepo?.owner as any;
|
||||
if (user instanceof UserModel) {
|
||||
const check = await GitHubBase.checkToken(user.accessTokens.github);
|
||||
if (check) {
|
||||
return user.accessTokens.github;
|
||||
}
|
||||
}
|
||||
if (existingRepo) {
|
||||
return existingRepo.source.accessToken;
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import Repository from "../Repository";
|
||||
import { Readable } from "stream";
|
||||
import UserModel from "../database/users/users.model";
|
||||
import AnonymousError from "../AnonymousError";
|
||||
import { Octokit } from "@octokit/rest";
|
||||
|
||||
export default abstract class GitHubBase {
|
||||
type: "GitHubDownload" | "GitHubStream" | "Zip";
|
||||
@@ -54,28 +55,32 @@ export default abstract class GitHubBase {
|
||||
});
|
||||
}
|
||||
|
||||
static async checkToken(token: string) {
|
||||
const octokit = new Octokit({ auth: token });
|
||||
try {
|
||||
await octokit.users.getAuthenticated();
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async getToken() {
|
||||
const user = await UserModel.findById(this.repository.owner.id);
|
||||
if (user && user.accessTokens.github) {
|
||||
return user.accessTokens.github as string;
|
||||
}
|
||||
if (this.accessToken) {
|
||||
try {
|
||||
// const app = new OAuthApp({
|
||||
// clientType: "github-app",
|
||||
// clientId: config.CLIENT_ID,
|
||||
// clientSecret: config.CLIENT_SECRET,
|
||||
// });
|
||||
// await app.checkToken({
|
||||
// token: this.accessToken,
|
||||
// });
|
||||
const check = await GitHubBase.checkToken(user.accessTokens.github);
|
||||
if (check) {
|
||||
this.accessToken = user.accessTokens.github;
|
||||
return this.accessToken;
|
||||
} catch (error) {
|
||||
console.debug("[ERROR] Token is invalid", this.repository.repoId);
|
||||
this.accessToken = config.GITHUB_TOKEN;
|
||||
}
|
||||
}
|
||||
return config.GITHUB_TOKEN;
|
||||
if (this.accessToken) {
|
||||
if (await GitHubBase.checkToken(this.accessToken)) {
|
||||
return this.accessToken;
|
||||
}
|
||||
}
|
||||
this.accessToken = config.GITHUB_TOKEN;
|
||||
return this.accessToken;
|
||||
}
|
||||
|
||||
get url() {
|
||||
@@ -86,8 +91,8 @@ export default abstract class GitHubBase {
|
||||
return {
|
||||
type: this.type,
|
||||
fullName: this.githubRepository.fullName?.toString(),
|
||||
branch: this.branch.name,
|
||||
commit: this.branch.commit,
|
||||
branch: this.branch?.name,
|
||||
commit: this.branch?.commit,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user