oauth2_utils

Crates.iooauth2_utils
lib.rsoauth2_utils
version2.0.0
sourcesrc
created_at2023-12-18 00:38:46.220369
updated_at2024-04-09 13:46:06.305124
descriptioncrate that provides utility functions for working with OAuth2
homepage
repositoryhttps://github.com/ashgw/oauth2_utils
max_upload_size
id1072853
size52,689
Ashref Gwader (AshGw)

documentation

README

Overview

This crate provides utility functions for working with OAuth2:

  • PKCE
  • URL-safe tokens
  • URL-safe base64 encoding/decoding

Installation

cargo add oauth2_utils

Usage

To generate a PKCE pair with the corresponding method (SHA256 by default):

use oauth2_utils::pkce::PKCE;

let pkce = PKCE::new();

println!("PKCE Code Challenge: {}", pkce.code_challenge);
println!("PKCE Code Verifier: {}", pkce.code_verifier);
println!("PKCE Code method: {}", pkce.method);

To generate a code verifier with a custom length:

use oauth2_utils::errors::CodeVerifierError;

use oauth2_utils::pkce::gen::{gen_code_challenge, gen_code_verifier};

  
pub fn main() -> Result<(), CodeVerifierError> {
    let code_verifier = gen_code_verifier(Some(128))?;
    eprintln!("Code Verifier: {}", code_verifier);
    let code_challenge = gen_code_challenge(&code_verifier);
    eprintln!("Code Challenge: {}", code_challenge);
    Ok(())
}

To generate a URL-safe token for Nonce, State, etc..

use oauth2_utils::urlsafe::urlsafe_token;

println!("URL-safe Token: {}", urlsafe_token(32))

For base64 encoding/decoding operations:


use oauth2_utils::errors::B64Error;
use oauth2_utils::urlsafe::b64::{urlsafe_b64decode, urlsafe_b64encode};

pub fn main() -> Result<(), B64Error> {
    let val = String::from("some value");
    let encoded = urlsafe_b64encode(val);
    println!("{}", encoded);
    let decoded = urlsafe_b64decode(encoded)?;
    println!("{}", decoded);
    Ok(())
}

License

GPL

Commit count: 79

cargo fmt