1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//! An interface for fetching Google API tokens targeting
//! [Server to Server](https://developers.google.com/identity/protocols/OAuth2ServiceAccount)
//! applications
//!
//! # examples
//!
//! ```no_run
//! // gauthz interfaces
//! extern crate gauthz;
//! // tokio async io
//! extern crate tokio_core;
//! // futures combinators
//! extern crate futures;
//!
//! use futures::Stream;
//! use gauthz::{Credentials, Scope, Tokens};
//! use tokio_core::reactor::Core;
//! use std::thread::sleep_ms;
//!
//! fn main() {
//!   let mut core = Core::new().unwrap();
//!   let tokens = Tokens::new(
//!      &core.handle(),
//!      Credentials::default().unwrap(),
//!     vec![Scope::CloudPlatform],
//!   );
//!
//!   // warning: this token dispenser never ends!
//!   let dispenser = tokens.stream().for_each(|token| {
//!       println!("{}", token.value());
//!       Ok(sleep_ms(2000)) // do something interesting
//!    });
//!
//!   println!("{:#?}", core.run(dispenser))
//! }
//! ```
//!
//! # Cargo features
//!
//! This crate has one Cargo feature, `tls`, which adds HTTPS support via the `Tokens::new`
//! constructor. This feature is enabled by default.
#![warn(missing_docs)]

#[macro_use]
extern crate serde_derive;
extern crate medallion;
extern crate serde_json;
extern crate time;
#[macro_use]
extern crate error_chain;
extern crate futures;
extern crate hyper;
#[cfg(feature = "tls")]
extern crate hyper_tls;
extern crate tokio_core;

use std::env;
use std::fs::File;
use std::io::Read;
use std::time::{Duration, Instant};

use futures::{Future as StdFuture, Stream as StdStream, future, stream};
use hyper::{Client as HyperClient, Method, Request};
use hyper::client::{Connect, HttpConnector};
use hyper::header::ContentType;
#[cfg(feature = "tls")]
use hyper_tls::HttpsConnector;
use medallion::{Algorithm, Header, Payload, Token};
use tokio_core::reactor::Handle;

pub mod error;
use error::*;
pub use error::{Error, Result};
mod scope;
pub use scope::*;

/// A `Future` with an error type pinned to `gauthz::Error`
pub type Future<T> = Box<StdFuture<Item = T, Error = Error>>;

/// A `Stream` with an error type pinned to `gauthz::Error`
pub type Stream<T> = Box<StdStream<Item = T, Error = Error>>;

const TOKEN_URL: &str = "https://www.googleapis.com/oauth2/v4/token";

/// Authentication credential information generated from google api console
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Credentials {
    /// a pem encoded rsa key
    private_key: String,
    /// account email
    client_email: String,
}

impl Credentials {
    /// Attempts to resolve credentials from location
    /// defined by common Google API env var
    /// `GOOGLE_APPLICATION_CREDENTIALS`
    pub fn default() -> Result<Credentials> {
        let file = File::open(
            env::var("GOOGLE_APPLICATION_CREDENTIALS").map_err(|_| {
                ErrorKind::Msg("missing GOOGLE_APPLICATION_CREDENTIALS".into())
            })?,
        )?;
        Self::from_reader(file)
    }
    /// Convenience method for parsing credentials from json str
    pub fn from_str(s: &str) -> Result<Credentials> {
        serde_json::from_str(s).map_err(Error::from)
    }
    /// Convenience method for parsing credentials from a reader
    /// ( i.e. a `std::fs:File` )
    pub fn from_reader<R>(r: R) -> Result<Credentials>
    where
        R: Read,
    {
        serde_json::from_reader(r).map_err(Error::from)
    }
}

#[derive(Default, Serialize, Deserialize, PartialEq, Debug)]
struct Claims {
    iss: String,
    scope: String,
    aud: String,
    exp: i64,
    iat: i64,
}

/// An access token can be used to authenticate
/// google api requests
///
/// Instances of these can be onbtained from one of the methods provided by
/// `gauthz.Tokens`
#[derive(Default, Deserialize, PartialEq, Debug, Clone)]
pub struct AccessToken {
    access_token: String,
    expires_in: u64,
    #[serde(default, skip)]
    instant: Option<Instant>,
    #[serde(default, skip)]
    duration: Option<Duration>,
}

impl AccessToken {
    /// Returns string value of access token
    ///
    /// This is typically the value you use for HTTP Authorization: Bearer
    /// header values
    pub fn value(&self) -> &str {
        &self.access_token
    }
    /// Returns true if this access token has has expired
    ///
    /// This is typically one hour in practice
    pub fn expired(&self) -> bool {
        match (self.instant, self.duration) {
            (Some(inst), Some(dur)) => inst.elapsed() >= dur,
            _ => false,
        }
    }

    fn start(mut self) -> Self {
        self.instant = Some(Instant::now());
        self.duration = Some(Duration::from_secs(self.expires_in));
        self
    }
}

/// An interface for generating access tokens to authenticate
/// google api requests
///
/// A scope is required to limit access to target apis
/// some scopes, like https://www.googleapis.com/auth/cloud-platform,
/// provide access to multiple apis
#[derive(Clone)]
pub struct Tokens<C>
where
    C: Connect + Clone,
{
    http: HyperClient<C>,
    credentials: Credentials,
    scopes: String,
}

#[cfg(feature = "tls")]
impl Tokens<HttpsConnector<HttpConnector>> {
    /// Creates a new instance of `Tokens` using a `hyper::Client`
    /// preconfigured for tls.
    ///
    /// For client customization use `Tokens::custom` instead
    pub fn new<Iter>(
        handle: &Handle,
        credentials: Credentials,
        scopes: Iter,
    ) -> Self
    where
        Iter: ::std::iter::IntoIterator<Item = Scope>,
    {
        let connector = HttpsConnector::new(4, handle).unwrap();
        let hyper = HyperClient::configure()
            .connector(connector)
            .keep_alive(true)
            .build(handle);
        Tokens::custom(hyper, credentials, scopes)
    }
}

impl<C: Connect + Clone> Tokens<C> {
    /// Creates a new instance of `Tokens` with a custom `hyper::Client`
    /// with a customly configured `hyper::Client`
    pub fn custom<Iter>(
        http: HyperClient<C>,
        credentials: Credentials,
        scopes: Iter,
    ) -> Self
    where
        Iter: ::std::iter::IntoIterator<Item = Scope>,
    {
        Self {
            http,
            credentials,
            scopes: scopes
                .into_iter()
                .map(|s| s.url())
                .collect::<Vec<_>>()
                .join(","),
        }
    }

    /// Returns a `Stream` of `AccessTokens`. The same `AccessToken` will be
    /// yielded multiple times until it is expired. After which, a new token
    /// will be fetched
    pub fn stream(&self) -> Stream<AccessToken> {
        let instance = self.clone();
        let tokens =
            stream::unfold::<
                _,
                _,
                Future<(AccessToken, Option<AccessToken>)>,
                _,
            >(None, move |state| {
                Some(match state {
                    Some(ref token) if !token.expired() => {
                        Box::new(future::ok((token.clone(), state.clone())))
                    }
                    _ => {
                        Box::new(instance.get().map(|token| {
                            let next = Some(token.clone());
                            (token, next)
                        }))
                    }
                })
            });
        Box::new(tokens)
    }

    fn new_request(&self) -> Request {
        let header: Header<()> = Header {
            alg: Algorithm::RS256,
            ..Default::default()
        };
        let iat = time::now_utc().to_timespec().sec;
        let exp = iat + 3600;
        let payload = Payload {
            claims: Some(Claims {
                iss: self.credentials.clone().client_email,
                scope: self.scopes.clone(),
                aud: TOKEN_URL.into(),
                exp: exp,
                iat: iat,
            }),
            ..Default::default()
        };
        let signed = Token::new(header, payload)
            .sign(&self.credentials.clone().private_key.into_bytes())
            .unwrap();
        let mut req = Request::new(Method::Post, TOKEN_URL.parse().unwrap());
        req.headers_mut().set(ContentType::form_url_encoded());
        let body = format!(
            "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion={assertion}",
            assertion = signed.as_str()
        );
        req.set_body(body);
        req
    }

    /// Returns a `Future` yielding a new `AccessToken`
    pub fn get(&self) -> Future<AccessToken> {
        Box::new(
            self.http
                .request(self.new_request())
                .map_err(Error::from)
                .and_then(|response| {
                    let status = response.status();
                    let body = response.body().concat2().map_err(Error::from);
                    body.and_then(move |body| if status.is_success() {
                        serde_json::from_slice::<AccessToken>(&body)
                            .map_err(|err| ErrorKind::Codec(err).into())
                            .map(AccessToken::start)
                    } else {
                        Err(match serde_json::from_slice::<ApiError>(&body) {
                            Err(err) => ErrorKind::Codec(err).into(),
                            Ok(err) => {
                                ErrorKind::Api(err.error, err.error_description)
                                    .into()
                            }
                        })
                    })
                }),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::AccessToken;
    use std::time::Duration;


    #[test]
    fn tokens_value() {
        let token = AccessToken {
            access_token: "test".into(),
            expires_in: 1,
            ..Default::default()
        };
        assert_eq!(token.value(), token.access_token)
    }

    #[test]
    fn tokens_expire() {
        let token = AccessToken {
            access_token: "test".into(),
            expires_in: 1,
            ..Default::default()
        }.start();
        assert!(!token.expired());
        let duration = Duration::from_secs(1);
        assert_eq!(token.duration, Some(duration));
        ::std::thread::sleep(duration);
        assert!(token.expired())
    }
}