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

feat: Access token verification for GitHub #2 #689

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
37 changes: 37 additions & 0 deletions lib/build/recipe/thirdparty/providers/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,36 @@ function Github(input) {
if (input.config.tokenEndpoint === undefined) {
input.config.tokenEndpoint = "https://github.com/login/oauth/access_token";
}
if (input.config.validateAccessToken === undefined) {
input.config.validateAccessToken = ({ accessToken, clientConfig }) =>
__awaiter(this, void 0, void 0, function* () {
const basicAuthToken = Buffer.from(
`${clientConfig.clientId}:${
clientConfig.clientSecret === undefined ? "" : clientConfig.clientSecret
}`
).toString("base64");
const applicationsResponse = yield cross_fetch_1.default(
`https://api.github.com/applications/${clientConfig.clientId}/token`,
{
headers: {
Authorization: `Basic ${basicAuthToken}`,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
access_token: accessToken,
}),
}
);
if (applicationsResponse.status !== 200) {
throw new Error("Invalid access token");
}
const body = yield applicationsResponse.json();
if (body.app === undefined || body.app.client_id !== clientConfig.clientId) {
throw new Error("Access token does not belong to your application");
}
});
}
const oOverride = input.override;
input.override = function (originalImplementation) {
const oGetConfig = originalImplementation.getConfigForClientType;
Expand All @@ -98,6 +128,13 @@ function Github(input) {
};
originalImplementation.getUserInfo = function (input) {
return __awaiter(this, void 0, void 0, function* () {
if (originalImplementation.config.validateAccessToken !== undefined) {
yield originalImplementation.config.validateAccessToken({
accessToken: input.oAuthTokens.access_token,
clientConfig: originalImplementation.config,
userContext: input.userContext,
});
}
const headers = {
Authorization: `Bearer ${input.oAuthTokens.access_token}`,
Accept: "application/vnd.github.v3+json",
Expand Down
40 changes: 40 additions & 0 deletions lib/ts/recipe/thirdparty/providers/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,38 @@ export default function Github(input: ProviderInput): TypeProvider {
input.config.tokenEndpoint = "https://github.com/login/oauth/access_token";
}

if (input.config.validateAccessToken === undefined) {
input.config.validateAccessToken = async ({ accessToken, clientConfig }) => {
const basicAuthToken = Buffer.from(
`${clientConfig.clientId}:${clientConfig.clientSecret === undefined ? "" : clientConfig.clientSecret}`
).toString("base64");

const applicationsResponse = await fetch(
`https://api.github.com/applications/${clientConfig.clientId}/token`,
{
headers: {
Authorization: `Basic ${basicAuthToken}`,
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
access_token: accessToken,
}),
}
);

if (applicationsResponse.status !== 200) {
throw new Error("Invalid access token");
}

const body = await applicationsResponse.json();

if (body.app === undefined || body.app.client_id !== clientConfig.clientId) {
throw new Error("Access token does not belong to your application");
}
};
}

const oOverride = input.override;

input.override = function (originalImplementation) {
Expand All @@ -73,6 +105,14 @@ export default function Github(input: ProviderInput): TypeProvider {
};

originalImplementation.getUserInfo = async function (input) {
if (originalImplementation.config.validateAccessToken !== undefined) {
await originalImplementation.config.validateAccessToken({
accessToken: input.oAuthTokens.access_token,
clientConfig: originalImplementation.config,
userContext: input.userContext,
});
}

const headers = {
Authorization: `Bearer ${input.oAuthTokens.access_token}`,
Accept: "application/vnd.github.v3+json",
Expand Down
Loading