11import Feature from "./Feature" ;
22import Permission from "./Permission" ;
3- import Check , { AccessCheckRequest , CheckMany , FeatureCheck , PermissionCheck } from "../types/Check" ;
3+ import Check , { AccessCheckRequest , CheckMany , CheckWarrant , FeatureCheck , PermissionCheck } from "../types/Check" ;
44import Warrant , { isSubject , isWarrantObject } from "../types/Warrant" ;
55import WarrantClient from "../WarrantClient" ;
66
77export default class Authorization {
88 public static async check ( check : Check ) : Promise < boolean > {
99 const accessCheckRequest : AccessCheckRequest = {
1010 warrants : [ {
11- objectType : isWarrantObject ( check . object ) ? check . object . getObjectType ( ) : check . object . objectType ,
12- objectId : isWarrantObject ( check . object ) ? check . object . getObjectId ( ) : check . object . objectId ,
11+ object : check . object ,
1312 relation : check . relation ,
14- subject : isSubject ( check . subject ) ? check . subject : { objectType : check . subject . getObjectType ( ) , objectId : check . subject . getObjectId ( ) } ,
13+ subject : check . subject ,
1514 context : check . context
1615 } ] ,
17- consistentRead : check . consistentRead ,
1816 debug : check . debug
1917 }
2018 if ( WarrantClient . config . authorizeEndpoint ) {
@@ -25,19 +23,17 @@ export default class Authorization {
2523 }
2624
2725 public static async checkMany ( check : CheckMany ) : Promise < boolean > {
28- let warrants : Warrant [ ] = check . warrants . map ( ( warrant ) => {
26+ let warrants : CheckWarrant [ ] = check . warrants . map ( ( warrant ) => {
2927 return {
30- objectType : isWarrantObject ( warrant . object ) ? warrant . object . getObjectType ( ) : warrant . object . objectType ,
31- objectId : isWarrantObject ( warrant . object ) ? warrant . object . getObjectId ( ) : warrant . object . objectId ,
28+ object : warrant . object ,
3229 relation : warrant . relation ,
33- subject : isSubject ( warrant . subject ) ? warrant . subject : { objectType : warrant . subject . getObjectType ( ) , objectId : warrant . subject . getObjectId ( ) } ,
30+ subject : warrant . subject ,
3431 context : warrant . context
3532 }
3633 } )
3734 const accessCheckRequest : AccessCheckRequest = {
3835 op : check . op ,
3936 warrants : warrants ,
40- consistentRead : check . consistentRead ,
4137 debug : check . debug
4238 }
4339
@@ -54,7 +50,6 @@ export default class Authorization {
5450 relation : "member" ,
5551 subject : featureCheck . subject ,
5652 context : featureCheck . context ,
57- consistentRead : featureCheck . consistentRead ,
5853 debug : featureCheck . debug
5954 } )
6055 }
@@ -65,17 +60,20 @@ export default class Authorization {
6560 relation : "member" ,
6661 subject : permissionCheck . subject ,
6762 context : permissionCheck . context ,
68- consistentRead : permissionCheck . consistentRead ,
6963 debug : permissionCheck . debug
7064 } )
7165 }
7266
7367 // Private methods
7468 private static async authorize ( accessCheckRequest : AccessCheckRequest ) : Promise < boolean > {
7569 try {
70+
7671 const response = await WarrantClient . httpClient . post ( {
7772 url : "/v2/authorize" ,
78- data : accessCheckRequest ,
73+ data : {
74+ ...accessCheckRequest ,
75+ warrants : this . mapWarrantsForRequest ( accessCheckRequest . warrants ) ,
76+ } ,
7977 } ) ;
8078
8179 return response . code === 200 ;
@@ -84,21 +82,36 @@ export default class Authorization {
8482 }
8583 }
8684
87- private static async edgeAuthorize ( warrantCheck : AccessCheckRequest ) : Promise < boolean > {
85+ private static async edgeAuthorize ( accessCheckRequest : AccessCheckRequest ) : Promise < boolean > {
8886 try {
8987 const response = await WarrantClient . httpClient . post ( {
9088 baseUrl : WarrantClient . config . authorizeEndpoint ,
9189 url : "/v2/authorize" ,
92- data : warrantCheck ,
90+ data : {
91+ ...accessCheckRequest ,
92+ warrants : this . mapWarrantsForRequest ( accessCheckRequest . warrants ) ,
93+ } ,
9394 } ) ;
9495
9596 return response . code === 200 ;
9697 } catch ( e ) {
9798 if ( e . code === "cache_not_ready" ) {
98- return this . authorize ( warrantCheck ) ;
99+ return this . authorize ( accessCheckRequest ) ;
99100 }
100101
101102 throw e ;
102103 }
103104 }
105+
106+ private static mapWarrantsForRequest ( warrants : CheckWarrant [ ] ) : any [ ] {
107+ return warrants . map ( ( warrant ) => {
108+ return {
109+ objectType : isWarrantObject ( warrant . object ) ? warrant . object . getObjectType ( ) : warrant . object . objectType ,
110+ objectId : isWarrantObject ( warrant . object ) ? warrant . object . getObjectId ( ) : warrant . object . objectId ,
111+ relation : warrant . relation ,
112+ subject : isSubject ( warrant . subject ) ? warrant . subject : { objectType : warrant . subject . getObjectType ( ) , objectId : warrant . subject . getObjectId ( ) } ,
113+ context : warrant . context
114+ }
115+ } )
116+ }
104117}
0 commit comments