Skip to content

Commit

Permalink
Add firebase push notification plugin (#175)
Browse files Browse the repository at this point in the history
* Add firebase push notification plugin

Signed-off-by: jamshale <jamiehalebc@gmail.com>


---------

Signed-off-by: jamshale <jamiehalebc@gmail.com>
  • Loading branch information
jamshale authored Mar 18, 2024
1 parent dcc459e commit 8197d6e
Show file tree
Hide file tree
Showing 31 changed files with 4,362 additions and 0 deletions.
22 changes: 22 additions & 0 deletions firebase_push_notifications/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.134.0/containers/python-3/.devcontainer/base.Dockerfile
ARG VARIANT="3.9-bullseye"
FROM mcr.microsoft.com/vscode/devcontainers/python:0-${VARIANT}

ARG POETRY_VERSION="1.7.1"
ENV POETRY_HOME="/opt/poetry" \
POETRY_VERSION=${POETRY_VERSION}

RUN curl -sSL https://install.python-poetry.org | python3 - \
&& update-alternatives --install /usr/local/bin/poetry poetry /opt/poetry/bin/poetry 900 \
# Enable tab completion for bash
&& poetry completions bash >> /home/vscode/.bash_completion \
# Enable tab completion for Zsh
&& mkdir -p /home/vscode/.zfunc/ \
&& poetry completions zsh > /home/vscode/.zfunc/_poetry \
&& echo "fpath+=~/.zfunc\nautoload -Uz compinit && compinit" >> /home/vscode/.zshrc

COPY pyproject.toml ./
# COPY pyproject.toml poetry.lock ./
RUN poetry config virtualenvs.create false \
&& poetry install --no-root --no-interaction --with integration --extras "aca-py" \
&& rm -rf /root/.cache/pypoetry
50 changes: 50 additions & 0 deletions firebase_push_notifications/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/python
{
"name": "firebase_push_notifications",
"build": {
"dockerfile": "Dockerfile",
"context": "..",
"args": {
"VARIANT": "3.9-bullseye",
"POETRY_VERSION": "1.7.1"
}
},
"customizations": {
"vscode": {
"extensions": ["ms-python.python", "ms-python.vscode-pylance"],
"settings": {
"python.testing.pytestArgs": ["./firebase_push_notifications", "--no-cov"],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.testing.pytestPath": "pytest",
"editor.defaultFormatter": null,
"editor.formatOnSave": false, // enable per language
"[python]": {
"editor.formatOnSave": true
},
"python.formatting.provider": "black",
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
"python.formatting.blackArgs": []
}
}
},

"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {
"moby": false
}
},

// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode",

"remoteEnv": {
"RUST_LOG": "aries-askar::log::target=error"
},

"mounts": [],
// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [3000, 3001],
"postCreateCommand": "bash ./.devcontainer/post-install.sh"
}
14 changes: 14 additions & 0 deletions firebase_push_notifications/.devcontainer/post-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
set -ex

# Convenience workspace directory for later use
WORKSPACE_DIR=$(pwd)

# install all ACA-Py requirements
python -m pip install --upgrade pip

# install black for formatting
pip3 install black

# Generate Poetry Lock file
poetry lock --no-update
62 changes: 62 additions & 0 deletions firebase_push_notifications/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Run/Debug Plugin",
"type": "python",
"request": "launch",
"module": "aries_cloudagent",
"justMyCode": false,
"args": ["start", "--arg-file=${workspaceRoot}/docker/default.yml"]
},
{
"name": "Run/Debug Mediator",
"type": "python",
"request": "launch",
"module": "aries_cloudagent",
"justMyCode": false,
"args": ["start", "--arg-file=${workspaceRoot}/docker/integration.yml"]
},
{
"name": "Run/Debug Bob",
"type": "python",
"request": "launch",
"module": "aries_cloudagent",
"justMyCode": false,
"args": ["start", "--arg-file=${workspaceRoot}/docker/bob.yml"]
},
{
"name": "Run/Debug Alice",
"type": "python",
"request": "launch",
"module": "aries_cloudagent",
"justMyCode": false,
"args": ["start", "--arg-file=${workspaceRoot}/docker/alice.yml"]
},
{
"name": "ruff - firebase_push_notifications",
"type": "python",
"request": "launch",
"module": "ruff",
"console": "integratedTerminal",
"sudo": true,
"justMyCode": true,
"cwd": "${workspaceFolder}/firebase_push_notifications",
"args": ["check", "."]
},
{
"name": "ruff fix - firebase_push_notifications",
"type": "debugpy",
"request": "launch",
"module": "ruff",
"console": "integratedTerminal",
"sudo": true,
"justMyCode": true,
"cwd": "${workspaceFolder}/firebase_push_notifications",
"args": ["check", ".", "--fix"]
}
]
}
77 changes: 77 additions & 0 deletions firebase_push_notifications/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
### Description:

Only to be used with a medaitor agent.

Allows mobile agents to send firebase tokens to the mediator service. The mediator service creates a connection with the firebase server and will relay push notifications to the mobile agent on mediator forwarding events on the event_bus.

### Initialization
``` mermaid
sequenceDiagram
participant Mobile
participant Mediator
Note left of Mobile: Logs In
Note left of Mobile: Checks if already registered
Mobile->>Mediator: Do you support firebase protocol?
Mediator->>Mobile: No
Note left of Mobile: Do Nothing
Mediator->>Mobile: Yes
Note left of Mobile: Open notification Permission Modal
Mobile->>Mobile: User says "not now"
Note left of Mobile: Close Modal and set user denied to true
Mobile->>Mobile: User says "Allow"
Mobile->>Mediator: Send device token
Note right of Mediator: Save device token for conenction
Mobile->>Mobile: OS permissions popup
Note left of Mobile: Approve or deny OS level permission
```

### New Message
``` mermaid
sequenceDiagram
participant Agent (Faber)
participant Mediator
participant Mobile (Alice)
participant Firebase
Note left of Agent (Faber): Wants to send message to Mobile (Alice)
Agent (Faber)->>Mediator: Message
Mediator->>Mobile (Alice): Message
Mediator->>Mediator: Receives Forwarding event in aca-py
Note right of Mediator: Get device token for connection
Mediator->>Mediator: token is blank or None
Note right of Mediator: Do nothing
Mediator->>Mediator: message sent withing 'n' minutes
Note right of Mediator: Do nothing
Mediator->>Mediator: Token exists
Mediator->>Firebase: Request: Send OS notification to token
Firebase->>Mobile (Alice): Sends OS notification
```

### Disable/Enable
``` mermaid
sequenceDiagram
participant Mobile
participant Mediator
Mobile->>Mediator: Sends blank token
Note right of Mediator: Saves blank token (disabled)
Mobile->>Mediator: Sends device token
Note right of Mediator: Saves device token (enabled)
```


### Configuration:

To use the push notification protocol plugin you must have a firebase project to send the notifications to and a service account json file with `Firebase Service Management Service Agent` roles.

In the project .env file you need to supply the information in the `Firebase Plugin Configuration` section.

```
USE_FIREBASE_PLUGIN=true
FIREBASE_PROJECT_ID=287275049656
FIREBASE_NOTIFICATION_TITLE=You have important information in your digital wallet
FIREBASE_NOTIFICATION_BODY=Please open your wallet
FIREBASE_SERVICE_ACCOUNT={ flattend service account json }
```

* If `USE_FIREBASE_PLUGIN` is false it will not load the plugin.
* `FIREBASE_PROJECT_ID` can be found in the firebase console
* `FIREBASE_NOTIFICATION_TITLE` and `FIREBASE_NOTIFICATION_BODY` is the information displayed in the push notification
31 changes: 31 additions & 0 deletions firebase_push_notifications/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM python:3.9-slim AS base
WORKDIR /usr/src/app

# Install and configure poetry
USER root

ENV POETRY_VERSION=1.7.1
ENV POETRY_HOME=/opt/poetry
RUN apt-get update && apt-get install -y curl && apt-get clean
RUN curl -sSL https://install.python-poetry.org | python -

ENV PATH="/opt/poetry/bin:$PATH"
RUN poetry config virtualenvs.in-project true

# Setup project
RUN mkdir firebase_push_notifications && touch firebase_push_notifications/__init__.py
COPY pyproject.toml poetry.lock README.md ./
ARG install_flags='--with integration --extras aca-py'
RUN poetry install ${install_flags}
USER $user

FROM python:3.9-bullseye
WORKDIR /usr/src/app
COPY --from=base /usr/src/app/.venv /usr/src/app/.venv
ENV PATH="/usr/src/app/.venv/bin:$PATH"

COPY firebase_push_notifications/ firebase_push_notifications/
COPY docker/*.yml ./

ENTRYPOINT ["/bin/bash", "-c", "aca-py \"$@\"", "--"]
CMD ["start", "--arg-file", "default.yml"]
38 changes: 38 additions & 0 deletions firebase_push_notifications/docker/alice.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Alice agent configuration

auto-provision: true
label: alice

inbound-transport:
- [http, 0.0.0.0, 9010]

outbound-transport: http

wallet-type: askar
wallet-storage-type: default
wallet-name: alice-wallet
wallet-key: alice-wallet-key

admin-insecure-mode: true

admin: [0.0.0.0, 9011]

endpoint: http://localhost:9010

no-ledger: true

# Connections
debug-connections: true
auto-accept-invites: true
auto-accept-requests: true
auto-ping-connection: true

auto-store-credential: true
preserve-exchange-records: true
auto-respond-messages: true

log-level: info

# Mediation
mediator-invitation: http://localhost:3000?c_i=eyJAdHlwZSI6ICJodHRwczovL2RpZGNvbW0ub3JnL2Nvbm5lY3Rpb25zLzEuMC9pbnZpdGF0aW9uIiwgIkBpZCI6ICIzYWJkNzMwOS1lMWY2LTRhNmEtYjVlMi0wNzYwZjZlMzhhODMiLCAibGFiZWwiOiAiTWVkaWF0b3IiLCAicmVjaXBpZW50S2V5cyI6IFsiRjY0QkF0aGc1RFh5V2JYRDFMQ3l0QXVkMzZhb0QzUHRpeXU4NEFWVjc2Tk4iXSwgInNlcnZpY2VFbmRwb2ludCI6ICJodHRwOi8vbG9jYWxob3N0OjMwMDAifQ==
mediator-connections-invite: true
34 changes: 34 additions & 0 deletions firebase_push_notifications/docker/bob.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Alice agent configuration

auto-provision: true
label: bob

inbound-transport:
- [http, 0.0.0.0, 9020]

outbound-transport: http

wallet-type: askar
wallet-storage-type: default
wallet-name: bob-wallet
wallet-key: bob-wallet-key

admin-insecure-mode: true

admin: [0.0.0.0, 9021]

endpoint: http://localhost:9020

no-ledger: true

# Connections
debug-connections: true
auto-accept-invites: true
auto-accept-requests: true
auto-ping-connection: true

auto-store-credential: true
preserve-exchange-records: true
auto-respond-messages: true

log-level: info
35 changes: 35 additions & 0 deletions firebase_push_notifications/docker/default.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
label: firebase_push_notifications

admin: [0.0.0.0, 3001]
admin-insecure-mode: true
# admin-api-key: change-me

inbound-transport:
- [http, 0.0.0.0, 3000]
- [ws, 0.0.0.0, 3002]
outbound-transport: http
endpoint:
- http://host.docker.internal:3000

plugin:
- firebase_push_notifications.v1_0

genesis-url: http://test.bcovrin.vonx.io/genesis

log-level: info

# Connections
debug-connections: true
auto-accept-invites: true
auto-respond-messages: true
auto-ping-connection: true
auto-accept-requests: true

# Invitation
connections-invite: true
invite-label: "Mediator"
invite-multi-use: true

# Mediation
open-mediation: true
enable-undelivered-queue: true
Loading

0 comments on commit 8197d6e

Please sign in to comment.