1+ import fetch from 'node-fetch' ;
2+
3+ // This interface represents AppFramework token data
4+ export interface appFrameworkTokens {
5+ accessToken ?: string , // access token
6+ refreshToken ?: string , // refresh token
7+ }
8+
9+ // constant URLs, can be overridden
10+
11+ const IDP_TOKEN_URL : string = 'https://api.paloaltonetworks.com/api/oauth2/RequestToken'
12+ const IDP_REVOKE_URL : string = 'https://api.paloaltonetworks.com/api/oauth2/RevokeToken'
13+ const IDP_BASE_URL : string = 'https://identity.paloaltonetworks.com/as/authorization.oauth2'
14+
15+
16+ export class Credentials {
17+ private access_token : string
18+ private refresh_token : string
19+ private client_id : string
20+ private client_secret : string
21+ private idp_token_url : string
22+ private code : string
23+ // TODO: region, instance_id, redirect_uri, scope, token_revoke_url, base_url, etc
24+
25+ constructor ( client_id : string , client_secret : string , refresh_token ?: string , code ?: string , idp_token_url ?: string ) {
26+ if ( ! refresh_token && ! code ) throw ( `PanCloudError() Invalid Credentials (code or refresh token missing)` )
27+ this . client_id = client_id
28+ this . client_secret = client_secret
29+ this . refresh_token = refresh_token || undefined
30+ this . idp_token_url = idp_token_url || IDP_TOKEN_URL
31+ this . code = code || undefined
32+ //console.log('this idp =', this.idp_token_url)
33+ }
34+
35+ public get_access_token ( ) : string {
36+ return this . access_token ;
37+ }
38+
39+ // version 3.0 with async/await
40+ public async fetch_tokens ( ) : Promise < appFrameworkTokens > {
41+ let res = await fetch ( this . idp_token_url , {
42+ method : 'POST' ,
43+ headers : {
44+ 'Content-Type' : 'application/json' ,
45+ 'Accept' : 'application/json'
46+ } ,
47+ body : JSON . stringify ( {
48+ "client_id" : this . client_id ,
49+ "client_secret" : this . client_secret ,
50+ "refresh_token" : this . refresh_token || undefined ,
51+ "code" : this . code || undefined
52+ } )
53+ } )
54+ // console.log('debug:', JSON.stringify({
55+ // "client_id": this.client_id,
56+ // "client_secret": this.client_secret,
57+ // "refresh_token": this.refresh_token || undefined,
58+ // "code": this.code || undefined
59+ // }))
60+ if ( res . ok !== true && res . size === 0 )
61+ throw ( `PanCloudError() ${ res . status } ${ res . statusText } ` )
62+
63+ try {
64+ let r_json = await res . json ( )
65+ if ( r_json . error || r_json . error_description )
66+ throw ( `PanCloudError(): ` + await res . text ( ) )
67+ let ret : appFrameworkTokens = {
68+ accessToken : r_json . access_token
69+ }
70+ this . access_token = r_json . access_token
71+
72+ if ( r_json . refresh_token ) { // a new refresh token is returned
73+ this . refresh_token = r_json . refresh_token
74+ ret . refreshToken = r_json . refresh_token
75+ }
76+ //ret.refreshToken = 'test_refresh_token' // TODO: remove this
77+ console . log ( 'Credentials(): Authorization token successfully retrieved' )
78+ return ret
79+ } catch ( exception ) {
80+ throw ( `PanCloudError() Invalid JSON: ${ exception } ` )
81+ }
82+ }
83+ } ;
0 commit comments