11import Feature from "./Feature" ;
22import Permission from "./Permission" ;
3- import Check , { AccessCheckRequest , CheckMany , CheckWarrantRequest , 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 } ] ,
1716 debug : check . debug
@@ -24,12 +23,11 @@ export default class Authorization {
2423 }
2524
2625 public static async checkMany ( check : CheckMany ) : Promise < boolean > {
27- let warrants : CheckWarrantRequest [ ] = check . warrants . map ( ( warrant ) => {
26+ let warrants : CheckWarrant [ ] = check . warrants . map ( ( warrant ) => {
2827 return {
29- objectType : isWarrantObject ( warrant . object ) ? warrant . object . getObjectType ( ) : warrant . object . objectType ,
30- objectId : isWarrantObject ( warrant . object ) ? warrant . object . getObjectId ( ) : warrant . object . objectId ,
28+ object : warrant . object ,
3129 relation : warrant . relation ,
32- subject : isSubject ( warrant . subject ) ? warrant . subject : { objectType : warrant . subject . getObjectType ( ) , objectId : warrant . subject . getObjectId ( ) } ,
30+ subject : warrant . subject ,
3331 context : warrant . context
3432 }
3533 } )
@@ -69,9 +67,13 @@ export default class Authorization {
6967 // Private methods
7068 private static async authorize ( accessCheckRequest : AccessCheckRequest ) : Promise < boolean > {
7169 try {
70+
7271 const response = await WarrantClient . httpClient . post ( {
7372 url : "/v2/authorize" ,
74- data : accessCheckRequest ,
73+ data : {
74+ ...accessCheckRequest ,
75+ warrants : this . mapWarrantsForRequest ( accessCheckRequest . warrants ) ,
76+ } ,
7577 } ) ;
7678
7779 return response . code === 200 ;
@@ -80,21 +82,36 @@ export default class Authorization {
8082 }
8183 }
8284
83- private static async edgeAuthorize ( warrantCheck : AccessCheckRequest ) : Promise < boolean > {
85+ private static async edgeAuthorize ( accessCheckRequest : AccessCheckRequest ) : Promise < boolean > {
8486 try {
8587 const response = await WarrantClient . httpClient . post ( {
8688 baseUrl : WarrantClient . config . authorizeEndpoint ,
8789 url : "/v2/authorize" ,
88- data : warrantCheck ,
90+ data : {
91+ ...accessCheckRequest ,
92+ warrants : this . mapWarrantsForRequest ( accessCheckRequest . warrants ) ,
93+ } ,
8994 } ) ;
9095
9196 return response . code === 200 ;
9297 } catch ( e ) {
9398 if ( e . code === "cache_not_ready" ) {
94- return this . authorize ( warrantCheck ) ;
99+ return this . authorize ( accessCheckRequest ) ;
95100 }
96101
97102 throw e ;
98103 }
99104 }
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+ }
100117}
0 commit comments