@@ -7,18 +7,23 @@ export class Codefresh {
77 logger . info ( 'Codefresh class instance created.' ) ;
88 }
99
10- getCredentials ( ) {
10+ async getCredentials ( ) {
1111 logger . info ( 'Fetching Codefresh credentials...' ) ;
1212 const envToken = Deno . env . get ( 'CF_API_KEY' ) ;
1313 const envUrl = Deno . env . get ( 'CF_URL' ) ;
1414 let cf_creds : CodefreshCredentials | null = null ;
1515
1616 if ( envToken && envUrl ) {
1717 logger . info ( 'Using Codefresh credentials from environment variables.' ) ;
18+ const formattedUrl = envUrl . endsWith ( '/' ) ? envUrl . slice ( 0 , - 1 ) : envUrl ;
1819 cf_creds = {
1920 headers : { Authorization : envToken } ,
20- baseUrl : `${ envUrl } /api` ,
21+ baseUrl : `${ formattedUrl } /api` ,
2122 } ;
23+ const isValid = await this . validateCredentials ( cf_creds ) ;
24+ if ( ! isValid ) {
25+ return null ;
26+ }
2227 return cf_creds ;
2328 }
2429
@@ -36,17 +41,39 @@ export class Codefresh {
3641 headers : { Authorization : currentContext . token } ,
3742 baseUrl : `${ currentContext . url } /api` ,
3843 } ;
44+ const isValid = await this . validateCredentials ( cf_creds ) ;
45+ if ( ! isValid ) {
46+ return null ;
47+ }
3948 }
49+
4050 return cf_creds ;
4151 }
4252
53+ async validateCredentials ( cfCreds : CodefreshCredentials ) {
54+ logger . info ( 'Validating Codefresh credentials...' ) ;
55+ const tokenID = cfCreds . headers [ 'Authorization' ] . split ( '.' ) [ 0 ] ;
56+ const response = await fetch ( `${ cfCreds . baseUrl } /auth/key/${ tokenID } ` , {
57+ method : 'GET' ,
58+ headers : cfCreds . headers ,
59+ } ) ;
60+
61+ if ( ! response . ok ) {
62+ logger . error ( `Invalid Codefresh credentials. Status: ${ response . status } ` ) ;
63+ return false ;
64+ }
65+ logger . info ( 'Codefresh credentials are valid.' ) ;
66+ return true ;
67+ }
68+
4369 async getAccountRuntimes ( cfCreds : CodefreshCredentials ) {
4470 logger . info ( 'Fetching account runtimes...' ) ;
4571 const response = await fetch ( `${ cfCreds . baseUrl } /runtime-environments` , {
4672 method : 'GET' ,
4773 headers : cfCreds . headers ,
4874 } ) ;
4975 const runtimes = await response . json ( ) ;
76+ console . debug ( runtimes ) ;
5077 return runtimes ;
5178 }
5279
0 commit comments