Skip to content

Commit 53d0c66

Browse files
authored
Added OAuth and its testing (#6)
* Authenticate/Authorise on resources (#5) * Added PyJWT * Resource API now requires authentication/authorisation * Greeting makes use of the new read fixture * Skipping tests when no OIDC configuration is available * More appropriate permissions * Tests should be running an install the first time they execute * Additional check whether the test configuration file exists * Fixed pylint complaint ... by ignoring the unnecessary lambda that needs to be there * Fixed flake8 complaints
1 parent e95f916 commit 53d0c66

20 files changed

+321
-116
lines changed

.idea/dataSources.xml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/mrmat_python_api_flask.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/mrmat_python_api_flask__oidc_.xml renamed to .idea/runConfigurations/mrmat_python_api_flask__default_.xml

Lines changed: 1 addition & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/mrmat_python_api_flask__debug_.xml renamed to .idea/runConfigurations/mrmat_python_api_flask__default__debug_.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/setup_py__install_.xml

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations/tests.xml

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,18 @@ Configure the following clients as needed:
228228
* Access Type: confidential (but if it wasn't Keycloak, should be public)
229229
* Flow: Device Authorization Grant
230230

231-
Keycloaks default polling interval during the device authorization flow is set to a rather long 600s. I strongly
232-
suggest to reduce that to 5s in the realm settings.
231+
Keycloak's default polling interval during the device authorization flow is set to a rather long 600s. I strongly
232+
suggest reducing that to 5s in the realm settings.
233233

234234
### Test Client
235235

236236
* Suggested client_id: mrmat-python-api-flask-test
237237
* Access Type: confidential
238238
* Flow: Client Credentials Grant (Keycloak: "Service Accounts Enabled")
239239

240+
### Scopes
241+
242+
The resource API uses two scopes that need to be defined within the IDP:
243+
244+
* mrmat-python-api-flask-resource-write - Permit create/modify/remove of resources
245+
* mrmat-python-api-flask-resource-read - Permit reading resources
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Owners and Resources
2+
3+
Revision ID: d11062fbec93
4+
Revises: 4594cf7d8bfb
5+
Create Date: 2021-06-26 17:57:31.057034
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = 'd11062fbec93'
14+
down_revision = '4594cf7d8bfb'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.create_table('owners',
22+
sa.Column('id', sa.BigInteger().with_variant(sa.Integer(), 'sqlite'), nullable=False),
23+
sa.Column('client_id', sa.String(length=255), nullable=False),
24+
sa.Column('name', sa.String(length=255), nullable=False),
25+
sa.PrimaryKeyConstraint('id'),
26+
sa.UniqueConstraint('client_id')
27+
)
28+
op.add_column('resources', sa.Column('owner_id', sa.Integer(), nullable=False))
29+
op.alter_column('resources', 'name',
30+
existing_type=sa.VARCHAR(length=50),
31+
nullable=False)
32+
op.create_foreign_key(None, 'resources', 'owners', ['owner_id'], ['id'])
33+
op.drop_column('resources', 'owner')
34+
# ### end Alembic commands ###
35+
36+
37+
def downgrade():
38+
# ### commands auto generated by Alembic - please adjust! ###
39+
op.add_column('resources', sa.Column('owner', sa.VARCHAR(length=50), autoincrement=False, nullable=True))
40+
op.drop_constraint(None, 'resources', type_='foreignkey')
41+
op.alter_column('resources', 'name',
42+
existing_type=sa.VARCHAR(length=50),
43+
nullable=True)
44+
op.drop_column('resources', 'owner_id')
45+
op.drop_table('owners')
46+
# ### end Alembic commands ###

mrmat_python_api_flask/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ def create_app(config_override=None, instance_path=None):
101101
app.logger.info(f'Creating new instance path at {app.instance_path}')
102102
os.makedirs(app.instance_path)
103103
else:
104-
app.logger.info(f'Using instance path at {app.instance_path}')
104+
app.logger.info(f'Using existing instance path at {app.instance_path}')
105105
except OSError:
106-
app.logger.error(f'Failed to create instance path at {app.instance_path}')
106+
app.logger.error(f'Failed to create new instance path at {app.instance_path}')
107107
sys.exit(1)
108108

109109
# When using Flask-SQLAlchemy, there is no need to explicitly import DAO classes because they themselves

mrmat_python_api_flask/apis/resource/v1/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424
"""
2525

2626
from .api import bp as api_resource_v1 # noqa: F401
27-
from .model import Resource, ResourceSchema # noqa: F401
27+
from .model import Owner, Resource, OwnerSchema, ResourceSchema # noqa: F401

0 commit comments

Comments
 (0)