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
use crate::{
    token_response, ClientCredentialsTokenRequest, CodeTokenRequest, ErrorResponse, HttpRequest,
    HttpResponse, PasswordTokenRequest, RefreshTokenRequest, RequestTokenError, TokenResponse,
    TokenType,
};
use async_trait::async_trait;
use failure::Fail;
use futures_0_3::Future;

///
/// Asynchronous request to exchange an authorization code for an access token.
///
#[async_trait]
pub trait AsyncCodeTokenRequest<TE, TR, TT>
where
    TE: ErrorResponse + 'static,
    TR: TokenResponse<TT> + Send,
    TT: TokenType + Send,
{
    ///
    /// Asynchronously sends the request to the authorization server.
    ///
    async fn request_async<C, F, RE>(self, http_client: C) -> Result<TR, RequestTokenError<RE, TE>>
    where
        C: FnOnce(HttpRequest) -> F + Send,
        F: Future<Output = Result<HttpResponse, RE>> + Send,
        RE: Fail;
}

#[async_trait]
impl<TE, TR, TT> AsyncCodeTokenRequest<TE, TR, TT> for CodeTokenRequest<'_, TE, TR, TT>
where
    TE: ErrorResponse + 'static,
    TR: TokenResponse<TT> + Send,
    TT: TokenType + Send,
{
    ///
    /// Asynchronously sends the request to the authorization server and returns a Future.
    ///
    async fn request_async<C, F, RE>(self, http_client: C) -> Result<TR, RequestTokenError<RE, TE>>
    where
        C: FnOnce(HttpRequest) -> F + Send,
        F: Future<Output = Result<HttpResponse, RE>> + Send,
        RE: Fail,
    {
        let http_request = self.prepare_request()?;
        let http_response = http_client(http_request)
            .await
            .map_err(RequestTokenError::Request)?;
        token_response(http_response)
    }
}

///
/// Asynchronous request to exchange a refresh token for an access token.
///
#[async_trait]
pub trait AsyncRefreshTokenRequest<TE, TR, TT>
where
    TE: ErrorResponse + 'static,
    TR: TokenResponse<TT> + Send,
    TT: TokenType + Send,
{
    ///
    /// Asynchronously sends the request to the authorization server and awaits a response.
    ///
    async fn request_async<C, F, RE>(self, http_client: C) -> Result<TR, RequestTokenError<RE, TE>>
    where
        C: FnOnce(HttpRequest) -> F + Send,
        F: Future<Output = Result<HttpResponse, RE>> + Send,
        RE: Fail;
}

#[async_trait]
impl<TE, TR, TT> AsyncRefreshTokenRequest<TE, TR, TT> for RefreshTokenRequest<'_, TE, TR, TT>
where
    TE: ErrorResponse + 'static,
    TR: TokenResponse<TT> + Send,
    TT: TokenType + Send,
{
    ///
    /// Asynchronously sends the request to the authorization server and awaits a response.
    ///
    async fn request_async<C, F, RE>(self, http_client: C) -> Result<TR, RequestTokenError<RE, TE>>
    where
        C: FnOnce(HttpRequest) -> F + Send,
        F: Future<Output = Result<HttpResponse, RE>> + Send,
        RE: Fail,
    {
        let http_request = self.prepare_request()?;
        let http_response = http_client(http_request)
            .await
            .map_err(RequestTokenError::Request)?;
        token_response(http_response)
    }
}

///
/// Asynchronous request to exchange resource owner credentials for an access token.
///
#[async_trait]
pub trait AsyncPasswordTokenRequest<TE, TR, TT>
where
    TE: ErrorResponse + 'static,
    TR: TokenResponse<TT> + Send,
    TT: TokenType + Send,
{
    ///
    /// Asynchronously sends the request to the authorization server and awaits a response.
    ///
    async fn request_async<C, F, RE>(self, http_client: C) -> Result<TR, RequestTokenError<RE, TE>>
    where
        C: FnOnce(HttpRequest) -> F + Send,
        F: Future<Output = Result<HttpResponse, RE>> + Send,
        RE: Fail;
}

#[async_trait]
impl<TE, TR, TT> AsyncPasswordTokenRequest<TE, TR, TT> for PasswordTokenRequest<'_, TE, TR, TT>
where
    TE: ErrorResponse + 'static,
    TR: TokenResponse<TT> + Send,
    TT: TokenType + Send,
{
    ///
    /// Asynchronously sends the request to the authorization server and awaits a response.
    ///
    async fn request_async<C, F, RE>(self, http_client: C) -> Result<TR, RequestTokenError<RE, TE>>
    where
        C: FnOnce(HttpRequest) -> F + Send,
        F: Future<Output = Result<HttpResponse, RE>> + Send,
        RE: Fail,
    {
        let http_request = self.prepare_request()?;
        let http_response = http_client(http_request)
            .await
            .map_err(RequestTokenError::Request)?;
        token_response(http_response)
    }
}

///
/// Asynchronous request to exchange client credentials for an access token.
///
#[async_trait]
pub trait AsyncClientCredentialsTokenRequest<TE, TR, TT>
where
    TE: ErrorResponse + 'static,
    TR: TokenResponse<TT> + Send,
    TT: TokenType + Send,
{
    ///
    /// Asynchronously sends the request to the authorization server and awaits a response.
    ///
    async fn request_async<C, F, RE>(self, http_client: C) -> Result<TR, RequestTokenError<RE, TE>>
    where
        C: FnOnce(HttpRequest) -> F + Send,
        F: Future<Output = Result<HttpResponse, RE>> + Send,
        RE: Fail;
}

#[async_trait]
impl<TE, TR, TT> AsyncClientCredentialsTokenRequest<TE, TR, TT>
    for ClientCredentialsTokenRequest<'_, TE, TR, TT>
where
    TE: ErrorResponse + 'static,
    TR: TokenResponse<TT> + Send,
    TT: TokenType + Send,
{
    ///
    /// Asynchronously sends the request to the authorization server and awaits a response.
    ///
    async fn request_async<C, F, RE>(self, http_client: C) -> Result<TR, RequestTokenError<RE, TE>>
    where
        C: FnOnce(HttpRequest) -> F + Send,
        F: Future<Output = Result<HttpResponse, RE>> + Send,
        RE: Fail,
    {
        let http_request = self.prepare_request()?;
        let http_response = http_client(http_request)
            .await
            .map_err(RequestTokenError::Request)?;
        token_response(http_response)
    }
}