#keycloak #graphql #rest #login #openid

rs-keycloak

使用Keycloak对您的项目进行身份验证

12个不稳定版本 (3个重大变更)

0.4.4 2023年9月6日
0.4.3 2023年9月6日
0.3.1 2023年8月4日
0.2.2 2023年8月4日
0.1.1 2023年8月2日

#164身份验证

Download history 17/week @ 2024-03-15 11/week @ 2024-03-22 32/week @ 2024-03-29 11/week @ 2024-04-05 6/week @ 2024-05-10 2/week @ 2024-05-17

每月161次下载
用于 async-graphql-utils

MIT/Apache

15KB
302

RS-KEYCLOAK

使用Rust为Keycloak提供身份验证层。

安装

使用以下命令将rs-keycloak添加到项目中:cargo add rs-keycloak

使用客户端进行登录

use client::OpenID;

fn main() {
    let server_url = "https://seuhost.com.br";
    let client_id = "client";
    let realm_name = "realm";
    let client_secret = "client_secret";

    let open_id = OpenID::login_with_client(
        server_url,
        realm_name,
        client_id,
        client_secret,
    ).unwrap();

    println!("{}", open_id.get_access_token()); // veja outros métodos disponíveis na struct OpenID
}

使用用户名和密码进行登录

use client::OpenID;

fn main() {
    let server_url = "https://seuhost.com.br";
    let client_id = "client";
    let realm_name = "realm";
    let client_secret = "client_secret";
    let username = "meuusuario";
    let password = "minhasenhasecreta";

    let open_id = OpenID::login_with_password(
        server_url,
        realm_name,
        client_id,
        client_secret,
        username,
        password,
    ).unwrap();

    println!("{}", open_id.get_access_token()); // veja outros métodos disponíveis na struct OpenID
}

使用refresh_token进行登出

use client::OpenID;

fn main() {
    let server_url = "https://seuhost.com.br";
    let client_id = "client";
    let realm_name = "realm";
    let client_secret = "client_secret";
    let refresh_token = "refresh token";

    let open_id = OpenID::logout(
        server_url,
        realm_name,
        client_id,
        client_secret
        refresh_token,
    );

    if open_id.is_ok() {
        println!("Logout bem sucedido");
    } else {
        println!("Erro ao deslogar");
    }
}

对令牌进行introspect操作并验证角色

use client::OpenID;

fn main() {
    let server_url = "https://seuhost.com.br";
    let client_id = "client";
    let realm_name = "realm";
    let client_secret = "client_secret";
    let access_token = "eyJhbGciO.....H38Xw";

    let open_id = OpenID::introspect(
        server_url,
        realm_name,
        client_id,
        client_secret,
        access_token,
    ).unwrap();
    
    // Obtendo todas as roles
    let roles = open_id.get_roles();
    
    // printando todas as roles
    println!("{:?}", roles);
    
    // validando se o token possui
    // Será verdadeiro se existir ALGUMA das roles informadas
    let uma_ou_outra = open_id.has_any_roles(&["cobrancas.boleto", "user"]);

    println!("Alguma persmissão existe? {:?}", uma_ou_outra);
    
    // validando se o token possui
    // Será verdadeiro se existir ALGUMA das roles informadas
    let todas =    open_id.has_all_roles(&["cobrancas.boleto", "uma_authorization"]);

    println!("Todas as permissões existem? {:?}", todas);
}

依赖关系

~1–11MB
~124K SLoC