Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs PasetoParser::<V4, Local> #26

Open
rainyEra opened this issue May 19, 2023 · 5 comments
Open

Docs PasetoParser::<V4, Local> #26

rainyEra opened this issue May 19, 2023 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@rainyEra
Copy link

Hello, could you please add additional information to the docs that if token is expired, then PasetoParser::<V4, Local> will throw an error? That would be cool, because I was trying to find a way to verify token itself. It's a good design choice, but that was not mentioned in docs, or I'm simply blind (sorry 😢)

@rrrodzilla
Copy link
Owner

It's actually in the PASETO specification, but you're right. User experience could be made better by surfacing those types of things into the documentation. I'll get to it as soon as I can. Thanks!

@rrrodzilla rrrodzilla added the enhancement New feature or request label May 19, 2023
@rrrodzilla rrrodzilla self-assigned this May 19, 2023
@rrrodzilla
Copy link
Owner

Oh I guess it is in the docs. If you click on the error returned as described in the docs, you'll see the PasetoClaimError which describes all the different ways a claim can fail (including expiration).

@rainyEra
Copy link
Author

Well, now I see, but still it would be worth to mention that it will throw an error.

@rrrodzilla
Copy link
Owner

Where would have been a good place to mention it?

@rainyEra
Copy link
Author

rainyEra commented May 21, 2023

https://github.com/rrrodzilla/rusty_paseto#a-default-token
Somewhere here would be good to mention. NOTE: If token is expired, then PasetoParser will throw an error: "Error: token is expired".

But I would add a separate example, if you don't mind: Verifying token

use rusty_paseto::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
struct MyData {
    name: String,
    role: String,
}

fn main() {
    // Create a key specifying the PASETO version and purpose
    let key = PasetoSymmetricKey::<V4, Local>::from(Key::from(b"wubbalubbadubdubwubbalubbadubdub"));

    // Create a user
    let user = MyData {
        name: String::from("userLogin"),
        role: String::from("admin"),
    };

    // Serialize MyData into JSON
    let formatted_user_json = format!(
        "{}",
        serde_json::to_string(&user).expect("Failed to serialize MyData")
    );

    // Building our token
    let token = {
        let mut builder = PasetoBuilder::<V4, Local>::default();
        builder
            .set_claim(
                CustomClaim::try_from(("user", &formatted_user_json)).expect("Failed to set claim"),
            )
            .set_claim(
                ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")
                    .expect("Failed to set expiration claim"),
            )
            .build(&key)
            .expect("Failed to build token")
    };
    // Checking our token
    match PasetoParser::<V4, Local>::default().parse(&token, &key) {
        Ok(json_value) => {
            // whole token
            println!("JSON: {:?}", json_value);

            // token payload
            println!("Token payload: {}", json_value["user"]);

            //printing token
            println!("PASETO token: {}", token);
        }
        Err(err) => {
            // Handle the error
            eprintln!("Error: {}", err);
        }
    };
}


@rrrodzilla rrrodzilla reopened this May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants