Skip to content

Commit f233b2c

Browse files
committed
feat: allow to customize scopes
1 parent dfb1444 commit f233b2c

File tree

2 files changed

+100
-72
lines changed

2 files changed

+100
-72
lines changed

src/config.rs

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,62 @@ lazy_static! {
3434
DatabaseConfiguration::extract().unwrap();
3535
}
3636

37-
#[derive(Deserialize, Debug, Clone, Getters)]
38-
#[get = "pub"]
39-
pub struct Config {
40-
#[serde(default = "default_global_rate_limit")]
41-
global_rate_limit: u64,
42-
oidc_provider: String,
43-
#[serde(default = "default_oidc_audience")]
44-
oidc_audience: String,
45-
oidc_issuer: Option<String>,
46-
config_path: Option<String>,
47-
otlp_endpoint: Option<String>,
48-
#[serde(default = "default_service_name")]
49-
service_name: String
50-
}
37+
macro_rules! config {
38+
(($($ident:ident: $type:ty $(,)? )*), ($($dident:ident: $dtype:ty = $default:expr $(,)?)*)) => {
39+
paste! {
40+
#[derive(Deserialize, Debug, Clone, Getters)]
41+
#[get = "pub"]
42+
pub struct Config {
43+
$(
44+
$ident: $type,
45+
)*
46+
47+
$(
48+
#[serde(default = "default_" $dident)]
49+
$dident: $dtype,
50+
)*
51+
}
5152

52-
#[inline]
53-
fn default_global_rate_limit() -> u64 {
54-
10
55-
}
5653

57-
#[inline]
58-
fn default_oidc_audience() -> String {
59-
"feedback-fusion".to_owned()
54+
$(
55+
#[inline]
56+
fn [<default_ $dident>]() -> $dtype {
57+
$default.to_owned()
58+
}
59+
)*
60+
}
61+
};
6062
}
6163

62-
#[inline]
63-
fn default_service_name() -> String {
64-
"feedback-fusion".to_owned()
65-
}
64+
config!(
65+
(
66+
oidc_provider: String,
67+
oidc_issuer: Option<String>,
68+
config_path: Option<String>,
69+
otlp_endpoint: Option<String>,
70+
),
71+
72+
(
73+
global_rate_limit: u64 = 10,
74+
service_name: String = "feedback-fusion"
75+
oidc_audience: String = "feedback-fusion",
76+
77+
oidc_scope_api: String = "api:feedback-fusion",
78+
oidc_scope_write: String = "feedback-fusion:write",
79+
oidc_scope_read: String = "feedback-fusion:read",
80+
81+
oidc_scope_write_target: String = "feedback-fusion:writeTarget",
82+
oidc_scope_read_target: String = "feedback-fusion:readTarget"
83+
84+
oidc_scope_write_prompt: String = "feedback-fusion:writePrompt",
85+
oidc_scope_read_prompt: String = "feedback-fusion:readPrompt"
86+
87+
oidc_scope_write_field: String = "feedback-fusion:writeField",
88+
oidc_scope_read_field: String = "feedback-fusion:readField"
89+
90+
oidc_scope_read_response: String = "feedback-fusion:readResponse"
91+
)
92+
);
6693

6794
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
6895
pub struct InstanceConfig {

src/services/v1/mod.rs

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222

2323
use crate::{database::schema::feedback::Prompt, prelude::*};
24-
use aliri_oauth2::{policy, scope, HasScope};
24+
use aliri_oauth2::{policy, HasScope};
2525
use aliri_traits::Policy;
2626
use feedback_fusion_common::proto::{
2727
feedback_fusion_v1_server::FeedbackFusionV1,
@@ -53,7 +53,7 @@ pub struct PublicFeedbackFusionV1Context {
5353

5454
// https://github.com/neoeinstein/aliri/blob/main/aliri_tower/examples/.tonic.rs#L35
5555
macro_rules! handler {
56-
($handler:path, $self:ident, $request:ident, $policy:ident, $($scope:literal $(,)?)*) => {{
56+
($handler:path, $self:ident, $request:ident, $policy:ident, $($scope:expr $(,)?)*) => {{
5757
$policy
5858
.evaluate(
5959
$request
@@ -76,12 +76,12 @@ macro_rules! handler {
7676

7777
handler!($handler, $self, $request)
7878
}};
79-
($handler:path, $self:ident, $request:ident, $($scope:literal $(,)?)*, $target:block) => {{
79+
($handler:path, $self:ident, $request:ident, $($scope:expr $(,)?)* => $target:block) => {{
8080
paste! {
8181
let policy = policy![
82-
scope!["api:feedback-fusion"]
82+
aliri_oauth2::Scope::empty().and(aliri_oauth2::scope::ScopeToken::from_string(CONFIG.oidc_scope_api().clone()).unwrap())
8383
$(,
84-
scope![$scope]
84+
aliri_oauth2::Scope::empty().and(aliri_oauth2::scope::ScopeToken::from_string($scope.to_string()).unwrap())
8585
)*
8686
];
8787

@@ -145,11 +145,11 @@ macro_rules! handler {
145145
}
146146
}
147147
}};
148-
($handler:path, $self:ident, $request:ident, $($scope:literal $(,)?)*) => {{
148+
($handler:path, $self:ident, $request:ident, $($scope:expr $(,)?)*) => {{
149149
let policy = policy![
150-
scope!["api:feedback-fusion"]
150+
aliri_oauth2::Scope::empty().and(aliri_oauth2::scope::ScopeToken::from_string(CONFIG.oidc_scope_api().clone()).unwrap())
151151
$(,
152-
scope![$scope]
152+
aliri_oauth2::Scope::empty().and(aliri_oauth2::scope::ScopeToken::from_string($scope.to_string()).unwrap())
153153
)*
154154
];
155155

@@ -196,7 +196,8 @@ impl FeedbackFusionV1 for FeedbackFusionV1Context {
196196
target::create_target,
197197
self,
198198
request,
199-
"feedback-fusion:write"
199+
CONFIG.oidc_scope_write(),
200+
CONFIG.oidc_scope_write_target()
200201
)
201202
}
202203

@@ -209,9 +210,9 @@ impl FeedbackFusionV1 for FeedbackFusionV1Context {
209210
target::get_target,
210211
self,
211212
request,
212-
"feedback-fusion:read",
213-
"feedback-fusion:getTarget",
214-
{ Ok::<_, FeedbackFusionError>(Some(request.get_ref().id.clone())) }
213+
CONFIG.oidc_scope_read(),
214+
CONFIG.oidc_scope_read_target()
215+
=> { Ok::<_, FeedbackFusionError>(Some(request.get_ref().id.clone())) }
215216
)
216217
}
217218

@@ -224,8 +225,8 @@ impl FeedbackFusionV1 for FeedbackFusionV1Context {
224225
target::get_targets,
225226
self,
226227
request,
227-
"feedback-fusion:read",
228-
"feedback-fusion:listTargets"
228+
CONFIG.oidc_scope_read(),
229+
CONFIG.oidc_scope_read_target()
229230
)
230231
}
231232

@@ -238,9 +239,9 @@ impl FeedbackFusionV1 for FeedbackFusionV1Context {
238239
target::update_target,
239240
self,
240241
request,
241-
"feedback-fusion:write",
242-
"feedback-fusion:putTarget",
243-
{ Ok::<_, FeedbackFusionError>(Some(request.get_ref().id.clone())) }
242+
CONFIG.oidc_scope_write(),
243+
CONFIG.oidc_scope_write_target()
244+
=> { Ok::<_, FeedbackFusionError>(Some(request.get_ref().id.clone())) }
244245
)
245246
}
246247

@@ -253,9 +254,9 @@ impl FeedbackFusionV1 for FeedbackFusionV1Context {
253254
target::delete_target,
254255
self,
255256
request,
256-
"feedback-fusion:write",
257-
"feedback-fusion:deleteTarget",
258-
{ Ok::<_, FeedbackFusionError>(Some(request.get_ref().id.clone())) }
257+
CONFIG.oidc_scope_write(),
258+
CONFIG.oidc_scope_write_target()
259+
=> { Ok::<_, FeedbackFusionError>(Some(request.get_ref().id.clone())) }
259260
)
260261
}
261262

@@ -268,9 +269,9 @@ impl FeedbackFusionV1 for FeedbackFusionV1Context {
268269
prompt::create_prompt,
269270
self,
270271
request,
271-
"feedback-fusion:write",
272-
"feedback-fusion:writePrompt",
273-
{ Ok::<_, FeedbackFusionError>(Some(request.get_ref().target.clone())) }
272+
CONFIG.oidc_scope_write(),
273+
CONFIG.oidc_scope_write_prompt()
274+
=> { Ok::<_, FeedbackFusionError>(Some(request.get_ref().target.clone())) }
274275
)
275276
}
276277

@@ -283,9 +284,9 @@ impl FeedbackFusionV1 for FeedbackFusionV1Context {
283284
prompt::get_prompts,
284285
self,
285286
request,
286-
"feedback-fusion:read",
287-
"feedback-fusion:listPrompts",
288-
{ Ok::<_, FeedbackFusionError>(Some(request.get_ref().target.clone())) }
287+
CONFIG.oidc_scope_read(),
288+
CONFIG.oidc_scope_read_prompt()
289+
=> { Ok::<_, FeedbackFusionError>(Some(request.get_ref().target.clone())) }
289290
)
290291
}
291292

@@ -298,9 +299,9 @@ impl FeedbackFusionV1 for FeedbackFusionV1Context {
298299
prompt::update_prompt,
299300
self,
300301
request,
301-
"feedback-fusion:write",
302-
"feedback-fusion:putPrompt",
303-
{
302+
CONFIG.oidc_scope_write(),
303+
CONFIG.oidc_scope_write_prompt()
304+
=> {
304305
Ok::<_, FeedbackFusionError>(
305306
database_request!(
306307
Prompt::select_by_id(self.connection(), request.get_ref().id.as_str())
@@ -322,9 +323,9 @@ impl FeedbackFusionV1 for FeedbackFusionV1Context {
322323
prompt::delete_prompt,
323324
self,
324325
request,
325-
"feedback-fusion:write",
326-
"feedback-fusion:deleteTarget",
327-
{
326+
CONFIG.oidc_scope_write(),
327+
CONFIG.oidc_scope_write_prompt()
328+
=> {
328329
Ok::<_, FeedbackFusionError>(
329330
database_request!(
330331
Prompt::select_by_id(self.connection(), request.get_ref().id.as_str())
@@ -346,9 +347,9 @@ impl FeedbackFusionV1 for FeedbackFusionV1Context {
346347
field::create_field,
347348
self,
348349
request,
349-
"feedback-fusion:write",
350-
"feedback-fusion:writeField",
351-
{
350+
CONFIG.oidc_scope_write(),
351+
CONFIG.oidc_scope_write_field()
352+
=> {
352353
Ok::<_, FeedbackFusionError>(
353354
database_request!(
354355
Prompt::select_by_id(self.connection(), request.get_ref().prompt.as_str())
@@ -370,9 +371,9 @@ impl FeedbackFusionV1 for FeedbackFusionV1Context {
370371
field::get_fields,
371372
self,
372373
request,
373-
"feedback-fusion:read",
374-
"feedback-fusion:listFields",
375-
{
374+
CONFIG.oidc_scope_read(),
375+
CONFIG.oidc_scope_read_field()
376+
=> {
376377
Ok::<_, FeedbackFusionError>(
377378
database_request!(
378379
Prompt::select_by_id(self.connection(), request.get_ref().prompt.as_str())
@@ -394,9 +395,9 @@ impl FeedbackFusionV1 for FeedbackFusionV1Context {
394395
field::update_field,
395396
self,
396397
request,
397-
"feedback-fusion:write",
398-
"feedback-fusion:putField",
399-
{
398+
CONFIG.oidc_scope_write(),
399+
CONFIG.oidc_scope_write_field()
400+
=> {
400401
let prompt: Option<Prompt> = database_request!(
401402
self.connection()
402403
.query_decode(
@@ -420,9 +421,9 @@ impl FeedbackFusionV1 for FeedbackFusionV1Context {
420421
field::delete_field,
421422
self,
422423
request,
423-
"feedback-fusion:write",
424-
"feedback-fusion:deleteField",
425-
{
424+
CONFIG.oidc_scope_write(),
425+
CONFIG.oidc_scope_write_field()
426+
=> {
426427
let prompt: Option<Prompt> = database_request!(
427428
self.connection()
428429
.query_decode(
@@ -446,9 +447,9 @@ impl FeedbackFusionV1 for FeedbackFusionV1Context {
446447
response::get_responses,
447448
self,
448449
request,
449-
"feedback-fusion:read",
450-
"feedback-fusion:listResponses",
451-
{
450+
CONFIG.oidc_scope_read(),
451+
CONFIG.oidc_scope_read_response()
452+
=> {
452453
Ok::<_, FeedbackFusionError>(
453454
database_request!(
454455
Prompt::select_by_id(self.connection(), request.get_ref().prompt.as_str())

0 commit comments

Comments
 (0)