|
| 1 | +import * as admin from 'firebase-admin'; |
| 2 | +import {Octokit} from '@octokit/rest'; |
| 3 | +import {createAppAuth} from '@octokit/auth-app'; |
| 4 | +import * as functions from 'firebase-functions'; |
| 5 | + |
| 6 | +/** Parameters for blocking a user. */ |
| 7 | +export interface BlockUserParams { |
| 8 | + username: string; |
| 9 | + blockUntil: string; |
| 10 | + context: string; |
| 11 | + comments: string; |
| 12 | +} |
| 13 | + |
| 14 | +/** Parameters for unblocking a user. */ |
| 15 | +export interface UnblockUserParams { |
| 16 | + username: string; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Convertor to ensure the data types for javascript and firestore storage are in sync. |
| 21 | + */ |
| 22 | +export const converter: admin.firestore.FirestoreDataConverter<BlockedUser> = { |
| 23 | + toFirestore: (user: BlockedUser) => { |
| 24 | + return user; |
| 25 | + }, |
| 26 | + fromFirestore: (data: admin.firestore.QueryDocumentSnapshot<BlockedUser>) => { |
| 27 | + return { |
| 28 | + username: data.get('username'), |
| 29 | + context: data.get('context'), |
| 30 | + comments: data.get('comments'), |
| 31 | + blockedBy: data.get('blockedBy'), |
| 32 | + blockUntil: new Date(data.get('blockUntil')), |
| 33 | + blockedOn: new Date(data.get('blockedOn')), |
| 34 | + }; |
| 35 | + }, |
| 36 | +}; |
| 37 | + |
| 38 | +/** Get the firestore collection for the blocked users, with the converter already set up. */ |
| 39 | +export const blockedUsersCollection = () => |
| 40 | + admin.firestore().collection('blockedUsers').withConverter(converter); |
| 41 | + |
| 42 | +/** A blocked user stored in Firestore. */ |
| 43 | +export interface BlockedUser extends admin.firestore.DocumentData { |
| 44 | + blockedBy: string; |
| 45 | + blockedOn: Date; |
| 46 | + username: string; |
| 47 | + blockUntil: Date; |
| 48 | + context: string; |
| 49 | + comments: string; |
| 50 | +} |
| 51 | + |
| 52 | +/** A CallableContext which is confirmed to already have an authorized user. */ |
| 53 | +interface AuthenticatedCallableContext extends functions.https.CallableContext { |
| 54 | + auth: NonNullable<functions.https.CallableContext['auth']>; |
| 55 | +} |
| 56 | + |
| 57 | +/** Verify that the incoming request is authenticated and authorized for access. */ |
| 58 | +export function checkAuthenticationAndAccess( |
| 59 | + context: functions.https.CallableContext, |
| 60 | +): asserts context is AuthenticatedCallableContext { |
| 61 | + // Authentication is managed by firebase as this occurs within the Firebase functions context. |
| 62 | + // If the user is unauthenticted, the authorization object will be undefined. |
| 63 | + if (context.auth == undefined) { |
| 64 | + // Throwing an HttpsError so that the client gets the error details. |
| 65 | + throw new functions.https.HttpsError('unauthenticated', 'This action requires authentication'); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/** Retrieves a Github client instance authenticated as the Angular Robot Github App. */ |
| 70 | +export async function getAuthenticatedGithubClient() { |
| 71 | + const GITHUB_APP_PEM = Buffer.from( |
| 72 | + process.env['ANGULAR_ROBOT_APP_PRIVATE_KEY']!, |
| 73 | + 'base64', |
| 74 | + ).toString('utf-8'); |
| 75 | + |
| 76 | + const applicationGithub = new Octokit({ |
| 77 | + authStrategy: createAppAuth, |
| 78 | + auth: {appId: process.env['ANGULAR_ROBOT_APP_ID']!, privateKey: GITHUB_APP_PEM}, |
| 79 | + }); |
| 80 | + /** The specific installation id for the provided repository. */ |
| 81 | + const {id: installation_id} = (await applicationGithub.apps.getOrgInstallation({org: 'angular'})) |
| 82 | + .data; |
| 83 | + /** A temporary github access token. */ |
| 84 | + const {token} = ( |
| 85 | + await applicationGithub.rest.apps.createInstallationAccessToken({installation_id}) |
| 86 | + ).data; |
| 87 | + |
| 88 | + return new Octokit({auth: token}); |
| 89 | +} |
0 commit comments