pub struct AzureAuth { /* private fields */ }
Expand description

AzureAuth is the what you’ll use to validate your token.

Defaults

  • Public key expiration: dafault set to 24h, use set_expiration to set a different expiration in hours.
  • Hashing algorithm: Sha256, you can’t change this setting. Submit an issue in the github repo if this is important to you
  • Retry on no match. If no matching key is found and our keys are older than an hour, we refresh the keys and try once more. Limited to once in an hour. You can disable this by calling set_no_retry().
  • The timestamps are given a 60s “leeway” to account for time skew between servers

Errors

  • If one of Microsofts enpoints for public keys are down
  • If the token can’t be parsed as a valid Azure token
  • If the tokens fails it’s authenticity test
  • If the token is invalid

Implementations§

source§

impl AzureAuth

source

pub async fn new(aud: impl Into<String>) -> Result<Self, AuthErr>

Creates a new dafault instance. This method will call the Microsoft apis to fetch the current keys which can fail. The public keys are fetched since we need them to perform verification. Please note that fetching the OpenID manifest and public keys are quite slow since we call an external API in a blocking manner. Try keeping a single instance alive instead of creating new ones for every validation. If you need to pass around an instance of the object, creating a pool of instances at startup or wrapping a single instance in a Mutex is better than creating many new instances.

Errors

If there is a connection issue to the Microsoft APIs.

source

pub fn new_offline( aud: impl Into<String>, public_keys: Vec<Jwk> ) -> Result<Self, AuthErr>

Does not call the Microsoft openid configuration endpoint or fetches the JWK set. Use this if you want to handle updating the public keys yourself

source

pub async fn validate_token( &mut self, token: &str ) -> Result<TokenData<AzureJwtClaims>, AuthErr>

Dafault validation, see AzureAuth documentation for the defaults.

source

pub async fn validate_custom<T>( &mut self, token: &str, validator: &Validation ) -> Result<TokenData<T>, AuthErr>
where for<'de> T: Serialize + Deserialize<'de>,

Allows for a custom validator and mapping the token to your own type. Useful in situations where you get fields you that are not covered by the default mapping or want to change the validaion requirements (i.e if you want the leeway set to two minutes instead of one).

Note

You’ll need to pull in jsonwebtoken to use Validation from that crate.

Example
use azure_oauth_r1s::*;
use jsonwebtoken::{Validation, Token};
use serde::{Seralize, Deserialize};

let mut validator = Validation::new();
validator.leeway = 120;

#[derive(Serialize, Deserialize)]
struct MyClaims {
    group: String,
    roles: Vec<String>,
}

let auth = AzureAuth::new(my_client_id_from_azure).unwrap();

let valid_token: Token<MyClaims>  = auth.validate_custom(some_token, &validator).unwrap();
source

pub fn set_expiration(&mut self, hours: i64)

Sets the expiration of the cached public keys in hours. Pr. 04.2019 Microsoft rotates these every 24h.

source

pub fn set_no_retry(&mut self)

source

pub async fn refresh_rwks_uri(&mut self) -> Result<(), AuthErr>

Refreshes the jwks_uri by re-fetching it from the the OpenID metadata document. See: https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderMetadata Usually, this is not needed but for some cases you might want to try to fetch a new uri on receiving an error.

source

pub fn set_public_keys(&mut self, pub_keys: Vec<Jwk>)

If you use the “offline” variant you’ll need this to update the public keys, if you don’t use the offline version you probably don’t want to change these unless you’re testing.

Trait Implementations§

source§

impl Clone for AzureAuth

source§

fn clone(&self) -> AzureAuth

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for AzureAuth

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more