pypomes-iam 0.6.2__py3-none-any.whl → 0.8.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pypomes-iam might be problematic. Click here for more details.
- pypomes_iam/__init__.py +20 -12
- pypomes_iam/iam_actions.py +343 -122
- pypomes_iam/iam_common.py +71 -29
- pypomes_iam/iam_pomes.py +123 -100
- pypomes_iam/iam_services.py +330 -126
- pypomes_iam/provider_pomes.py +197 -30
- pypomes_iam/token_pomes.py +27 -0
- {pypomes_iam-0.6.2.dist-info → pypomes_iam-0.8.2.dist-info}/METADATA +2 -2
- pypomes_iam-0.8.2.dist-info/RECORD +11 -0
- pypomes_iam-0.6.2.dist-info/RECORD +0 -11
- {pypomes_iam-0.6.2.dist-info → pypomes_iam-0.8.2.dist-info}/WHEEL +0 -0
- {pypomes_iam-0.6.2.dist-info → pypomes_iam-0.8.2.dist-info}/licenses/LICENSE +0 -0
pypomes_iam/iam_common.py
CHANGED
|
@@ -3,7 +3,10 @@ import sys
|
|
|
3
3
|
from datetime import datetime
|
|
4
4
|
from enum import StrEnum, auto
|
|
5
5
|
from logging import Logger
|
|
6
|
-
from pypomes_core import
|
|
6
|
+
from pypomes_core import (
|
|
7
|
+
APP_PREFIX, TZ_LOCAL, exc_format,
|
|
8
|
+
env_get_str, env_get_int, env_get_enums
|
|
9
|
+
)
|
|
7
10
|
from pypomes_crypto import crypto_jwk_convert
|
|
8
11
|
from threading import RLock
|
|
9
12
|
from typing import Any, Final
|
|
@@ -21,12 +24,14 @@ class IamParam(StrEnum):
|
|
|
21
24
|
"""
|
|
22
25
|
Parameters for configuring *IAM* servers.
|
|
23
26
|
"""
|
|
27
|
+
|
|
24
28
|
ADMIN_ID = "admin-id"
|
|
25
29
|
ADMIN_SECRET = "admin-secret"
|
|
26
30
|
CLIENT_ID = "client-id"
|
|
27
31
|
CLIENT_REALM = "client-realm"
|
|
28
32
|
CLIENT_SECRET = "client-secret"
|
|
29
33
|
ENDPOINT_CALLBACK = "endpoint-callback"
|
|
34
|
+
ENDPOINT_CALLBACK_EXCHANGE = "endpoint-callback-exchange"
|
|
30
35
|
ENDPOINT_LOGIN = "endpoint-login"
|
|
31
36
|
ENDPOINT_LOGOUT = "endpoint_logout"
|
|
32
37
|
ENDPOINT_TOKEN = "endpoint-token"
|
|
@@ -34,8 +39,9 @@ class IamParam(StrEnum):
|
|
|
34
39
|
LOGIN_TIMEOUT = "login-timeout"
|
|
35
40
|
PK_EXPIRATION = "pk-expiration"
|
|
36
41
|
PK_LIFETIME = "pk-lifetime"
|
|
37
|
-
PUBLIC_KEY = "public-key"
|
|
38
42
|
RECIPIENT_ATTR = "recipient-attr"
|
|
43
|
+
# dynamic attributes
|
|
44
|
+
PUBLIC_KEY = "public-key"
|
|
39
45
|
URL_BASE = "url-base"
|
|
40
46
|
USERS = "users"
|
|
41
47
|
|
|
@@ -54,31 +60,66 @@ class UserParam(StrEnum):
|
|
|
54
60
|
REDIRECT_URI = "redirect-uri"
|
|
55
61
|
|
|
56
62
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
63
|
+
def __get_iam_data() -> dict[IamServer, dict[IamParam, Any]]:
|
|
64
|
+
"""
|
|
65
|
+
Obtain the configuration data for select *IAM* servers.
|
|
66
|
+
|
|
67
|
+
The configuration parameters for the IAM servers are specified dynamically with environment variables,
|
|
68
|
+
or dynamically with calls to *iam_setup_server()*. Specifying configuration parameters with environment
|
|
69
|
+
variables can be done by following these steps:
|
|
70
|
+
|
|
71
|
+
1. Specify *<APP_PREFIX>_IAM_SERVERS* with a list of names among the values found in *IamServer* class
|
|
72
|
+
(currently, *jusbr* and *keycloak* are supported), and the data set below for each server, where
|
|
73
|
+
*<IAM>* stands for the server's name as presented in *IamServer* class:
|
|
74
|
+
- *<APP_PREFIX>_<IAM>_ADMIN_ID* (optional, required if administrative duties are performed)
|
|
75
|
+
- *<APP_PREFIX>_<IAM>_ADMIN_PWD* (optional, required if administrative duties are performed)
|
|
76
|
+
- *<APP_PREFIX>_<IAM>_CLIENT_ID* (required)
|
|
77
|
+
- *<APP_PREFIX>_<IAM>_CLIENT_REALM* (required)
|
|
78
|
+
- *<APP_PREFIX>_<IAM>_CLIENT_SECRET* (required)
|
|
79
|
+
- *<APP_PREFIX>_<IAM>_LOGIN_TIMEOUT* (optional, defaults to no timeout)
|
|
80
|
+
- *<APP_PREFIX>_<IAM>_PK_LIFETIME* (optional, defaults to non-terminating lifetime)
|
|
81
|
+
- *<APP_PREFIX>_<IAM>_RECIPIENT_ATTR* (required)
|
|
82
|
+
- *<APP_PREFIX>_<IAM>_URL_BASE* (required)
|
|
83
|
+
|
|
84
|
+
2. A group of special environment variables identifying endpoints for authentication services may be specified,
|
|
85
|
+
following the same scheme as presented in item *1* above. These are not part of the *IAM* server's setup,
|
|
86
|
+
but are meant to be used by function *iam_setup_endpoints()*, wherein the values in those variables
|
|
87
|
+
would represent default values for its parameters, respectively:
|
|
88
|
+
- *<APP_PREFIX>_<IAM>_ENDPOINT_CALLBACK*
|
|
89
|
+
- *<APP_PREFIX>_<IAM>_ENDPOINT_CALLBACK_EXCHANGE*
|
|
90
|
+
- *<APP_PREFIX>_<IAM>_ENDPOINT_EXCHANGE*
|
|
91
|
+
- *<APP_PREFIX>_<IAM>_ENDPOINT_LOGIN*
|
|
92
|
+
- *<APP_PREFIX>_<IAM>_ENDPOINT_LOGOUT*
|
|
93
|
+
- *<APP_PREFIX>_<IAM>_ENDPOINT_TOKEN*
|
|
94
|
+
- *<APP_PREFIX>_<IAM>_ENDPOINT_USERINFO*
|
|
95
|
+
|
|
96
|
+
:return: the configuration data for the select *IAM* servers.
|
|
97
|
+
"""
|
|
98
|
+
# initialize the return variable
|
|
99
|
+
result: dict[IamServer, dict[IamParam, Any]] = {}
|
|
100
|
+
|
|
101
|
+
servers: list[IamServer] = env_get_enums(key=f"{APP_PREFIX}_IAM_SERVERS",
|
|
102
|
+
enum_class=IamServer) or []
|
|
103
|
+
for server in servers:
|
|
104
|
+
prefix = server.name
|
|
105
|
+
result[server] = {
|
|
106
|
+
IamParam.ADMIN_ID: env_get_str(key=f"{APP_PREFIX}_{prefix}_ADMIN_ID"),
|
|
107
|
+
IamParam.ADMIN_SECRET: env_get_str(key=f"{APP_PREFIX}_{prefix}_ADMIN_SECRET"),
|
|
108
|
+
IamParam.CLIENT_ID: env_get_str(key=f"{APP_PREFIX}_{prefix}_CLIENT_ID"),
|
|
109
|
+
IamParam.CLIENT_REALM: env_get_str(key=f"{APP_PREFIX}_{prefix}_CLIENT_REALM"),
|
|
110
|
+
IamParam.CLIENT_SECRET: env_get_str(key=f"{APP_PREFIX}_{prefix}_CLIENT_SECRET"),
|
|
111
|
+
IamParam.LOGIN_TIMEOUT: env_get_str(key=f"{APP_PREFIX}_{prefix}_LOGIN_TIMEOUT"),
|
|
112
|
+
IamParam.PK_LIFETIME: env_get_int(key=f"{APP_PREFIX}_{prefix}_PUBLIC_KEY_LIFETIME"),
|
|
113
|
+
IamParam.RECIPIENT_ATTR: env_get_str(key=f"{APP_PREFIX}_{prefix}_RECIPIENT_ATTR"),
|
|
114
|
+
IamParam.URL_BASE: env_get_str(key=f"{APP_PREFIX}_{prefix}_URL_AUTH_BASE"),
|
|
115
|
+
# dynamically set
|
|
116
|
+
IamParam.PK_EXPIRATION: 0,
|
|
117
|
+
IamParam.PUBLIC_KEY: None,
|
|
118
|
+
IamParam.USERS: {}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return result
|
|
122
|
+
|
|
82
123
|
|
|
83
124
|
# registry structure:
|
|
84
125
|
# { <IamServer>:
|
|
@@ -91,6 +132,7 @@ class UserParam(StrEnum):
|
|
|
91
132
|
# "client-realm": <str,
|
|
92
133
|
# "client-timeout": <int>,
|
|
93
134
|
# "recipient-attr": <str>,
|
|
135
|
+
# # dynamic attributes
|
|
94
136
|
# "public-key": <str>,
|
|
95
137
|
# "pk-lifetime": <int>,
|
|
96
138
|
# "pk-expiration": <int>,
|
|
@@ -112,10 +154,10 @@ class UserParam(StrEnum):
|
|
|
112
154
|
# },
|
|
113
155
|
# ...
|
|
114
156
|
# }
|
|
115
|
-
_IAM_SERVERS: Final[dict[IamServer, dict[IamParam, Any]]] =
|
|
157
|
+
_IAM_SERVERS: Final[dict[IamServer, dict[IamParam, Any]]] = __get_iam_data()
|
|
116
158
|
|
|
117
159
|
|
|
118
|
-
# the lock protecting the data in '
|
|
160
|
+
# the lock protecting the data in '_<IAM>_SERVERS'
|
|
119
161
|
# (because it is 'Final' and set at declaration time, it can be accessed through simple imports)
|
|
120
162
|
_iam_lock: Final[RLock] = RLock()
|
|
121
163
|
|
pypomes_iam/iam_pomes.py
CHANGED
|
@@ -1,37 +1,39 @@
|
|
|
1
1
|
from flask import Flask
|
|
2
|
-
from
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
from pypomes_core import (
|
|
3
|
+
APP_PREFIX,
|
|
4
|
+
env_get_int, env_get_str,
|
|
5
|
+
func_capture_params, func_defaulted_params
|
|
6
|
+
)
|
|
5
7
|
|
|
6
8
|
from .iam_common import (
|
|
7
9
|
_IAM_SERVERS, IamServer, IamParam, _iam_lock
|
|
8
10
|
)
|
|
9
|
-
from .iam_actions import action_token
|
|
10
11
|
from .iam_services import (
|
|
11
|
-
service_login, service_logout,
|
|
12
|
+
service_login, service_logout,
|
|
13
|
+
service_callback, service_callback_exchange,
|
|
14
|
+
service_exchange, service_get_token, service_userinfo
|
|
12
15
|
)
|
|
13
16
|
|
|
14
17
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
callback_endpoint: str = None,
|
|
27
|
-
exchange_endpoint: str = None,
|
|
28
|
-
login_endpoint: str = None,
|
|
29
|
-
logout_endpoint: str = None,
|
|
30
|
-
token_endpoint: str = None) -> None:
|
|
18
|
+
@func_capture_params
|
|
19
|
+
def iam_setup_server(iam_server: IamServer,
|
|
20
|
+
admin_id: str = None,
|
|
21
|
+
admin_secret: str = None,
|
|
22
|
+
client_id: str = None,
|
|
23
|
+
client_realm: str = None,
|
|
24
|
+
client_secret: str = None,
|
|
25
|
+
login_timeout: int = None,
|
|
26
|
+
pk_lifetime: int = None,
|
|
27
|
+
recipient_attr: str = None,
|
|
28
|
+
url_base: str = None) -> None:
|
|
31
29
|
"""
|
|
32
|
-
|
|
30
|
+
Setup the *IAM* server *iam_server*.
|
|
31
|
+
|
|
32
|
+
For the parameters not effectively passed, an attempt is made to obtain a value from the corresponding
|
|
33
|
+
environment variables. Most parameters are required to have values, which must be assigned either
|
|
34
|
+
throught the function invocation, or from the corresponding environment variables.
|
|
33
35
|
|
|
34
|
-
The parameters *admin_id* and *admin_* are required only if administrative
|
|
36
|
+
The parameters *admin_id* and *admin_* are required only if performing administrative task are intended.
|
|
35
37
|
The optional parameter *client_timeout* refers to the maximum time in seconds allowed for the
|
|
36
38
|
user to login at the *IAM* server's login page, and defaults to no time limit.
|
|
37
39
|
|
|
@@ -39,47 +41,122 @@ def iam_setup(flask_app: Flask,
|
|
|
39
41
|
it is not provided, but *admin_id* and *admin_secret* are, it is obtained from the *IAM* server itself
|
|
40
42
|
the first time it is needed.
|
|
41
43
|
|
|
42
|
-
:param
|
|
43
|
-
:param iam_server: identifies the supported *IAM* server (*jusbr* or *keycloak*)
|
|
44
|
-
:param base_url: base URL to request services
|
|
45
|
-
:param client_id: the client's identification with the *IAM* server
|
|
46
|
-
:param client_realm: the client realm
|
|
47
|
-
:param client_secret: the client's password with the *IAM* server
|
|
48
|
-
:param recipient_attribute: attribute in the token's payload holding the token's subject
|
|
44
|
+
:param iam_server: identifies the supported *IAM* server (currently, *jusbr* or *keycloak*)
|
|
49
45
|
:param admin_id: identifies the realm administrator
|
|
50
46
|
:param admin_secret: password for the realm administrator
|
|
47
|
+
:param client_id: the client's identification with the *IAM* server
|
|
48
|
+
:param client_realm: the client's realm
|
|
49
|
+
:param client_secret: the client's password with the *IAM* server
|
|
51
50
|
:param login_timeout: timeout for login authentication (in seconds,defaults to no timeout)
|
|
52
|
-
:param
|
|
53
|
-
:param
|
|
54
|
-
:param
|
|
55
|
-
:param login_endpoint: endpoint for redirecting user to the *IAM* server's login page
|
|
56
|
-
:param logout_endpoint: endpoint for terminating user access
|
|
57
|
-
:param token_endpoint: endpoint for retrieving authentication token
|
|
51
|
+
:param pk_lifetime: how long to use *IAM* server's public key, before refreshing it (in seconds)
|
|
52
|
+
:param recipient_attr: attribute in the token's payload holding the token's subject
|
|
53
|
+
:param url_base: base URL to request services
|
|
58
54
|
"""
|
|
55
|
+
# obtain the defaulted parameters
|
|
56
|
+
defaulted_params: list[str] = func_defaulted_params.get()
|
|
57
|
+
|
|
58
|
+
# read from the environment variables
|
|
59
|
+
prefix: str = iam_server.name
|
|
60
|
+
if "admin_id" in defaulted_params:
|
|
61
|
+
admin_id = env_get_str(key=f"{APP_PREFIX}_{prefix}_ADMIN_ID")
|
|
62
|
+
if "admin_secret" in defaulted_params:
|
|
63
|
+
admin_secret = env_get_str(key=f"{APP_PREFIX}_{prefix}_ADMIN_SECRET")
|
|
64
|
+
if "client_id" in defaulted_params:
|
|
65
|
+
client_id = env_get_str(key=f"{APP_PREFIX}_{prefix}_CLIENT_ID")
|
|
66
|
+
if "client_realm" in defaulted_params:
|
|
67
|
+
client_realm = env_get_str(key=f"{APP_PREFIX}_{prefix}_CLIENT_REALM")
|
|
68
|
+
if "client_secret" in defaulted_params:
|
|
69
|
+
client_secret = env_get_str(key=f"{APP_PREFIX}_{prefix}_CLIENT_SECRET")
|
|
70
|
+
if "login_timeout" in defaulted_params:
|
|
71
|
+
login_timeout = env_get_str(key=f"{APP_PREFIX}_{prefix}_LOGIN_TIMEOUT")
|
|
72
|
+
if "pk_lifetime" in defaulted_params:
|
|
73
|
+
pk_lifetime = env_get_int(key=f"{APP_PREFIX}_{prefix}_PUBLIC_KEY_LIFETIME")
|
|
74
|
+
if "recipient_attr" in defaulted_params:
|
|
75
|
+
recipient_attr = env_get_str(key=f"{APP_PREFIX}_{prefix}_RECIPIENT_ATTR")
|
|
76
|
+
if "url_base" in defaulted_params:
|
|
77
|
+
url_base = env_get_str(key=f"{APP_PREFIX}_{prefix}_URL_AUTH_BASE")
|
|
59
78
|
|
|
60
79
|
# configure the Keycloak registry
|
|
61
80
|
with _iam_lock:
|
|
62
81
|
_IAM_SERVERS[iam_server] = {
|
|
63
|
-
IamParam.URL_BASE: base_url,
|
|
64
82
|
IamParam.CLIENT_ID: client_id,
|
|
65
83
|
IamParam.CLIENT_REALM: client_realm,
|
|
66
84
|
IamParam.CLIENT_SECRET: client_secret,
|
|
67
|
-
IamParam.RECIPIENT_ATTR:
|
|
85
|
+
IamParam.RECIPIENT_ATTR: recipient_attr,
|
|
68
86
|
IamParam.ADMIN_ID: admin_id,
|
|
69
87
|
IamParam.ADMIN_SECRET: admin_secret,
|
|
70
88
|
IamParam.LOGIN_TIMEOUT: login_timeout,
|
|
71
|
-
IamParam.PK_LIFETIME:
|
|
89
|
+
IamParam.PK_LIFETIME: pk_lifetime,
|
|
90
|
+
IamParam.URL_BASE: url_base,
|
|
91
|
+
# dynamic attributes
|
|
72
92
|
IamParam.PK_EXPIRATION: 0,
|
|
73
93
|
IamParam.PUBLIC_KEY: None,
|
|
74
94
|
IamParam.USERS: {}
|
|
75
95
|
}
|
|
76
96
|
|
|
97
|
+
|
|
98
|
+
@func_capture_params
|
|
99
|
+
def iam_setup_endpoints(flask_app: Flask,
|
|
100
|
+
iam_server: IamServer,
|
|
101
|
+
callback_endpoint: str = None,
|
|
102
|
+
callback_exchange_endpoint: str = None,
|
|
103
|
+
exchange_endpoint: str = None,
|
|
104
|
+
login_endpoint: str = None,
|
|
105
|
+
logout_endpoint: str = None,
|
|
106
|
+
token_endpoint: str = None,
|
|
107
|
+
userinfo_endpoint: str = None) -> None:
|
|
108
|
+
"""
|
|
109
|
+
Setup the endpoints for accessing the services provided by *iam_server*.
|
|
110
|
+
|
|
111
|
+
For the parameters not effectively passed, an attempt is made to obtain a value from the corresponding
|
|
112
|
+
environment variables.
|
|
113
|
+
|
|
114
|
+
:param flask_app: the Flask application
|
|
115
|
+
:param iam_server: identifies the supported *IAM* server (currently, *jusbr* or *keycloak*)
|
|
116
|
+
:param callback_endpoint: endpoint for the callback from the front end
|
|
117
|
+
:param callback_exchange_endpoint: endpoint for the combination callback and exchange
|
|
118
|
+
:param exchange_endpoint: endpoint for requesting token exchange
|
|
119
|
+
:param login_endpoint: endpoint for redirecting user to the *IAM* server's login page
|
|
120
|
+
:param logout_endpoint: endpoint for terminating user access
|
|
121
|
+
:param token_endpoint: endpoint for retrieving authentication token
|
|
122
|
+
:param userinfo_endpoint: endpoint for retrieving user data
|
|
123
|
+
"""
|
|
124
|
+
# obtain the defaulted parameters
|
|
125
|
+
defaulted_params: list[str] = func_defaulted_params.get()
|
|
126
|
+
|
|
127
|
+
# read from the environment variables
|
|
128
|
+
prefix: str = iam_server.name
|
|
129
|
+
if "callback_endpoint" in defaulted_params:
|
|
130
|
+
callback_endpoint = env_get_str(key=f"{APP_PREFIX}_{prefix}_ENDPOINT_CALLBACK")
|
|
131
|
+
if "callback_exchange_endpoint" in defaulted_params:
|
|
132
|
+
callback_exchange_endpoint = env_get_str(key=f"{APP_PREFIX}_{prefix}_ENDPOINT_CALLBACK_EXCHANGE")
|
|
133
|
+
if "exchange_endpoint" in defaulted_params:
|
|
134
|
+
callback_endpoint = env_get_str(key=f"{APP_PREFIX}_{prefix}_ENDPOINT_EXCHANGE")
|
|
135
|
+
if "login_endpoint" in defaulted_params:
|
|
136
|
+
login_endpoint = env_get_str(key=f"{APP_PREFIX}_{prefix}_ENDPOINT_LOGIN")
|
|
137
|
+
if "logout_endpoint" in defaulted_params:
|
|
138
|
+
logout_endpoint = env_get_str(key=f"{APP_PREFIX}_{prefix}_ENDPOINT_LOGOUT")
|
|
139
|
+
if "token_endpoint" in defaulted_params:
|
|
140
|
+
token_endpoint = env_get_str(key=f"{APP_PREFIX}_{prefix}_ENDPOINT_TOKEN")
|
|
141
|
+
if "userinfo_endpoint" in defaulted_params:
|
|
142
|
+
userinfo_endpoint = env_get_str(key=f"{APP_PREFIX}_{prefix}_ENDPOINT_USERINFO")
|
|
143
|
+
|
|
77
144
|
# establish the endpoints
|
|
78
145
|
if callback_endpoint:
|
|
79
146
|
flask_app.add_url_rule(rule=callback_endpoint,
|
|
80
147
|
endpoint=f"{iam_server}-callback",
|
|
81
148
|
view_func=service_callback,
|
|
82
149
|
methods=["GET"])
|
|
150
|
+
if callback_exchange_endpoint:
|
|
151
|
+
flask_app.add_url_rule(rule=callback_exchange_endpoint,
|
|
152
|
+
endpoint=f"{iam_server}-callback-exchange",
|
|
153
|
+
view_func=service_callback_exchange,
|
|
154
|
+
methods=["GET"])
|
|
155
|
+
if exchange_endpoint:
|
|
156
|
+
flask_app.add_url_rule(rule=exchange_endpoint,
|
|
157
|
+
endpoint=f"{iam_server}-exchange",
|
|
158
|
+
view_func=service_exchange,
|
|
159
|
+
methods=["POST"])
|
|
83
160
|
if login_endpoint:
|
|
84
161
|
flask_app.add_url_rule(rule=login_endpoint,
|
|
85
162
|
endpoint=f"{iam_server}-login",
|
|
@@ -89,68 +166,14 @@ def iam_setup(flask_app: Flask,
|
|
|
89
166
|
flask_app.add_url_rule(rule=logout_endpoint,
|
|
90
167
|
endpoint=f"{iam_server}-logout",
|
|
91
168
|
view_func=service_logout,
|
|
92
|
-
methods=["
|
|
169
|
+
methods=["POST"])
|
|
93
170
|
if token_endpoint:
|
|
94
171
|
flask_app.add_url_rule(rule=token_endpoint,
|
|
95
172
|
endpoint=f"{iam_server}-token",
|
|
96
|
-
view_func=
|
|
173
|
+
view_func=service_get_token,
|
|
174
|
+
methods=["GET"])
|
|
175
|
+
if userinfo_endpoint:
|
|
176
|
+
flask_app.add_url_rule(rule=userinfo_endpoint,
|
|
177
|
+
endpoint=f"{iam_server}-userinfo",
|
|
178
|
+
view_func=service_userinfo,
|
|
97
179
|
methods=["GET"])
|
|
98
|
-
if exchange_endpoint:
|
|
99
|
-
flask_app.add_url_rule(rule=exchange_endpoint,
|
|
100
|
-
endpoint=f"{iam_server}-exchange",
|
|
101
|
-
view_func=service_exchange,
|
|
102
|
-
methods=["POST"])
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
def iam_get_env_parameters(iam_prefix: str = None) -> dict[str, Any]:
|
|
106
|
-
"""
|
|
107
|
-
Retrieve the set parameters for a *IAM* server from the environment.
|
|
108
|
-
|
|
109
|
-
the parameters are returned ready to be used as a '**kwargs' parameter set in a call to *iam_setup()*,
|
|
110
|
-
and sorted in the order appropriate to use them instead with a '*args' parameter set.
|
|
111
|
-
|
|
112
|
-
:param iam_prefix: the prefix classifying the parameters
|
|
113
|
-
:return: the sorted parameters classified by *prefix*
|
|
114
|
-
"""
|
|
115
|
-
return {
|
|
116
|
-
"base_url": env_get_str(key=f"{APP_PREFIX}_{iam_prefix}_URL_AUTH_BASE"),
|
|
117
|
-
"client_id": env_get_str(key=f"{APP_PREFIX}_{iam_prefix}_CLIENT_ID"),
|
|
118
|
-
"client_realm": env_get_str(key=f"{APP_PREFIX}_{iam_prefix}_CLIENT_REALM"),
|
|
119
|
-
"client_secret": env_get_str(key=f"{APP_PREFIX}_{iam_prefix}_CLIENT_SECRET"),
|
|
120
|
-
"recipient_attribute": env_get_str(key=f"{APP_PREFIX}_{iam_prefix}_RECIPIENT_ATTR"),
|
|
121
|
-
"admin_id": env_get_str(key=f"{APP_PREFIX}_{iam_prefix}_ADMIN_ID"),
|
|
122
|
-
"admin_secret": env_get_str(key=f"{APP_PREFIX}_{iam_prefix}_ADMIN_SECRET"),
|
|
123
|
-
"login_timeout": env_get_str(key=f"{APP_PREFIX}_{iam_prefix}_LOGIN_TIMEOUT"),
|
|
124
|
-
"public_key_lifetime": env_get_int(key=f"{APP_PREFIX}_{iam_prefix}_PUBLIC_KEY_LIFETIME"),
|
|
125
|
-
"callback_endpoint": env_get_str(key=f"{APP_PREFIX}_{iam_prefix}_ENDPOINT_CALLBACK"),
|
|
126
|
-
"exchange_endpoint": env_get_str(key=f"{APP_PREFIX}_{iam_prefix}_ENDPOINT_EXCHANGE"),
|
|
127
|
-
"login_endpoint": env_get_str(key=f"{APP_PREFIX}_{iam_prefix}_ENDPOINT_LOGIN"),
|
|
128
|
-
"logout_endpoint": env_get_str(key=f"{APP_PREFIX}_{iam_prefix}_ENDPOINT_LOGOUT"),
|
|
129
|
-
"token_endpoint": env_get_str(key=f"{APP_PREFIX}_{iam_prefix}_ENDPOINT_TOKEN")
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
def iam_get_token(iam_server: IamServer,
|
|
134
|
-
user_id: str,
|
|
135
|
-
errors: list[str] = None,
|
|
136
|
-
logger: Logger = None) -> str:
|
|
137
|
-
"""
|
|
138
|
-
Retrieve an authentication token for *user_id*.
|
|
139
|
-
|
|
140
|
-
:param iam_server: identifies the *IAM* server
|
|
141
|
-
:param user_id: identifies the user
|
|
142
|
-
:param errors: incidental errors
|
|
143
|
-
:param logger: optional logger
|
|
144
|
-
:return: the uthentication tokem
|
|
145
|
-
"""
|
|
146
|
-
# declare the return variable
|
|
147
|
-
result: str
|
|
148
|
-
|
|
149
|
-
# retrieve the token
|
|
150
|
-
args: dict[str, Any] = {"user-id": user_id}
|
|
151
|
-
with _iam_lock:
|
|
152
|
-
result = action_token(iam_server=iam_server,
|
|
153
|
-
args=args,
|
|
154
|
-
errors=errors,
|
|
155
|
-
logger=logger)
|
|
156
|
-
return result
|