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
//! A trait representing requests that can be signed with OAuth.

use std::borrow::Borrow;
use std::collections::BTreeSet;
use std::fmt::Display;

use crate::signer::Signer;
use crate::{Options, Request, SignatureMethod};

/// Types that can be made into a `Request` using given credentials.
///
/// ## `#[derive(Authorize)]`
///
/// `oauth1-request` crate provides a derive macro for `Authorize`trait.
///
/// It generates a code to create a query string using the struct's field names and
/// `Display` implementation of the values.
///
/// You can customize the trait implementation produced by the derive macro with the following
/// field attributes:
///
/// - `#[oauth1(encoded)]`
///
/// Do not percent encode the value when appending it to query string.
///
/// - `#[oauth1(fmt = "path")]`
///
/// Format the value using the given function. The function must be callable as
/// `fn(&T, &mut Formatter<'_>) -> fmt::Result` (same as `Display::fmt`).
///
/// - `#[oauth1(option = "true")]` (or `#[oauth1(option = "false")]`)
///
/// If set to "true", skip the field when the value is `None` or use the unwrapped value otherwise.
/// The value's type must be `Option<T>` in that case.
///
/// When the field's type name is `Option<_>`, the attribute is implicitly set to `"true"`.
/// Use `#[oauth1(option = "false")]` if you need to opt out of that behavior.
///
/// - `#[oauth1(rename = "name")]`
///
/// Use the given string as the key of the query pair. The given string must be URI-safe.
///
/// - `#[oauth1(skip)]`
///
/// Unconditionally skip the field.
///
/// - `#[oauth1(skip_if = "path")]`
///
/// Call the given function and skip the field if the function returns `true`.
/// The function must be callable as `fn(&T) -> bool`.
pub trait Authorize {
    /// Signs `self` using `signer`.
    ///
    /// Users of the trait should use `authorize` or `authorize_form` instead.
    fn authorize_with<SM>(
        &self,
        signer: Signer<SM>,
        consumer_key: &str,
        options: Option<&Options<'_>>,
    ) -> Request
    where
        SM: SignatureMethod;

    /// Signs `self` using the given credentials and returns a `Request` with a URI with query
    /// string.
    fn authorize<'a, SM>(
        &self,
        method: &str,
        uri: impl Display,
        consumer_key: &str,
        consumer_secret: &str,
        token_secret: impl Into<Option<&'a str>>,
        signature_method: SM,
        options: impl Into<Option<&'a Options<'a>>>,
    ) -> Request
    where
        SM: SignatureMethod,
    {
        let signer = Signer::with_signature_method(
            signature_method,
            method,
            uri,
            consumer_secret,
            token_secret,
        );
        self.authorize_with(signer, consumer_key, options.into())
    }

    /// Signs `self` using the given credentials and returns a `Request` with
    /// an `x-www-form-urlencoded` string.
    fn authorize_form<'a, SM>(
        &self,
        method: &str,
        uri: impl Display,
        consumer_key: &str,
        consumer_secret: &str,
        token_secret: impl Into<Option<&'a str>>,
        signature_method: SM,
        options: impl Into<Option<&'a Options<'a>>>,
    ) -> Request
    where
        SM: SignatureMethod,
    {
        let signer = Signer::form_with_signature_method(
            signature_method,
            method,
            uri,
            consumer_secret,
            token_secret,
        );
        self.authorize_with(signer, consumer_key, options.into())
    }
}

impl<'a, A: Authorize + ?Sized> Authorize for &'a A {
    fn authorize_with<SM>(
        &self,
        signer: Signer<SM>,
        consumer_key: &str,
        options: Option<&Options<'_>>,
    ) -> Request
    where
        SM: SignatureMethod,
    {
        (**self).authorize_with(signer, consumer_key, options)
    }
}

impl<'a, A: Authorize + ?Sized> Authorize for &'a mut A {
    fn authorize_with<SM>(
        &self,
        signer: Signer<SM>,
        consumer_key: &str,
        options: Option<&Options<'_>>,
    ) -> Request
    where
        SM: SignatureMethod,
    {
        (**self).authorize_with(signer, consumer_key, options)
    }
}

/// Authorizes a request with no query pairs.
impl Authorize for () {
    fn authorize_with<SM>(
        &self,
        signer: Signer<SM>,
        consumer_key: &str,
        options: Option<&Options<'_>>,
    ) -> Request
    where
        SM: SignatureMethod,
    {
        signer.finish(consumer_key, options)
    }
}

impl<K: Borrow<str>, V: Borrow<str>> Authorize for BTreeSet<(K, V)> {
    fn authorize_with<SM>(
        &self,
        mut signer: Signer<SM>,
        consumer_key: &str,
        options: Option<&Options<'_>>,
    ) -> Request
    where
        SM: SignatureMethod,
    {
        let mut params = self.iter().map(|&(ref k, ref v)| (k.borrow(), v.borrow()));

        let (mut signer, mut pair) = loop {
            let (k, v) = match params.next() {
                Some(kv) => kv,
                None => break (signer.oauth_parameters(consumer_key, options), None),
            };
            if k > "oauth_" {
                break (signer.oauth_parameters(consumer_key, options), Some((k, v)));
            }
            signer.parameter(k, v);
        };

        while let Some((k, v)) = pair {
            signer.parameter(k, v);
            pair = params.next();
        }

        signer.finish()
    }
}

impl<A: Authorize> Authorize for Option<A> {
    fn authorize_with<SM>(
        &self,
        signer: Signer<SM>,
        consumer_key: &str,
        options: Option<&Options<'_>>,
    ) -> Request
    where
        SM: SignatureMethod,
    {
        if let Some(ref this) = *self {
            this.authorize_with(signer, consumer_key, options)
        } else {
            signer.finish(consumer_key, options)
        }
    }
}