8 releases

0.4.6 Jul 25, 2024
0.4.5 Jun 17, 2024
0.4.4 Mar 21, 2024
0.4.2 Feb 9, 2024
0.1.0 Dec 16, 2020

#114 in Authentication

Download history 13603/week @ 2024-07-05 16325/week @ 2024-07-12 16194/week @ 2024-07-19 17811/week @ 2024-07-26 15974/week @ 2024-08-02 17184/week @ 2024-08-09 15922/week @ 2024-08-16 15019/week @ 2024-08-23 16060/week @ 2024-08-30 20196/week @ 2024-09-06 17291/week @ 2024-09-13 18375/week @ 2024-09-20 21311/week @ 2024-09-27 20770/week @ 2024-10-04 23694/week @ 2024-10-11 23700/week @ 2024-10-18

92,989 downloads per month
Used in 58 crates (13 directly)

MIT/Apache

33KB
653 lines

cargo-credential

This package is a library to assist writing a Cargo credential helper, which provides an interface to store tokens for authorizing access to a registry such as https://crates.io/.

Documentation about credential processes may be found at https://doc.rust-lang.org/nightly/cargo/reference/credential-provider-protocol.html

Example implementations may be found at https://github.com/rust-lang/cargo/tree/master/credential

Usage

Create a Cargo project with this as a dependency:

# Add this to your Cargo.toml:

[dependencies]
cargo-credential = "0.4"

And then include a main.rs binary which implements the Credential trait, and calls the main function which will call the appropriate method of the trait:

// src/main.rs

use cargo_credential::{Credential, Error};

struct MyCredential;

impl Credential for MyCredential {
    /// implement trait methods here...
}

fn main() {
    cargo_credential::main(MyCredential);
}

lib.rs:

Helper library for writing Cargo credential providers.

A credential process should have a struct that implements the Credential trait. The main function should be called with an instance of that struct, such as:

fn main() {
    cargo_credential::main(MyCredential);
}

While in the perform function, stdin and stdout will be re-attached to the active console. This allows credential providers to be interactive if necessary.

Error handling

Error::UrlNotSupported

A credential provider may only support some registry URLs. If this is the case and an unsupported index URL is passed to the provider, it should respond with Error::UrlNotSupported. Other credential providers may be attempted by Cargo.

Error::NotFound

When attempting an Action::Get or Action::Logout, if a credential can not be found, the provider should respond with Error::NotFound. Other credential providers may be attempted by Cargo.

Error::OperationNotSupported

A credential provider might not support all operations. For example if the provider only supports Action::Get, Error::OperationNotSupported should be returned for all other requests.

Error::Other

All other errors go here. The error will be shown to the user in Cargo, including the full error chain using std::error::Error::source.

Example

Dependencies

~1–10MB
~107K SLoC