MCPcopy Create free account
hub / github.com/angular/dev-infra / checkAuthenticationAndAccess

Function checkAuthenticationAndAccess

apps/functions/code-of-conduct/shared.ts:59–102  ·  view source on GitHub ↗
(
  context: functions.https.CallableRequest,
)

Source from the content-addressed store, hash-verified

57
58/** Verify that the incoming request is authenticated and authorized for access. */
59export 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. */
105export async function getAuthenticatedGithubClient(): Promise<Octokit> {

Callers 5

unblockUser.tsFile · 0.85
getBlockedUsers.tsFile · 0.85
blockUser.tsFile · 0.85
syncUsers.tsFile · 0.85
updateUser.tsFile · 0.85

Calls 1

Tested by

no test coverage detected