#sso #oauth2 #authentication #qonfucius #token-response

qonfucius-sso-utility

Qonfucius SSO 对资源服务器的 Rust 实现

4 个版本

0.1.0-alpha.42021年4月23日
0.1.0-alpha.3 2021年4月21日
0.1.0-alpha.22020年11月26日

#12 in #sso

MIT 许可证

19KB
213

Qonfucius SSO Utility

pipeline status MIT licensed Crates.io docs

qonfucius-sso-utility 是一个简单的工具,用于实现 Qonfucius 的 SSO 并在您的后端资源网页应用程序中处理令牌和作用域验证。

功能

要能够将 UtilityError 作为 actix-web 响应错误使用,您需要启用以下 actix 功能

qonfucius-sso-utility = { version = "0.1.0-alpha", features = ["actix"] }

请注意,当前实现会引发此问题

  • ParsingError 将导致返回 StatusCode::INTERNAL_SERVER_ERROR (500)
  • TokenError 将导致返回 StatusCode::UNAUTHORIZED (401)
  • ScopeError 将导致返回 StatusCode::FORBIDDEN (403)

虽然我可以为您工作,但我们强烈建议您在自己的侧进行自定义实现。

环境变量

名称 描述
SSO_URI SSO 实例的 URL
SSO_SELF_DOMAIN 您的应用程序域名,用于识别您的范围

配置

安装后,您需要定义一个 范围资源 类型,通常是一个实现以下特征的 enum

  • Eq
  • Hash
  • Clone
  • Debug
  • FromStr

示例

这是我们在自己的 API 中使用的定义。

use qonfucius_sso_utility::UtilityError;

#[derive(Clone, Debug, PartialEq, Hash, Eq)]
pub enum ScopedResource {
    Content,
    ContentSchema,
    Media,
}

impl FromStr for ScopedResource {
    type Err = UtilityError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "content" => Ok(Self::Content),
            "content_schema" => Ok(Self::ContentSchema),
            "media" => Ok(Self::Media),
            _ => Err(UtilityError::ParsingError(String::from(
                r#"Scoped Resource part must be 'content', 'content_schema' or 'media"#,
            ))),
        }
    }
}

一旦您声明了该类型,您就可以将其用作 Scope<T>T 值。

范围

当您验证令牌时,scope 将看起来像这样

{
  "scopes": {
    "my_domain": ["content::read", "notifications::write", "medias::write"],
    "not_my_domain": ["some_resource::read", "stuff::write"]
  }
}

环境变量 SELF_SSO_DOMAIN 将定义要使用哪个子集,并将解析作用域。每个作用域看起来像这样:$RESOURCE::$ACL。当 $ACLwriteread 时,$RESOURCE 是由您定义的,就像我们之前展示的那样。

步骤

要验证令牌并访问授权的作用域,步骤如下:

async fn main() {
    let token = "TokenToCheck";
    // Calls the SSO to verify the token and retrieves the `Token` response
    let token_response = Token::verify_token(&token).await.unwrap();
    // Parse the scopes matching your domain (`SSO_SELF_DOMAIN`)
    let scope: Scope<ScopedResource> = token_response.parse_scope().unwrap();
    // you can now use the `Scope<T>` object to handle your authorizations
}

依赖项

~3–10MB
~216K SLoC