11const Router = require ( 'koa-router' ) ;
2+ const debug = require ( 'debug' ) ( 'app' ) ;
23
34const agent = require ( 'lib/request' ) . default ;
45const logger = require ( '../logger' ) ;
56const utils = require ( '../utils' ) ;
67
78const router = new Router ( ) ;
8- const header = { } ;
9-
109const authEndpoint = 'oauth2/token' ;
1110
1211router . post ( '/api/*' , async ctx => {
@@ -23,70 +22,65 @@ router.post('/api/*', async ctx => {
2322
2423 logger ( { method, url, body } ) ;
2524
26- const authToken = utils . getTokenGroupFromCtx ( ctx ) ;
27- const unAuthToken = utils . getTokenGroupFromCtx ( ctx , 'un_auth' ) ;
28-
2925 const browserUrl = ctx . headers . referer ;
26+ // todo
3027 const endUrl = browserUrl
3128 . split ( '/' )
3229 . slice ( 3 )
3330 . join ( '/' ) ;
3431 const usingNoAuthToken = endUrl === '' || endUrl . startsWith ( 'apps' ) || body . isGlobalQuery ;
3532 delete body . isGlobalQuery ;
3633
37- // defalut special token params
38- const tokenData = {
39- grant_type : 'client_credentials' ,
34+ const authParams = {
4035 client_id : clientId ,
4136 client_secret : clientSecret ,
4237 scope : ''
4338 } ;
4439
45- // retrieve special token
46- if ( usingNoAuthToken && ! unAuthToken . access_token ) {
47- const res = await agent . send ( 'post' , [ apiServer , authEndpoint ] . join ( '/' ) , tokenData ) ;
48-
49- if ( ! res || ! res . access_token ) {
50- ctx . throw ( 401 , 'Retrieve token failed' ) ;
51- }
52-
53- utils . saveTokenResponseToCookie ( ctx , res , 'un_auth' ) ;
40+ // get current auth info from cookie
41+ const prefix = usingNoAuthToken ? 'no_auth' : '' ;
42+ const authInfo = utils . getTokenGroupFromCtx ( ctx , prefix ) ;
43+ const {
44+ token_type, access_token, refresh_token, expires_in
45+ } = authInfo ;
46+
47+ const payload = usingNoAuthToken
48+ ? Object . assign ( authParams , {
49+ grant_type : 'client_credentials'
50+ } )
51+ : Object . assign ( authParams , {
52+ grant_type : 'refresh_token' ,
53+ refresh_token
54+ } ) ;
5455
55- Object . assign ( unAuthToken , res ) ;
56+ if ( ! usingNoAuthToken && ! refresh_token ) {
57+ // need login
58+ ctx . throw ( 401 , 'refresh token expired' ) ;
5659 }
5760
58- const chooseToken = usingNoAuthToken ? unAuthToken : authToken ;
59-
60- // check if token expired, retrieve refresh token or special token
61- const { access_token, refresh_token } = chooseToken ;
62- if ( ! access_token && refresh_token ) {
63- // refresh token params
64- if ( ! usingNoAuthToken ) {
65- tokenData . grant_type = 'refresh_token' ;
66- tokenData . refresh_token = chooseToken . refresh_token ;
67- }
68-
69- const res = await agent . send ( 'post' , [ apiServer , authEndpoint ] . join ( '/' ) , tokenData ) ;
61+ if ( ! access_token || expires_in < Date . now ( ) ) {
62+ const res = await agent . post ( [ apiServer , authEndpoint ] . join ( '/' ) , payload ) ;
63+ debug ( `Using refresh token to exchange auth info: %O` , res ) ;
7064
7165 if ( ! res || ! res . access_token ) {
72- ctx . throw ( 401 , 'Refresh token failed' ) ;
66+ ctx . throw ( 401 , 'Retrieve access token failed' ) ;
7367 }
7468
75- utils . saveTokenResponseToCookie ( ctx , res , usingNoAuthToken ? 'un_auth' : '' ) ;
76- Object . assign ( chooseToken , res ) ;
69+ utils . saveTokenResponseToCookie ( ctx , res , prefix ) ;
70+ Object . assign ( authInfo , res ) ;
7771 }
7872
79- if ( ! chooseToken . access_token ) {
73+ if ( ! authInfo . access_token ) {
8074 ctx . throw ( 401 , 'Unauthorized: invalid access token' ) ;
81- } else {
82- header . Authorization = `${ chooseToken . token_type } ${ chooseToken . access_token } ` ;
75+ }
8376
84- delete body . method ;
77+ delete body . method ;
8578
86- ctx . body = await agent . send ( method , url , body , {
87- header
88- } ) ;
89- }
79+ ctx . body = await agent . send ( method , url , body , {
80+ header : {
81+ Authorization : `${ authInfo . token_type } ${ authInfo . access_token } `
82+ }
83+ } ) ;
9084} ) ;
9185
9286module . exports = router ;
0 commit comments