Skip to content

Commit 064bfe4

Browse files
committed
move create client back
1 parent b71f2ca commit 064bfe4

File tree

3 files changed

+70
-68
lines changed

3 files changed

+70
-68
lines changed

bin/fence_create.py

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import argparse
44
import os
55
import sys
6-
import logging
76

87
from cdislogging import get_logger
98

fence/scripting/fence_create.py

+1-67
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
from fence.utils import (
6060
get_valid_expiration,
6161
generate_client_credentials,
62-
get_SQLAlchemyDriver, logger,
62+
get_SQLAlchemyDriver, logger, create_client,
6363
)
6464
from sqlalchemy.orm.attributes import flag_modified
6565
from gen3authz.client.arborist.client import ArboristClient
@@ -1823,69 +1823,3 @@ def access_token_polling_job(
18231823
with driver.session as db_session:
18241824
loop = asyncio.get_event_loop()
18251825
loop.run_until_complete(job.update_tokens(db_session))
1826-
1827-
1828-
def create_client(
1829-
DB,
1830-
username=None,
1831-
urls=[],
1832-
name="",
1833-
description="",
1834-
auto_approve=False,
1835-
is_admin=False,
1836-
grant_types=None,
1837-
confidential=True,
1838-
arborist=None,
1839-
policies=None,
1840-
allowed_scopes=None,
1841-
expires_in=None,
1842-
):
1843-
client_id, client_secret, hashed_secret = generate_client_credentials(confidential)
1844-
if arborist is not None:
1845-
arborist.create_client(client_id, policies)
1846-
driver = get_SQLAlchemyDriver(DB)
1847-
auth_method = "client_secret_basic" if confidential else "none"
1848-
1849-
allowed_scopes = allowed_scopes or config["CLIENT_ALLOWED_SCOPES"]
1850-
if not set(allowed_scopes).issubset(set(config["CLIENT_ALLOWED_SCOPES"])):
1851-
raise ValueError(
1852-
"Each allowed scope must be one of: {}".format(
1853-
config["CLIENT_ALLOWED_SCOPES"]
1854-
)
1855-
)
1856-
1857-
if "openid" not in allowed_scopes:
1858-
allowed_scopes.append("openid")
1859-
logger.warning('Adding required "openid" scope to list of allowed scopes.')
1860-
1861-
with driver.session as s:
1862-
user = None
1863-
if username:
1864-
user = query_for_user(session=s, username=username)
1865-
if not user:
1866-
user = User(username=username, is_admin=is_admin)
1867-
s.add(user)
1868-
1869-
if s.query(Client).filter(Client.name == name).first():
1870-
if arborist is not None:
1871-
arborist.delete_client(client_id)
1872-
raise Exception("client {} already exists".format(name))
1873-
1874-
client = Client(
1875-
client_id=client_id,
1876-
client_secret=hashed_secret,
1877-
user=user,
1878-
redirect_uris=urls,
1879-
allowed_scopes=" ".join(allowed_scopes),
1880-
description=description,
1881-
name=name,
1882-
auto_approve=auto_approve,
1883-
grant_types=grant_types,
1884-
is_confidential=confidential,
1885-
token_endpoint_auth_method=auth_method,
1886-
expires_in=expires_in,
1887-
)
1888-
s.add(client)
1889-
s.commit()
1890-
1891-
return client_id, client_secret

fence/utils.py

+69
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818

1919
from cdislogging import get_logger
2020
import flask
21+
from userdatamodel.user import User
2122

2223
from fence.errors import UserError
2324
from fence.config import config
2425
from authlib.oauth2.rfc6749.util import scope_to_list
2526
from authlib.oauth2.rfc6749.errors import InvalidScopeError
2627

28+
from fence.models import query_for_user, Client
29+
2730
rng = SystemRandom()
2831
alphanumeric = string.ascii_uppercase + string.ascii_lowercase + string.digits
2932
logger = get_logger(__name__)
@@ -321,3 +324,69 @@ def validate_scopes(request_scopes, client):
321324
raise InvalidScopeError("Failed to Authorize due to unsupported scope")
322325

323326
return True
327+
328+
329+
def create_client(
330+
DB,
331+
username=None,
332+
urls=[],
333+
name="",
334+
description="",
335+
auto_approve=False,
336+
is_admin=False,
337+
grant_types=None,
338+
confidential=True,
339+
arborist=None,
340+
policies=None,
341+
allowed_scopes=None,
342+
expires_in=None,
343+
):
344+
client_id, client_secret, hashed_secret = generate_client_credentials(confidential)
345+
if arborist is not None:
346+
arborist.create_client(client_id, policies)
347+
driver = get_SQLAlchemyDriver(DB)
348+
auth_method = "client_secret_basic" if confidential else "none"
349+
350+
allowed_scopes = allowed_scopes or config["CLIENT_ALLOWED_SCOPES"]
351+
if not set(allowed_scopes).issubset(set(config["CLIENT_ALLOWED_SCOPES"])):
352+
raise ValueError(
353+
"Each allowed scope must be one of: {}".format(
354+
config["CLIENT_ALLOWED_SCOPES"]
355+
)
356+
)
357+
358+
if "openid" not in allowed_scopes:
359+
allowed_scopes.append("openid")
360+
logger.warning('Adding required "openid" scope to list of allowed scopes.')
361+
362+
with driver.session as s:
363+
user = None
364+
if username:
365+
user = query_for_user(session=s, username=username)
366+
if not user:
367+
user = User(username=username, is_admin=is_admin)
368+
s.add(user)
369+
370+
if s.query(Client).filter(Client.name == name).first():
371+
if arborist is not None:
372+
arborist.delete_client(client_id)
373+
raise Exception("client {} already exists".format(name))
374+
375+
client = Client(
376+
client_id=client_id,
377+
client_secret=hashed_secret,
378+
user=user,
379+
redirect_uris=urls,
380+
allowed_scopes=" ".join(allowed_scopes),
381+
description=description,
382+
name=name,
383+
auto_approve=auto_approve,
384+
grant_types=grant_types,
385+
is_confidential=confidential,
386+
token_endpoint_auth_method=auth_method,
387+
expires_in=expires_in,
388+
)
389+
s.add(client)
390+
s.commit()
391+
392+
return client_id, client_secret

0 commit comments

Comments
 (0)