sa-token-core 0.2.0

Core library for sa-token-rust, a powerful authentication and authorization framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
// Author: 金书记 | Author: Jin Shuji
//
//! Authorization Service | 授权服务
//!
//! 权限与角色的**唯一**入口:读、写、校验、封禁回落全部经过本服务。
//! 这里也是**唯一**做「自定义数据源 vs 存储」优先级判断的地方。
//!
//! The single entry point for permissions and roles: reads, writes, checks and
//! the ban fallback all funnel through here. It is also the **only** place that
//! decides between a custom data source and storage.

use std::sync::Arc;

use crate::config::{GrantWritePolicy, SaTokenConfig};
use crate::context::SaTokenContext;
use crate::error::{SaTokenError, SaTokenResult};
use crate::event::{SaTokenEvent, SaTokenEventBus};
use crate::permission::{AntPermissionMatcher, ExactMatcher, PermissionMatcher};
use crate::repository::GrantRepo;
use crate::service::grant_cache::{GrantCache, GrantKind};
use crate::stp_interface::{StorageStpInterface, StpInterface};

/// 授权领域服务 | Authorization domain service
pub struct AuthzService {
    grant_repo: Arc<GrantRepo>,
    storage_iface: Arc<StorageStpInterface>,
    custom_iface: Option<Arc<dyn StpInterface>>,
    perm_matcher: Arc<dyn PermissionMatcher>,
    role_matcher: Arc<dyn PermissionMatcher>,
    cache: Option<Arc<GrantCache>>,
    write_policy: GrantWritePolicy,
    request_scope: bool,
    event_bus: SaTokenEventBus,
}

impl std::fmt::Debug for AuthzService {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AuthzService")
            .field("has_custom_iface", &self.custom_iface.is_some())
            .field("write_policy", &self.write_policy)
            .field("request_scope", &self.request_scope)
            .finish()
    }
}

impl AuthzService {
    /// 构造授权服务 | Construct the authorization service
    pub fn new(
        grant_repo: Arc<GrantRepo>,
        config: &SaTokenConfig,
        event_bus: SaTokenEventBus,
        custom_iface: Option<Arc<dyn StpInterface>>,
    ) -> Self {
        let storage_iface = Arc::new(StorageStpInterface::new(Arc::clone(&grant_repo)));

        let role_matcher: Arc<dyn PermissionMatcher> = if config.role_wildcard {
            Arc::new(AntPermissionMatcher)
        } else {
            Arc::new(ExactMatcher)
        };

        Self {
            grant_repo,
            storage_iface,
            custom_iface,
            perm_matcher: Arc::new(AntPermissionMatcher),
            role_matcher,
            cache: GrantCache::from_config(config),
            write_policy: config.grant_write_policy,
            request_scope: config.grant_request_scope,
            event_bus,
        }
    }

    /// 替换权限匹配策略 | Replace the permission matching strategy
    pub fn with_permission_matcher(mut self, matcher: Arc<dyn PermissionMatcher>) -> Self {
        self.perm_matcher = matcher;
        self
    }

    /// 替换角色匹配策略 | Replace the role matching strategy
    pub fn with_role_matcher(mut self, matcher: Arc<dyn PermissionMatcher>) -> Self {
        self.role_matcher = matcher;
        self
    }

    /// 当前权限匹配策略 | Current permission matcher
    pub fn permission_matcher(&self) -> &Arc<dyn PermissionMatcher> {
        &self.perm_matcher
    }

    /// 当前角色匹配策略 | Current role matcher
    pub fn role_matcher(&self) -> &Arc<dyn PermissionMatcher> {
        &self.role_matcher
    }

    /// 是否已注入自定义数据源 | Whether a custom data source is injected
    pub fn has_custom_interface(&self) -> bool {
        self.custom_iface.is_some()
    }

    // ==================== 私有基础设施 | Private infrastructure ====================

    /// 生效的数据源:自定义优先,否则用存储外观。
    /// The effective data source, custom first.
    #[inline]
    fn iface(&self) -> &dyn StpInterface {
        match &self.custom_iface {
            Some(custom) => custom.as_ref(),
            None => self.storage_iface.as_ref() as &dyn StpInterface,
        }
    }

    /// 直接向数据源取数(不经任何缓存)| Fetch straight from the data source
    async fn fetch(
        &self,
        kind: GrantKind,
        login_type: &str,
        login_id: &str,
    ) -> SaTokenResult<Vec<String>> {
        match kind {
            GrantKind::Permission => self.iface().get_permission_list(login_id, login_type).await,
            GrantKind::Role => self.iface().get_role_list(login_id, login_type).await,
        }
    }

    /// 三级读取的统一实现:请求级快照 → 进程级缓存 → 数据源。
    /// Unified three-tier read: request snapshot, process cache, data source.
    async fn load(
        &self,
        kind: GrantKind,
        login_type: &str,
        login_id: &str,
    ) -> SaTokenResult<Arc<[String]>> {
        let key = GrantCache::cache_key(kind, login_type, login_id);

        let scope = if self.request_scope {
            SaTokenContext::current_grant_scope()
        } else {
            None
        };
        if let Some(scope) = scope.as_ref() {
            if let Some(hit) = scope.get(&key) {
                return Ok(hit);
            }
        }

        let value: Arc<[String]> = match self.cache.as_ref() {
            Some(cache) => {
                cache
                    .get_or_load(key.clone(), || self.fetch(kind, login_type, login_id))
                    .await?
            }
            None => Arc::from(self.fetch(kind, login_type, login_id).await?),
        };

        if let Some(scope) = scope {
            scope.put(key, Arc::clone(&value));
        }

        Ok(value)
    }

    /// 写前校验 | Pre-write guard
    fn ensure_writable(&self, operation: &str) -> SaTokenResult<()> {
        let Some(custom) = self.custom_iface.as_ref() else {
            return Ok(());
        };
        if custom.is_writable() {
            return Ok(());
        }

        match self.write_policy {
            GrantWritePolicy::Allow => Ok(()),
            GrantWritePolicy::Warn => {
                tracing::warn!(
                    operation,
                    "grant write landed in storage, but the injected StpInterface is read-only \
                     (is_writable() == false), so reads will not observe it; implement \
                     is_writable() or set grant_write_policy = Reject"
                );
                Ok(())
            }
            GrantWritePolicy::Reject => Err(SaTokenError::ConfigError(format!(
                "{operation} rejected: the injected StpInterface is read-only \
                 (is_writable() == false) and grant_write_policy is Reject"
            ))),
        }
    }

    /// 写后处理:失效两级缓存 + 发布变更事件。
    /// Post-write hook: invalidate both cache tiers, then publish the event.
    async fn after_write(&self, login_type: &str, login_id: &str) {
        if let Some(cache) = self.cache.as_ref() {
            cache.invalidate_account(login_type, login_id);
        }

        if self.request_scope {
            if let Some(scope) = SaTokenContext::current_grant_scope() {
                scope.remove(&GrantCache::cache_key(
                    GrantKind::Permission,
                    login_type,
                    login_id,
                ));
                scope.remove(&GrantCache::cache_key(
                    GrantKind::Role,
                    login_type,
                    login_id,
                ));
            }
        }

        self.event_bus
            .publish(SaTokenEvent::grant_changed(login_id, login_type))
            .await;
    }

    // ==================== 读 | Reads ====================

    /// 读取权限列表(零拷贝)| Read the permission list without copying
    pub async fn get_permissions_arc(
        &self,
        login_type: &str,
        login_id: &str,
    ) -> SaTokenResult<Arc<[String]>> {
        self.load(GrantKind::Permission, login_type, login_id).await
    }

    /// 读取权限列表(`Vec` 形式)| Read the permission list as a `Vec`
    pub async fn get_permissions(
        &self,
        login_type: &str,
        login_id: &str,
    ) -> SaTokenResult<Vec<String>> {
        Ok(self
            .get_permissions_arc(login_type, login_id)
            .await?
            .to_vec())
    }

    /// 读取角色列表(零拷贝)| Read the role list without copying
    pub async fn get_roles_arc(
        &self,
        login_type: &str,
        login_id: &str,
    ) -> SaTokenResult<Arc<[String]>> {
        self.load(GrantKind::Role, login_type, login_id).await
    }

    /// 读取角色列表(`Vec` 形式)| Read the role list as a `Vec`
    pub async fn get_roles(&self, login_type: &str, login_id: &str) -> SaTokenResult<Vec<String>> {
        Ok(self.get_roles_arc(login_type, login_id).await?.to_vec())
    }

    // ==================== 写 | Writes ====================

    /// 覆盖权限列表 | Overwrite the permission list
    pub async fn set_permissions(
        &self,
        login_type: &str,
        login_id: &str,
        permissions: &[String],
    ) -> SaTokenResult<()> {
        self.ensure_writable("set_permissions")?;
        self.grant_repo
            .set_permissions(login_type, login_id, permissions)
            .await?;
        self.after_write(login_type, login_id).await;
        Ok(())
    }

    /// 追加单个权限 | Append one permission
    pub async fn add_permission(
        &self,
        login_type: &str,
        login_id: &str,
        permission: String,
    ) -> SaTokenResult<()> {
        self.ensure_writable("add_permission")?;
        self.grant_repo
            .add_permission(login_type, login_id, permission)
            .await?;
        self.after_write(login_type, login_id).await;
        Ok(())
    }

    /// 移除单个权限 | Remove one permission
    pub async fn remove_permission(
        &self,
        login_type: &str,
        login_id: &str,
        permission: &str,
    ) -> SaTokenResult<()> {
        self.ensure_writable("remove_permission")?;
        self.grant_repo
            .remove_permission(login_type, login_id, permission)
            .await?;
        self.after_write(login_type, login_id).await;
        Ok(())
    }

    /// 清空权限(删键)| Clear permissions by deleting the key
    pub async fn clear_permissions(&self, login_type: &str, login_id: &str) -> SaTokenResult<()> {
        self.ensure_writable("clear_permissions")?;
        self.grant_repo
            .clear_permissions(login_type, login_id)
            .await?;
        self.after_write(login_type, login_id).await;
        Ok(())
    }

    /// 覆盖角色列表 | Overwrite the role list
    pub async fn set_roles(
        &self,
        login_type: &str,
        login_id: &str,
        roles: &[String],
    ) -> SaTokenResult<()> {
        self.ensure_writable("set_roles")?;
        self.grant_repo
            .set_roles(login_type, login_id, roles)
            .await?;
        self.after_write(login_type, login_id).await;
        Ok(())
    }

    /// 追加单个角色 | Append one role
    pub async fn add_role(
        &self,
        login_type: &str,
        login_id: &str,
        role: String,
    ) -> SaTokenResult<()> {
        self.ensure_writable("add_role")?;
        self.grant_repo.add_role(login_type, login_id, role).await?;
        self.after_write(login_type, login_id).await;
        Ok(())
    }

    /// 移除单个角色 | Remove one role
    pub async fn remove_role(
        &self,
        login_type: &str,
        login_id: &str,
        role: &str,
    ) -> SaTokenResult<()> {
        self.ensure_writable("remove_role")?;
        self.grant_repo
            .remove_role(login_type, login_id, role)
            .await?;
        self.after_write(login_type, login_id).await;
        Ok(())
    }

    /// 清空角色(删键)| Clear roles by deleting the key
    pub async fn clear_roles(&self, login_type: &str, login_id: &str) -> SaTokenResult<()> {
        self.ensure_writable("clear_roles")?;
        self.grant_repo.clear_roles(login_type, login_id).await?;
        self.after_write(login_type, login_id).await;
        Ok(())
    }

    // ==================== 权限校验 | Permission checks ====================

    /// 是否拥有指定权限 | Whether the account holds a permission
    pub async fn has_permission(
        &self,
        login_type: &str,
        login_id: &str,
        permission: &str,
    ) -> SaTokenResult<bool> {
        let owned = self.get_permissions_arc(login_type, login_id).await?;
        Ok(self.perm_matcher.matches(&owned, permission))
    }

    /// AND:全部权限都必须命中 | AND semantics
    pub async fn has_all_permissions(
        &self,
        login_type: &str,
        login_id: &str,
        permissions: &[&str],
    ) -> SaTokenResult<bool> {
        let owned = self.get_permissions_arc(login_type, login_id).await?;
        Ok(self.perm_matcher.matches_all(&owned, permissions))
    }

    /// OR:任一权限命中即可 | OR semantics
    pub async fn has_any_permission(
        &self,
        login_type: &str,
        login_id: &str,
        permissions: &[&str],
    ) -> SaTokenResult<bool> {
        let owned = self.get_permissions_arc(login_type, login_id).await?;
        Ok(self.perm_matcher.matches_any(&owned, permissions))
    }

    /// 校验权限,失败返回 `PermissionDeniedDetail`。
    /// Checks a permission, returning `PermissionDeniedDetail` on failure.
    pub async fn check_permission(
        &self,
        login_type: &str,
        login_id: &str,
        permission: &str,
    ) -> SaTokenResult<()> {
        if self
            .has_permission(login_type, login_id, permission)
            .await?
        {
            return Ok(());
        }
        tracing::debug!(login_type, login_id, permission, "permission check denied");
        Err(SaTokenError::PermissionDeniedDetail(permission.to_string()))
    }

    /// 校验多个权限(AND)| Check multiple permissions with AND semantics
    pub async fn check_all_permissions(
        &self,
        login_type: &str,
        login_id: &str,
        permissions: &[&str],
    ) -> SaTokenResult<()> {
        let owned = self.get_permissions_arc(login_type, login_id).await?;
        for required in permissions {
            if !self.perm_matcher.matches(&owned, required) {
                tracing::debug!(
                    login_type,
                    login_id,
                    permission = required,
                    "permission check denied (AND)"
                );
                return Err(SaTokenError::PermissionDeniedDetail(
                    (*required).to_string(),
                ));
            }
        }
        Ok(())
    }

    /// 校验多个权限(OR)| Check multiple permissions with OR semantics
    pub async fn check_any_permission(
        &self,
        login_type: &str,
        login_id: &str,
        permissions: &[&str],
    ) -> SaTokenResult<()> {
        if self
            .has_any_permission(login_type, login_id, permissions)
            .await?
        {
            return Ok(());
        }
        tracing::debug!(
            login_type,
            login_id,
            required = ?permissions,
            "permission check denied (OR)"
        );
        Err(SaTokenError::PermissionDeniedDetail(permissions.join(",")))
    }

    // ==================== 角色校验 | Role checks ====================

    /// 是否拥有指定角色 | Whether the account holds a role
    pub async fn has_role(
        &self,
        login_type: &str,
        login_id: &str,
        role: &str,
    ) -> SaTokenResult<bool> {
        let owned = self.get_roles_arc(login_type, login_id).await?;
        Ok(self.role_matcher.matches(&owned, role))
    }

    /// AND:全部角色都必须命中 | AND semantics
    pub async fn has_all_roles(
        &self,
        login_type: &str,
        login_id: &str,
        roles: &[&str],
    ) -> SaTokenResult<bool> {
        let owned = self.get_roles_arc(login_type, login_id).await?;
        Ok(self.role_matcher.matches_all(&owned, roles))
    }

    /// OR:任一角色命中即可 | OR semantics
    pub async fn has_any_role(
        &self,
        login_type: &str,
        login_id: &str,
        roles: &[&str],
    ) -> SaTokenResult<bool> {
        let owned = self.get_roles_arc(login_type, login_id).await?;
        Ok(self.role_matcher.matches_any(&owned, roles))
    }

    /// 校验角色 | Check a role
    pub async fn check_role(
        &self,
        login_type: &str,
        login_id: &str,
        role: &str,
    ) -> SaTokenResult<()> {
        if self.has_role(login_type, login_id, role).await? {
            return Ok(());
        }
        tracing::debug!(login_type, login_id, role, "role check denied");
        Err(SaTokenError::RoleDenied(role.to_string()))
    }

    /// 校验多个角色(AND)| Check multiple roles with AND semantics
    pub async fn check_all_roles(
        &self,
        login_type: &str,
        login_id: &str,
        roles: &[&str],
    ) -> SaTokenResult<()> {
        let owned = self.get_roles_arc(login_type, login_id).await?;
        for required in roles {
            if !self.role_matcher.matches(&owned, required) {
                tracing::debug!(
                    login_type,
                    login_id,
                    role = required,
                    "role check denied (AND)"
                );
                return Err(SaTokenError::RoleDenied((*required).to_string()));
            }
        }
        Ok(())
    }

    /// 校验多个角色(OR)| Check multiple roles with OR semantics
    pub async fn check_any_role(
        &self,
        login_type: &str,
        login_id: &str,
        roles: &[&str],
    ) -> SaTokenResult<()> {
        if self.has_any_role(login_type, login_id, roles).await? {
            return Ok(());
        }
        tracing::debug!(login_type, login_id, required = ?roles, "role check denied (OR)");
        Err(SaTokenError::RoleDenied(roles.join(",")))
    }

    // ==================== 封禁回落 | Ban fallback ====================

    /// 向数据源查询封禁等级;`None` 表示未封禁。
    ///
    /// 只在**存储中查不到**封禁记录时被 `disable.rs` 调用。
    ///
    /// Queries the data source for a ban level; `None` means not banned.
    pub async fn is_disabled(&self, login_id: &str, service: &str) -> SaTokenResult<Option<i32>> {
        match self.custom_iface.as_ref() {
            Some(custom) => custom.is_disabled(login_id, service).await,
            None => Ok(None),
        }
    }

    // ==================== 缓存运维 | Cache maintenance ====================

    /// 失效某账号的权限与角色缓存。
    ///
    /// Invalidates an account's cached permissions and roles.
    pub fn invalidate_account(&self, login_type: &str, login_id: &str) {
        if let Some(cache) = self.cache.as_ref() {
            cache.invalidate_account(login_type, login_id);
        }
        if self.request_scope {
            if let Some(scope) = SaTokenContext::current_grant_scope() {
                scope.remove(&GrantCache::cache_key(
                    GrantKind::Permission,
                    login_type,
                    login_id,
                ));
                scope.remove(&GrantCache::cache_key(
                    GrantKind::Role,
                    login_type,
                    login_id,
                ));
            }
        }
    }

    /// 清空全部授权缓存 | Clear the entire grant cache
    pub fn invalidate_all(&self) {
        if let Some(cache) = self.cache.as_ref() {
            cache.clear();
        }
        if self.request_scope {
            if let Some(scope) = SaTokenContext::current_grant_scope() {
                scope.clear();
            }
        }
    }

    /// 当前缓存条目数(诊断用)| Cached entry count for diagnostics
    pub fn cache_len(&self) -> usize {
        self.cache.as_ref().map_or(0, |c| c.len())
    }
}