Skip to content

Commit

Permalink
probably working backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Repumba committed May 30, 2023
1 parent 7bde325 commit 16cb3b1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
2 changes: 2 additions & 0 deletions mwdb/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
GroupListResource,
GroupMemberResource,
GroupResource,
JoinGroupInviteLinkResource,
RequestGroupInviteLinkResource,
)
from mwdb.resources.karton import KartonAnalysisResource, KartonObjectResource
Expand Down Expand Up @@ -346,6 +347,7 @@ def require_auth():
api.add_resource(GroupResource, "/group/<name>")
api.add_resource(GroupMemberResource, "/group/<name>/member/<login>")
api.add_resource(RequestGroupInviteLinkResource, "/group/<name>/invite/<invited_user>")
api.add_resource(JoinGroupInviteLinkResource, "/group/join")

# OAuth endpoints
if app_config.mwdb.enable_oidc:
Expand Down
21 changes: 13 additions & 8 deletions mwdb/model/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,19 @@ def generate_group_invite_token(self, group_id, inviter):
inviter=inviter,
)

@staticmethod
def verify_group_invite_token(token):
result = User._verify_token(
token=token,
fields=[],
scope=AuthScope.group_invite,
)
return None if result is None else result[0]
def join_group_with_token(self, token):
data = verify_token(token, AuthScope.group_invite)

if data is None:
return False

group_id = data.get("group_id")
if group_id is None:
return False

group_obj = db.session.query(Group).filter(Group.id == group_id).first()

return group_obj.add_member(self)

@staticmethod
def verify_session_token(token) -> Optional[Tuple["User", Optional[str]]]:
Expand Down
20 changes: 15 additions & 5 deletions mwdb/resources/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,8 @@ def post(self, name, invited_user):
" not configured or unavailable."
)

return token


@rate_limited_resource
class JoinGroupInviteLinkResource(Resource):
Expand All @@ -703,17 +705,24 @@ def post(selt):
security:
- bearerAuth: []
parameters:
- in: query
name: token
schema:
type: string
description: token
tags:
- group
responses:
200:
description: When user joined group successfully
content:
application/json:
schema: GroupSuccessResponseSchema
400:
description: When request body is invalid
403:
description: When there was a problem with the token
409:
description: When user is already a member of this group
503:
description: |
Request canceled due to database statement timeout.
Expand All @@ -723,7 +732,8 @@ def post(selt):
if token is None:
raise Forbidden("Token not found")

invite_data = User.verify_group_invite_token(token)
success = g.auth_user.join_group_with_token(token)

if invite_data is None:
raise Forbidden("There was a problem while decoding your token")
if not success:
raise Forbidden("There was a problem while processing your request")
db.session.commit()
2 changes: 1 addition & 1 deletion mwdb/templates/mail/invitation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ Hi {login}

You have been invited to join new group.

To view the invitation click this link: {base_url}/.../?token={group_invite_token}
To view the invitation click this link: {base_url}/group/join/?token={group_invite_token}
4 changes: 2 additions & 2 deletions mwdb/web/src/commons/api/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ function setGroupAdmin(
return axios.put(`/group/${name}/member/${member}`, { group_admin });
}

function requestGroupInviteLink(name: string, invited_user: string){
return axios.post(`/group/${name}/invite/${invited_user}`)
function requestGroupInviteLink(name: string, invited_user: string) {
return axios.post(`/group/${name}/invite/${invited_user}`);
}

function getUsers(): GetUsersResponse {
Expand Down

0 comments on commit 16cb3b1

Please sign in to comment.