Skip to content

Commit

Permalink
Add auth (#27)
Browse files Browse the repository at this point in the history
* wip

* polishing

* trailing comma fixed

* wip

* refactoring

* fix CI

* fix CI

* fix CI

* fix CI

* fix CI

* fix CI

* set default auth to be none

* resolve merge conflict
  • Loading branch information
davorrunje authored Nov 11, 2024
1 parent 5a4f849 commit e52bc88
Show file tree
Hide file tree
Showing 19 changed files with 269 additions and 9 deletions.
58 changes: 58 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "python-3.12",
"image": "mcr.microsoft.com/devcontainers/python:3.12",
"workspaceFolder": "/workspaces/cookiecutter-fastagency",
// "runArgs": [],
"remoteEnv": {},
"features": {
"ghcr.io/devcontainers/features/common-utils:2": {
"installZsh": true,
"installOhMyZsh": true,
"configureZshAsDefaultShell": true,
"username": "vscode",
"userUid": "1000",
"userGid": "1000"
// "upgradePackages": "true"
},
// "ghcr.io/devcontainers/features/python:1": {},
// "ghcr.io/devcontainers/features/node:1": {},
// The below configuration with "version" set to "latest" fails in codespace
// "ghcr.io/devcontainers/features/git:1": {
// "version": "latest",
// "ppa": true
// },
"ghcr.io/devcontainers/features/git:1": {},
// "ghcr.io/devcontainers/features/git-lfs:1": {},
// "ghcr.io/robbert229/devcontainer-features/postgresql-client:1": {}
},
"updateContentCommand": "bash .devcontainer/setup.sh",
// "postCreateCommand": "npm install && npx playwright install-deps && npx playwright install",
"customizations": {
"vscode": {
"settings": {
"python.linting.enabled": true,
"python.testing.pytestEnabled": true,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "always"
},
"[python]": {
"editor.defaultFormatter": "ms-python.vscode-pylance"
},
"editor.rulers": [
80
]
},
"extensions": [
"ms-python.python",
"ms-toolsai.jupyter",
"ms-toolsai.vscode-jupyter-cell-tags",
"ms-toolsai.jupyter-keymap",
"ms-toolsai.jupyter-renderers",
"ms-toolsai.vscode-jupyter-slideshow",
"ms-python.vscode-pylance",
"ms-playwright.playwright"
]
}
}
}
7 changes: 7 additions & 0 deletions .devcontainer/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# update pip
pip install --upgrade pip

pip install cookiecutter pre-commit detect-secrets

# install pre-commit hook if not installed already
pre-commit install
21 changes: 21 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
exclude: |
(?x)^(
^{{cookiecutter.project_slug}}/.devcontainer/docker-compose.yml|
^{{cookiecutter.project_slug}}/.github/workflows/test.yml
)
- id: check-added-large-files

- repo: https://github.com/Yelp/detect-secrets
rev: v1.5.0
hooks:
- id: detect-secrets
args: ["--baseline", ".secrets.baseline"]
127 changes: 127 additions & 0 deletions .secrets.baseline
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"version": "1.5.0",
"plugins_used": [
{
"name": "ArtifactoryDetector"
},
{
"name": "AWSKeyDetector"
},
{
"name": "AzureStorageKeyDetector"
},
{
"name": "Base64HighEntropyString",
"limit": 4.5
},
{
"name": "BasicAuthDetector"
},
{
"name": "CloudantDetector"
},
{
"name": "DiscordBotTokenDetector"
},
{
"name": "GitHubTokenDetector"
},
{
"name": "GitLabTokenDetector"
},
{
"name": "HexHighEntropyString",
"limit": 3.0
},
{
"name": "IbmCloudIamDetector"
},
{
"name": "IbmCosHmacDetector"
},
{
"name": "IPPublicDetector"
},
{
"name": "JwtTokenDetector"
},
{
"name": "KeywordDetector",
"keyword_exclude": ""
},
{
"name": "MailchimpDetector"
},
{
"name": "NpmDetector"
},
{
"name": "OpenAIDetector"
},
{
"name": "PrivateKeyDetector"
},
{
"name": "PypiTokenDetector"
},
{
"name": "SendGridDetector"
},
{
"name": "SlackDetector"
},
{
"name": "SoftlayerDetector"
},
{
"name": "SquareOAuthDetector"
},
{
"name": "StripeDetector"
},
{
"name": "TelegramBotTokenDetector"
},
{
"name": "TwilioKeyDetector"
}
],
"filters_used": [
{
"path": "detect_secrets.filters.allowlist.is_line_allowlisted"
},
{
"path": "detect_secrets.filters.common.is_ignored_due_to_verification_policies",
"min_level": 2
},
{
"path": "detect_secrets.filters.heuristic.is_indirect_reference"
},
{
"path": "detect_secrets.filters.heuristic.is_likely_id_string"
},
{
"path": "detect_secrets.filters.heuristic.is_lock_file"
},
{
"path": "detect_secrets.filters.heuristic.is_not_alphanumeric_string"
},
{
"path": "detect_secrets.filters.heuristic.is_potential_uuid"
},
{
"path": "detect_secrets.filters.heuristic.is_prefixed_with_dollar_sign"
},
{
"path": "detect_secrets.filters.heuristic.is_sequential_string"
},
{
"path": "detect_secrets.filters.heuristic.is_swagger_file"
},
{
"path": "detect_secrets.filters.heuristic.is_templated_secret"
}
],
"results": {},
"generated_at": "2024-11-07T14:40:41Z"
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,4 @@
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.
limitations under the License.
3 changes: 2 additions & 1 deletion cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"project_name": "My FastAgency App",
"project_slug": "{{ cookiecutter.project_name.lower().replace(' ', '_').replace('-', '_') }}",
"app_type": ["fastapi+mesop", "mesop", "nats+fastapi+mesop"],
"python_version": ["3.12", "3.11", "3.10"]
"python_version": ["3.12", "3.11", "3.10"],
"authentication": ["none", "google"]
}
2 changes: 1 addition & 1 deletion hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
if not path:
continue
paths = list(glob.glob(path)) if "*" in path else [path]

for p in paths:
if p and os.path.exists(p):
if os.path.isdir(p):
Expand Down
Empty file added scripts/test_all.py
Empty file.
6 changes: 5 additions & 1 deletion scripts/test_cookiecutter.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/bin/bash
#!/bin/env bash

# This script is used to test the cookiecutter template
# It will create a new project using the cookiecutter template under the generated/$1-$2-$3/ directory
# The script accepts two positional arguments: app-type and python-version
# The cookiecutter replay file is used to provide the default values for the cookiecutter template

set -o errexit

# Accept two arguments: app-type and python-version
echo -e "\033[32mGenerating project using cookiecutter template with app-type: $1, python-version: $2 and authentication: $3\033[0m"
rm -rf generated/$1-$2-$3
Expand All @@ -17,6 +19,8 @@ cd generated/$1-$2-$3/my_fastagency_app && pip install -e .[dev] && cd ../../..
# Initialize git in the generated project(needed for pre-commit)
echo -e "\033[32mInitializing git in the generated project\033[0m"
cd generated/$1-$2-$3/my_fastagency_app && git init && git add . && cd ../../..
# uncomment this for debugging
# cd generated/$1-$2-$3/my_fastagency_app && git commit -m "init" --no-verify && cd ../../..

# Run pre-commit
echo -e "\033[32mRunning pre-commit\033[0m"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"ANTHROPIC_API_KEY": {
"description": "This key is optional and only needed if you are working with Anthropic API-related code. Leave it blank if not required. You can always set it later as an environment variable in the codespace terminal."
},
}
},
"shutdownAction": "stopCompose",
"workspaceFolder": "/workspaces/{{cookiecutter.project_slug}}",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
serviceAccountKey.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# list all allowed users
- [email protected]
# - [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# You can find the guide for getting the config values here:
# https://support.google.com/firebase/answer/7015592?hl=en#web&zippy=%2Cin-this-article
api_key: "<put your api_key here>"
auth_domain: "<put your auth_domain here>"
project_id: "<put your project_id here>"
storage_bucket: "<put your storage_bucket here>"
messaging_sender_id: "<put your messaging_sender_id here>"
app_id: "<put your app_id here>"
6 changes: 5 additions & 1 deletion {{cookiecutter.project_slug}}/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ RUN apt-get update && apt-get install -y --no-install-recommends nginx gettext \

COPY {{cookiecutter.project_slug}} /app/{{cookiecutter.project_slug}}

COPY pyproject.toml README.md docker/run_fastagency.sh docker/nginx.conf.template /app/
COPY pyproject.toml README.md /app/
COPY docker/content/* /app/
{% if cookiecutter.authentication == "google" %}
COPY deployment/firebase/* /app/
{% endif %}

RUN pip install --upgrade pip && pip install --no-cache-dir -e "."

Expand Down
5 changes: 4 additions & 1 deletion {{cookiecutter.project_slug}}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ version = "0.1.0"
name = "{{cookiecutter.project_slug}}"

dependencies = [
"fastagency[autogen,mesop,server{% if "fastapi" in cookiecutter.app_type %},fastapi{% endif %}{% if "nats" in cookiecutter.app_type %},nats{% endif %}]>=0.3.0",
"fastagency[autogen,mesop,server{% if "fastapi" in cookiecutter.app_type %},fastapi{% endif %}{% if "nats" in cookiecutter.app_type %},nats{% endif %}{% if cookiecutter.authentication == "google" %},firebase{% endif %}]>=0.3.0",
{% if cookiecutter.authentication == "google" %}
"PyYAML>=6.0.2",
{% endif %}
]

[project.optional-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion {{cookiecutter.project_slug}}/scripts/run_docker.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

docker run -d --name deploy_fastagency -e OPENAI_API_KEY=$OPENAI_API_KEY {% if "nats" in cookiecutter.app_type %}-e NATS_URL=$NATS_URL -e FASTAGENCY_NATS_PASSWORD=$FASTAGENCY_NATS_PASSWORD -p 8000:8000{% endif %}{% if "fastapi" in cookiecutter.app_type %} -p 8008:8008{% endif %} -p 8888:8888 {% if "nats" in cookiecutter.app_type %}--network=host{% endif %} deploy_fastagency
docker run -it -e OPENAI_API_KEY=$OPENAI_API_KEY {% if cookiecutter.authentication == "google"%}-e GOOGLE_APPLICATION_CREDENTIALS="serviceAccountKey.json"{% endif %} {% if "nats" in cookiecutter.app_type %}-e NATS_URL=$NATS_URL -e FASTAGENCY_NATS_PASSWORD=$FASTAGENCY_NATS_PASSWORD -p 8000:8000{% endif %}{% if "fastapi" in cookiecutter.app_type %} -p 8008:8008{% endif %} -p 8888:8888 {% if "nats" in cookiecutter.app_type %}--network=host{% endif %} deploy_fastagency
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
{%- if cookiecutter.authentication == "google" -%}
from pathlib import Path

import yaml
{% endif -%}
from fastagency.adapters.fastapi import FastAPIAdapter
from fastagency.app import FastAgency
from fastagency.ui.mesop import MesopUI
from fastagency.ui.mesop import MesopUI{% if cookiecutter.authentication == "google" %}
from fastagency.ui.mesop.auth.firebase import FirebaseAuth, FirebaseConfig{% endif %}

fastapi_url = "http://localhost:8008"

provider = FastAPIAdapter.create_provider(
fastapi_url=fastapi_url,
)

{%- if cookiecutter.authentication == "google" %}
with Path("firebase_config.yaml").open() as f:
firebase_config = FirebaseConfig(**yaml.safe_load(f))
with Path("allowed_users.yaml").open() as f:
allowed_users = yaml.safe_load(f)

auth = FirebaseAuth(
sign_in_methods=["google"],
config=firebase_config,
allowed_users=allowed_users,
)

ui = MesopUI(auth=auth)
{% else %}
ui = MesopUI()
{% endif %}

app = FastAgency(
provider=provider,
ui=MesopUI(),
Expand Down

0 comments on commit e52bc88

Please sign in to comment.