Skip to content

Commit

Permalink
Use GitHub app for authentication instead of token (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Oct 13, 2022
1 parent a80df39 commit 70b583e
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@nestjs/platform-ws": "^8.4.4",
"@nestjs/schedule": "^2.1.0",
"@nestjs/websockets": "^8.4.4",
"@octokit/auth-app": "^4.0.6",
"@octokit/rest": "^19.0.4",
"@octokit/webhooks": "^10.1.5",
"@octokit/webhooks-types": "^6.4.0",
Expand Down
14 changes: 12 additions & 2 deletions services/bots/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@ const conf = convict({
format: ['development', 'production'],
},
github: {
token: {
appId: {
default: '',
env: 'GITHUB_APP_ID',
format: String,
},
installationId: {
default: '',
env: 'GITHUB_INSTALLATION_ID',
format: String,
},
keyContents: {
default: '',
env: 'GITHUB_TOKEN',
env: 'GITHUB_KEY_CONTENTS',
format: String,
},
webhookSecret: {
Expand Down
10 changes: 9 additions & 1 deletion services/bots/src/github-webhook/github-webhook.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ServiceError } from '@lib/common';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { createAppAuth } from '@octokit/auth-app';

import { EventType, WEBHOOK_HANDLERS } from './github-webhook.const';
import { GithubClient, WebhookContext } from './github-webhook.model';
Expand All @@ -11,7 +12,14 @@ export class GithubWebhookService {
private githubClient: GithubClient;

constructor(configService: ConfigService) {
this.githubClient = new GithubClient({ auth: configService.get('github.token') });
this.githubClient = new GithubClient({
authStrategy: createAppAuth,
auth: {
appId: Number(configService.get('github.appId')),
installationId: Number(configService.get('github.installationId')),
privateKey: configService.get('github.keyContents'),
},
});
}

async handleWebhook(headers: Record<string, any>, payload: Record<string, any>): Promise<void> {
Expand Down
233 changes: 233 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,65 @@ __metadata:
languageName: node
linkType: hard

"@octokit/auth-app@npm:^4.0.6":
version: 4.0.6
resolution: "@octokit/auth-app@npm:4.0.6"
dependencies:
"@octokit/auth-oauth-app": ^5.0.0
"@octokit/auth-oauth-user": ^2.0.0
"@octokit/request": ^6.0.0
"@octokit/request-error": ^3.0.0
"@octokit/types": ^7.0.0
"@types/lru-cache": ^5.1.0
deprecation: ^2.3.1
lru-cache: ^6.0.0
universal-github-app-jwt: ^1.0.1
universal-user-agent: ^6.0.0
checksum: 342fece6db4470ee489e710af8aa14ffd3a89e666815a15fadbabe3a382932a714d5bece24375f0ca39f3310f9d10bd28bc348210282cbc6701d27dd9b2ff7ed
languageName: node
linkType: hard

"@octokit/auth-oauth-app@npm:^5.0.0":
version: 5.0.3
resolution: "@octokit/auth-oauth-app@npm:5.0.3"
dependencies:
"@octokit/auth-oauth-device": ^4.0.0
"@octokit/auth-oauth-user": ^2.0.0
"@octokit/request": ^6.0.0
"@octokit/types": ^7.0.0
"@types/btoa-lite": ^1.0.0
btoa-lite: ^1.0.0
universal-user-agent: ^6.0.0
checksum: 7841b7f1ad0552183a7346633f708ac9ce96ff4dbe420a5e0971b3d1de28a5cd3ae4f018ca60d44ab7e921bd50f0e90ddcd3456ca0d0b84adbc52c34caf3df77
languageName: node
linkType: hard

"@octokit/auth-oauth-device@npm:^4.0.0":
version: 4.0.2
resolution: "@octokit/auth-oauth-device@npm:4.0.2"
dependencies:
"@octokit/oauth-methods": ^2.0.0
"@octokit/request": ^6.0.0
"@octokit/types": ^7.0.0
universal-user-agent: ^6.0.0
checksum: 49cd76ae8644fa537a6dd0155b85321a46a517d2064a0cb69ee2a9cfa5b081cd70a906958e002ee4e63895e966d942a28b2be9c527490f25b8685e229d17addd
languageName: node
linkType: hard

"@octokit/auth-oauth-user@npm:^2.0.0":
version: 2.0.3
resolution: "@octokit/auth-oauth-user@npm:2.0.3"
dependencies:
"@octokit/auth-oauth-device": ^4.0.0
"@octokit/oauth-methods": ^2.0.0
"@octokit/request": ^6.0.0
"@octokit/types": ^7.0.0
btoa-lite: ^1.0.0
universal-user-agent: ^6.0.0
checksum: a62f85021bf251de3987abdda747079f5b479d37de898f35c92816dda0e538a8837e1487ee639414fa83ab710d786c8bc1ea84c39c6a505f5d47dcc45beb9fb7
languageName: node
linkType: hard

"@octokit/auth-token@npm:^3.0.0":
version: 3.0.1
resolution: "@octokit/auth-token@npm:3.0.1"
Expand Down Expand Up @@ -980,6 +1039,26 @@ __metadata:
languageName: node
linkType: hard

"@octokit/oauth-authorization-url@npm:^5.0.0":
version: 5.0.0
resolution: "@octokit/oauth-authorization-url@npm:5.0.0"
checksum: bc457c4af9559e9e8f752e643fc9d116247f4e4246e69959d99b9e39196c93d7af53c1c8e3bd946bd0e4fc29f7ba27efe9bced8525ffa41fe45ef56a8281014b
languageName: node
linkType: hard

"@octokit/oauth-methods@npm:^2.0.0":
version: 2.0.3
resolution: "@octokit/oauth-methods@npm:2.0.3"
dependencies:
"@octokit/oauth-authorization-url": ^5.0.0
"@octokit/request": ^6.0.0
"@octokit/request-error": ^3.0.0
"@octokit/types": ^7.0.0
btoa-lite: ^1.0.0
checksum: 9cc4635128eb665217ed8ba0aaeb245fd14a09c2c4781e58346e1716cfa122482de9ea5b46a5a763526c71068e6700539da3af451bb9bc7059218a9fb7be352f
languageName: node
linkType: hard

"@octokit/openapi-types@npm:^13.9.0":
version: 13.9.0
resolution: "@octokit/openapi-types@npm:13.9.0"
Expand Down Expand Up @@ -1372,6 +1451,13 @@ __metadata:
languageName: node
linkType: hard

"@types/btoa-lite@npm:^1.0.0":
version: 1.0.0
resolution: "@types/btoa-lite@npm:1.0.0"
checksum: 4d0c3c36cc8aa5669d286d62ca45d925e3ea0db75222ebacb0d9f4fd7822b8e162da8773887e045c11d64c42373807d2ab2ad97a5d8a683d2e1c981e6a05ce33
languageName: node
linkType: hard

"@types/connect@npm:*":
version: 3.4.35
resolution: "@types/connect@npm:3.4.35"
Expand Down Expand Up @@ -1491,13 +1577,29 @@ __metadata:
languageName: node
linkType: hard

"@types/jsonwebtoken@npm:^8.3.3":
version: 8.5.9
resolution: "@types/jsonwebtoken@npm:8.5.9"
dependencies:
"@types/node": "*"
checksum: 33815ab02d1371b423118316b7706d2f2ec03eeee5e1494be72da50425d2384e5e0a09ea193f7a5ab4b4f6a9c5847147305f50e965f3d927a95bdf8adb471b2a
languageName: node
linkType: hard

"@types/long@npm:^4.0.0":
version: 4.0.2
resolution: "@types/long@npm:4.0.2"
checksum: d16cde7240d834cf44ba1eaec49e78ae3180e724cd667052b194a372f350d024cba8dd3f37b0864931683dab09ca935d52f0c4c1687178af5ada9fc85b0635f4
languageName: node
linkType: hard

"@types/lru-cache@npm:^5.1.0":
version: 5.1.1
resolution: "@types/lru-cache@npm:5.1.1"
checksum: e1d6c0085f61b16ec5b3073ec76ad1be4844ea036561c3f145fc19f71f084b58a6eb600b14128aa95809d057d28f1d147c910186ae51219f58366ffd2ff2e118
languageName: node
linkType: hard

"@types/mime@npm:*":
version: 3.0.1
resolution: "@types/mime@npm:3.0.1"
Expand Down Expand Up @@ -2491,6 +2593,20 @@ __metadata:
languageName: node
linkType: hard

"btoa-lite@npm:^1.0.0":
version: 1.0.0
resolution: "btoa-lite@npm:1.0.0"
checksum: c2d61993b801f8e35a96f20692a45459c753d9baa29d86d1343e714f8d6bbe7069f1a20a5ae868488f3fb137d5bd0c560f6fbbc90b5a71050919d2d2c97c0475
languageName: node
linkType: hard

"buffer-equal-constant-time@npm:1.0.1":
version: 1.0.1
resolution: "buffer-equal-constant-time@npm:1.0.1"
checksum: 80bb945f5d782a56f374b292770901065bad21420e34936ecbe949e57724b4a13874f735850dd1cc61f078773c4fb5493a41391e7bda40d1fa388d6bd80daaab
languageName: node
linkType: hard

"buffer-from@npm:^1.0.0, buffer-from@npm:^1.1.0":
version: 1.1.2
resolution: "buffer-from@npm:1.1.2"
Expand Down Expand Up @@ -3208,6 +3324,15 @@ __metadata:
languageName: node
linkType: hard

"ecdsa-sig-formatter@npm:1.0.11":
version: 1.0.11
resolution: "ecdsa-sig-formatter@npm:1.0.11"
dependencies:
safe-buffer: ^5.0.1
checksum: 207f9ab1c2669b8e65540bce29506134613dd5f122cccf1e6a560f4d63f2732d427d938f8481df175505aad94583bcb32c688737bb39a6df0625f903d6d93c03
languageName: node
linkType: hard

"ee-first@npm:1.1.1":
version: 1.1.1
resolution: "ee-first@npm:1.1.1"
Expand Down Expand Up @@ -4875,13 +5000,52 @@ __metadata:
languageName: node
linkType: hard

"jsonwebtoken@npm:^8.5.1":
version: 8.5.1
resolution: "jsonwebtoken@npm:8.5.1"
dependencies:
jws: ^3.2.2
lodash.includes: ^4.3.0
lodash.isboolean: ^3.0.3
lodash.isinteger: ^4.0.4
lodash.isnumber: ^3.0.3
lodash.isplainobject: ^4.0.6
lodash.isstring: ^4.0.1
lodash.once: ^4.0.0
ms: ^2.1.1
semver: ^5.6.0
checksum: 93c9e3f23c59b758ac88ba15f4e4753b3749dfce7a6f7c40fb86663128a1e282db085eec852d4e0cbca4cefdcd3a8275ee255dbd08fcad0df26ad9f6e4cc853a
languageName: node
linkType: hard

"just-extend@npm:^4.0.2":
version: 4.2.1
resolution: "just-extend@npm:4.2.1"
checksum: ff9fdede240fad313efeeeb68a660b942e5586d99c0058064c78884894a2690dc09bba44c994ad4e077e45d913fef01a9240c14a72c657b53687ac58de53b39c
languageName: node
linkType: hard

"jwa@npm:^1.4.1":
version: 1.4.1
resolution: "jwa@npm:1.4.1"
dependencies:
buffer-equal-constant-time: 1.0.1
ecdsa-sig-formatter: 1.0.11
safe-buffer: ^5.0.1
checksum: ff30ea7c2dcc61f3ed2098d868bf89d43701605090c5b21b5544b512843ec6fd9e028381a4dda466cbcdb885c2d1150f7c62e7168394ee07941b4098e1035e2f
languageName: node
linkType: hard

"jws@npm:^3.2.2":
version: 3.2.2
resolution: "jws@npm:3.2.2"
dependencies:
jwa: ^1.4.1
safe-buffer: ^5.0.1
checksum: f0213fe5b79344c56cd443428d8f65c16bf842dc8cb8f5aed693e1e91d79c20741663ad6eff07a6d2c433d1831acc9814e8d7bada6a0471fbb91d09ceb2bf5c2
languageName: node
linkType: hard

"levn@npm:^0.4.1":
version: 0.4.1
resolution: "levn@npm:0.4.1"
Expand Down Expand Up @@ -4963,6 +5127,48 @@ __metadata:
languageName: node
linkType: hard

"lodash.includes@npm:^4.3.0":
version: 4.3.0
resolution: "lodash.includes@npm:4.3.0"
checksum: 71092c130515a67ab3bd928f57f6018434797c94def7f46aafa417771e455ce3a4834889f4267b17887d7f75297dfabd96231bf704fd2b8c5096dc4a913568b6
languageName: node
linkType: hard

"lodash.isboolean@npm:^3.0.3":
version: 3.0.3
resolution: "lodash.isboolean@npm:3.0.3"
checksum: b70068b4a8b8837912b54052557b21fc4774174e3512ed3c5b94621e5aff5eb6c68089d0a386b7e801d679cd105d2e35417978a5e99071750aa2ed90bffd0250
languageName: node
linkType: hard

"lodash.isinteger@npm:^4.0.4":
version: 4.0.4
resolution: "lodash.isinteger@npm:4.0.4"
checksum: 6034821b3fc61a2ffc34e7d5644bb50c5fd8f1c0121c554c21ac271911ee0c0502274852845005f8651d51e199ee2e0cfebfe40aaa49c7fe617f603a8a0b1691
languageName: node
linkType: hard

"lodash.isnumber@npm:^3.0.3":
version: 3.0.3
resolution: "lodash.isnumber@npm:3.0.3"
checksum: 913784275b565346255e6ae6a6e30b760a0da70abc29f3e1f409081585875105138cda4a429ff02577e1bc0a7ae2a90e0a3079a37f3a04c3d6c5aaa532f4cab2
languageName: node
linkType: hard

"lodash.isplainobject@npm:^4.0.6":
version: 4.0.6
resolution: "lodash.isplainobject@npm:4.0.6"
checksum: 29c6351f281e0d9a1d58f1a4c8f4400924b4c79f18dfc4613624d7d54784df07efaff97c1ff2659f3e085ecf4fff493300adc4837553104cef2634110b0d5337
languageName: node
linkType: hard

"lodash.isstring@npm:^4.0.1":
version: 4.0.1
resolution: "lodash.isstring@npm:4.0.1"
checksum: eaac87ae9636848af08021083d796e2eea3d02e80082ab8a9955309569cb3a463ce97fd281d7dc119e402b2e7d8c54a23914b15d2fc7fff56461511dc8937ba0
languageName: node
linkType: hard

"lodash.memoize@npm:4.x":
version: 4.1.2
resolution: "lodash.memoize@npm:4.1.2"
Expand All @@ -4977,6 +5183,13 @@ __metadata:
languageName: node
linkType: hard

"lodash.once@npm:^4.0.0":
version: 4.1.1
resolution: "lodash.once@npm:4.1.1"
checksum: d768fa9f9b4e1dc6453be99b753906f58990e0c45e7b2ca5a3b40a33111e5d17f6edf2f768786e2716af90a8e78f8f91431ab8435f761fef00f9b0c256f6d245
languageName: node
linkType: hard

"lodash.snakecase@npm:^4.1.1":
version: 4.1.1
resolution: "lodash.snakecase@npm:4.1.1"
Expand Down Expand Up @@ -6415,6 +6628,15 @@ __metadata:
languageName: node
linkType: hard

"semver@npm:^5.6.0":
version: 5.7.1
resolution: "semver@npm:5.7.1"
bin:
semver: ./bin/semver
checksum: 57fd0acfd0bac382ee87cd52cd0aaa5af086a7dc8d60379dfe65fea491fb2489b6016400813930ecd61fd0952dae75c115287a1b16c234b1550887117744dfaf
languageName: node
linkType: hard

"send@npm:0.18.0":
version: 0.18.0
resolution: "send@npm:0.18.0"
Expand Down Expand Up @@ -6477,6 +6699,7 @@ __metadata:
"@nestjs/schematics": ^8.0.10
"@nestjs/testing": ^8.4.4
"@nestjs/websockets": ^8.4.4
"@octokit/auth-app": ^4.0.6
"@octokit/rest": ^19.0.4
"@octokit/webhooks": ^10.1.5
"@octokit/webhooks-types": ^6.4.0
Expand Down Expand Up @@ -7332,6 +7555,16 @@ __metadata:
languageName: node
linkType: hard

"universal-github-app-jwt@npm:^1.0.1":
version: 1.1.0
resolution: "universal-github-app-jwt@npm:1.1.0"
dependencies:
"@types/jsonwebtoken": ^8.3.3
jsonwebtoken: ^8.5.1
checksum: b6ebbe2533881639701e936ca54c9ec02aae1e9b36bf2495ed66632d5057e06a7d4a118ac38a3d96f63993e19a47ee6e282858e1f4a9c2ceede19a856ca08fcc
languageName: node
linkType: hard

"universal-user-agent@npm:^6.0.0":
version: 6.0.0
resolution: "universal-user-agent@npm:6.0.0"
Expand Down

0 comments on commit 70b583e

Please sign in to comment.