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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! Provides the handling for Access Token Requests
use std::borrow::Cow;
use std::collections::HashMap;

use chrono::{Duration, Utc};
use serde_json;

use code_grant::error::{AccessTokenError, AccessTokenErrorType};
use primitives::authorizer::Authorizer;
use primitives::issuer::{IssuedToken, Issuer};
use primitives::grant::{Extensions, Grant};
use primitives::registrar::{Registrar, RegistrarError};

/// Trait based retrieval of parameters necessary for access token request handling.
pub trait Request {
    /// Received request might not be encoded correctly. This method gives implementors the chance
    /// to signal that a request was received but its encoding was generally malformed. If this is
    /// the case, then no other attribute will be queried. This method exists mainly to make
    /// frontends straightforward by not having them handle special cases for malformed requests.
    fn valid(&self) -> bool;

    /// The authorization code grant for which an access token is wanted.
    fn code(&self) -> Option<Cow<str>>;

    /// User:password of a basic authorization header.
    fn authorization(&self) -> Option<(Cow<str>, Cow<[u8]>)>;

    /// The client_id, optional parameter for public clients.
    fn client_id(&self) -> Option<Cow<str>>;

    /// Valid request have the redirect url used to request the authorization code grant.
    fn redirect_uri(&self) -> Option<Cow<str>>;

    /// Valid requests have this set to "authorization_code"
    fn grant_type(&self) -> Option<Cow<str>>;

    /// Retrieve an additional parameter used in an extension
    fn extension(&self, key: &str) -> Option<Cow<str>>;

    /// Credentials in body should only be enabled if use of HTTP Basic is not possible. 
    ///
    /// Allows the request body to contain the `client_secret` as a form parameter. This is NOT
    /// RECOMMENDED and need not be supported. The parameters MUST NOT appear in the request URI
    /// itself.
    ///
    /// Under these considerations, support must be explicitely enabled.
    fn allow_credentials_in_body(&self) -> bool {
        false
    }
}

/// A system of addons provided additional data.
///
/// An endpoint not having any extension may use `&mut ()` as the result of system.
pub trait Extension {
    /// Inspect the request and extension data to produce extension data.
    ///
    /// The input data comes from the extension data produced in the handling of the
    /// authorization code request.
    fn extend(&mut self, request: &dyn Request, data: Extensions) -> std::result::Result<Extensions, ()>;
}

impl Extension for () {
    fn extend(&mut self, _: &dyn Request, _: Extensions) -> std::result::Result<Extensions, ()> {
        Ok(Extensions::new())
    }
}

/// Required functionality to respond to access token requests.
///
/// Each method will only be invoked exactly once when processing a correct and authorized request,
/// and potentially less than once when the request is faulty.  These methods should be implemented
/// by internally using `primitives`, as it is implemented in the `frontend` module.
pub trait Endpoint {
    /// Get the client corresponding to some id.
    fn registrar(&self) -> &dyn Registrar;

    /// Get the authorizer from which we can recover the authorization.
    fn authorizer(&mut self) -> &mut dyn Authorizer;

    /// Return the issuer instance to create the access token.
    fn issuer(&mut self) -> &mut dyn Issuer;

    /// The system of used extension, extending responses.
    ///
    /// It is possible to use `&mut ()`.
    fn extension(&mut self) -> & mut dyn Extension;
}

enum Credentials<'a> {
    /// No credentials were offered.
    None,
    /// One set of credentials was offered.
    Authenticated {
        client_id: &'a str,
        passphrase: &'a [u8],
    },
    /// No password but name was offered.
    ///
    /// This must happen only when the credentials were part of the request body but used to
    /// indicate the name of a public client.
    Unauthenticated {
        client_id: &'a str,
    },
    /// Multiple possible credentials were offered.
    ///
    /// This is a security issue, only one attempt must be made per request.
    Duplicate,
}

/// Try to redeem an authorization code.
pub fn access_token(handler: &mut dyn Endpoint, request: &dyn Request) -> Result<BearerToken> {
    if !request.valid() {
        return Err(Error::invalid())
    }

    let authorization = request.authorization();
    let client_id = request.client_id();
    let client_secret = request.extension("client_secret");

    let mut credentials = Credentials::None;
    if let Some((client_id, auth)) = &authorization {
        credentials.authenticate(client_id.as_ref(), auth.as_ref());
    }

    if let Some(client_id) = &client_id {
        match &client_secret {
            Some(auth) if request.allow_credentials_in_body() => {
                credentials.authenticate(client_id.as_ref(), auth.as_ref().as_bytes())
            },
            // Ignore parameter if not allowed.
            Some(_) | None => {
                credentials.unauthenticated(client_id.as_ref())
            },
        }
    }

    let (client_id, auth) = credentials
        .into_client()
        .ok_or_else(Error::invalid)?;

    handler.registrar()
        .check(&client_id, auth)
        .map_err(|err| match err {
            RegistrarError::Unspecified => Error::unauthorized("basic"),
            RegistrarError::PrimitiveError => Error::Primitive(PrimitiveError {
                grant: None,
                extensions: None,
            }),
        })?;

    match request.grant_type() {
        Some(ref cow) if cow == "authorization_code" => (),
        None => return Err(Error::invalid()),
        Some(_) => return Err(Error::invalid_with(AccessTokenErrorType::UnsupportedGrantType)),
    };

    let code = request
        .code()
        .ok_or_else(Error::invalid)?;
    let code = code.as_ref();

    let saved_params = match handler.authorizer().extract(code) {
        Err(()) => return Err(Error::Primitive(PrimitiveError {
            grant: None,
            extensions: None,
        })),
        Ok(None) => return Err(Error::invalid()),
        Ok(Some(v)) => v,
    };

    let redirect_uri = request
        .redirect_uri()
        .ok_or_else(Error::invalid)?;
    let redirect_uri = redirect_uri
        .as_ref()
        .parse()
        .map_err(|_| Error::invalid())?;

    if (saved_params.client_id.as_ref(), &saved_params.redirect_uri) != (client_id, &redirect_uri) {
        return Err(Error::invalid_with(AccessTokenErrorType::InvalidGrant))
    }

    if saved_params.until < Utc::now() {
        return Err(Error::invalid_with(AccessTokenErrorType::InvalidGrant))
    }

    let code_extensions = saved_params.extensions;
    let access_extensions = handler.extension().extend(request, code_extensions);
    let access_extensions = match access_extensions {
        Ok(extensions) => extensions,
        Err(_) =>  return Err(Error::invalid()),
    };

    let token = handler.issuer().issue(Grant {
        client_id: saved_params.client_id,
        owner_id: saved_params.owner_id,
        redirect_uri: saved_params.redirect_uri,
        scope: saved_params.scope.clone(),
        until: Utc::now() + Duration::hours(1),
        extensions: access_extensions,
    }).map_err(|()| Error::Primitive(PrimitiveError {
        // FIXME: endpoint should get and handle these.
        grant: None,
        extensions: None,
    }))?;

    Ok(BearerToken{ 0: token, 1: saved_params.scope.to_string() })
}

impl<'a> Credentials<'a> {
    pub fn authenticate(&mut self, client_id: &'a str, passphrase: &'a [u8]) {
        self.add(Credentials::Authenticated { client_id, passphrase })
    }

    pub fn unauthenticated(&mut self, client_id: &'a str) {
        self.add(Credentials::Unauthenticated { client_id })
    }

    pub fn into_client(self) -> Option<(&'a str, Option<&'a [u8]>)> {
        match self {
            Credentials::Authenticated { client_id, passphrase, }
                => Some((client_id, Some(passphrase))),
            Credentials::Unauthenticated { client_id }
                => Some((client_id, None)),
            _ => None,
        }
    }

    fn add(&mut self, new: Self) {
        use std::mem::replace;
        let old = replace(self, Credentials::None);
        let next = match old {
            Credentials::None => new,
            _ => Credentials::Duplicate,
        };
        replace(self, next);
    }
}

/// Defines actions for the response to an access token request.
pub enum Error {
    /// The token did not represent a valid token.
    Invalid(ErrorDescription),

    /// The client did not properly authorize itself.
    Unauthorized(ErrorDescription, String),

    /// An underlying primitive operation did not complete successfully.
    ///
    /// This is expected to occur with some endpoints. See `PrimitiveError` for
    /// more details on when this is returned.
    Primitive(PrimitiveError),
}

/// The endpoint should have enough control over its primitives to find
/// out what has gone wrong, e.g. they may externall supply error 
/// information.
/// 
/// In this case, all previous results returned by the primitives are
/// included in the return value. Through this mechanism, one can
/// accomodate async handlers by implementing a sync-based result cache
/// that is filled with these partial values. In case only parts of the
/// outstanding futures, invoked during internal calls, are ready the
/// cache can be refilled through the error eliminating polls to already
/// sucessful futures.
///
/// Note that `token` is not included in this list, since the handler
/// can never fail after supplying a token to the backend.
pub struct PrimitiveError {
    /// The already extracted grant.
    ///
    /// You may reuse this, or more precisely you must to fulfill this exact request in case of
    /// an error recovery attempt.
    pub grant: Option<Grant>,

    /// The extensions that were computed.
    pub extensions: Option<Extensions>,
}

/// Simple wrapper around AccessTokenError to imbue the type with addtional json functionality. In
/// addition this enforces backend specific behaviour for obtaining or handling the access error.
pub struct ErrorDescription {
    error: AccessTokenError,
}

type Result<T> = std::result::Result<T, Error>;

/// Represents an access token, a refresh token and the associated scope for serialization.
pub struct BearerToken(IssuedToken, String);

impl Error {
    fn invalid() -> Self {
        Error::Invalid(ErrorDescription {
            error: AccessTokenError::default()
        })
    }

    fn invalid_with(with_type: AccessTokenErrorType) -> Self {
        Error::Invalid(ErrorDescription {
            error: {
                let mut error = AccessTokenError::default();
                error.set_type(with_type);
                error
            },
        })
    }

    fn unauthorized(authtype: &str) -> Error {
        Error::Unauthorized(ErrorDescription {
                error: {
                    let mut error = AccessTokenError::default();
                    error.set_type(AccessTokenErrorType::InvalidClient);
                    error
                },
            },
            authtype.to_string())
    }

    /// Get a handle to the description the client will receive.
    ///
    /// Some types of this error don't return any description which is represented by a `None`
    /// result.
    pub fn description(&mut self) -> Option<&mut AccessTokenError> {
        match self {
            Error::Invalid(description) => Some(description.description()),
            Error::Unauthorized(description, _) => Some(description.description()),
            Error::Primitive(_) => None,
        }
    }
}

impl ErrorDescription {
    /// Convert the error into a json string, viable for being sent over a network with
    /// `application/json` encoding.
    pub fn to_json(self) -> String {
        let asmap = self.error.into_iter()
            .map(|(k, v)| (k.to_string(), v.into_owned()))
            .collect::<HashMap<String, String>>();
        serde_json::to_string(&asmap).unwrap()
    }

    /// Get a handle to the description the client will receive.
    pub fn description(&mut self) -> &mut AccessTokenError {
        &mut self.error
    }
}

impl BearerToken {
    /// Convert the token into a json string, viable for being sent over a network with
    /// `application/json` encoding.
    // FIXME: rename to `into_json` or have `&self` argument.
    pub fn to_json(self) -> String {
        #[derive(Serialize)]
        struct Serial<'a> {
            access_token: &'a str,
            #[serde(skip_serializing_if="Option::is_none")]
            refresh_token: Option<&'a str>,
            token_type: &'a str,
            expires_in: String,
            scope: &'a str,
        }

        let remaining = self.0.until.signed_duration_since(Utc::now());
        let serial = Serial {
            access_token: self.0.token.as_str(),
            refresh_token: Some(self.0.refresh.as_str())
                .filter(|_| self.0.refreshable()),
            token_type: "bearer",
            expires_in: remaining.num_seconds().to_string(),
            scope: self.1.as_str(),
        };

        serde_json::to_string(&serial).unwrap()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    #[test]
    fn bearer_token_encoding() {
        let token = BearerToken(IssuedToken {
            token: "access".into(),
            refresh: "refresh".into(),
            until: Utc::now(),
        }, "scope".into());

        let json = token.to_json();
        let mut token = serde_json::from_str::<HashMap<String, String>>(&json).unwrap();

        assert_eq!(token.remove("access_token"), Some("access".to_string()));
        assert_eq!(token.remove("refresh_token"), Some("refresh".to_string()));
        assert_eq!(token.remove("scope"), Some("scope".to_string()));
        assert_eq!(token.remove("token_type"), Some("bearer".to_string()));
        assert!(token.remove("expires_in").is_some());
    }

    #[test]
    fn no_refresh_encoding() {
        let token = BearerToken(IssuedToken::without_refresh(
            "access".into(),
            Utc::now(),
        ), "scope".into());

        let json = token.to_json();
        let mut token = serde_json::from_str::<HashMap<String, String>>(&json).unwrap();

        assert_eq!(token.remove("access_token"), Some("access".to_string()));
        assert_eq!(token.remove("refresh_token"), None);
        assert_eq!(token.remove("scope"), Some("scope".to_string()));
        assert_eq!(token.remove("token_type"), Some("bearer".to_string()));
        assert!(token.remove("expires_in").is_some());
    }
}