2022-06-07 08:40:50 +02:00
|
|
|
from typing import Dict, Optional, Tuple, Union
|
2022-06-07 15:40:33 +02:00
|
|
|
from urllib.parse import parse_qsl
|
2022-05-27 13:28:26 +02:00
|
|
|
|
2022-05-27 13:34:32 +02:00
|
|
|
from flask import Blueprint, Response, request
|
2022-06-07 15:40:33 +02:00
|
|
|
from urllib3.util import parse_url
|
2022-05-27 13:28:26 +02:00
|
|
|
|
|
|
|
from fittrackee import db
|
2022-05-28 15:36:18 +02:00
|
|
|
from fittrackee.responses import (
|
|
|
|
HttpResponse,
|
|
|
|
InvalidPayloadErrorResponse,
|
|
|
|
NotFoundErrorResponse,
|
|
|
|
)
|
2022-05-27 13:28:26 +02:00
|
|
|
from fittrackee.users.models import User
|
|
|
|
|
2022-06-19 20:04:42 +02:00
|
|
|
from .client import create_oauth2_client
|
2022-06-15 19:16:14 +02:00
|
|
|
from .exceptions import InvalidOAuth2Scopes
|
|
|
|
from .models import OAuth2Client, OAuth2Token
|
|
|
|
from .server import authorization_server, require_auth
|
2022-05-27 13:28:26 +02:00
|
|
|
|
2022-06-19 20:04:42 +02:00
|
|
|
oauth2_blueprint = Blueprint('oauth2', __name__)
|
2022-05-27 13:28:26 +02:00
|
|
|
|
|
|
|
EXPECTED_METADATA_KEYS = [
|
|
|
|
'client_name',
|
|
|
|
'client_uri',
|
|
|
|
'redirect_uris',
|
|
|
|
'scope',
|
|
|
|
]
|
2022-05-28 15:36:18 +02:00
|
|
|
DEFAULT_PER_PAGE = 5
|
|
|
|
|
|
|
|
|
2022-06-07 15:40:33 +02:00
|
|
|
def is_errored(url: str) -> Optional[str]:
|
|
|
|
query = dict(parse_qsl(parse_url(url).query))
|
|
|
|
if query.get('error'):
|
|
|
|
return query.get('error_description', 'invalid payload')
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2022-06-19 20:04:42 +02:00
|
|
|
@oauth2_blueprint.route('/oauth/apps', methods=['GET'])
|
2022-05-28 15:36:18 +02:00
|
|
|
@require_auth()
|
|
|
|
def get_clients(auth_user: User) -> Dict:
|
2022-07-14 18:36:19 +02:00
|
|
|
"""
|
|
|
|
Get OAuth2 clients (apps) for authenticated user with pagination
|
|
|
|
(5 clients/page).
|
|
|
|
|
|
|
|
This endpoint is only accessible by FitTrackee client (first-party
|
|
|
|
application).
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
- without parameters:
|
2022-07-14 18:36:19 +02:00
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
GET /api/oauth/apps HTTP/1.1
|
|
|
|
Content-Type: application/json
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
- with 'page' parameter:
|
2022-07-14 18:36:19 +02:00
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
GET /api/oauth/apps?page=2 HTTP/1.1
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 SUCCESS
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"clients": [
|
|
|
|
{
|
|
|
|
"client_description": "",
|
|
|
|
"client_id": "o22a27s2aBPUoxJbxV3UjDOx",
|
|
|
|
"id": 1,
|
|
|
|
"issued_at": "Thu, 14 July 2022 06:27:53 GMT",
|
|
|
|
"name": "GPX Importer",
|
|
|
|
"redirect_uris": [
|
|
|
|
" https://example.com/callback"
|
|
|
|
],
|
|
|
|
"scope": "profile:read workouts:write",
|
|
|
|
"website": "https://example.com"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"pagination": {
|
|
|
|
"has_next": false,
|
|
|
|
"has_prev": false,
|
|
|
|
"page": 1,
|
|
|
|
"pages": 1,
|
|
|
|
"total": 1
|
|
|
|
},
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
|
|
|
:query integer page: page for pagination (default: 1)
|
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
:statuscode 200: ``success``
|
2022-07-14 18:36:19 +02:00
|
|
|
:statuscode 401:
|
2023-06-18 20:45:39 +02:00
|
|
|
- ``provide a valid auth token``
|
|
|
|
- ``signature expired, please log in again``
|
|
|
|
- ``invalid token, please log in again``
|
2022-07-14 18:36:19 +02:00
|
|
|
"""
|
2022-05-28 15:36:18 +02:00
|
|
|
params = request.args.copy()
|
|
|
|
page = int(params.get('page', 1))
|
|
|
|
per_page = DEFAULT_PER_PAGE
|
|
|
|
clients_pagination = (
|
|
|
|
OAuth2Client.query.filter_by(user_id=auth_user.id)
|
|
|
|
.order_by(OAuth2Client.id.desc())
|
2022-11-26 12:54:56 +01:00
|
|
|
.paginate(page=page, per_page=per_page, error_out=False)
|
2022-05-28 15:36:18 +02:00
|
|
|
)
|
|
|
|
clients = clients_pagination.items
|
|
|
|
return {
|
|
|
|
'status': 'success',
|
|
|
|
'data': {
|
|
|
|
'clients': [
|
|
|
|
client.serialize(with_secret=False) for client in clients
|
|
|
|
]
|
|
|
|
},
|
|
|
|
'pagination': {
|
|
|
|
'has_next': clients_pagination.has_next,
|
|
|
|
'has_prev': clients_pagination.has_prev,
|
|
|
|
'page': clients_pagination.page,
|
|
|
|
'pages': clients_pagination.pages,
|
|
|
|
'total': clients_pagination.total,
|
|
|
|
},
|
|
|
|
}
|
2022-05-27 13:28:26 +02:00
|
|
|
|
|
|
|
|
2022-06-19 20:04:42 +02:00
|
|
|
@oauth2_blueprint.route('/oauth/apps', methods=['POST'])
|
2022-05-27 15:51:40 +02:00
|
|
|
@require_auth()
|
2022-05-27 13:28:26 +02:00
|
|
|
def create_client(auth_user: User) -> Union[HttpResponse, Tuple[Dict, int]]:
|
2022-07-14 18:36:19 +02:00
|
|
|
"""
|
|
|
|
Create an OAuth2 client (app) for the authenticated user.
|
|
|
|
|
|
|
|
This endpoint is only accessible by FitTrackee client (first-party
|
|
|
|
application).
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
POST /api/oauth/apps HTTP/1.1
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 SUCCESS
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"client": {
|
|
|
|
"client_description": "",
|
|
|
|
"client_id": "o22a27s2aBPUoxJbxV3UjDOx",
|
|
|
|
"client_secret": "<CLIENT SECRET>",
|
|
|
|
"id": 1,
|
|
|
|
"issued_at": "Thu, 14 July 2022 06:27:53 GMT",
|
|
|
|
"name": "GPX Importer",
|
|
|
|
"redirect_uris": [
|
|
|
|
"https://example.com/callback"
|
|
|
|
],
|
|
|
|
"scope": "profile:read workouts:write",
|
|
|
|
"website": "https://example.com"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"status": "created"
|
|
|
|
}
|
|
|
|
|
|
|
|
:json string client_name: client name
|
|
|
|
:json string client_uri: client URL
|
|
|
|
:json array redirect_uri: list of client redirect URLs (string)
|
|
|
|
:json string scope: client scopes
|
2023-06-24 12:17:56 +02:00
|
|
|
:json string client_description: client description (optional)
|
2022-07-14 18:36:19 +02:00
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
:statuscode 200: ``success``
|
|
|
|
:statuscode 400: ``invalid payload``
|
2022-07-14 18:36:19 +02:00
|
|
|
:statuscode 401:
|
2023-06-18 20:45:39 +02:00
|
|
|
- ``provide a valid auth token``
|
|
|
|
- ``signature expired, please log in again``
|
|
|
|
- ``invalid token, please log in again``
|
2022-07-14 18:36:19 +02:00
|
|
|
"""
|
2022-05-27 13:28:26 +02:00
|
|
|
client_metadata = request.get_json()
|
|
|
|
if not client_metadata:
|
|
|
|
return InvalidPayloadErrorResponse(
|
2022-06-19 20:04:42 +02:00
|
|
|
message='OAuth2 client metadata missing'
|
2022-05-27 13:28:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
missing_keys = [
|
|
|
|
key
|
|
|
|
for key in EXPECTED_METADATA_KEYS
|
|
|
|
if key not in client_metadata.keys()
|
|
|
|
]
|
|
|
|
if missing_keys:
|
|
|
|
return InvalidPayloadErrorResponse(
|
|
|
|
message=(
|
2022-06-19 20:04:42 +02:00
|
|
|
'OAuth2 client metadata missing keys: '
|
2022-05-27 13:28:26 +02:00
|
|
|
f'{", ".join(missing_keys)}'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-06-15 19:16:14 +02:00
|
|
|
try:
|
2022-06-19 20:04:42 +02:00
|
|
|
new_client = create_oauth2_client(client_metadata, auth_user)
|
2022-06-15 19:16:14 +02:00
|
|
|
except InvalidOAuth2Scopes:
|
|
|
|
return InvalidPayloadErrorResponse(
|
2022-06-19 20:04:42 +02:00
|
|
|
message='OAuth2 client invalid scopes'
|
2022-06-15 19:16:14 +02:00
|
|
|
)
|
|
|
|
|
2022-05-27 13:28:26 +02:00
|
|
|
db.session.add(new_client)
|
|
|
|
db.session.commit()
|
|
|
|
return (
|
|
|
|
{
|
|
|
|
'status': 'created',
|
2022-05-28 15:36:18 +02:00
|
|
|
'data': {'client': new_client.serialize(with_secret=True)},
|
2022-05-27 13:28:26 +02:00
|
|
|
},
|
|
|
|
201,
|
|
|
|
)
|
2022-05-27 13:34:32 +02:00
|
|
|
|
|
|
|
|
2022-06-07 08:40:50 +02:00
|
|
|
def get_client(
|
|
|
|
auth_user: User,
|
|
|
|
client_id: Optional[int],
|
|
|
|
client_client_id: Optional[str],
|
|
|
|
) -> Union[Dict, HttpResponse]:
|
|
|
|
key = 'id' if client_id else 'client_id'
|
|
|
|
value = client_id if client_id else client_client_id
|
2022-05-28 15:36:18 +02:00
|
|
|
client = OAuth2Client.query.filter_by(
|
2022-06-07 08:40:50 +02:00
|
|
|
**{key: value, 'user_id': auth_user.id}
|
2022-05-28 15:36:18 +02:00
|
|
|
).first()
|
|
|
|
|
|
|
|
if not client:
|
2022-06-19 20:04:42 +02:00
|
|
|
return NotFoundErrorResponse('OAuth2 client not found')
|
2022-05-28 15:36:18 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
'status': 'success',
|
|
|
|
'data': {'client': client.serialize(with_secret=False)},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-14 18:36:19 +02:00
|
|
|
@oauth2_blueprint.route(
|
|
|
|
'/oauth/apps/<string:client_client_id>', methods=['GET']
|
|
|
|
)
|
2022-06-07 08:40:50 +02:00
|
|
|
@require_auth()
|
|
|
|
def get_client_by_client_id(
|
2022-07-14 18:36:19 +02:00
|
|
|
auth_user: User, client_client_id: str
|
2022-06-07 08:40:50 +02:00
|
|
|
) -> Union[Dict, HttpResponse]:
|
2022-07-14 18:36:19 +02:00
|
|
|
"""
|
|
|
|
Get an OAuth2 client (app) by 'client_id'.
|
|
|
|
|
|
|
|
This endpoint is only accessible by FitTrackee client (first-party
|
|
|
|
application).
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
GET /api/oauth/apps/o22a27s2aBPUoxJbxV3UjDOx HTTP/1.1
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example responses**:
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
- success:
|
2022-07-14 18:36:19 +02:00
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 SUCCESS
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"client": {
|
|
|
|
"client_description": "",
|
|
|
|
"client_id": "o22a27s2aBPUoxJbxV3UjDOx",
|
|
|
|
"id": 1,
|
|
|
|
"issued_at": "Thu, 14 July 2022 06:27:53 GMT",
|
|
|
|
"name": "GPX Importer",
|
|
|
|
"redirect_uris": [
|
|
|
|
"https://example.com/callback"
|
|
|
|
],
|
|
|
|
"scope": "profile:read workouts:write",
|
|
|
|
"website": "https://example.com"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
- not found:
|
2022-07-14 18:36:19 +02:00
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 404 NOT FOUND
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"status": "not found",
|
|
|
|
"message": "OAuth2 client not found"
|
|
|
|
}
|
|
|
|
|
|
|
|
:param string client_client_id: OAuth2 client client_id
|
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
:statuscode 200: ``success``
|
2022-07-14 18:36:19 +02:00
|
|
|
:statuscode 401:
|
2023-06-18 20:45:39 +02:00
|
|
|
- ``provide a valid auth token``
|
|
|
|
- ``signature expired, please log in again``
|
|
|
|
- ``invalid token, please log in again``
|
|
|
|
:statuscode 404: ``OAuth2 client not found``
|
2022-07-14 18:36:19 +02:00
|
|
|
"""
|
|
|
|
return get_client(
|
|
|
|
auth_user, client_id=None, client_client_id=client_client_id
|
|
|
|
)
|
2022-06-07 08:40:50 +02:00
|
|
|
|
|
|
|
|
2022-06-19 20:04:42 +02:00
|
|
|
@oauth2_blueprint.route('/oauth/apps/<int:client_id>/by_id', methods=['GET'])
|
2022-06-07 08:40:50 +02:00
|
|
|
@require_auth()
|
|
|
|
def get_client_by_id(
|
|
|
|
auth_user: User, client_id: int
|
|
|
|
) -> Union[Dict, HttpResponse]:
|
2022-07-14 18:36:19 +02:00
|
|
|
"""
|
|
|
|
Get an OAuth2 client (app) by id (integer value).
|
|
|
|
|
|
|
|
This endpoint is only accessible by FitTrackee client (first-party
|
|
|
|
application).
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
GET /api/oauth/apps/1/by_id HTTP/1.1
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example responses**:
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
- success:
|
2022-07-14 18:36:19 +02:00
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 SUCCESS
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"client": {
|
|
|
|
"client_description": "",
|
|
|
|
"client_id": "o22a27s2aBPUoxJbxV3UjDOx",
|
|
|
|
"id": 1,
|
|
|
|
"issued_at": "Thu, 14 July 2022 06:27:53 GMT",
|
|
|
|
"name": "GPX Importer",
|
|
|
|
"redirect_uris": [
|
|
|
|
"https://example.com/callback"
|
|
|
|
],
|
|
|
|
"scope": "profile:read workouts:write",
|
|
|
|
"website": "https://example.com"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
- not found:
|
2022-07-14 18:36:19 +02:00
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 404 NOT FOUND
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"status": "not found",
|
|
|
|
"message": "OAuth2 client not found"
|
|
|
|
}
|
|
|
|
|
|
|
|
:param integer client_id: OAuth2 client id
|
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
:statuscode 200: ``success``
|
2022-07-14 18:36:19 +02:00
|
|
|
:statuscode 401:
|
2023-06-18 20:45:39 +02:00
|
|
|
- ``provide a valid auth token``
|
|
|
|
- ``signature expired, please log in again``
|
|
|
|
- ``invalid token, please log in again``
|
|
|
|
:statuscode 404: ``OAuth2 client not found``
|
2022-07-14 18:36:19 +02:00
|
|
|
"""
|
2022-06-07 08:40:50 +02:00
|
|
|
return get_client(auth_user, client_id=client_id, client_client_id=None)
|
|
|
|
|
|
|
|
|
2022-06-19 20:04:42 +02:00
|
|
|
@oauth2_blueprint.route('/oauth/apps/<int:client_id>', methods=['DELETE'])
|
2022-05-28 15:36:18 +02:00
|
|
|
@require_auth()
|
|
|
|
def delete_client(
|
2022-06-15 19:16:14 +02:00
|
|
|
auth_user: User, client_id: int
|
2022-05-28 15:36:18 +02:00
|
|
|
) -> Union[Tuple[Dict, int], HttpResponse]:
|
2022-07-14 18:36:19 +02:00
|
|
|
"""
|
|
|
|
Delete an OAuth2 client (app).
|
|
|
|
|
|
|
|
This endpoint is only accessible by FitTrackee client (first-party
|
|
|
|
application).
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
DELETE /api/oauth/apps/1 HTTP/1.1
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 204 NO CONTENT
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
:param integer client_id: OAuth2 client id
|
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
|
|
|
:statuscode 204: OAuth2 client deleted
|
|
|
|
:statuscode 401:
|
2023-06-18 20:45:39 +02:00
|
|
|
- ``provide a valid auth token``
|
|
|
|
- ``signature expired, please log in again``
|
|
|
|
- ``invalid token, please log in again``
|
|
|
|
:statuscode 404: ``OAuth2 client not found``
|
2022-07-14 18:36:19 +02:00
|
|
|
"""
|
2022-05-28 15:36:18 +02:00
|
|
|
client = OAuth2Client.query.filter_by(
|
|
|
|
id=client_id,
|
|
|
|
user_id=auth_user.id,
|
|
|
|
).first()
|
|
|
|
|
|
|
|
if not client:
|
2022-06-19 20:04:42 +02:00
|
|
|
return NotFoundErrorResponse('OAuth2 client not found')
|
2022-05-28 15:36:18 +02:00
|
|
|
|
|
|
|
db.session.delete(client)
|
|
|
|
db.session.commit()
|
|
|
|
return {'status': 'no content'}, 204
|
|
|
|
|
|
|
|
|
2022-06-19 20:04:42 +02:00
|
|
|
@oauth2_blueprint.route('/oauth/apps/<int:client_id>/revoke', methods=['POST'])
|
2022-06-12 17:15:18 +02:00
|
|
|
@require_auth()
|
|
|
|
def revoke_client_tokens(
|
|
|
|
auth_user: User, client_id: int
|
|
|
|
) -> Union[Dict, HttpResponse]:
|
2022-07-14 18:36:19 +02:00
|
|
|
"""
|
|
|
|
Revoke all tokens associated to an OAuth2 client (app).
|
|
|
|
|
|
|
|
This endpoint is only accessible by FitTrackee client (first-party
|
|
|
|
application).
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
POST /api/oauth/apps/1/revoke HTTP/1.1
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 SUCCESS
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
|
|
|
:param integer client_id: OAuth2 client id
|
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
:statuscode 200: ``success``
|
2022-07-14 18:36:19 +02:00
|
|
|
:statuscode 401:
|
2023-06-18 20:45:39 +02:00
|
|
|
- ``provide a valid auth token``
|
|
|
|
- ``signature expired, please log in again``
|
|
|
|
- ``invalid token, please log in again``
|
|
|
|
:statuscode 404: ``OAuth2 client not found``
|
2022-07-14 18:36:19 +02:00
|
|
|
"""
|
2022-06-12 17:15:18 +02:00
|
|
|
client = OAuth2Client.query.filter_by(id=client_id).first()
|
|
|
|
|
|
|
|
if not client:
|
2022-06-19 20:04:42 +02:00
|
|
|
return NotFoundErrorResponse('OAuth2 client not found')
|
2022-06-12 17:15:18 +02:00
|
|
|
|
|
|
|
OAuth2Token.revoke_client_tokens(client.client_id)
|
|
|
|
return {'status': 'success'}
|
|
|
|
|
|
|
|
|
2022-06-19 20:04:42 +02:00
|
|
|
@oauth2_blueprint.route('/oauth/authorize', methods=['POST'])
|
2022-05-27 15:51:40 +02:00
|
|
|
@require_auth()
|
2022-06-07 15:40:33 +02:00
|
|
|
def authorize(auth_user: User) -> Union[HttpResponse, Dict]:
|
2022-07-14 18:36:19 +02:00
|
|
|
"""
|
|
|
|
Authorize an OAuth2 client (app).
|
|
|
|
If successful, it redirects to the client callback URL with the code to
|
|
|
|
issue a token.
|
|
|
|
|
|
|
|
This endpoint is only accessible by FitTrackee client (first-party
|
|
|
|
application).
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
POST /api/oauth/authorize HTTP/1.1
|
|
|
|
Content-Type: multipart/form-data
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 SUCCESS
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
|
|
|
:form string client_id: OAuth2 client 'client_id'
|
|
|
|
:form string response_type: client response type (only 'code' is supported
|
|
|
|
by FitTrackee)
|
|
|
|
:form string scopes: OAuth2 client scopes
|
2023-06-18 15:02:21 +02:00
|
|
|
:form boolean confirm: confirmation (must be ``true``)
|
2022-07-14 18:36:19 +02:00
|
|
|
:form string state: unique value to prevent cross-site request forgery
|
2022-08-03 14:01:03 +02:00
|
|
|
(not mandatory but recommended)
|
2022-07-14 18:36:19 +02:00
|
|
|
:form string code_challenge: string generated from a code verifier
|
2022-08-03 14:01:03 +02:00
|
|
|
(for PKCE, not mandatory but recommended)
|
2022-07-14 18:36:19 +02:00
|
|
|
:form string code_challenge_method: method used to create challenge,
|
2022-08-03 14:01:03 +02:00
|
|
|
for instance "S256" (mandatory if `code_challenge`
|
|
|
|
provided)
|
2022-07-14 18:36:19 +02:00
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
:statuscode 200: ``success``
|
2022-07-14 18:36:19 +02:00
|
|
|
:statuscode 400:
|
2023-06-18 20:45:39 +02:00
|
|
|
- ``invalid payload``
|
2022-07-14 18:36:19 +02:00
|
|
|
- errors returned by Authlib library
|
|
|
|
:statuscode 401:
|
2023-06-18 20:45:39 +02:00
|
|
|
- ``provide a valid auth token``
|
|
|
|
- ``signature expired, please log in again``
|
|
|
|
- ``invalid token, please log in again``
|
2022-07-14 18:36:19 +02:00
|
|
|
"""
|
2022-05-27 13:34:32 +02:00
|
|
|
data = request.form
|
2022-08-03 14:01:03 +02:00
|
|
|
if (
|
|
|
|
not data
|
|
|
|
or 'client_id' not in data
|
|
|
|
or 'response_type' not in data
|
|
|
|
or data.get('response_type') != 'code'
|
|
|
|
):
|
2022-05-27 13:34:32 +02:00
|
|
|
return InvalidPayloadErrorResponse()
|
|
|
|
|
2022-06-07 15:40:33 +02:00
|
|
|
confirm = data.get('confirm', 'false')
|
|
|
|
grant_user = auth_user if confirm.lower() == 'true' else None
|
|
|
|
response = authorization_server.create_authorization_response(
|
|
|
|
grant_user=grant_user
|
2022-05-27 13:34:32 +02:00
|
|
|
)
|
2022-06-07 15:40:33 +02:00
|
|
|
error_message = is_errored(url=response.location)
|
|
|
|
if error_message:
|
|
|
|
return InvalidPayloadErrorResponse(error_message)
|
|
|
|
return {'redirect_url': response.location}
|
2022-05-27 14:08:07 +02:00
|
|
|
|
|
|
|
|
2022-06-19 20:04:42 +02:00
|
|
|
@oauth2_blueprint.route('/oauth/token', methods=['POST'])
|
2022-05-27 14:08:07 +02:00
|
|
|
def issue_token() -> Response:
|
2022-07-14 18:36:19 +02:00
|
|
|
"""
|
|
|
|
Issue or refresh token for a given OAuth2 client (app).
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
POST /api/oauth/token HTTP/1.1
|
|
|
|
Content-Type: multipart/form-data
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 SUCCESS
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"access_token": "rOEHv64THCG28WcewZHRnVLUsOdUvw8NVnHKCmL57e",
|
|
|
|
"expires_in": 864000,
|
|
|
|
"refresh_token": "NuV9cY8VQOnrQKHTZ5pQAq2Zw7mSH0MorNPJr14AmSwD6f6I",
|
|
|
|
"scope": ["profile:read", "workouts:write"],
|
|
|
|
"token_type": "Bearer",
|
|
|
|
"expires_at": 1658660147.0667062
|
|
|
|
}
|
|
|
|
|
|
|
|
:form string client_id: OAuth2 client 'client_id'
|
|
|
|
:form string client_secret: OAuth2 client secret
|
|
|
|
:form string grant_type: OAuth2 client grant type
|
|
|
|
(only 'authorization_code' (for token issue)
|
|
|
|
and 'refresh_token' (for token refresh)
|
|
|
|
are supported by FitTrackee)
|
|
|
|
:form string code: code generated after authorizing the client
|
|
|
|
(for token issue)
|
|
|
|
:form string code_verifier: code verifier
|
2022-08-03 14:01:03 +02:00
|
|
|
(for token issue with PKCE, not mandatory)
|
2022-07-14 18:36:19 +02:00
|
|
|
:form string refresh_token: refresh token (for token refresh)
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
:statuscode 200: ``success``
|
|
|
|
:statuscode 400: errors returned by Authlib library
|
2022-07-14 18:36:19 +02:00
|
|
|
:statuscode 401:
|
2023-06-18 20:45:39 +02:00
|
|
|
- ``provide a valid auth token``
|
|
|
|
- ``signature expired, please log in again``
|
|
|
|
- ``invalid token, please log in again``
|
2022-07-14 18:36:19 +02:00
|
|
|
"""
|
2022-05-27 14:08:07 +02:00
|
|
|
return authorization_server.create_token_response()
|
2022-05-27 14:46:03 +02:00
|
|
|
|
|
|
|
|
2022-06-19 20:04:42 +02:00
|
|
|
@oauth2_blueprint.route('/oauth/revoke', methods=['POST'])
|
2022-05-27 14:46:03 +02:00
|
|
|
def revoke_token() -> Response:
|
2022-07-14 18:36:19 +02:00
|
|
|
"""
|
|
|
|
Revoke a token for a given OAuth2 client (app).
|
|
|
|
|
|
|
|
**Example request**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
POST /api/oauth/revoke HTTP/1.1
|
|
|
|
Content-Type: multipart/form-data
|
|
|
|
|
|
|
|
**Example response**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 SUCCESS
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
:form string client_id: OAuth2 client 'client_id'
|
|
|
|
:form string client_secret: OAuth2 client secret
|
|
|
|
:form string token: access token to revoke
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
:statuscode 200: ``success``
|
|
|
|
:statuscode 400: errors returned by Authlib library
|
2022-07-14 18:36:19 +02:00
|
|
|
:statuscode 401:
|
2023-06-18 20:45:39 +02:00
|
|
|
- ``provide a valid auth token``
|
|
|
|
- ``signature expired, please log in again``
|
|
|
|
- ``invalid token, please log in again``
|
2022-07-14 18:36:19 +02:00
|
|
|
"""
|
2022-05-27 14:46:03 +02:00
|
|
|
return authorization_server.create_endpoint_response('revocation')
|