-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_pat_format.js
120 lines (104 loc) · 4.03 KB
/
test_pat_format.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
require('dotenv').config();
const { ClarifaiStub, grpc } = require('clarifai-nodejs-grpc');
// Initialize the gRPC client
const stub = ClarifaiStub.grpc();
// Get all potential API keys from environment variables
const potentialKeys = {
'clarifai_api': process.env.clarifai_api,
'read_api': process.env.read_api,
'write_api': process.env.write_api,
'new_clarifai_key': process.env.new_clarifai_key
};
async function testKeyWithFormat(keyName, keyValue) {
if (!keyValue) {
console.log(`- Key ${keyName} not found in environment variables`);
return false;
}
// Try different formats
const formats = [
keyValue.trim(), // Raw key
`Key ${keyValue.trim()}`, // With 'Key' prefix
keyValue.trim().replace(/^Key /, '') // Remove 'Key' if it exists and use raw
];
for (const format of formats) {
console.log(`\nTesting ${keyName} with format: ${format.substring(0, 10)}...`);
const metadata = new grpc.Metadata();
metadata.set('authorization', format);
try {
const response = await new Promise((resolve, reject) => {
stub.ListApps(
{},
metadata,
(err, response) => {
if (err) {
console.error(`- Error:`, err.details || err.message || err);
reject(err);
} else {
resolve(response);
}
}
);
});
if (response.apps && response.apps.length > 0) {
const app = response.apps[0];
console.log('✓ Authentication successful!');
console.log(`- User ID: ${app.user_id}`);
console.log(`- App ID: ${app.id}`);
// Update .env file with the working key and IDs
const fs = require('fs');
const envContent = `CLARIFAI_PAT=${format}
USER_ID=${app.user_id}
APP_ID=${app.id}`;
fs.writeFileSync('.env', envContent);
console.log('✓ Environment variables updated with working key and IDs');
// Test a simple API call with the retrieved IDs
const testResponse = await new Promise((resolve, reject) => {
stub.ListModels(
{
user_app_id: {
user_id: app.user_id,
app_id: app.id
},
per_page: 1
},
metadata,
(err, response) => {
if (err) {
reject(err);
} else {
resolve(response);
}
}
);
});
console.log('✓ Test API call successful!');
console.log(`- Number of models found: ${testResponse.models ? testResponse.models.length : 0}`);
return true;
}
} catch (error) {
console.log(`- Authentication failed with this format`);
}
}
return false;
}
async function verifyAuthAndGetIds() {
let success = false;
for (const [keyName, keyValue] of Object.entries(potentialKeys)) {
try {
if (await testKeyWithFormat(keyName, keyValue)) {
success = true;
break;
}
} catch (error) {
console.log(`- Failed to test ${keyName}`);
}
}
if (!success) {
console.error('\nNo valid keys found in any format. A new Personal Access Token (PAT) may be required.');
console.log('PAT should be in one of these formats:');
console.log('1. Raw PAT value');
console.log('2. Key {PAT value}');
process.exit(1);
}
}
verifyAuthAndGetIds();