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

Use of Trait Flow #57

Closed
martell opened this issue Feb 7, 2017 · 2 comments
Closed

Use of Trait Flow #57

martell opened this issue Feb 7, 2017 · 2 comments

Comments

@martell
Copy link
Contributor

martell commented Feb 7, 2017

Hey guys,

Currently working with a fork of yup-oauth2 modified for another service and I wanted to upstream the changes and use the vanilla version.

Here is the basic usage of device-auth.

let ssl = hyper_native_tls::NativeTlsClient::new().unwrap();
    let connector = hyper::net::HttpsConnector::new(ssl);
    let client = hyper::Client::with_connector(connector);

    match oauth2::Authenticator::new(&secret, StdoutHandler, client, 
                                    oauth2::NullStorage, 
                                    Some(oauth2::FlowType::Device(String::from("https://deviceurl")))).token(&scope) {
        Ok(t) => {
            println!("Authentication granted!");
            println!("You should store the following information for use, or revoke it.");
            println!("All dates are given in UTC.");
            println!("{:?}", t);
        },
        Err(err) => {
            println!("Access token wasn't obtained: {}", err);
            std::process::exit(10);
        }
    }

I see the trait Flow with the function type_id implemented for DeviceFlow

impl<C> Flow for DeviceFlow<C> {
    fn type_id() -> FlowType {
        FlowType::Device(String::new())
    }
}

I first noticed that I can't use DeviceFlow as though it was a FlowType::Device
So I exported Flow in lib.rs

diff --git a/src/lib.rs b/src/lib.rs
index 38500f7..a6aff23 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -96,7 +96,7 @@ mod types;
 
 pub use device::{GOOGLE_DEVICE_CODE_URL, DeviceFlow};
 pub use refresh::{RefreshFlow, RefreshResult};
-pub use types::{Token, FlowType, ApplicationSecret, ConsoleApplicationSecret, Scheme, TokenType};
+pub use types::{Token, Flow, FlowType, ApplicationSecret, ConsoleApplicationSecret, Scheme, TokenType};
 pub use installed::{InstalledFlow, InstalledFlowReturnMethod};
 pub use storage::{TokenStorage, NullStorage, MemoryStorage, DiskTokenStorage};
 pub use authenticator::{Authenticator, Retry, GetToken};

I want to use it in this way

use oauth2::Flow;

let ssl = hyper_native_tls::NativeTlsClient::new().unwrap();
let connector = hyper::net::HttpsConnector::new(ssl);
let client = hyper::Client::with_connector(connector);

let mut flow = oauth2::DeviceFlow::new_device(client, &secret,
String::from("https://login.devicecode/blah"), Some(String::from("device_code")));

match oauth2::Authenticator::new(&secret, StdoutHandler, client,·
oauth2::NullStorage, Some(flow.type_id())).token(&scope) {

the function new_device is a modification of new with an added Option<String> so that we can set the grant_type. If no String is passed we default to http://oauth.net/grant_type/device/1.0 to keep the original functionality and new just wraps around this without passing the 4th argument.

My current error is

error: no method named `type_id` found for type `oauth2::DeviceFlow<hyper::Client>` in the current scope
note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: candidate #1 is defined in the trait `oauth2::Flow`

I haven't being using rust more then a week or so so I could be wrong but I assume I am trying to use this Trait in the correct manner?

@dermesser
Copy link
Owner

type_id() is not a method, it's an associated function (f.k.a. static function). It has to be called as DeviceFlow::type_id().

@martell
Copy link
Contributor Author

martell commented Jun 19, 2017

Thanks @dermesser.
Going to close this because I had since learned we can not override in rust in the way I expected.
Thanks for the explanation on the type_id

@martell martell closed this as completed Jun 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants