Skip to content
Snippets Groups Projects
Commit 0da8888b authored by kaiyou's avatar kaiyou
Browse files

Store authorization codes in database

parent 098f3828
No related branches found
No related tags found
No related merge requests found
...@@ -10,10 +10,6 @@ from hiboo import models, utils, identity ...@@ -10,10 +10,6 @@ from hiboo import models, utils, identity
import flask import flask
AUTHORIZATION_CODES = {}
NONCES = {}
class Config(object): class Config(object):
""" Handles service configuration and forms. """ Handles service configuration and forms.
""" """
...@@ -90,27 +86,26 @@ class AuthorizationCodeGrant(grants_oauth2.AuthorizationCodeGrant): ...@@ -90,27 +86,26 @@ class AuthorizationCodeGrant(grants_oauth2.AuthorizationCodeGrant):
def create_authorization_code(self, client, grant_user, request): def create_authorization_code(self, client, grant_user, request):
code = gen_salt(48) # TODO code = gen_salt(48) # TODO
nonce = request.data.get('nonce') authorization_code = AuthorizationCode(
item = AuthorizationCode( code=code, nonce=request.data.get('nonce'),
code=code,
client_id=client.client_id, client_id=client.client_id,
redirect_uri=request.redirect_uri, redirect_uri=request.redirect_uri,
scope=request.scope, scope=request.scope,
user_id=grant_user.uuid, user_id=grant_user.uuid
nonce=nonce
) )
AUTHORIZATION_CODES[code] = item models.db.session.add(authorization_code)
NONCES[nonce] = item models.db.session.commit()
return code return code
def parse_authorization_code(self, code, client): def parse_authorization_code(self, code, client):
code = AUTHORIZATION_CODES.get(code) return AuthorizationCode.query.filter_by(
if code and code.client_id == client.client_id: client_id=client.client_id,
return code code=code
).first()
def delete_authorization_code(self, authorization_code): def delete_authorization_code(self, authorization_code):
del AUTHORIZATION_CODES[authorization_code.code] models.db.session.delete(authorization_code)
del NONCES[authorization_code.nonce] models.db.session.commit()
def authenticate_user(self, authorization_code): def authenticate_user(self, authorization_code):
profile = models.Identity.query.get(authorization_code.user_id) profile = models.Identity.query.get(authorization_code.user_id)
...@@ -120,6 +115,7 @@ class AuthorizationCodeGrant(grants_oauth2.AuthorizationCodeGrant): ...@@ -120,6 +115,7 @@ class AuthorizationCodeGrant(grants_oauth2.AuthorizationCodeGrant):
class AuthorizationCode(models.db.Model, models_oauth2.OIDCAuthorizationCodeMixin): class AuthorizationCode(models.db.Model, models_oauth2.OIDCAuthorizationCodeMixin):
""" Authorization code object for storage """ Authorization code object for storage
""" """
__tablename__ = "oidc_authorization_code"
user_id = models.db.Column(models.db.Text()) user_id = models.db.Column(models.db.Text())
...@@ -129,8 +125,9 @@ class OpenIDCode(oidc.grants.OpenIDCode): ...@@ -129,8 +125,9 @@ class OpenIDCode(oidc.grants.OpenIDCode):
""" """
def exists_nonce(self, nonce, request): def exists_nonce(self, nonce, request):
nonce = NONCES.get(nonce) return bool(AuthorizationCode.query.filter_by(
return nonce and nonce["client_id"] == request.client_id nonce=nonce, client_id=request.client_id).first()
)
def get_jwt_config(self, grant): def get_jwt_config(self, grant):
return { # TODO return { # TODO
......
""" Add authorization codes in the database
Revision ID: 5271f611b98b
Revises: cfb466a78348
Create Date: 2019-11-03 17:57:55.989647
"""
from alembic import op
import sqlalchemy as sa
revision = '5271f611b98b'
down_revision = 'cfb466a78348'
branch_labels = None
depends_on = None
def upgrade():
op.create_table('oidc_authorization_code',
sa.Column('code', sa.String(length=120), nullable=False),
sa.Column('client_id', sa.String(length=48), nullable=True),
sa.Column('redirect_uri', sa.Text(), nullable=True),
sa.Column('response_type', sa.Text(), nullable=True),
sa.Column('scope', sa.Text(), nullable=True),
sa.Column('auth_time', sa.Integer(), nullable=False),
sa.Column('nonce', sa.Text(), nullable=True),
sa.Column('user_id', sa.Text(), nullable=True),
sa.Column('uuid', sa.String(length=36), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.Column('comment', sa.String(length=255), nullable=True),
sa.PrimaryKeyConstraint('uuid'),
sa.UniqueConstraint('code')
)
def downgrade():
op.drop_table('oidc_authorization_code')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment