Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[B-06293] zt_registration checks if registrant is authorized #73

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion server/src/routes/zt_registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,45 @@ const cleanUpMembers = (address, apiToken, networkId) => {
.catch((e) => console.log("Unable to deauthorize - Error: ", e))
}

/**
* Check if a given user (identified by email) is alredy registered and if registration parameters are correct
* @param email
*/
const isVerifiedUser = async (email) => {
const mongodbApiKey = await SETTINGS.get('mongodb_api_key')
const mongodbApiUrl = "https://eu-central-1.aws.data.mongodb-api.com/app/data-fcrur/endpoint/data/v1/action/findOne"

let headers = new Headers()
headers.append("Content-Type", "application/json")
headers.append("api-key", mongodbApiKey)

let filter = {
"collection":"registrations",
"database":"opsconsoledb",
"dataSource":"Cluster0",
"filter": {
"email": email
}
}

let resp = await fetch(mongodbApiUrl, {
headers,
method: "POST",
body: JSON.stringify(filter)
})

let user = await resp.json()

if (user.document && user.document.registrationCode.length > 0) {
let host = user.document.registrationCode.find(el => el.role === "host")

if (host.approved) return true
}

console.log(`user ${email} was not found in registration database`)
return false
}

/**
* It adds a new member to the ZeroTier network.
* @returns A 200 status code and a message.
Expand All @@ -82,7 +121,9 @@ const handle = async req => {
const { data } = payload
const { email, holochain_agent_id, zerotier_address, holoport_url } = data

return addZeroTierMember(zerotier_address, holochain_agent_id, email)
if (isVerifiedUser(email)) return addZeroTierMember(zerotier_address, holochain_agent_id, email)

respond(401)
} catch (e) {
console.log(e)
return respond(401)
Expand Down
4 changes: 2 additions & 2 deletions server/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ command = "npm install && npm run build"
name = "auth-server"
route = "auth-server.holo.host/*"
kv_namespaces = [
{ binding = "SETTINGS", id = "5181f479e6d84fc9835c5195b08a7029"}
{ binding = "SETTINGS", id = "56ecfe1cf54d43839e2c867798e5003b"}
]

main = "dist/main.js"
main = "dist/main.js"