1+ const Customer = require ( '../models/Customer' ) ;
2+ const ProtectedApp = require ( '../models/ProtectedApp' ) ;
3+ const Decoy = require ( '../models/Decoy-data' ) ;
4+
5+ const authorizationFromPa_id = async ( req , res , next ) => {
6+ const token = req . headers [ 'authorization' ] ?. split ( ' ' ) [ 1 ] ;
7+ if ( ! token ) return res . status ( 401 ) . json ( { code : 401 , type : 'error' , message : 'Unauthorized' } ) ;
8+
9+ const customer_id = await extractCustomersFromToken ( token ) ;
10+ if ( ! customer_id ) return res . status ( 401 ) . json ( { code : 401 , type : 'error' , message : 'Invalid authorization token' } ) ;
11+
12+ const pa_id = req . params . pa_id || ( req . body && req . body . pa_id ) ;
13+ if ( ! pa_id ) return res . status ( 400 ) . json ( { code : 400 , type : 'error' , message : 'Protected app ID is missing' } ) ;
14+
15+ const protectedApp = await ProtectedApp . findOne ( { where : { id : pa_id } , include : [ { model : Customer , as : 'customer' } ] } ) ;
16+ if ( ! protectedApp ) return res . status ( 404 ) . json ( { code : 404 , type : 'error' , message : 'Protected app not found' } ) ;
17+ if ( ! customer_id == protectedApp . customer . id ) return res . status ( 403 ) . json ( { code : 403 , type : 'error' , message : 'Forbidden' } ) ;
18+ next ( ) ;
19+ }
20+ const authorizationFromCu_id = async ( req , res , next ) => {
21+ const token = req . headers [ 'authorization' ] ?. split ( ' ' ) [ 1 ] ;
22+ if ( ! token ) return res . status ( 401 ) . json ( { code : 401 , type : 'error' , message : 'Unauthorized' } ) ;
23+
24+ const customer_id = await extractCustomersFromToken ( token ) ;
25+ if ( ! customer_id ) return res . status ( 401 ) . json ( { code : 401 , type : 'error' , message : 'Invalid authorization token' } ) ;
26+ req . cu_id = customer_id ;
27+ next ( ) ;
28+ }
29+ const authorizationFromDecoyId = async ( req , res , next ) => {
30+ const token = req . headers [ 'authorization' ] ?. split ( ' ' ) [ 1 ] ;
31+ if ( ! token ) return res . status ( 401 ) . json ( { code : 401 , type : 'error' , message : 'Unauthorized' } ) ;
32+
33+ const customer_id = await extractCustomersFromToken ( token ) ;
34+ if ( ! customer_id ) return res . status ( 401 ) . json ( { code : 401 , type : 'error' , message : 'Invalid authorization token' } ) ;
35+
36+ const decoyId = req . params . id || ( req . body && req . body . id ) ;
37+ if ( ! decoyId ) return res . status ( 400 ) . json ( { code : 400 , type : 'error' , message : 'Decoy ID is missing' } ) ;
38+
39+ const decoy = await Decoy . findOne ( { where : { id : decoyId } , include : [ { model : ProtectedApp , as : 'protectedApp' } ] } ) ;
40+ if ( ! decoy ) return res . status ( 404 ) . json ( { code : 404 , type : 'error' , message : 'Decoy not found' } ) ;
41+
42+ if ( ! customer_id == decoy . protectedApp . cu_id ) return res . status ( 403 ) . json ( { code : 403 , type : 'error' , message : 'Forbidden' } ) ;
43+ next ( ) ;
44+ }
45+
46+ const extractCustomersFromToken = async ( token ) => {
47+ const parts = token . split ( '.' ) ;
48+ if ( parts . length !== 3 ) return null ;
49+
50+ const payload = Buffer . from ( parts [ 1 ] , 'base64' ) . toString ( 'utf8' ) ;
51+ const data = JSON . parse ( payload ) ;
52+
53+ if ( ! data || ! data . groups ) return null ;
54+
55+ const customer = await Customer . findOne ( { where : { name : data . groups } , attributes : [ 'id' ] } ) ;
56+ if ( ! customer ) return null ;
57+ return customer . id ;
58+ }
59+
60+ module . exports = {
61+ authorizationFromPa_id,
62+ authorizationFromCu_id,
63+ authorizationFromDecoyId
64+ } ;
0 commit comments