Skip to content

Commit

Permalink
using API token instead of old username and password combination
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpapst committed Nov 5, 2024
1 parent be201c8 commit 03ff824
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 29 deletions.
Binary file modified Kimai2.grandtotalplugin.zip
Binary file not shown.
12 changes: 2 additions & 10 deletions Kimai2.grandtotalplugin/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@
<key>type</key>
<string>string</string>
</dict>
<dict>
<key>label</key>
<string>Your Kimai username</string>
<key>name</key>
<string>username</string>
<key>type</key>
<string>string</string>
</dict>
<dict>
<key>label</key>
<string>Your Kimai API token</string>
Expand Down Expand Up @@ -58,9 +50,9 @@
</dict>
</array>
<key>copyright</key>
<string>© 2019-2020 Kevin Papst</string>
<string>© 2019-2024 Kevin Papst</string>
<key>CFBundleVersion</key>
<string>2.0</string>
<string>2.1</string>
<key>GlobalsInfo</key>
<string>Enter your Kimai URL, username and API token to allow access to your data.</string>
<key>GrandTotalMinimumVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion Kimai2.grandtotalplugin/de.lproj/Localizable.strings
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"Your Kimai API token" = "API Passwort";
"Your Kimai API token" = "API Token";
"Your Kimai URL" = "Kimai URL";
"Your Kimai username" = "Benutzername";
"More about Kimai" = "Mehr über Kimai erfahren";
Expand Down
2 changes: 1 addition & 1 deletion Kimai2.grandtotalplugin/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"Your Kimai API token" = "API Password";
"Your Kimai API token" = "API Token";
"Your Kimai URL" = "Kimai URL";
"Your Kimai username" = "Username";
"More about Kimai" = "More about Kimai";
Expand Down
2 changes: 1 addition & 1 deletion Kimai2.grandtotalplugin/hr.lproj/Localizable.strings
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"Your Kimai API token" = "API lozinka";
"Your Kimai API token" = "API token";
"Your Kimai URL" = "Kimai URL";
"Your Kimai username" = "Korisničko ime";
"More about Kimai" = "Više o Kimaiju";
Expand Down
29 changes: 14 additions & 15 deletions Kimai2.grandtotalplugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,25 @@
*/

// check for credentials first and show a proper message if missing
if (url === undefined || url === null || url === '' || username === undefined || username === null || username === '' || token === undefined || token === null || token === '') {
localize("Enter your Kimai URL, username and API token to allow access to your data.");
if (url === undefined || url === null || url === '' || token === undefined || token === null || token === '') {
localize("Enter your Kimai URL and API token to allow access to your data.");
} else {
// make sure the api base path is properly prefixed
var apiUrl = url.replace(new RegExp('\/$'), '') + '/api';
var version = kimaiValidateVersion(apiUrl, username, token);
var version = kimaiValidateVersion(apiUrl, token);

if (version !== true) {
// looks weird ;-) but this is how the embedded javascript engine works
version;
} else {
kimaiGetTimesheets(apiUrl, username, token);
kimaiGetTimesheets(apiUrl, token);
}
}

function kimaiGetApiJson(url, username, token)
function kimaiGetApiJson(url, token)
{
var header = {'X-AUTH-USER': username, 'X-AUTH-TOKEN': token};
var header = {'Authorization': 'Bearer ' + token};
var result = loadURL("GET", url, header);

// this should be improved
if (result.length === 0) {
return null;
Expand All @@ -57,9 +56,9 @@ function kimaiGetApiJson(url, username, token)
return JSON.parse(result);
}

function kimaiValidateVersion(url, username, token)
function kimaiValidateVersion(url, token)
{
var version = kimaiGetApiJson(url + '/version', username, token);
var version = kimaiGetApiJson(url + '/version', token);

if (version["grandtotal_error"] !== undefined) {
return localize(version["grandtotal_error"]);
Expand Down Expand Up @@ -90,39 +89,39 @@ function kimaiValidateVersion(url, username, token)
return true;
}

function kimaiGetTimesheets(url, username, token)
function kimaiGetTimesheets(url, token)
{
// load all entities for mapping
var customers = {};
var projects = {};
var activities = {};
var users = {};

var apiCustomers = kimaiGetApiJson(url + '/customers?visible=3', username, token);
var apiCustomers = kimaiGetApiJson(url + '/customers?visible=3', token);
if (apiCustomers["grandtotal_error"]) {
return apiCustomers["grandtotal_error"];
}
for (var customer of apiCustomers) {
customers[customer['id']] = customer;
}

var apiProjects = kimaiGetApiJson(url + '/projects?visible=3', username, token);
var apiProjects = kimaiGetApiJson(url + '/projects?visible=3', token);
if (apiProjects["grandtotal_error"]) {
return apiProjects["grandtotal_error"];
}
for (var project of apiProjects) {
projects[project['id']] = project;
}

var apiActivities = kimaiGetApiJson(url + '/activities?visible=3', username, token);
var apiActivities = kimaiGetApiJson(url + '/activities?visible=3', token);
if (apiActivities["grandtotal_error"]) {
return apiActivities["grandtotal_error"];
}
for (var activity of apiActivities) {
activities[activity['id']] = activity;
}

var apiUsers = kimaiGetApiJson(url + '/users?visible=3', username, token);
var apiUsers = kimaiGetApiJson(url + '/users?visible=3', token);
if (apiUsers["grandtotal_error"]) {
return apiUsers["grandtotal_error"];
}
Expand All @@ -141,7 +140,7 @@ function kimaiGetTimesheets(url, username, token)
if (loadExported !== null && loadExported !== undefined && loadExported !== true) {
timesUrl = timesUrl + '&exported=0';
}
var aItems = kimaiGetApiJson(timesUrl, username, token);
var aItems = kimaiGetApiJson(timesUrl, token);

for (var aEntry of aItems)
{
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Kevin Papst @ https://www.keleo.de
Copyright (c) Kevin Papst @ https://www.kimai.org

For more information visit https://github.com/Keleo/kimai2-grandtotal

Expand Down

0 comments on commit 03ff824

Please sign in to comment.