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

[WIP] [ENH] - Add User table to database #975

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Working on adding new user to the database
  • Loading branch information
peytondmurray committed Nov 11, 2024
commit 26060e1c84522b04c7914abefeaf194fac9df5c1
24 changes: 12 additions & 12 deletions conda-store-server/conda_store_server/_internal/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,16 +828,6 @@ class Role(Base):
name = Column(Enum(schema.Role), default=schema.BuildStatus.QUEUED)


class User(Base):
"""User which contains permissions to namespaces and environments."""

__tablename__ = "user"

id = Column(Integer, primary_key=True)
name = Column(Unicode, unique=True)
permissions = relationship("UserPermission", back_populates="user")


class UserPermission(Base):
"""The permissions a User has for an namespace/environment.

Expand All @@ -850,7 +840,17 @@ class UserPermission(Base):
__tablename__ = "userpermission"

id = Column(Integer, primary_key=True)
user_id = ForeignKey("user.id")
namespace_id = ForeignKey("namespace.id")
namespace_id = Column(Integer, ForeignKey("namespace.id"))
role_id = Column(Integer, ForeignKey("role.id"))
role = relationship(Role)


class User(Base):
"""User which contains permissions to namespaces and environments."""

__tablename__ = "user"

id = Column(Integer, primary_key=True)
name = Column(Unicode, unique=True)
permissions = relationship(UserPermission)
permissions_id = Column(Integer, ForeignKey('userpermission.id'))
5 changes: 0 additions & 5 deletions conda-store-server/conda_store_server/_internal/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,8 +830,3 @@ class APIPutSetting(APIResponse):
# GET /api/v1/usage/
class APIGetUsage(APIResponse):
data: Dict[str, Dict[str, Any]]


# PUT /api/v1/user/
class APIPutUser(APIAckResponse):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ async def api_post_token(
detail="Requested expiration of token is greater than current permissions",
)


with conda_store.get_db() as db:


return {
"status": "ok",
"data": {"token": auth.authentication.encrypt_token(new_entity)},
Expand Down Expand Up @@ -1486,14 +1490,3 @@ async def api_put_settings(
"data": None,
"message": f"global setting keys {list(data.keys())} updated",
}


@router_api.put(
"/user/",
response_model=schema.APIPutUser,
)
async def api_put_user(
request: Request,
auth: Authentication = Depends(dependencies.get_auth),
):
auth.authenticate_request(request)
20 changes: 19 additions & 1 deletion conda-store-server/conda_store_server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from __future__ import annotations

import re
from typing import Any, Dict, List, Union
import uuid
from typing import Any, Dict, List, Optional, Union

from sqlalchemy import distinct, func, null, or_
from sqlalchemy.orm import Query, aliased, session
Expand Down Expand Up @@ -823,3 +824,20 @@ def set_kvstore_key_values(db, prefix: str, d: Dict[str, Any], update: bool = Tr
elif update:
record.value = value
db.commit()


def set_new_user(
db: session.Session,
token: schema.AuthenticationToken,
username: Optional[str] = None,
):
# Parse the token into a set of namespace/environment permissions
user_permissions = []

# Add the user with the given permissions
db.add(
orm.User(
name=username if username else uuid.uuid4(),
permissions=user_permissions,
)
)
24 changes: 23 additions & 1 deletion conda-store-server/conda_store_server/server/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,29 @@ def post_logout_method(self, request: Request, next: Optional[str] = None):
response.set_cookie(self.cookie_name, "", domain=self.cookie_domain, expires=0)
return response

def authenticate_request(self, request: Request, require=False):
def authenticate_request(
self,
request: Request,
require: bool = False
) -> Optional[schema.AuthenticationToken]:
"""Authenticate a request.

Parameters
----------
request : Request
Web request to authenticate
require : bool
Require that there be a token in either the request's 'Authorization'
header or in the request cookies. If such a token exists, it must be able to
be decrypted or parsed as a valid schema.AuthenticationToken; if no token
exists or the token isn't valid, a 401 will be returned if this argument is
True.

Returns
-------
Optional[schema.AuthenticationToken]
User authentication token (if present), else None
"""
if hasattr(request.state, "entity"):
pass # only authenticate once
elif request.cookies.get(self.cookie_name):
Expand Down