Skip to content
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
21 changes: 14 additions & 7 deletions auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,28 @@ function auth0Middleware() {
const uid = agent.split("id/")[1]
const user = new User(uid)
user.getSelf().then(async (u) => {
if (u?.profile) {
req.user = u
next()
return
}
user.data = {
_id: uid,
agent,
_sub: payload.sub,
email: payload.name,
profile: { displayName: payload.nickname },
}
user.save()
req.user = user
if(!u || !u?.profile) {
user.save()
req.user = user
next()
return
}
if(u?.inviteCode || u._sub.includes("temp-")) {
user.update()
req.user = user
next()
return
}
req.user = u
next()
return
})
} catch (error) {
next(error)
Expand Down
51 changes: 30 additions & 21 deletions classes/Project/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,37 @@ export default class Project {
const roles = this.parseRoles(rolesString)
const projectTitle = this.data?.label ?? this.data?.title ?? 'TPEN Project'
let message = `You have been invited to the TPEN project ${projectTitle}.
View project <a href='https://three.t-pen.org/project/${this.data._id}'>here</a>.`
View project <a href='${process.env.TPENINTERFACES}project/${this.data._id}'>here</a>.`
if (user) {
await this.inviteExistingTPENUser(user._id, roles)
} else {
const inviteCode = await this.inviteNewTPENUser(email, roles)
// We will replace this URL with the correct url
const url = `https://three.t-pen.org/login?invite-code=${inviteCode}`
message += `<p>Click the button below to get started with your project</p>
<button class = "buttonStyle" ><a href=${url} >Get Started</a> </button>
or copy the following link into your web browser <a href=${url}>${url}</a> </p>`
}
else {
const inviteData = await this.inviteNewTPENUser(email, roles)
const returnTo = encodeURIComponent(`${process.env.TPENINTERFACES}project?projectID=${this.data._id}&inviteCode=${inviteData.tpenUserID}`)
// Signup starting at the TPEN3 public site
const signup = `${process.env.TPENTHREE}login
?inviteCode=${inviteData.tpenUserID}
&returnTo=${returnTo}
`
// TODO decline endpoint in TPEN Services
const decline = `${process.env.TPENINTERFACES}project/decline
?inviteCode=${inviteData.tpenUserID}
&groupID=${inviteData.tpenGroupID}
`
message += `
<p>
Click the button below to get started with your project</p>
<button class="buttonStyle" ><a href="${signup}">Get Started</a></button>
or copy the following link into your web browser <a href="${signup}">${signup}</a>
</p>
<p>
This E-mail address may be visible to members of the project so that they know
about the potential of new members. You may decline this invitation which will keep
you out of the project and remove the visibility of this E-mail address from project details. <br>
<a href="${decline}">Click here to decline the invitation.</a>
</p>
`
}

await sendMail(email, `Invitation to ${projectTitle}`, message)
return this
} catch (error) {
Expand Down Expand Up @@ -133,15 +152,14 @@ export default class Project {

async inviteNewTPENUser(email, roles) {
const user = new User()
const inviteCode = this.#generateInviteCode(user._id)
const inviteCode = user._id
const agent = `https://store.rerum.io/v1/id/${user._id}`
const profile = { displayName: email.split("@")[0] }
const _sub = `temp-${user._id}` // This is a temporary sub for the user until they verify their email
user.data = { email, _sub, profile, agent, inviteCode }
await user.save()
await this.inviteExistingTPENUser(user._id, roles)

return inviteCode
return { "tpenUserID":user._id, "tpenGroupID":this.data.group }
}

async removeMember(userId) {
Expand Down Expand Up @@ -238,15 +256,6 @@ export default class Project {
return await database.save(this.data, process.env.TPENPROJECTS)
}

#generateInviteCode(userId) {
const date = Date.now().toString()
const data = `${date}:${userId}`

const hash = createHash("sha256")
hash.update(data)
return hash.digest("hex")
}

async #load() {
return database.getById(this._id, "projects").then((resp) => {
this.data = resp
Expand Down
9 changes: 9 additions & 0 deletions classes/User/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ export default class User {
*/

async update(){
if (!this._id) {
throw new Error("User must have an _id")
}
if (!this.data.email) {
throw new Error("User must have an email")
}
if (!this.data.profile?.displayName) {
throw new Error("User must have a profile with a displayName")
}
return database.update(this.data, "users")
}

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function makeCleanFileFromMarkdown(file) {
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://three.t-pen.org/assets/css/main.css">
<link rel="stylesheet" href="${process.env.TPENTHREE}assets/css/main.css">
<title>API Documentation</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
Expand Down
Loading