Skip to content

Commit be7b467

Browse files
committed
Initial
0 parents  commit be7b467

File tree

7 files changed

+928
-0
lines changed

7 files changed

+928
-0
lines changed

Diff for: .github/workflows/publish.yml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Publish Python 🐍 distribution 📦 to PyPI
2+
3+
on:
4+
push:
5+
tags:
6+
- '*'
7+
8+
jobs:
9+
build-n-publish:
10+
name: Build and publish Python 🐍 distribution 📦 to PyPI
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v3
15+
- name: Set up Python
16+
uses: actions/setup-python@v4
17+
with:
18+
python-version: "3.x"
19+
20+
- name: Install pypa/build
21+
run: >-
22+
python -m
23+
pip install
24+
build
25+
--user
26+
- name: Build a binary wheel and a source tarball
27+
run: >-
28+
python -m
29+
build
30+
--sdist
31+
--wheel
32+
--outdir dist/
33+
.
34+
35+
- name: Publish distribution 📦 to PyPI
36+
uses: pypa/gh-action-pypi-publish@release/v1
37+
with:
38+
password: ${{ secrets.PYPI_API_TOKEN }}

Diff for: .gitignore

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# Distribution / packaging
7+
.Python
8+
build/
9+
develop-eggs/
10+
dist/
11+
downloads/
12+
eggs/
13+
.eggs/
14+
lib/
15+
lib64/
16+
parts/
17+
sdist/
18+
var/
19+
wheels/
20+
*.egg-info/
21+
.installed.cfg
22+
*.egg
23+
24+
# PyCharm
25+
.idea/
26+
27+
# VS Code
28+
.vscode/
29+
30+
# Operating System Files
31+
.DS_Store
32+
Thumbs.db

Diff for: LICENSE

+674
Large diffs are not rendered by default.

Diff for: README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# IntegrationOS AuthKit SDK for Python
2+
3+
Secure token generation for [IntegrationOS AuthKit](https://docs.integrationos.com/docs/authkit)
4+
using [PyPI](https://pypi.org/).
5+
6+
## Installation
7+
8+
You can install the IntegrationOS AuthKit SDK using pip:
9+
10+
```
11+
pip install integrationos-authkit
12+
```
13+
14+
## Usage
15+
16+
Here's a quick example of how to use the SDK:
17+
18+
```python
19+
from integrationos import AuthKit
20+
21+
# Initialize the AuthKit with your secret
22+
auth_kit = AuthKit("your_secret_here")
23+
24+
# Create an embed token
25+
payload = {
26+
"group": "your_group",
27+
"label": "your_label",
28+
# Add other required parameters
29+
}
30+
31+
result = auth_kit.create(payload)
32+
print(result)
33+
```
34+
35+
You'll want to switch out the API Key for your own, which will later tell your frontend which integrations you'd like to
36+
make available to your users.
37+
38+
You'll also want to populate the `Group` and `Label` fields depending on how you want to organize and query your users'
39+
connected accounts. The Group is especially important as it's used to generate the
40+
unique [Connection Key](https://docs.integrationos.com/docs/setup) for the user once they successfully connect an
41+
account.
42+
43+
## Full Documentation
44+
45+
Please refer to the official [IntegrationOS AuthKit](https://docs.integrationos.com/docs/authkit) docs for a more
46+
holistic understanding of IntegrationOS AuthKit.

Diff for: integrationos/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .auth_kit import AuthKit
2+
3+
__all__ = ['AuthKit']
4+
5+
__version__ = "0.1.0"

Diff for: integrationos/auth_kit.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import time
2+
import requests
3+
from typing import Dict, List, Union
4+
5+
class AuthKit:
6+
def __init__(self, secret: str, configs: Dict = None):
7+
self.secret = secret
8+
self.configs = configs or {}
9+
self.environment = "live" if "sk_live_" in secret else "test"
10+
11+
def _get_url(self, type: str) -> str:
12+
services_url = self.configs.get("base_url", "https://api.integrationos.com")
13+
14+
if "localhost" in services_url:
15+
api_url = "http://localhost:3005"
16+
elif "development" in services_url:
17+
api_url = "https://development-api.integrationos.com"
18+
else:
19+
api_url = "https://api.integrationos.com"
20+
21+
urls = {
22+
"get_settings": f"{services_url}/internal/v1/settings/get",
23+
"create_event_link": f"{services_url}/internal/v1/event-links/create",
24+
"get_connection_definitions": f"{api_url}/v1/public/connection-definitions?limit=100",
25+
"create_embed_token": f"{services_url}/internal/v1/embed-tokens/create",
26+
"get_session_id": f"{services_url}/v1/public/generate-id/session_id",
27+
}
28+
return urls[type]
29+
30+
def _get_headers(self, type: str = "buildable") -> Dict[str, str]:
31+
if type == "buildable":
32+
return {
33+
"X-Buildable-Secret": self.secret,
34+
"Content-Type": "application/json",
35+
}
36+
elif type == "ios_secret":
37+
return {
38+
"x-integrationos-secret": self.secret
39+
}
40+
41+
def _api_call(self, method_type: str, url: str, payload: Dict = None, headers: Dict = None) -> Dict:
42+
kwargs = {}
43+
if payload:
44+
kwargs['json'] = payload
45+
if headers:
46+
kwargs['headers'] = headers
47+
48+
response = requests.request(method_type, url, **kwargs)
49+
response.raise_for_status()
50+
return response.json()
51+
52+
def _get_settings(self) -> Dict:
53+
return self._api_call("POST", self._get_url("get_settings"), headers=self._get_headers())
54+
55+
def _create_event_link(self, payload: Dict) -> Dict:
56+
payload["environment"] = "test" if self.secret.startswith("sk_test") else "live"
57+
payload["usageSource"] = "sdk"
58+
return self._api_call("POST", self._get_url("create_event_link"), payload, self._get_headers())
59+
60+
def _get_connection_definitions(self) -> Dict:
61+
return self._api_call("GET", self._get_url("get_connection_definitions"), headers=self._get_headers("ios_secret"))
62+
63+
def _get_session_id(self) -> Dict:
64+
return self._api_call("GET", self._get_url("get_session_id"))
65+
66+
def _create_embed_token(self, connected_platforms: List, event_links: Dict, settings: Dict) -> Dict:
67+
token_payload = {
68+
"linkSettings": {
69+
"connectedPlatforms": connected_platforms,
70+
"eventIncToken": event_links["token"]
71+
},
72+
"group": event_links["group"],
73+
"label": event_links["label"],
74+
"environment": "test" if self.secret.startswith("sk_test") else "live",
75+
"expiresAt": int(time.time() * 1000) + (5 * 1000 * 60),
76+
"sessionId": self._get_session_id()['id'],
77+
"features": settings["features"]
78+
}
79+
return self._api_call("POST", self._get_url("create_embed_token"), token_payload, self._get_headers())
80+
81+
def create(self, payload: Dict) -> Union[Dict, str]:
82+
try:
83+
settings = self._get_settings()
84+
event_link = self._create_event_link(payload)
85+
connection_definitions = self._get_connection_definitions()
86+
87+
active_connection_definitions = [cd for cd in connection_definitions.get("rows", []) if cd.get("active")]
88+
connected_platforms = [
89+
platform for platform in settings.get("connectedPlatforms", [])
90+
if platform.get("connectionDefinitionId") in [cd.get("_id") for cd in active_connection_definitions]
91+
and platform.get("active")
92+
and (
93+
self.environment == "live"
94+
and platform.get("environment") == "live"
95+
or self.environment == "test"
96+
and (platform.get("environment") == "test" or "environment" not in platform)
97+
)
98+
]
99+
100+
return self._create_embed_token(connected_platforms, event_link, settings)
101+
except requests.RequestException as e:
102+
return {"message": str(e)}

Diff for: setup.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from setuptools import setup, find_packages
2+
3+
with open("README.md", "r", encoding="utf-8") as fh:
4+
long_description = fh.read()
5+
6+
setup(
7+
name="integrationos-authkit",
8+
version="0.1.0",
9+
author="IntegrationOS",
10+
author_email="[email protected]",
11+
description="Secure token generation for IntegrationOS AuthKit in Python",
12+
long_description=long_description,
13+
long_description_content_type="text/markdown",
14+
url="https://github.com/integration-os/authkit-python",
15+
packages=['integrationos'],
16+
classifiers=[
17+
"Development Status :: 3 - Alpha",
18+
"Intended Audience :: Developers",
19+
"License :: OSI Approved :: MIT License",
20+
"Operating System :: OS Independent",
21+
"Programming Language :: Python :: 3",
22+
"Programming Language :: Python :: 3.6",
23+
"Programming Language :: Python :: 3.7",
24+
"Programming Language :: Python :: 3.8",
25+
"Programming Language :: Python :: 3.9",
26+
],
27+
python_requires=">=3.6",
28+
install_requires=[
29+
"requests>=2.25.1",
30+
],
31+
)

0 commit comments

Comments
 (0)