Skip to content

Commit 0891454

Browse files
authored
API Key Validation (#47)
* add API token validation
1 parent 5c9b9e8 commit 0891454

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/commands/onprem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export async function onpremCMD(namespace?: string) {
77
const k8s = new K8s();
88
const utils = new Utils();
99

10-
const cfCreds = cf.getCredentials();
10+
const cfCreds = await cf.getCredentials();
1111

1212
if (cfCreds && cfCreds.baseUrl === 'https://g.codefresh.io/api') {
1313
console.error(

src/commands/pipelines.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export async function pipelinesCMD(namespace?: string, runtime?: string) {
66
const cf = new Codefresh();
77
const k8s = new K8s();
88
const utils = new Utils();
9-
const cfCreds = cf.getCredentials();
9+
const cfCreds = await cf.getCredentials();
1010

1111
if (!namespace) {
1212
logger.info('No namespace provided, prompting user to select one.');

src/logic/codefresh.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)