Struct TokenManager

Source
pub struct TokenManager { /* private fields */ }

Implementations§

Source§

impl TokenManager

Source

pub fn new(api_key: &str, endpoint: &str) -> Self

Examples found in repository?
examples/pdp_auth.rs (line 15)
6pub fn main() -> Result<(), Box<dyn std::error::Error>> {
7    // production IAM endpoint
8    let endpoint = "https://iam.cloud.ibm.com";
9
10    // get a user token from IAM
11    // normally this would be something your web service or server would receive from a user
12    // but we grab one here for the sake of completeness
13    let api_key =
14        std::env::var("IBMCLOUD_API_KEY").expect("Could not read IBMCLOUD_API_KEY from env");
15    let tm = TokenManager::new(&api_key, &endpoint);
16    let user_token = tm.token()?;
17
18    // validate user token, this checks the signature and returns claims
19    let _claims = validate_token(&user_token, &endpoint);
20
21    // Service ID API key, this is unique to your web service/server
22    let service_id_key =
23        std::env::var("SERVICE_ID_KEY").expect("Could not parse SERVICE_ID_KEY from env");
24
25    // build PDP client object for interacting with the IAM PDP endpoint
26    let pdp = pdpapi::PDPClient::new(&service_id_key, &endpoint);
27
28    // get Subject type from user token
29    let sub = pdpapi::subject_from_token(&user_token);
30
31    // action on the resource to be authorized
32    let action = "books.dashboard.view";
33
34    // create Resource object for PDP authorization request
35    // this is specific to your web service/server and should
36    // match up with the Attributes allowed in your IAM Service definition
37    let mut resource = Resource::new();
38    [
39        ("serviceName", "books"),
40        ("accountId", "1111222233334444"),
41        ("ctype", "public"),
42        ("serviceInstance", "9e386139-0000-000-8101-103771fa7793"),
43    ]
44    .iter()
45    .for_each(|tup| {
46        resource.insert(tup.0.to_string(), tup.1.to_string());
47    });
48
49    // build the final request, send to IAM, get and return the response
50    let resp = pdp.authorize(sub, &action, resource)?;
51
52    println!("Authorization Decision: {:?}", resp);
53
54    Ok(())
55}
Source

pub fn token(&self) -> Result<Token, Box<dyn Error>>

Examples found in repository?
examples/pdp_auth.rs (line 16)
6pub fn main() -> Result<(), Box<dyn std::error::Error>> {
7    // production IAM endpoint
8    let endpoint = "https://iam.cloud.ibm.com";
9
10    // get a user token from IAM
11    // normally this would be something your web service or server would receive from a user
12    // but we grab one here for the sake of completeness
13    let api_key =
14        std::env::var("IBMCLOUD_API_KEY").expect("Could not read IBMCLOUD_API_KEY from env");
15    let tm = TokenManager::new(&api_key, &endpoint);
16    let user_token = tm.token()?;
17
18    // validate user token, this checks the signature and returns claims
19    let _claims = validate_token(&user_token, &endpoint);
20
21    // Service ID API key, this is unique to your web service/server
22    let service_id_key =
23        std::env::var("SERVICE_ID_KEY").expect("Could not parse SERVICE_ID_KEY from env");
24
25    // build PDP client object for interacting with the IAM PDP endpoint
26    let pdp = pdpapi::PDPClient::new(&service_id_key, &endpoint);
27
28    // get Subject type from user token
29    let sub = pdpapi::subject_from_token(&user_token);
30
31    // action on the resource to be authorized
32    let action = "books.dashboard.view";
33
34    // create Resource object for PDP authorization request
35    // this is specific to your web service/server and should
36    // match up with the Attributes allowed in your IAM Service definition
37    let mut resource = Resource::new();
38    [
39        ("serviceName", "books"),
40        ("accountId", "1111222233334444"),
41        ("ctype", "public"),
42        ("serviceInstance", "9e386139-0000-000-8101-103771fa7793"),
43    ]
44    .iter()
45    .for_each(|tup| {
46        resource.insert(tup.0.to_string(), tup.1.to_string());
47    });
48
49    // build the final request, send to IAM, get and return the response
50    let resp = pdp.authorize(sub, &action, resource)?;
51
52    println!("Authorization Decision: {:?}", resp);
53
54    Ok(())
55}

Trait Implementations§

Source§

impl Default for TokenManager

Source§

fn default() -> Self

Returns the “default value” for a type. 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.

Source§

impl<T> Instrument for T

Source§

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

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

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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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>,

Source§

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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> ErasedDestructor for T
where T: 'static,