@@ -15,6 +15,7 @@ type Mode = "auto" | "cookies" | "manual"
15
15
export class Authorizer {
16
16
public mode ! : Mode ;
17
17
public endpoint : string | undefined = undefined ;
18
+ public requestHeaders : StringKV | undefined = undefined ;
18
19
public permission : Permission | undefined ;
19
20
public cookieKey : string | undefined = undefined ;
20
21
public cacheExpiredTime = 60 ; // Seconds
@@ -27,17 +28,21 @@ export class Authorizer {
27
28
* "auto": Specify the casbin server endpoint, and Casbin.js will load permission from it when the identity changes
28
29
* "cookies": Casbin.js load the permission data from the cookie "access_perm" or the specified cookie key.
29
30
* "manual": Load the permission mannually with "setPermission"
31
+ * @param args
30
32
* @param args.endpoint Casbin service endpoint, REQUIRED when mode == "auto"
31
33
* @param args.cacheExpiredTime The expired time of local cache, Unit: seconds, Default: 60s, activated when mode == "auto"
32
34
* @param args.cookieKey The cookie key when loading permission, activated when mode == "cookies"
33
35
*/
34
- constructor ( mode : Mode = " manual" , args : { endpoint ?: string , /*cookieKey ?: string,*/ cacheExpiredTime ?: number } = { } ) {
36
+ constructor ( mode : Mode = ' manual' , args : { endpoint ?: string ; cacheExpiredTime ?: number ; requestHeaders ?: StringKV } = { } ) {
35
37
if ( mode == 'auto' ) {
36
38
if ( ! args . endpoint ) {
37
39
throw new Error ( "Specify the endpoint when initializing casbin.js with mode == 'auto'" ) ;
38
40
} else {
39
41
this . mode = mode ;
40
42
this . endpoint = args . endpoint ;
43
+ if ( args . requestHeaders ) {
44
+ this . requestHeaders = args . requestHeaders ;
45
+ }
41
46
if ( args . cacheExpiredTime !== null && args . cacheExpiredTime !== undefined ) {
42
47
this . cacheExpiredTime = args . cacheExpiredTime ;
43
48
}
@@ -100,7 +105,9 @@ export class Authorizer {
100
105
if ( this . endpoint === undefined || this . endpoint === null ) {
101
106
throw Error ( "Endpoint is null or not specified." ) ;
102
107
}
103
- const resp = await axios . get < BaseResponse > ( `${ this . endpoint } ?subject=${ this . user } ` ) ;
108
+ const resp = await axios . get < BaseResponse > ( `${ this . endpoint } ?subject=${ this . user } ` , {
109
+ headers : this . requestHeaders ,
110
+ } ) ;
104
111
return resp . data . data ;
105
112
}
106
113
0 commit comments