10 releases

0.2.4 Dec 26, 2023
0.2.3 May 23, 2023
0.2.2 Dec 22, 2022
0.2.1 Nov 1, 2022
0.1.2 Jun 9, 2022

#177 in Authentication

Download history 25/week @ 2024-07-22 32/week @ 2024-07-29 30/week @ 2024-08-05 20/week @ 2024-08-12 20/week @ 2024-08-19 66/week @ 2024-08-26 24/week @ 2024-09-02 26/week @ 2024-09-09 42/week @ 2024-09-16 99/week @ 2024-09-23 23/week @ 2024-09-30 1/week @ 2024-10-07 18/week @ 2024-10-14 24/week @ 2024-10-21 16/week @ 2024-10-28 23/week @ 2024-11-04

81 downloads per month
Used in 8 crates (3 directly)

MIT license

97KB
2K SLoC

Qiniu-Credential

qiniu-credential docs.rs Run Test Cases GitHub release MIT licensed

概览

负责存储调用七牛 API 所必要的认证信息,提供 CredentialProvider 方便扩展获取认证信息的方式。 同时提供阻塞接口和异步接口(异步接口需要启用 async 功能)。 提供 CredentialProvider 的多个实现方式,例如:

  • GlobalCredentialProvider 使用全局变量配置的认证信息
  • EnvCredentialProvider 使用环境变量配置的认证信息
  • ChainCredentialsProvider 配置多个 CredentialProvider 形成认证信息串,遍历找寻第一个可用的认证信息

不启用异步接口

[dependencies]
qiniu-credential = "0.2.1"

启用异步接口

[dependencies]
qiniu-credential = { version = "0.2.1", features = ["async"] }

代码示例

阻塞代码示例

计算七牛鉴权签名 V1

use qiniu_credential::{Credential, HeaderValue, prelude::*};
use mime::APPLICATION_WWW_FORM_URLENCODED;
use std::io::Cursor;

let credential = Credential::new("abcdefghklmnopq", "1234567890");
let authorization = credential
    .get(Default::default())?
    .authorization_v1_for_request_with_body_reader(
        &"http://upload.qiniup.com/".parse()?,
        Some(&HeaderValue::from_str(APPLICATION_WWW_FORM_URLENCODED.as_ref())?),
        &mut Cursor::new(b"name=test&language=go"),
    );

计算七牛鉴权签名 V2

use qiniu_credential::{Credential, Method, HeaderMap, HeaderValue, prelude::*};
use http::header::CONTENT_TYPE;
use mime::APPLICATION_JSON;
use std::io::Cursor;

let credential = Credential::new("abcdefghklmnopq", "1234567890");
let mut headers = HeaderMap::new();
headers.insert(CONTENT_TYPE, HeaderValue::from_str(APPLICATION_JSON.as_ref())?);
let authorization = credential
    .get(Default::default())?
    .authorization_v2_for_request_with_body_reader(
        &Method::GET,
        &"http://upload.qiniup.com/".parse()?,
        &headers,
        &mut Cursor::new(b"{\"name\":\"test\"}"),
    );

计算下载地址签名

use qiniu_credential::{Credential, prelude::*};
use std::time::Duration;

let credential = Credential::new("abcdefghklmnopq", "1234567890");
let url = "http://www.qiniu.com/?go=1".parse()?;
let url = credential
    .get(Default::default())?
    .sign_download_url(url, Duration::from_secs(3600));
println!("{}", url);

异步代码示例

计算七牛鉴权签名 V1

use qiniu_credential::{Credential, HeaderValue, prelude::*};
use mime::APPLICATION_WWW_FORM_URLENCODED;
use std::io::Cursor;

let credential = Credential::new("abcdefghklmnopq", "1234567890");
let authorization = credential
    .async_get(Default::default()).await?
    .authorization_v1_for_request_with_async_body_reader(
        &"http://upload.qiniup.com/".parse()?,
        Some(&HeaderValue::from_str(APPLICATION_WWW_FORM_URLENCODED.as_ref())?),
        &mut Cursor::new(b"name=test&language=go"),
    ).await?;

计算七牛鉴权签名 V2

use qiniu_credential::{Credential, Method, HeaderMap, HeaderValue, prelude::*};
use http::header::CONTENT_TYPE;
use mime::APPLICATION_JSON;
use std::io::Cursor;

let credential = Credential::new("abcdefghklmnopq", "1234567890");
let mut headers = HeaderMap::new();
headers.insert(CONTENT_TYPE, HeaderValue::from_str(APPLICATION_JSON.as_ref())?);
let authorization = credential
    .async_get(Default::default()).await?
    .authorization_v2_for_request_with_async_body_reader(
        &Method::GET,
        &"http://upload.qiniup.com/".parse()?,
        &headers,
        &mut Cursor::new(b"{\"name\":\"test\"}"),
    ).await?;

最低支持的 Rust 版本(MSRV)

1.70.0

联系我们

  • 如果需要帮助,请提交工单(在portal右侧点击咨询和建议提交工单,或者直接向 support@qiniu.com 发送邮件)
  • 如果有什么问题,可以到问答社区提问,问答社区
  • 更详细的文档,见官方文档站
  • 如果发现了bug, 欢迎提交 Issue
  • 如果有功能需求,欢迎提交 Issue
  • 如果要提交代码,欢迎提交 Pull Request
  • 欢迎关注我们的微信 微博,及时获取动态信息。

代码许可

This project is licensed under the MIT license.

Dependencies

~3.5MB
~71K SLoC