owl-map/matcher/osm_oauth.py

80 lines
2.1 KiB
Python
Raw Normal View History

"""OSM Authentication."""
import typing
2021-06-16 14:42:04 +01:00
from datetime import datetime
from urllib.parse import urlencode
2021-06-16 14:42:04 +01:00
import lxml.etree
from flask import current_app, g, session
from requests_oauthlib import OAuth1Session
2021-06-16 14:42:04 +01:00
from . import user_agent_headers
from .model import User
2021-06-16 14:42:04 +01:00
osm_api_base = "https://api.openstreetmap.org/api/0.6"
def api_put_request(path, **kwargs):
user = g.user
assert user.is_authenticated
oauth = OAuth1Session(
current_app.config["CLIENT_KEY"],
client_secret=current_app.config["CLIENT_SECRET"],
resource_owner_key=user.osm_oauth_token,
resource_owner_secret=user.osm_oauth_token_secret,
)
return oauth.request(
"PUT", osm_api_base + path, headers=user_agent_headers(), **kwargs
)
def api_request(path, **params):
user = g.user
assert user.is_authenticated
app = current_app
url = osm_api_base + path
if params:
url += "?" + urlencode(params)
client_key = app.config["CLIENT_KEY"]
client_secret = app.config["CLIENT_SECRET"]
oauth = OAuth1Session(
client_key,
client_secret=client_secret,
resource_owner_key=user.osm_oauth_token,
resource_owner_secret=user.osm_oauth_token_secret,
)
return oauth.get(url, timeout=4)
def parse_iso_date(value):
return datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ")
def parse_userinfo_call(xml):
root = lxml.etree.fromstring(xml)
user = root[0]
img = user.find(".//img")
account_created = parse_iso_date(user.get("account_created"))
assert user.tag == "user"
return {
"account_created": account_created,
"id": int(user.get("id")),
"username": user.get("display_name"),
"description": user.findtext(".//description"),
"img": (img.get("href") if img is not None else None),
}
def get_username() -> str | None:
"""Get username of current user."""
2021-06-16 14:42:04 +01:00
if "user_id" not in session:
return None # not authorized
2021-06-16 14:42:04 +01:00
user_id = session["user_id"]
user = User.query.get(user_id)
return typing.cast(str, user.username)