( context: functions.https.CallableRequest, )
| 57 | |
| 58 | /** Verify that the incoming request is authenticated and authorized for access. */ |
| 59 | export async function checkAuthenticationAndAccess( |
| 60 | context: functions.https.CallableRequest, |
| 61 | ): Promise<AuthenticatedCallableContext> { |
| 62 | // Authentication is managed by firebase as this occurs within the Firebase functions context. |
| 63 | // If the user is unauthenticted, the authorization object will be undefined. |
| 64 | if (context.auth == undefined) { |
| 65 | // Throwing an HttpsError so that the client gets the error details. |
| 66 | throw new functions.https.HttpsError('unauthenticated', 'This action requires authentication'); |
| 67 | } |
| 68 | |
| 69 | const user = await admin.auth().getUser(context.auth.uid); |
| 70 | const githubProvider = user.providerData.find((data) => data.providerId === 'github.com'); |
| 71 | |
| 72 | if (!githubProvider) { |
| 73 | throw new functions.https.HttpsError( |
| 74 | 'permission-denied', |
| 75 | 'You must link your Github account to use this application.', |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | const github = await getAuthenticatedGithubClient(); |
| 80 | |
| 81 | try { |
| 82 | // Resolve the github username from the connected UID |
| 83 | const {data: githubUser} = await github.request('GET /user/{user_id}', { |
| 84 | user_id: githubProvider.uid, |
| 85 | }); |
| 86 | |
| 87 | await github.orgs.checkMembershipForUser({ |
| 88 | org: 'angular', |
| 89 | username: githubUser.login, |
| 90 | }); |
| 91 | } catch (err: any) { |
| 92 | if (err.status === 404) { |
| 93 | throw new functions.https.HttpsError( |
| 94 | 'permission-denied', |
| 95 | 'You must be a member of the Angular Github organization.', |
| 96 | ); |
| 97 | } |
| 98 | throw err; |
| 99 | } |
| 100 | |
| 101 | return context as AuthenticatedCallableContext; |
| 102 | } |
| 103 | |
| 104 | /** Retrieves a Github client instance authenticated as the Angular Robot Github App. */ |
| 105 | export async function getAuthenticatedGithubClient(): Promise<Octokit> { |
no test coverage detected