Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Add GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Jul 15, 2019
1 parent 568b56f commit 9391ac8
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "staart-manager",
"version": "1.0.67",
"version": "1.0.68",
"main": "index.js",
"repository": "[email protected]:AnandChowdhary/staart.git",
"author": "Anand Chowdhary <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion setup/internal/staart-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.67
1.0.68
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ export const GOOGLE_CLIENT_ID = process.env.GOOGLE_CLIENT_ID || "";
export const GOOGLE_CLIENT_SECRET = process.env.GOOGLE_CLIENT_SECRET || "";
export const GITHUB_CLIENT_ID = process.env.GITHUB_CLIENT_ID || "";
export const GITHUB_CLIENT_SECRET = process.env.GITHUB_CLIENT_SECRET || "";
export const MICROSOFT_CLIENT_ID = process.env.MICROSOFT_CLIENT_ID || "";
export const MICROSOFT_CLIENT_SECRET =
process.env.MICROSOFT_CLIENT_SECRET || "";
export const FACEBOOK_CLIENT_ID = process.env.FACEBOOK_CLIENT_ID || "";
export const FACEBOOK_CLIENT_SECRET = process.env.FACEBOOK_CLIENT_SECRET || "";
export const SALESFORCE_CLIENT_ID = process.env.SALESFORCE_CLIENT_ID || "";
Expand Down
68 changes: 58 additions & 10 deletions src/controllers/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Request, Response, NextFunction, RequestHandler } from "express";
import { ErrorCode, UserRole } from "../interfaces/enum";
import { ErrorCode, UserRole, Tokens } from "../interfaces/enum";
import {
sendPasswordReset,
login,
Expand All @@ -13,7 +13,7 @@ import {
register,
login2FA
} from "../rest/auth";
import { verifyToken } from "../helpers/jwt";
import { verifyToken, LoginResponse } from "../helpers/jwt";
import {
Get,
Post,
Expand All @@ -33,7 +33,7 @@ import asyncHandler from "express-async-handler";
import { safeRedirect, joiValidate } from "../helpers/utils";
import Joi from "@hapi/joi";
import { FRONTEND_URL, BASE_URL } from "../config";
import { salesforce } from "../rest/oauth";
import { salesforce, github, microsoft } from "../rest/oauth";
import { stringify } from "querystring";

const OAuthRedirector = (action: RequestHandler) => (
Expand All @@ -51,6 +51,20 @@ const OAuthRedirector = (action: RequestHandler) => (
);
});
};
const OAuthRedirect = (
req: Request,
res: Response,
response: LoginResponse
) => {
return safeRedirect(
req,
res,
`${FRONTEND_URL}/auth/token?${stringify({
...response,
subject: Tokens.LOGIN
})}`
);
};

@Controller("auth")
@ClassMiddleware(bruteForceHandler)
Expand Down Expand Up @@ -248,15 +262,49 @@ export class AuthController {
@Get("oauth/salesforce/callback")
@Wrapper(OAuthRedirector)
async getOAuthCallbackSalesforce(req: Request, res: Response) {
safeRedirect(
return OAuthRedirect(
req,
res,
await salesforce.callback(
`${BASE_URL}/auth${req.path}?${stringify(req.query)}`,
res.locals
)
);
}

@Get("oauth/github")
@Wrapper(OAuthRedirector)
async getOAuthUrlGitHub(req: Request, res: Response) {
safeRedirect(req, res, github.client.code.getUri());
}
@Get("oauth/github/callback")
@Wrapper(OAuthRedirector)
async getOAuthCallbackGitHub(req: Request, res: Response) {
return OAuthRedirect(
req,
res,
await github.callback(
`${BASE_URL}/auth${req.path}?${stringify(req.query)}`,
res.locals
)
);
}

@Get("oauth/microsoft")
@Wrapper(OAuthRedirector)
async getOAuthUrlMicrosoft(req: Request, res: Response) {
safeRedirect(req, res, microsoft.client.code.getUri());
}
@Get("oauth/microsoft/callback")
@Wrapper(OAuthRedirector)
async getOAuthCallbackMicrosoft(req: Request, res: Response) {
return OAuthRedirect(
req,
res,
`${FRONTEND_URL}/auth/token?${stringify(
await salesforce.callback(
`${BASE_URL}/auth${req.path}?${stringify(req.query)}`,
res.locals
)
)}`
await microsoft.callback(
`${BASE_URL}/auth${req.path}?${stringify(req.query)}`,
res.locals
)
);
}
}
51 changes: 50 additions & 1 deletion src/rest/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import axios from "axios";
import {
SALESFORCE_CLIENT_ID,
SALESFORCE_CLIENT_SECRET,
BASE_URL
BASE_URL,
GITHUB_CLIENT_ID,
GITHUB_CLIENT_SECRET,
MICROSOFT_CLIENT_ID,
MICROSOFT_CLIENT_SECRET
} from "../config";
import { Locals } from "../interfaces/general";
import { ErrorCode, EventType } from "../interfaces/enum";
Expand Down Expand Up @@ -73,3 +77,48 @@ export const salesforce = {
return loginWithOAuth2Service("salesforce", data.name, data.email, locals);
}
};
export const github = {
client: new ClientOAuth2({
clientId: GITHUB_CLIENT_ID,
clientSecret: GITHUB_CLIENT_SECRET,
redirectUri: getRedirectUri("github"),
authorizationUri: "https://github.com/login/oauth/authorize",
accessTokenUri: "https://github.com/login/oauth/access_token",
scopes: ["read:user", "user:email"]
}),
callback: async (url: string, locals: Locals) => {
const token = (await github.client.code.getToken(url)).accessToken;
const data = (await axios.get("https://api.github.com/user", {
headers: {
Authorization: `token ${token}`
}
})).data;
return loginWithOAuth2Service("github", data.name, data.email, locals);
}
};
export const microsoft = {
client: new ClientOAuth2({
clientId: MICROSOFT_CLIENT_ID,
clientSecret: MICROSOFT_CLIENT_SECRET,
redirectUri: getRedirectUri("microsoft"),
authorizationUri:
"https://login.microsoftonline.com/common/oauth2/authorize",
accessTokenUri: "https://login.microsoftonline.com/common/oauth2/token",
scopes: ["user.read", "mail.read"]
}),
callback: async (url: string, locals: Locals) => {
const token = (await microsoft.client.code.getToken(url)).accessToken;
const data = (await axios.get("https://graph.microsoft.com/v1.0/me", {
headers: {
Authorization: `Bearer ${token}`
}
})).data;
console.log(JSON.stringify(data));
return loginWithOAuth2Service(
"microsoft",
data.displayName,
data.mail,
locals
);
}
};

0 comments on commit 9391ac8

Please sign in to comment.