20
20
21
21
omq = Blueprint ('endpoint' , __name__ )
22
22
23
+
23
24
def endpoint (f ):
24
- """
25
+ """
25
26
Default endpoint for omq routes to pass requests to; constructs flask HTTP request
26
27
27
28
Message (request) components:
28
29
29
30
"blueprint" - the flask blueprint
30
- "query" - the request query
31
+ "query" - the request query
31
32
"pubkey" - pk of client making request
32
33
"params" - a json value to dump as the the query parameters
33
34
@@ -52,64 +53,68 @@ def abort_request(code, msg, warn=True):
52
53
53
54
54
55
def require_client ():
55
- """
56
- Requires that an authenticated client was found in the OMQ instance; aborts with
57
- UNAUTHORIZED if the request has no client
56
+ """
57
+ Requires that an authenticated client was found in the OMQ instance; aborts with
58
+ UNAUTHORIZED if the request has no client
58
59
"""
59
60
if g .client_id is None :
60
61
abort_request (http .UNAUTHORIZED , 'OMQ client authentication required' )
61
62
62
63
63
64
def client_required (f ):
64
- """
65
- Decorator for an endpoint that requires a client; this calls require_client() at the
66
- beginning of the request to abort the request as UNAUTHORIZED if the client has not been
67
- previously authenticated
65
+ """
66
+ Decorator for an endpoint that requires a client; this calls require_client() at the
67
+ beginning of the request to abort the request as UNAUTHORIZED if the client has not been
68
+ previously authenticated
68
69
"""
69
70
70
71
@wraps (f )
71
72
def required_client_wrapper (* args , ** kwargs ):
72
73
require_client ()
73
74
return f (* args , ** kwargs )
74
-
75
+
75
76
return required_client_wrapper
76
77
77
78
78
79
def require_authlevel (admin = True ):
79
80
require_client ()
80
- if g .client_authlevel is not oxenmq .Authlevel .admin if admin else g .client_authlevel is not oxenmq .Authlevel .basic :
81
+ if (
82
+ g .client_authlevel is not oxenmq .Authlevel .admin
83
+ if admin
84
+ else g .client_authlevel is not oxenmq .Authlevel .basic
85
+ ):
81
86
abort_request (
82
- http .FORBIDDEN ,
83
- f"This endpoint requires oxenmq.Authlevel.{ 'admin' if admin else 'basic' } permissions"
87
+ http .FORBIDDEN ,
88
+ f"This endpoint requires oxenmq.Authlevel.{ 'admin' if admin else 'basic' } permissions" ,
84
89
)
85
90
86
91
87
92
def basic_required (f ):
88
- """ Decorator for an endpoint that requires a client has basic OMQ authorization """
93
+ """Decorator for an endpoint that requires a client has basic OMQ authorization"""
89
94
90
95
@wraps (f )
91
96
def required_basic_wrapper (* args , ** kwargs ):
92
97
require_authlevel (admin = False )
93
98
return f (* args , ** kwargs )
94
-
99
+
95
100
return required_basic_wrapper
96
101
97
102
98
103
def admin_required (f ):
99
- """ Decorator for an endpoint that requires a client has admin OMQ authorization """
104
+ """Decorator for an endpoint that requires a client has admin OMQ authorization"""
100
105
101
106
@wraps (f )
102
107
def required_admin_wrapper (* args , ** kwargs ):
103
108
require_authlevel (admin = True )
104
109
return f (* args , ** kwargs )
105
-
110
+
106
111
return required_admin_wrapper
107
112
108
113
109
114
def first_request (f ):
110
- """ Decorator for an endpoint that will be the very first request for a given client. This
115
+ """Decorator for an endpoint that will be the very first request for a given client. This
111
116
will ensure that the client is then registered for any subsequent requests.
112
-
117
+
113
118
This function will typically take the folling parameters:
114
119
- cid : unique client ID to be attributed
115
120
- authlevel (oxenmq)
@@ -119,7 +124,7 @@ def first_request(f):
119
124
def first_request_wrapper (* args , cid , authlevel , ** kwargs ):
120
125
handle_omq_registration (cid , authlevel )
121
126
return f (* args , cid = cid , authlevel = authlevel , ** kwargs )
122
-
127
+
123
128
return first_request_wrapper
124
129
125
130
@@ -128,9 +133,11 @@ def handle_omq_registration(sid, authlevel):
128
133
Registers client with OMQ instance before its very first request
129
134
"""
130
135
if hasattr (g , 'client_id' ) and hasattr (g , 'client_authlevel' ) and not g .client_reauth :
131
- app .logger .warning (f"Client { g .client_id } already registered for { g .client_authlevel } access" )
136
+ app .logger .warning (
137
+ f"Client { g .client_id } already registered for { g .client_authlevel } access"
138
+ )
132
139
return
133
-
140
+
134
141
"""
135
142
Here goes ye olde OMQ registration logic. We need to decide what identification will
136
143
be used to verify every connected client s.t. that information persists for all subsequent
@@ -147,11 +154,13 @@ def verify_omq_auth():
147
154
"""
148
155
Verifies OMQ authentication before each request
149
156
"""
150
-
157
+
151
158
# If there is already a g.o_id, then this is NOT the first request made by this client, unless
152
159
# g.client_reauth has been specifically set
153
160
if hasattr (g , 'client_id' ) and hasattr (g , 'client_authlevel' ) and not g .client_reauth :
154
- app .logger .debug (f"Client { g .client_id } already authenticated for { g .client_authlevel } access" )
161
+ app .logger .debug (
162
+ f"Client { g .client_id } already authenticated for { g .client_authlevel } access"
163
+ )
155
164
return
156
165
157
166
0 commit comments