11"use strict" ;
2+ // Copyright 2015-2019 Palo Alto Networks, Inc
3+ //
4+ // Licensed under the Apache License, Version 2.0 (the "License");
5+ // you may not use this file except in compliance with the License.
6+ // You may obtain a copy of the License at
7+ // http://www.apache.org/licenses/LICENSE-2.0
8+ //
9+ // Unless required by applicable law or agreed to in writing, software
10+ // distributed under the License is distributed on an "AS IS" BASIS,
11+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ // See the License for the specific language governing permissions and
13+ // limitations under the License.
14+ Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
215/**
316 * Provides common resources for other modules in the pancloud SDK
417 */
5- Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
6- var logLevel ;
7- ( function ( logLevel ) {
8- logLevel [ logLevel [ "DEBUG" ] = 0 ] = "DEBUG" ;
9- logLevel [ logLevel [ "INFO" ] = 1 ] = "INFO" ;
10- logLevel [ logLevel [ "ALERT" ] = 2 ] = "ALERT" ;
11- logLevel [ logLevel [ "ERROR" ] = 3 ] = "ERROR" ;
12- } ) ( logLevel = exports . logLevel || ( exports . logLevel = { } ) ) ;
18+ const error_1 = require ( "./error" ) ;
19+ const crypto_1 = require ( "crypto" ) ;
20+ var LogLevel ;
21+ ( function ( LogLevel ) {
22+ LogLevel [ LogLevel [ "DEBUG" ] = 0 ] = "DEBUG" ;
23+ LogLevel [ LogLevel [ "INFO" ] = 1 ] = "INFO" ;
24+ LogLevel [ LogLevel [ "ALERT" ] = 2 ] = "ALERT" ;
25+ LogLevel [ LogLevel [ "ERROR" ] = 3 ] = "ERROR" ;
26+ } ) ( LogLevel = exports . LogLevel || ( exports . LogLevel = { } ) ) ;
1327const LTYPES = {
1428 "panw.auth" : "" ,
1529 "panw.config" : "" ,
@@ -34,14 +48,18 @@ const LTYPES = {
3448 "tms.threat" : "" ,
3549 "tms.traps" : ""
3650} ;
51+ exports . region2EntryPoint = {
52+ 'americas' : 'https://api.us.paloaltonetworks.com' ,
53+ 'europe' : 'https://api.eu.paloaltonetworks.com'
54+ } ;
3755function isKnownLogType ( t ) {
3856 return LTYPES . hasOwnProperty ( t ) ;
3957}
4058exports . isKnownLogType = isKnownLogType ;
4159/**
4260 * Centralized logging capability for the whole pancloud SDK
4361 */
44- class sdkLogger {
62+ class SdkLogger {
4563 /**
4664 *
4765 * @param level only messages with a level equal or avobe this provided value will be loogged
@@ -52,16 +70,16 @@ class sdkLogger {
5270 this . stackTrace = stackTrace ;
5371 }
5472 error ( e ) {
55- this . format ( e . getSourceClass ( ) , e . getErrorMessage ( ) , logLevel . ERROR , e . name , e . getErrorCode ( ) , undefined , e . stack ) ;
73+ this . format ( e . getSourceClass ( ) , e . getErrorMessage ( ) , LogLevel . ERROR , e . name , e . getErrorCode ( ) , undefined , e . stack ) ;
5674 }
5775 alert ( source , message , name ) {
58- this . format ( source . className , message , logLevel . ALERT , name ) ;
76+ this . format ( source . className , message , LogLevel . ALERT , name ) ;
5977 }
6078 info ( source , message , name ) {
61- this . format ( source . className , message , logLevel . INFO , name ) ;
79+ this . format ( source . className , message , LogLevel . INFO , name ) ;
6280 }
6381 debug ( source , message , name , payload ) {
64- this . format ( source . className , message , logLevel . DEBUG , name , undefined , payload ) ;
82+ this . format ( source . className , message , LogLevel . DEBUG , name , undefined , payload ) ;
6583 }
6684 format ( source , message , level , name , code , payload , stack ) {
6785 if ( level >= this . level ) {
@@ -98,12 +116,12 @@ class sdkLogger {
98116 finalOutput += ` payload=${ payloadOut } ` ;
99117 }
100118 switch ( level ) {
101- case logLevel . ERROR : {
119+ case LogLevel . ERROR : {
102120 console . error ( finalOutput ) ;
103121 break ;
104122 }
105- case logLevel . ALERT :
106- case logLevel . INFO : {
123+ case LogLevel . ALERT :
124+ case LogLevel . INFO : {
107125 console . info ( finalOutput ) ;
108126 break ;
109127 }
@@ -120,7 +138,7 @@ class sdkLogger {
120138/**
121139 * Instantiate a module-provided logger at load time
122140 */
123- exports . commonLogger = new sdkLogger ( logLevel . INFO , false ) ;
141+ exports . commonLogger = new SdkLogger ( LogLevel . INFO , false ) ;
124142/**
125143 * Developer might decide to change the loglevel of the logger object at runtime
126144 * @param newLevel the new log level
@@ -164,3 +182,26 @@ async function retrier(source, n = 3, delay = 100, handler, ...params) {
164182 throw ( lastError ) ? lastError : new Error ( 'reties exhausted' ) ;
165183}
166184exports . retrier = retrier ;
185+ function expTokenExtractor ( source , token ) {
186+ let parts = token . split ( '.' ) ;
187+ if ( parts . length != 3 ) {
188+ throw new error_1 . PanCloudError ( source , 'PARSER' , 'Not a valid JWT token format' ) ;
189+ }
190+ let expAttribute ;
191+ try {
192+ expAttribute = JSON . parse ( Buffer . from ( parts [ 1 ] , 'base64' ) . toString ( ) ) . exp ;
193+ }
194+ catch ( _a ) {
195+ throw new error_1 . PanCloudError ( source , 'PARSER' , 'Not a valid JWT token format' ) ;
196+ }
197+ if ( typeof expAttribute == 'number' ) {
198+ return expAttribute ;
199+ }
200+ throw new error_1 . PanCloudError ( source , 'PARSER' , 'JWT token does not have a valid "exp" field' ) ;
201+ }
202+ exports . expTokenExtractor = expTokenExtractor ;
203+ function uid ( ) {
204+ let data = `pancloud${ Date . now ( ) } nodejs` ;
205+ return crypto_1 . createHash ( 'sha1' ) . update ( data ) . digest ( 'base64' ) ;
206+ }
207+ exports . uid = uid ;
0 commit comments