Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
theomonnom committed Sep 14, 2023
1 parent c8f35c3 commit 3549ee3
Show file tree
Hide file tree
Showing 38 changed files with 190 additions and 2 deletions.
18 changes: 18 additions & 0 deletions livekit/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2023 LiveKit, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""LiveKit Server SDK
"""

from .version import __version__
112 changes: 112 additions & 0 deletions livekit/api/access_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Copyright 2023 LiveKit, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import calendar
import dataclasses
import datetime

import jwt

DEFAULT_TTL = datetime.timedelta(hours=6)


@dataclasses.dataclass
class VideoGrants:
# actions on rooms
room_create: bool = False
room_list: bool = False
room_record: bool = False

# actions on a particular room
room_admin: bool = False
room_join: bool = False
room: str = ""

# permissions within a room
can_publish: bool = True
can_subscribe: bool = True
can_publish_data: bool = True

# TrackSource types that a participant may publish.
# When set, it supercedes CanPublish. Only sources explicitly set here can be
# published
can_publish_sources: list[str] = [] # keys keep track of each source

# by default, a participant is not allowed to update its own metadata
can_update_own_metadata: bool = False

# actions on ingresses
ingress_admin: bool = False # applies to all ingress

# participant is not visible to other participants (useful when making bots)
hidden: bool = False

# indicates to the room that current participant is a recorder
recorder: bool = False


@dataclasses.dataclass
class Claims:
name: str = ""
video: VideoGrants = dataclasses.field(default_factory=VideoGrants)
metadata: str = ""
sha256: str = ""


class AccessToken:
def __init__(self, api_key: str, api_secret: str):
self.api_key = api_key # iss
self.api_secret = api_secret
self.claims = Claims()

# default jwt claims
self.identity = "" # sub
self.ttl = DEFAULT_TTL # exp

def with_ttl(self, ttl: datetime.timedelta) -> 'AccessToken':
self.ttl = ttl
return self

def with_grants(self, grants: VideoGrants) -> 'AccessToken':
self.claims.video = grants
return self

def with_identity(self, identity: str) -> 'AccessToken':
self.identity = identity
return self

def with_name(self, name: str) -> 'AccessToken':
self.claims.name = name
return self

def with_metadata(self, metadata: str) -> 'AccessToken':
self.claims.metadata = metadata
return self

def with_sha256(self, sha256: str) -> 'AccessToken':
self.claims.sha256 = sha256
return self

def to_jwt(self) -> str:
claims = {
'sub': self.identity,
"iss": self.api_key,
"nbf": calendar.timegm(datetime.datetime.utcnow().utctimetuple()),
"exp": calendar.timegm(
(datetime.datetime.utcnow() + self.ttl).utctimetuple()
),
}

claims.update(dataclasses.asdict(self.claims))
return jwt.encode(claims, self.api_secret, algorithm='HS256')
1 change: 1 addition & 0 deletions livekit/api/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.0.1"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@

from typing import Optional

from livekit._proto import track_pb2 as proto_track

from ._ffi_client import FfiHandle, ffi_client
from ._proto import e2ee_pb2 as proto_e2ee
from ._proto import ffi_pb2 as proto_ffi
from ._proto import track_pb2 as proto_track
from .track import Track


Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
58 changes: 58 additions & 0 deletions setup_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2023 LiveKit, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import pathlib

import setuptools

here = pathlib.Path(__file__).parent.resolve()
about = {}
with open(os.path.join(here, 'livekit', 'api', 'version.py'), 'r') as f:
exec(f.read(), about)


setuptools.setup(
name="livekit-api",
version=about['__version__'],
description="LiveKit Python Server for LiveKit",
long_description=(here / "README.md").read_text(encoding="utf-8"),
long_description_content_type="text/markdown",
url="https://github.com/livekit/client-sdk-python",
classifiers=[
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Topic :: Multimedia :: Sound/Audio",
"Topic :: Multimedia :: Video",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3 :: Only",
],
keywords=["webrtc", "realtime", "audio", "video", "livekit"],
license="Apache-2.0",
packages=["livekit"],
python_requires=">=3.7.0",
install_requires=["pyjwt>=2.0.0",
"protobuf>=3.1.0",
"types-protobuf>=3.1.0"],
project_urls={
"Documentation": "https://docs.livekit.io",
"Website": "https://livekit.io/",
"Source": "https://github.com/livekit/client-sdk-python/",
},
)
File renamed without changes.

0 comments on commit 3549ee3

Please sign in to comment.