Skip to content

Commit fbe22ea

Browse files
committed
fix: wrong document property lead to error when checking github antisybil
1 parent 9612144 commit fbe22ea

File tree

2 files changed

+6
-31
lines changed

2 files changed

+6
-31
lines changed

packages/actions/src/helpers/security.ts

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,45 @@
11
import fetch from "@adobe/node-fetch-retry"
2-
32
/**
43
* This function will return the number of public repos of a user
54
* @param user <string> The username of the user
65
* @returns <number> The number of public repos
76
*/
87
const getNumberOfPublicReposGitHub = async (user: string): Promise<number> => {
9-
const response = await fetch(`https://api.github.com/users/${user}/repos`, {
8+
const response = await fetch(`https://api.github.com/user/${user}/repos`, {
109
method: "GET",
1110
headers: {
1211
Authorization: `token ${process.env.GITHUB_ACCESS_TOKEN!}`
1312
}
1413
})
1514
if (response.status !== 200)
1615
throw new Error("It was not possible to retrieve the number of public repositories. Please try again.")
17-
1816
const jsonData: any = await response.json()
19-
2017
return jsonData.length
2118
}
22-
2319
/**
2420
* This function will return the number of followers of a user
2521
* @param user <string> The username of the user
2622
* @returns <number> The number of followers
2723
*/
2824
const getNumberOfFollowersGitHub = async (user: string): Promise<number> => {
29-
const response = await fetch(`https://api.github.com/users/${user}/followers`, {
25+
const response = await fetch(`https://api.github.com/user/${user}/followers`, {
3026
method: "GET",
3127
headers: {
3228
Authorization: `token ${process.env.GITHUB_ACCESS_TOKEN!}`
3329
}
3430
})
35-
3631
if (response.status !== 200)
3732
throw new Error("It was not possible to retrieve the number of followers. Please try again.")
38-
3933
const jsonData: any = await response.json()
40-
4134
return jsonData.length
4235
}
43-
4436
/**
4537
* This function will return the number of following of a user
4638
* @param user <string> The username of the user
4739
* @returns <number> The number of following users
4840
*/
4941
const getNumberOfFollowingGitHub = async (user: string): Promise<number> => {
50-
const response = await fetch(`https://api.github.com/users/${user}/following`, {
42+
const response = await fetch(`https://api.github.com/user/${user}/following`, {
5143
method: "GET",
5244
headers: {
5345
Authorization: `token ${process.env.GITHUB_ACCESS_TOKEN!}`

packages/backend/src/functions/user.ts

+3-20
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { logAndThrowError, makeError, printLog, SPECIFIC_ERRORS } from "../lib/e
99
import { LogLevel } from "../types/enums"
1010

1111
dotenv.config()
12-
1312
/**
1413
* Record the authenticated user information inside the Firestore DB upon authentication.
1514
* @dev the data is recorded in a new document in the `users` collection.
@@ -24,31 +23,24 @@ export const registerAuthUser = functions
2423
.onCreate(async (user: UserRecord) => {
2524
// Get DB.
2625
const firestore = admin.firestore()
27-
2826
// Get user information.
2927
if (!user.uid) logAndThrowError(SPECIFIC_ERRORS.SE_AUTH_NO_CURRENT_AUTH_USER)
30-
3128
// The user object has basic properties such as display name, email, etc.
3229
const { displayName } = user
3330
const { email } = user
3431
const { photoURL } = user
3532
const { emailVerified } = user
36-
3733
// Metadata.
3834
const { creationTime } = user.metadata
3935
const { lastSignInTime } = user.metadata
40-
4136
// The user's ID, unique to the Firebase project. Do NOT use
4237
// this value to authenticate with your backend server, if
4338
// you have one. Use User.getToken() instead.
4439
const { uid } = user
45-
4640
// Reference to a document using uid.
4741
const userRef = firestore.collection(commonTerms.collections.users.name).doc(uid)
48-
4942
// html encode the display name
5043
const encodedDisplayName = encode(displayName)
51-
5244
// we only do reputation check if the user is not a coordinator
5345
if (
5446
!(
@@ -60,18 +52,18 @@ export const registerAuthUser = functions
6052
// if provider == github.com let's use our functions to check the user's reputation
6153
if (user.providerData[0].providerId === "github.com") {
6254
const vars = getGitHubVariables()
55+
6356
// this return true or false
6457
try {
6558
const res = await githubReputation(
66-
user.displayName!,
59+
user.providerData[0].uid,
6760
vars.minimumFollowing,
6861
vars.minimumFollowers,
6962
vars.minimumPublicRepos
7063
)
7164
if (!res) {
7265
// Delete user
7366
await auth.deleteUser(user.uid)
74-
7567
// Throw error
7668
logAndThrowError(
7769
makeError(
@@ -89,13 +81,12 @@ export const registerAuthUser = functions
8981
makeError(
9082
"permission-denied",
9183
"There was an error while checking the user's Github reputation.",
92-
`There was an error while checking the user's Github reputation. This is likely due to GitHub rate limiting. Please contact the administrator if you think this is a mistake.`
84+
`${error}`
9385
)
9486
)
9587
}
9688
}
9789
}
98-
9990
// Set document (nb. we refer to providerData[0] because we use Github OAuth provider only).
10091
await userRef.set({
10192
name: encodedDisplayName,
@@ -109,10 +100,8 @@ export const registerAuthUser = functions
109100
photoURL: photoURL || "",
110101
lastUpdated: getCurrentServerTimestampInMillis()
111102
})
112-
113103
printLog(`Authenticated user document with identifier ${uid} has been correctly stored`, LogLevel.DEBUG)
114104
})
115-
116105
/**
117106
* Set custom claims for role-based access control on the newly created user.
118107
* @notice this method is automatically triggered upon user authentication in the Firebase app
@@ -126,32 +115,26 @@ export const processSignUpWithCustomClaims = functions
126115
.onCreate(async (user: UserRecord) => {
127116
// Get user information.
128117
if (!user.uid) logAndThrowError(SPECIFIC_ERRORS.SE_AUTH_NO_CURRENT_AUTH_USER)
129-
130118
// Prepare state.
131119
let customClaims: any
132-
133120
// Check if user meets role criteria to be a coordinator.
134121
if (
135122
user.email &&
136123
(user.email.endsWith(`@${process.env.CUSTOM_CLAIMS_COORDINATOR_EMAIL_ADDRESS_OR_DOMAIN}`) ||
137124
user.email === process.env.CUSTOM_CLAIMS_COORDINATOR_EMAIL_ADDRESS_OR_DOMAIN)
138125
) {
139126
customClaims = { coordinator: true }
140-
141127
printLog(`Authenticated user ${user.uid} has been identified as coordinator`, LogLevel.DEBUG)
142128
} else {
143129
customClaims = { participant: true }
144-
145130
printLog(`Authenticated user ${user.uid} has been identified as participant`, LogLevel.DEBUG)
146131
}
147-
148132
try {
149133
// Set custom user claims on this newly created user.
150134
await admin.auth().setCustomUserClaims(user.uid, customClaims)
151135
} catch (error: any) {
152136
const specificError = SPECIFIC_ERRORS.SE_AUTH_SET_CUSTOM_USER_CLAIMS_FAIL
153137
const additionalDetails = error.toString()
154-
155138
logAndThrowError(makeError(specificError.code, specificError.message, additionalDetails))
156139
}
157140
})

0 commit comments

Comments
 (0)