-
-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate a web session from an api key (#1224)
* Create a token to exchange for a cookie * Added Route to exchange token for cookie * add missing migration Co-authored-by: Adrià Casajús <[email protected]>
- Loading branch information
Showing
10 changed files
with
215 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,5 @@ | |
fido, | ||
social, | ||
recovery, | ||
api_to_cookie, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import arrow | ||
from flask import redirect, url_for, request, flash | ||
from flask_login import current_user, login_user | ||
|
||
from app.auth.base import auth_bp | ||
from app.log import LOG | ||
from app.models import ApiToCookieToken | ||
from app.utils import sanitize_next_url | ||
|
||
|
||
@auth_bp.route("/api_to_cookie", methods=["GET"]) | ||
def api_to_cookie(): | ||
if current_user.is_authenticated: | ||
LOG.d("user is already authenticated, redirect to dashboard") | ||
return redirect(url_for("dashboard.index")) | ||
code = request.args.get("token") | ||
if not code: | ||
flash("Missing token", "error") | ||
return redirect(url_for("auth.login")) | ||
|
||
token = ApiToCookieToken.get_by(code=code) | ||
if not token or token.created_at < arrow.now().shift(minutes=-5): | ||
flash("Missing token", "error") | ||
return redirect(url_for("auth.login")) | ||
|
||
login_user(token.user) | ||
ApiToCookieToken.delete(token.id, commit=True) | ||
|
||
next_url = sanitize_next_url(request.args.get("next")) | ||
if next_url: | ||
return redirect(next_url) | ||
else: | ||
return redirect(url_for("dashboard.index")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
migrations/versions/2022_081016_9cc0f0712b29_add_api_to_cookie_token.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Add api to cookie token | ||
Revision ID: 9cc0f0712b29 | ||
Revises: c66f2c5b6cb1 | ||
Create Date: 2022-08-10 16:54:46.979196 | ||
""" | ||
import sqlalchemy_utils | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '9cc0f0712b29' | ||
down_revision = 'c66f2c5b6cb1' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('api_cookie_token', | ||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column('created_at', sqlalchemy_utils.types.arrow.ArrowType(), nullable=False), | ||
sa.Column('updated_at', sqlalchemy_utils.types.arrow.ArrowType(), nullable=True), | ||
sa.Column('code', sa.String(length=128), nullable=False), | ||
sa.Column('user_id', sa.Integer(), nullable=False), | ||
sa.Column('api_key_id', sa.Integer(), nullable=False), | ||
sa.ForeignKeyConstraint(['api_key_id'], ['api_key.id'], ondelete='cascade'), | ||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='cascade'), | ||
sa.PrimaryKeyConstraint('id'), | ||
sa.UniqueConstraint('code') | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table('api_cookie_token') | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from flask import url_for | ||
|
||
from app.models import ApiToCookieToken, ApiKey | ||
from tests.utils import create_new_user | ||
|
||
|
||
def test_get_cookie(flask_client): | ||
user = create_new_user() | ||
api_key = ApiKey.create( | ||
user_id=user.id, | ||
commit=True, | ||
) | ||
token = ApiToCookieToken.create( | ||
user_id=user.id, | ||
api_key_id=api_key.id, | ||
commit=True, | ||
) | ||
token_code = token.code | ||
token_id = token.id | ||
|
||
r = flask_client.get( | ||
url_for( | ||
"auth.api_to_cookie", token=token_code, next=url_for("dashboard.setting") | ||
), | ||
follow_redirects=True, | ||
) | ||
|
||
assert ApiToCookieToken.get(token_id) is None | ||
assert r.headers.getlist("Set-Cookie") is not None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters