22
22
. get ( "/auth" , async ( ctx , next ) => {
23
23
// request Authentication
24
24
const { callback} = ctx . query
25
- ctx . assert ( callback , 400 , `Missing Parameter "callback"` )
25
+ ctx . assert ( callback , 400 , null , { detail : `Missing Parameter "callback"` } )
26
26
const token = crypto . randomBytes ( 32 ) . hexSlice ( )
27
27
const expires = new Date ( Date . now ( ) + 300000 )
28
28
ctx . cookies . set ( "callback" , callback , { expires} )
34
34
// generate Authorization
35
35
const { code, state} = ctx . query
36
36
const token = ctx . cookies . get ( "token" )
37
- ctx . assert ( code && state , 400 , `Missing Parameter "code" and/or "state"` )
38
- ctx . assert ( ctx . cookies . get ( "callback" ) && token , 400 , `Missing Cookie "callback" and/or "token"` )
39
- ctx . assert ( crypto . createHmac ( "sha256" , secretKey ) . update ( state ) . digest ( "hex" ) === token , 400 , `Invalid Token` )
37
+ ctx . assert ( code && state , 400 , null , { detail : `Missing Parameter(s) "code" and/or "state"` } )
38
+ ctx . assert ( ctx . cookies . get ( "callback" ) && token , 400 , null , { detail : `Missing Cookie(s) "callback" and/or "token"` } )
39
+ ctx . assert ( crypto . createHmac ( "sha256" , secretKey ) . update ( state ) . digest ( "hex" ) === token , 400 , null , { detail : `Invalid token` } )
40
40
ctx . cookies . set ( "token" )
41
41
await new Promise ( resolve => setTimeout ( resolve , 500 ) )
42
42
await rp ( {
46
46
json : true
47
47
} )
48
48
. then ( auth => {
49
- ctx . assert ( auth . client_id === client_id , 500 , "Internal OAuth Request Failed" )
49
+ ctx . assert ( auth . client_id === client_id , 500 , null , { detail : "Internal OAuth Request Failed" } )
50
50
return Promise . all ( [
51
51
auth . token ,
52
52
rp ( {
80
80
. then ( ( [ token ] ) => {
81
81
ctx . redirect ( `${ ctx . cookies . get ( "callback" ) } ?token=${ token } ` )
82
82
ctx . cookies . set ( "callback" )
83
+ } ) . catch ( ( ) => {
84
+ ctx . throw ( 500 , null , { detail : "Internal OAuth Request Failed" } )
83
85
} )
84
86
await next ( )
85
87
} )
88
90
const { token} = ctx . params
89
91
await knex ( "users" ) . first ( "id" , "revoked" ) . where ( "token" , token )
90
92
. then ( user => {
91
- ctx . assert ( user , 404 , "Not Found" )
92
- ctx . assert ( ! user . revoked , 400 , "Already Revoked" )
93
+ ctx . assert ( user , 404 , null , { detail : `A token " ${ token } " is not found in this service` } )
94
+ ctx . assert ( ! user . revoked , 400 , null , { detail : `A token " ${ token } " is already revoked` } )
93
95
return knex ( "users" ) . where ( { id : user . id } ) . update ( { revoked : true , updated_at : knex . fn . now ( ) } )
94
96
} )
95
97
. then ( ( ) => ctx . body = { complete : true } )
@@ -98,11 +100,11 @@ router
98
100
. use ( "/:username/items/:id" , async ( ctx , next ) => {
99
101
// authentication
100
102
const auth = ctx . header . authorization
101
- ctx . assert ( auth , 401 , "Missing Authorization Header" )
103
+ ctx . assert ( auth , 401 , null , { detail : "Missing Authorization Header" } )
102
104
const token = auth . replace ( / ^ B e a r e r / , "" )
103
105
await knex ( "users" ) . first ( "id" ) . where ( { token, revoked : false } )
104
106
. then ( user => {
105
- ctx . assert ( user , 403 , "Invalid Authorization Token" )
107
+ ctx . assert ( user , 403 , null , { detail : "Invalid Authorization Token" } )
106
108
ctx . user = user . id
107
109
} )
108
110
await next ( )
@@ -128,7 +130,7 @@ router
128
130
} else if ( ! disliked . state ) {
129
131
return knex ( "item_dislike" ) . where ( { id, by_whom : ctx . user } ) . update ( { state : true , updated_at : knex . fn . now ( ) } )
130
132
} else {
131
- ctx . throw ( 409 , "Already Disliked" )
133
+ ctx . throw ( 409 , null , { detail : "Already Disliked" } )
132
134
}
133
135
} )
134
136
. then ( ( ) => ctx . body = { complete : true } )
171
173
} catch ( e ) {
172
174
console . error ( e . message )
173
175
ctx . status = e . status || 500
174
- ctx . type = "json"
175
- ctx . body = { code : e . status , message : e . message }
176
+ ctx . body = { type : "about:blank" , status : ctx . status , title : e . status ? e . message : "Internal Server Error" , detail : e . detail }
177
+ ctx . set ( "Content-Type" , "application/problem+json; charset=utf-8" )
176
178
}
177
179
} )
178
180
. use ( ratelimit ( { duration : 60000 , rate : rate_limit || 30 , id : ctx => `${ ctx . method } ${ ctx . user } ${ ctx . ip } ` , throw : true } ) )
0 commit comments