Skip to content

Commit 5c5b000

Browse files
authored
Merge pull request #12 from SakaDream/update-dependencies
Update dependencies
2 parents c9e24a3 + 9e082eb commit 5c5b000

File tree

6 files changed

+87
-48
lines changed

6 files changed

+87
-48
lines changed

Cargo.toml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,20 @@ edition = "2018"
77
[dependencies]
88
actix-web = "2.0.0"
99
actix-rt = "1.0.0"
10-
actix-service = "1.0.1"
10+
actix-service = "1.0.5"
1111
actix-cors = "0.2.0"
1212
log = "0.4.8"
1313
env_logger = "0.7.1"
1414
diesel_migrations = "1.4.0"
1515
serde = "1.0.104"
1616
serde_derive = "1.0.104"
17-
serde_json = "1.0.44"
17+
serde_json = "1.0.48"
1818
dotenv = "0.15.0"
19-
futures = "0.3.1"
19+
futures = "0.3.4"
2020
failure = "0.1.6"
21-
derive_more = "0.99.2"
22-
jsonwebtoken = "6.0.1"
21+
derive_more = "0.99.3"
22+
jsonwebtoken = "7.1.0"
2323
bcrypt = "0.6.1"
24-
time = "0.2.1"
2524

2625
[dependencies.diesel]
2726
version = "1.4.3"

README.md

Lines changed: 72 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ curl -X GET -i 'http://127.0.0.1:8000/api/ping'
4444
### `POST /api/auth/signup`: Signup
4545
```bash
4646
curl -X POST -i 'http://127.0.0.1:8000/api/auth/signup' \
47-
-H "Content-Type: application/json" --data '{"username": "c",
48-
"email": "c",
49-
"password": "c" }'
47+
-H "Content-Type: application/json" \
48+
--data '{
49+
"username": "user",
50+
"email": "user@email.com",
51+
"password": "4S3cr3tPa55w0rd"
52+
}'
5053
```
5154

5255
- Request body:
@@ -76,7 +79,7 @@ curl -X POST -i 'http://127.0.0.1:8000/api/auth/signup' \
7679
### `POST /api/auth/login`: Login
7780
```bash
7881
curl -X POST -H 'Content-Type: application/json' -i 'http://127.0.0.1:8000/api/auth/login' \
79-
--data '{"username_or_email":"c", "password":"c"}'
82+
--data '{"username_or_email":"user", "password":"4S3cr3tPa55w0rd"}'
8083
```
8184
- Request body:
8285
```
@@ -104,14 +107,17 @@ curl -X POST -H 'Content-Type: application/json' -i 'http://127.0.0.1:8000/api/a
104107
```
105108
106109
### `POST /api/auth/login`: Logout
107-
```bash
108-
curl -X POST -H 'Content-Type: application/json' -H 'Authorization: bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NzcyNTc4NzksImV4cCI6MTU3Nzg2MjY3OSwidXNlciI6ImMiLCJsb2dpbl9zZXNzaW9uIjoiYzUxNWE3NTg3NGYzNGVjNGFmNDJmNWE2M2QxMDVjMGYifQ.B9w6FxFdypb5GCRMKXZ9CZWFxQLFjvmPSusMCtcE-Ac' -i 'http://127.0.0.1:8000/api/auth/logout'
109-
```
110+
```bash
111+
curl -X POST -H 'Content-Type: application/json' \
112+
-H 'Authorization: bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NzcyNTc4NzksImV4cCI6MTU3Nzg2MjY3OSwidXNlciI6ImMiLCJsb2dpbl9zZXNzaW9uIjoiYzUxNWE3NTg3NGYzNGVjNGFmNDJmNWE2M2QxMDVjMGYifQ.B9w6FxFdypb5GCRMKXZ9CZWFxQLFjvmPSusMCtcE-Ac' \
113+
-i 'http://127.0.0.1:8000/api/auth/logout'
114+
```
110115

111116
### `GET /api/address-book`: Get all people information
112-
```
113-
curl -X GET -H 'Content-Type: application/json' -H 'Authorization: bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NzU4NzM4MjksImV4cCI6MTU3NjQ3ODYyOSwidXNlciI6ImMiLCJsb2dpbl9zZXNzaW9uIjoiZjU5N2M3MTIxZTExNDBhMGE0ZjE0YmQ4N2NjM2Q4MWUifQ.6qppDfRgOw45eExJ7MUEwpcu3AUXXe9_ifj_mp7k22k' -i 'http://127.0.0.1:8000/api/address-book'
114-
'
117+
```bash
118+
curl -X GET -H 'Content-Type: application/json' \
119+
-H 'Authorization: bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NzU4NzM4MjksImV4cCI6MTU3NjQ3ODYyOSwidXNlciI6ImMiLCJsb2dpbl9zZXNzaW9uIjoiZjU5N2M3MTIxZTExNDBhMGE0ZjE0YmQ4N2NjM2Q4MWUifQ.6qppDfRgOw45eExJ7MUEwpcu3AUXXe9_ifj_mp7k22k' \
120+
-i 'http://127.0.0.1:8000/api/address-book'
115121
```
116122
- Header:
117123
- Authorization: bearer \<token\>
@@ -135,8 +141,10 @@ curl -X GET -H 'Content-Type: application/json' -H 'Authorization: bearer eyJ0eX
135141
```
136142
137143
### `GET /api/address-book/{id}`: Get person information by id
138-
```
139-
curl -X GET -H 'Content-Type: application/json' -H 'Authorization: bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NzU4NzM4MjksImV4cCI6MTU3NjQ3ODYyOSwidXNlciI6ImMiLCJsb2dpbl9zZXNzaW9uIjoiZjU5N2M3MTIxZTExNDBhMGE0ZjE0YmQ4N2NjM2Q4MWUifQ.6qppDfRgOw45eExJ7MUEwpcu3AUXXe9_ifj_mp7k22k' -i 'http://127.0.0.1:8000/api/address-book/2'
144+
```bash
145+
curl -X GET -H 'Content-Type: application/json' \
146+
-H 'Authorization: bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NzU4NzM4MjksImV4cCI6MTU3NjQ3ODYyOSwidXNlciI6ImMiLCJsb2dpbl9zZXNzaW9uIjoiZjU5N2M3MTIxZTExNDBhMGE0ZjE0YmQ4N2NjM2Q4MWUifQ.6qppDfRgOw45eExJ7MUEwpcu3AUXXe9_ifj_mp7k22k' \
147+
-i 'http://127.0.0.1:8000/api/address-book/2'
140148
```
141149
- Param path:
142150
- id: int32
@@ -167,6 +175,11 @@ curl -X GET -H 'Content-Type: application/json' -H 'Authorization: bearer eyJ0eX
167175
```
168176
169177
### `GET /api/address-book/{query}`: Search for person information by keyword
178+
```bash
179+
curl -X GET -H 'Content-Type: application/json' \
180+
-H 'Authorization: bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NzU4NzM4MjksImV4cCI6MTU3NjQ3ODYyOSwidXNlciI6ImMiLCJsb2dpbl9zZXNzaW9uIjoiZjU5N2M3MTIxZTExNDBhMGE0ZjE0YmQ4N2NjM2Q4MWUifQ.6qppDfRgOw45eExJ7MUEwpcu3AUXXe9_ifj_mp7k22k' \
181+
-i 'http://127.0.0.1:8000/api/address-book/user'
182+
```
170183
- Param path:
171184
- query: string
172185
- Header:
@@ -191,15 +204,18 @@ curl -X GET -H 'Content-Type: application/json' -H 'Authorization: bearer eyJ0eX
191204
```
192205
193206
### `POST /api/address-book`: Add person information
194-
```
195-
curl -X POST -H 'Content-Type: application/json' -H 'Authorization: bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NzU4NzM4MjksImV4cCI6MTU3NjQ3ODYyOSwidXNlciI6ImMiLCJsb2dpbl9zZXNzaW9uIjoiZjU5N2M3MTIxZTExNDBhMGE0ZjE0YmQ4N2NjM2Q4MWUifQ.6qppDfRgOw45eExJ7MUEwpcu3AUXXe9_ifj_mp7k22k' -i 'http://127.0.0.1:8000/api/address-book' --data '{
196-
"name": "a",
197-
"gender": true,
198-
"age": 32,
199-
"address": "addr",
200-
"phone": "133",
201-
"email": "e@q.com"
202-
}
207+
```bash
208+
curl -X POST -H 'Content-Type: application/json' \
209+
-H 'Authorization: bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NzU4NzM4MjksImV4cCI6MTU3NjQ3ODYyOSwidXNlciI6ImMiLCJsb2dpbl9zZXNzaW9uIjoiZjU5N2M3MTIxZTExNDBhMGE0ZjE0YmQ4N2NjM2Q4MWUifQ.6qppDfRgOw45eExJ7MUEwpcu3AUXXe9_ifj_mp7k22k' \
210+
-i 'http://127.0.0.1:8000/api/address-book' \
211+
--data '{
212+
"name": "c",
213+
"gender": true,
214+
"age": 32,
215+
"address": "addr",
216+
"phone": "133",
217+
"email": "e@q.com"
218+
}
203219
'
204220
```
205221
- Header:
@@ -232,6 +248,20 @@ curl -X POST -H 'Content-Type: application/json' -H 'Authorization: bearer eyJ0e
232248
```
233249
234250
### `PUT /api/address-book/{id}`: Update person information by id
251+
```bash
252+
curl -X PUT -H 'Content-Type: application/json' \
253+
-H 'Authorization: bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NzU4NzM4MjksImV4cCI6MTU3NjQ3ODYyOSwidXNlciI6ImMiLCJsb2dpbl9zZXNzaW9uIjoiZjU5N2M3MTIxZTExNDBhMGE0ZjE0YmQ4N2NjM2Q4MWUifQ.6qppDfRgOw45eExJ7MUEwpcu3AUXXe9_ifj_mp7k22k' \
254+
-i 'http://127.0.0.1:8000/api/address-book/2' \
255+
--data '{
256+
"name": "b",
257+
"gender": true,
258+
"age": 32,
259+
"address": "addr",
260+
"phone": "133",
261+
"email": "b@q.com"
262+
}
263+
'
264+
```
235265
- Param path:
236266
- id: int32
237267
- Header:
@@ -264,6 +294,11 @@ curl -X POST -H 'Content-Type: application/json' -H 'Authorization: bearer eyJ0e
264294
```
265295
266296
### `DELETE /api/address-book/{id}`: Delete person information by id
297+
```bash
298+
curl -X DELETE -H 'Content-Type: application/json' \
299+
-H 'Authorization: bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NzU4NzM4MjksImV4cCI6MTU3NjQ3ODYyOSwidXNlciI6ImMiLCJsb2dpbl9zZXNzaW9uIjoiZjU5N2M3MTIxZTExNDBhMGE0ZjE0YmQ4N2NjM2Q4MWUifQ.6qppDfRgOw45eExJ7MUEwpcu3AUXXe9_ifj_mp7k22k' \
300+
-i 'http://127.0.0.1:8000/api/address-book/2'
301+
```
267302
- Param path:
268303
- id: int32
269304
- Header:
@@ -284,6 +319,22 @@ curl -X POST -H 'Content-Type: application/json' -H 'Authorization: bearer eyJ0e
284319
}
285320
```
286321
322+
### brower OPTIONS curl request example
323+
```bash
324+
curl -X OPTIONS -i 'http://127.0.0.1:8000/api/login' \
325+
-H "Origin: http://example.com" -H "Access-Control-Request-Method: POST"
326+
```
327+
- Response
328+
```
329+
HTTP/1.1 200 OK
330+
content-length: 0
331+
access-control-max-age: 3600
332+
access-control-allow-methods: POST,DELETE,GET,PUT
333+
access-control-allow-origin: *
334+
access-control-allow-headers: authorization,content-type,accept
335+
date: Tue, 07 Jan 2020 15:17:48 GMT
336+
```
337+
287338
### Errors:
288339
- Invalid or missing token
289340
- Status code: 401 Unauthorized
@@ -294,15 +345,3 @@ curl -X POST -H 'Content-Type: application/json' -H 'Authorization: bearer eyJ0e
294345
"data": ""
295346
}
296347
```
297-
### brower OPTIONS curl request example
298-
```
299-
curl -X OPTIONS -i 'http://127.0.0.1:8000/api/login' -H "Origin: http://example.com" -H "Access-Control-Request-Method: POST"
300-
```
301-
- Response
302-
HTTP/1.1 200 OK
303-
content-length: 0
304-
access-control-max-age: 3600
305-
access-control-allow-methods: POST,DELETE,GET,PUT
306-
access-control-allow-origin: *
307-
access-control-allow-headers: authorization,content-type,accept
308-
date: Tue, 07 Jan 2020 15:17:48 GMT

src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ extern crate derive_more;
2323
extern crate jsonwebtoken;
2424
extern crate uuid;
2525
extern crate bcrypt;
26-
extern crate time;
2726

2827
mod api;
2928
mod config;

src/middleware/authen_middleware.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ where
5757
let mut authenticate_pass: bool = false;
5858

5959
// Bypass some account routes
60-
debug!("{:?}",req.head_mut().headers());
6160
let headers = req.headers_mut();
6261
headers.append(HeaderName::from_static("content-length"),HeaderValue::from_static("true"));
6362
if Method::OPTIONS == *req.method() {

src/models/user_token.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
use crate::models::user::LoginInfoDTO;
2-
use jsonwebtoken::Header;
3-
use time::PrimitiveDateTime;
2+
use chrono::Utc;
3+
use jsonwebtoken::{
4+
EncodingKey,
5+
Header
6+
};
47

58
pub static KEY: [u8; 16] = *include_bytes!("../secret.key");
6-
static ONE_WEEK: i64 = 60 * 60 * 24 * 7;
9+
static ONE_WEEK: i64 = 60 * 60 * 24 * 7; // in seconds
710

811
#[derive(Serialize, Deserialize)]
912
pub struct UserToken {
@@ -18,14 +21,14 @@ pub struct UserToken {
1821

1922
impl UserToken {
2023
pub fn generate_token(login: LoginInfoDTO) -> String {
21-
let now = PrimitiveDateTime::now().timestamp();
24+
let now = Utc::now().timestamp_nanos() / 1_000_000_000; // nanosecond -> second
2225
let payload = UserToken {
2326
iat: now,
2427
exp: now + ONE_WEEK,
2528
user: login.username,
2629
login_session: login.login_session,
2730
};
2831

29-
jsonwebtoken::encode(&Header::default(), &payload, &KEY).unwrap()
32+
jsonwebtoken::encode(&Header::default(), &payload, &EncodingKey::from_secret(&KEY)).unwrap()
3033
}
3134
}

src/utils/token_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use crate::{
66
},
77
};
88
use actix_web::web;
9-
use jsonwebtoken::{TokenData, Validation};
9+
use jsonwebtoken::{DecodingKey, TokenData, Validation};
1010

1111
pub fn decode_token(token: String) -> jsonwebtoken::errors::Result<TokenData<UserToken>> {
12-
jsonwebtoken::decode::<UserToken>(&token, &KEY, &Validation::default())
12+
jsonwebtoken::decode::<UserToken>(&token, &DecodingKey::from_secret(&KEY), &Validation::default())
1313
}
1414

1515
pub fn verify_token(token_data: &TokenData<UserToken>, pool: &web::Data<Pool>) -> Result<String, String> {

0 commit comments

Comments
 (0)