Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions docs/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
**/dirs @beia/Platform
**/source.stack @beia/Platform

/projects/establish @vladwing
/projects/somedi @vladwing

/practice @beia/Junior-reviewers
5 changes: 4 additions & 1 deletion platform/makefiles/Makefile.docker
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ include $(THIS_DIR)/common.mak
.EXPORT_ALL_VARIABLES:

build: prebuild
@$(DOCKER) build -f $(DOCKER_FILE) -t $(IMAGE_NAME):$(IMAGE_VERSION_FIXED) $(DOCKER_FOLDER)
@$(DOCKER) build -f $(DOCKER_FOLDER)/$(DOCKER_FILE) -t $(IMAGE_NAME):$(IMAGE_VERSION_FIXED) $(DOCKER_FOLDER)

push:
@$(DOCKER) push $(IMAGE_NAME):$(IMAGE_VERSION_FIXED)


all: build push

include $(DOCKER_SOURCE)
.PHONY: $(DOCKER_PREBUILD)
prebuild: $(DOCKER_PREBUILD)
1 change: 1 addition & 0 deletions projects/establish/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
3 changes: 3 additions & 0 deletions projects/establish/source.docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DOCKER_FOLDER=src
IMAGE_NAME=beia/establish_garmin
IMAGE_VERSION_FIXED=latest
3 changes: 3 additions & 0 deletions projects/establish/source.stack
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
STACK_FILE=stack.yml
STACK_NAME=establish
DEPLOY_ENVS=production
1 change: 1 addition & 0 deletions projects/establish/src/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/__pycache__
10 changes: 10 additions & 0 deletions projects/establish/src/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM python:3.7.2
WORKDIR /srv/establish
RUN apt-get update && apt-get install libuv1
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY garmin garmin
COPY entry.sh .
EXPOSE 8080
CMD [ "./entry.sh" ]
4 changes: 4 additions & 0 deletions projects/establish/src/entry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

cd /srv/establish &&
python -m garmin
Empty file.
4 changes: 4 additions & 0 deletions projects/establish/src/garmin/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from garmin.web import app
from garmin.config import PORT

app.run(host='0.0.0.0', port=PORT)
13 changes: 13 additions & 0 deletions projects/establish/src/garmin/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os.path
from garmin.helpers import env_load

ACCESS_TOKEN_URL="https://connectapi.garmin.com/oauth-service/oauth/access_token"
BASE_AUTH_URL="https://connect.garmin.com/oauthConfirm"
CALLBACK_URL="https://establish.beia-consult.ro/oauth/callback"
REQUEST_TOKEN_URL="https://connectapi.garmin.com/oauth-service/oauth/request_token"
STATIC_FOLDER = os.path.join(os.path.dirname(__file__), "../static")
TEMPLATES_FOLDER = "../templates"
DB_LOCATION = env_load("DB_LOCATION")
CLIENT_KEY = env_load("CLIENT_KEY")
CLIENT_SECRET = env_load("CLIENT_SECRET")
PORT = env_load('VIRTUAL_PORT', default=8080)
36 changes: 36 additions & 0 deletions projects/establish/src/garmin/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import ujson as json
from garmin.config import DB_LOCATION

class Store:
users = []

def save(self):
with open(DB_LOCATION, "w") as f:
json.dump(self.users, f)

def load(self):
try:
with open(DB_LOCATION, "r") as f:
json.load(users, f)
except:
pass

def get_users(self):
return list(self.users)

def get_tokens(self):
return [u.token for u in self.users]

def add_user(self, token, secret):
self.users.append(
{
"token": token,
"secret": secret
}
)
self.save()

def find(self, token):
return [u for u in self.users if u["token"] == token][0]

store = Store()
11 changes: 11 additions & 0 deletions projects/establish/src/garmin/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os

def env_load(name, default=None, strip=True):
value = os.environ.get(name)
if value is not None:
return value.strip()
filename = os.environ.get(name + "__FILE")
if filename is not None:
with open(filename) as f:
return f.read().strip()
return default
1 change: 1 addition & 0 deletions projects/establish/src/garmin/tokens.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tokens = {}
74 changes: 74 additions & 0 deletions projects/establish/src/garmin/web.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import os
import requests
from requests_oauthlib import OAuth1Session, OAuth1
from sanic import Sanic
from sanic import response
from sanic.log import logger
from sanic.response import json
from garmin.config import (
CLIENT_KEY,
CLIENT_SECRET,
STATIC_FOLDER,
CALLBACK_URL,
REQUEST_TOKEN_URL,
BASE_AUTH_URL,
ACCESS_TOKEN_URL
)
from garmin.db import store
from garmin.tokens import tokens


app = Sanic()
store.load()

app.static('/favicon.ico', os.path.join(STATIC_FOLDER, 'img', 'favicon.ico'))
logger.info("CLIENT_KEY=%s", CLIENT_KEY)
logger.info("CLIENT_SECRET=%s", CLIENT_SECRET)
logger.info("STATIC_FOLDER=%s", STATIC_FOLDER)
logger.info("CALLBACK_URL=%s", CALLBACK_URL)
logger.info("REQUEST_TOKEN_URL=%s", REQUEST_TOKEN_URL)
logger.info("BASE_AUTH_UrL=%s", BASE_AUTH_URL)
logger.info("ACCESS_TOKEN_URL=%s", ACCESS_TOKEN_URL)

@app.exception(FileNotFoundError)
def ignore_file_not_found(request, exception: FileNotFoundError):
return response.text("File not found: {}".format(exception.path), 404)


@app.route('/register')
async def register(request):
oauth = OAuth1Session(
CLIENT_KEY,
client_secret=CLIENT_SECRET,
callback_uri=CALLBACK_URL)
req_token = oauth.fetch_request_token(REQUEST_TOKEN_URL)
logger.info("request token", req_token)
token = req_token["oauth_token"]
secret = req_token["oauth_token_secret"]
tokens[token] = secret
url = oauth.authorization_url(BASE_AUTH_URL)
return response.redirect(url)


@app.route('/callback')
@app.route('/oauth/callback')
async def oauth_callback(request):
token = request.args.get('oauth_token')
verifier = request.args.get('oauth_verifier')
if token not in tokens:
return response.text("haha, nice try!")
oauth = OAuth1Session(
CLIENT_KEY,
client_secret=CLIENT_SECRET,
resource_owner_key=token,
resource_owner_secret=tokens[token],
verifier=verifier)
auth_token = oauth.fetch_access_token(ACCESS_TOKEN_URL)
store.add_user(auth_token["oauth_token"], auth_token["oauth_token_secret"])
return response.text("Cool, registered!")


@app.route('/ping/<request_type:string>', methods=["GET", "POST"])
async def ping(request, request_type):
logger.info("Received a request %s %s", request_type, request.body)
return response.text("")
22 changes: 22 additions & 0 deletions projects/establish/src/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
aiofiles==0.4.0
asyncpg==0.18.3
certifi==2019.6.16
chardet==3.0.4
h11==0.8.1
h2==3.1.0
hpack==3.0.0
httpcore==0.3.0
httptools==0.0.13
hyperframe==5.2.0
idna==2.8
multidict==4.5.2
oauthlib==3.0.2
requests==2.22.0
requests-async==0.5.0
requests-oauthlib==1.2.0
rfc3986==1.3.2
sanic==19.6.2
ujson==1.35
urllib3==1.25.3
uvloop==0.12.2
websockets==6.0
34 changes: 34 additions & 0 deletions projects/establish/stack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
version: '3.6'

services:
garmin:
image: beia/establish_garmin
environment:
VIRTUAL_HOST: 'establish.beia.cloud,establish.beia-consult.ro'
VIRTUAL_PORT: 8080
LETSENCRYPT_HOST: 'establish.beia-consult.ro'
LETSENCRYPT_EMAIL: 'ssl-contact@beia.ro'
CLIENT_KEY__FILE: '/run/secrets/establish_client_key'
CLIENT_SECRET__FILE: '/run/secrets/establish_client_secret'
DB_LOCATION: '/srv/establish/garmin/db/user.json'
networks:
- proxy_net
volumes:
- establish_db:/srv/establish/garmin/db
secrets:
- establish_client_key
- establish_client_secret

networks:
proxy_net:
external: true

secrets:
establish_client_key:
external: true
establish_client_secret:
external: true

volumes:
establish_db:
external: true