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
#![allow(dead_code)]
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
extern crate time;
extern crate log;
extern crate smpl_jwt;
extern crate reqwest;
extern crate futures;
pub mod error;
pub mod auth;
pub mod scopes;
pub mod credentials;
use error::GOErr;
use auth::{Token, JwtClaims};
use credentials::Credentials;
use std::str::FromStr;
use smpl_jwt::Jwt;
use reqwest::Client;
use reqwest::async::Client as aClient;
use futures::future::Future;
const DEFAULT_URL: &str = "https://www.googleapis.com/oauth2/v4/token";
fn form_body(body: &str) -> Vec<(&str, &str)> {
vec!(("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer"), ("assertion", body))
}
pub fn get_token_legacy(jwt: &Jwt<JwtClaims>, url: Option<&str>) -> Result<Token, GOErr> {
let client = Client::new();
let final_jwt = jwt.finalize()?;
let request_body = form_body(&final_jwt);
let mut response = client.post(url.unwrap_or(DEFAULT_URL))
.form(&request_body).send()?;
Token::from_str(&response.text()?)
}
pub fn get_token_as_string_legacy(jwt: &Jwt<JwtClaims>, url: Option<&str>) -> Result<String, GOErr> {
Ok(serde_json::to_string(&get_token_legacy(jwt, url)?)?)
}
pub fn get_token_as_string(jwt: &Jwt<JwtClaims>, credentials: &Credentials) -> Result<String, GOErr> {
Ok(serde_json::to_string(&get_token_with_creds(jwt, credentials)?)?)
}
#[test]
fn test_jwt_encode() {
use auth::JwtClaims;
use smpl_jwt::{RSAKey, Jwt};
use scopes::Scope;
let token_url = "https://www.googleapis.com/oauth2/v4/token";
let iss = "some_iss";
let private_key_file = "random_rsa_for_testing";
let claims = JwtClaims::new(String::from(iss),
&Scope::DevStorageReadWrite,
String::from(token_url),
Some(1482317385), Some(3600));
let key = match RSAKey::from_pem(private_key_file) {
Ok(x) => x,
Err(e) => panic!("{}", e)
};
let jwt = Jwt::new(claims, key, None);
assert_eq!(jwt.finalize().unwrap(), "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzb21lX2lzcyIsInNjb3BlIjoiaHR0cHM6Ly93d3cuZ29vZ2xlYXBpcy5jb20vYXV0aC9kZXZzdG9yYWdlLnJlYWRfd3JpdGUiLCJhdWQiOiJodHRwczovL3d3dy5nb29nbGVhcGlzLmNvbS9vYXV0aDIvdjQvdG9rZW4iLCJleHAiOjE0ODIzMjA5ODUsImlhdCI6MTQ4MjMxNzM4NX0=.BldQozpzNYnLnYWBbqwAWY1j2hPDD3oVY9EOG0eRJN77sC4ZInEyGJT5eXLD39C726TdrEVCHmvhKBJFmaFL2BXNto69_v8lz-3oGnFL5FkUr4RRpukd_6tj7-RZzx15LIzdTqzKfAUlqWoZUdze8Fcd1NJ6w1g49CCghvN_eryvecALpjnHoBkKlIXnSm_udiSf26cYWvCikmW5g8nUqAduFsIYfR-4LMwyUfYH1hNC64SRsfLH9bL4-tyeaoUCv5MXTIhxrJbrhQy3TEOSc5didDrMoYNUu_qjJvxBQbq1Um1W1SpyvSd4eVJn18xZcOmCnoE73RDZcxT5hDpaRQ==");
}
#[test]
fn get_token_test() {
use auth::JwtClaims;
use smpl_jwt::{RSAKey, Jwt};
use scopes::Scope;
let token_url = "https://www.googleapis.com/oauth2/v4/token";
let iss = "some_iss";
let private_key_file = "random_rsa_for_testing";
let claims = JwtClaims::new(String::from(iss),
&Scope::DevStorageReadWrite,
String::from(token_url),
None, None);
let key = match RSAKey::from_pem(private_key_file) {
Ok(x) => x,
Err(e) => panic!("{}", e)
};
let jwt = Jwt::new(claims, key, None);
match get_token_legacy(&jwt, None) {
Ok(x) => println!("{}", x),
Err(e) => println!("{}", e)
};
}
pub fn get_token_with_creds(jwt: &Jwt<JwtClaims>, credentials: &Credentials) -> Result<Token, GOErr> {
let final_jwt = jwt.finalize()?;
let request_body = form_body(&final_jwt);
let client = Client::new();
let mut response = client.post(&credentials.token_uri())
.form(&request_body).send()?;
Token::from_str(&response.text()?)
}
pub fn get_token_with_creds_async(jwt: &Jwt<JwtClaims>, credentials: &Credentials) -> impl Future<Item=Result<Token, GOErr>> {
let final_jwt = match jwt.finalize() {
Ok(x) => x,
Err(e) => panic!(e)
};
let request_body = form_body(&final_jwt);
let client = aClient::new();
client.post(&credentials.token_uri())
.form(&request_body)
.send()
.and_then(|mut res| {
println!("{}", res.status());
res.json::<Token>()
}).map(Ok)
}