Skip to content

Commit

Permalink
Merge pull request #7 from naokiri/#6-implement-standard-error
Browse files Browse the repository at this point in the history
Issue#6 Implement standard error
  • Loading branch information
jfbilodeau authored Jan 3, 2021
2 parents 64946c8 + f84b811 commit 2c59f91
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
21 changes: 7 additions & 14 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
use jwks_client::error::Error;
use jwks_client::keyset::KeyStore;

#[tokio::main]
async fn main() {
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let jkws_url = "https://raw.githubusercontent.com/jfbilodeau/jwks-client/0.1.8/test/test-jwks.json";

let key_set = KeyStore::new_from(jkws_url).await.unwrap();

// ...

let token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjEifQ.eyJuYW1lIjoiQWRhIExvdmVsYWNlIiwiaXNzIjoiaHR0cHM6Ly9jaHJvbm9nZWFycy5jb20vdGVzdCIsImF1ZCI6InRlc3QiLCJhdXRoX3RpbWUiOjEwMCwidXNlcl9pZCI6InVpZDEyMyIsInN1YiI6InNidTEyMyIsImlhdCI6MjAwLCJleHAiOjUwMCwibmJmIjozMDAsImVtYWlsIjoiYWxvdmVsYWNlQGNocm9ub2dlYXJzLmNvbSJ9.eTQnwXrri_uY55fS4IygseBzzbosDM1hP153EZXzNlLH5s29kdlGt2mL_KIjYmQa8hmptt9RwKJHBtw6l4KFHvIcuif86Ix-iI2fCpqNnKyGZfgERV51NXk1THkgWj0GQB6X5cvOoFIdHa9XvgPl_rVmzXSUYDgkhd2t01FOjQeeT6OL2d9KdlQHJqAsvvKVc3wnaYYoSqv2z0IluvK93Tk1dUBU2yWXH34nX3GAVGvIoFoNRiiFfZwFlnz78G0b2fQV7B5g5F8XlNRdD1xmVZXU8X2-xh9LqRpnEakdhecciFHg0u6AyC4c00rlo_HBb69wlXajQ3R4y26Kpxn7HA";
let jwt = key_set.verify(token)?;
let issuer = jwt.payload().iss().unwrap();
let name = jwt.payload().get_str("name").unwrap();

match key_set.verify(token) {
Ok(jwt) => {
let issuer = jwt.payload().iss().unwrap();
let name = jwt.payload().get_str("name").unwrap();

println!("iss={}", issuer);
println!("name={}", name);
}
Err(Error { msg, typ: _ }) => {
eprintln!("Could not verify token. Reason: {}", msg);
}
}
println!("iss={}", issuer);
println!("name={}", name);
Ok(())
}
12 changes: 12 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
use std::fmt::{Display, Formatter};
use std::fmt;

#[derive(Debug, PartialEq)]
pub struct Error {
/// Debug message associated with error
pub msg: &'static str,
pub typ: Type,
}

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{:?}: {}", self.typ, self.msg)
}
}

impl std::error::Error for Error {
}

/// Type of error encountered
#[derive(Debug, PartialEq)]
pub enum Type {
Expand Down

0 comments on commit 2c59f91

Please sign in to comment.