Skip to content
Draft
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"officeparser": "^4.1.1",
"passport": "^0.5.2",
"passport-facebook": "^3.0.0",
"passport-github2": "^0.1.12",
"passport-google-oauth20": "^2.0.0",
"pdf-parse": "^1.1.1",
"sanitize-html": "^2.13.1",
Expand Down
121 changes: 121 additions & 0 deletions src/providers/github/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { Request, Response } from "express";
import Base from "../BaseProvider";
import { ConnectionCallbackResponse, PassportProfile } from "../../interfaces";
import { GithubProviderConfig } from "./interfaces";

const passport = require("passport");
const GitHubStrategy = require("passport-github2");

export default class GitHubProvider extends Base {
protected config: GithubProviderConfig;

public getProviderName() {
return "github";
}

public getProviderLabel() {
return "Github";
}

public getProviderApplicationUrl() {
return "https://github.com/";
}

public syncHandlers(): any[] {
return [];
}

public getScopes(): string[] {
return ["read:user", "user:email", "repo"];
}

public async connect(req: Request, res: Response, next: any): Promise<any> {
this.init();

const auth = passport.authenticate("github", {
scope: this.getScopes(),
});

return auth(req, res, next);
}

public async callback(req: Request, res: Response, next: any): Promise<ConnectionCallbackResponse> {
this.init();

return new Promise((resolve, reject) => {
passport.authenticate(
"github",
{
failureRedirect: "/failure/github",
failureMessage: true,
},
(err: any, user: any) => {
if (err) {
return reject(err);
}
if (!user) {
return reject(new Error("No user data returned from GitHub"));
}

const profile = this.formatProfile(user.profile);

resolve({
id: profile.id,
accessToken: user.accessToken,
refreshToken: user.refreshToken,
profile: {
username: profile.connectionProfile.username,
...profile,
},
});
}
)(req, res, next);
});
}

public async getApi(accessToken?: string, refreshToken?: string): Promise<any> {
// Placeholder for GitHub API integration
}

public init() {
passport.use(
new GitHubStrategy(
{
clientID: this.config.clientId,
clientSecret: this.config.clientSecret,
callbackURL: this.config.callbackUrl,
},
(accessToken: string, refreshToken: string, profile: any, cb: any) => {
return cb(null, {
accessToken,
refreshToken,
profile,
});
}
)
);
}

private formatProfile(githubProfile: any): PassportProfile {
const email = githubProfile.emails && githubProfile.emails.length
? githubProfile.emails[0].value
: null;

return {
id: githubProfile.id,
provider: this.getProviderName(),
displayName: githubProfile.displayName || email || githubProfile.username,
name: {
familyName: githubProfile.name?.split(" ").slice(-1)[0] || "",
givenName: githubProfile.name?.split(" ").slice(0, -1).join(" ") || "",
},
photos: githubProfile.photos || [],
connectionProfile: {
username: email ? email.split("@")[0] : githubProfile.username,
readableId: email || githubProfile.username,
email: email,
verified: githubProfile._json?.email_verified || false,
},
};
}
}
11 changes: 11 additions & 0 deletions src/providers/github/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { BaseHandlerConfig, BaseProviderConfig } from "../../../src/interfaces";

export interface GithubProviderConfig extends BaseProviderConfig {
clientId: string;
clientSecret: string;
callbackUrl: string;
}

export interface GithubHandlerConfig extends BaseHandlerConfig {
batchSize: number
}
8 changes: 8 additions & 0 deletions src/serverconfig.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@
"messagesPerGroupLimit": 100,
"maxGroupSize": 50,
"useDbPos": true
},
"github": {
"status": "active",
"label": "Github",
"clientId": "",
"clientSecret": "",
"batchSize": 50,
"maxSyncLoops": 1
}
},
"providerDefaults": {
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6737,6 +6737,13 @@ passport-facebook@^3.0.0:
dependencies:
passport-oauth2 "1.x.x"

passport-github2@^0.1.12:
version "0.1.12"
resolved "https://registry.yarnpkg.com/passport-github2/-/passport-github2-0.1.12.tgz#a72ebff4fa52a35bc2c71122dcf470d1116f772c"
integrity sha512-3nPUCc7ttF/3HSP/k9sAXjz3SkGv5Nki84I05kSQPo01Jqq1NzJACgMblCK0fGcv9pKCG/KXU3AJRDGLqHLoIw==
dependencies:
passport-oauth2 "1.x.x"

passport-google-oauth20@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/passport-google-oauth20/-/passport-google-oauth20-2.0.0.tgz#0d241b2d21ebd3dc7f2b60669ec4d587e3a674ef"
Expand Down
Loading