Skip to content

Commit

Permalink
Legacy Login for Linux Users
Browse files Browse the repository at this point in the history
  • Loading branch information
DeeJayTC committed Aug 30, 2019
1 parent 282d457 commit cefdd58
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Change Log
##
- Fixed a login issue for Linux users
- Potential fix for -> [Issue: 2](https://github.com/Teamwork/vscode-projects/issues/2)
## 1.0.15
- Added a direct link to the task when clicking on task title in preview card
- fixed a bug in time log preview
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ This extension contributes the following settings:
* `twp.showUnAssigned` : Show tasks assigned to "anyone" (default yes)
* `twp.enabletimeTracking` : Enable logging time and view entries on tasks (default yes)

## Linux users:
* When you're using the add-in on Linux you need to use the old APIKey Login.
* You get your APIKey from your user Profile in Teamwork Projects.

## Notes:
* Mac users -> When the extension isn't working after logging in, just restart VSCode
Expand Down
4 changes: 3 additions & 1 deletion src/model/teamworkAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ export class TeamworkAccount{
public userEmail: string;
public token: string;
public rootUrl: string;
public useApiKey: boolean;


constructor(installationId: number, userId: number, userName: string, userEmail: string, token: string, root: string) {
constructor(installationId: number, userId: number, userName: string, userEmail: string, token: string, root: string, useApiKey: boolean = false) {
this.userId = userId;
this.installationId = installationId;
this.userName = userName;
this.userEmail = userEmail;
this.token = token;
this.rootUrl = root;
this.useApiKey = useApiKey;
}


Expand Down
26 changes: 20 additions & 6 deletions src/teamworkProjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,26 +423,40 @@ export class TeamworkProjects{

public async SelectAccount() : Promise<Boolean>{

// We use a custom protocol for login vscode://xxx this does not work for Linux users
// Linux users get the old fashioned username+password login
var isLinux = process.platform === "linux";
if ( !isLinux ){
vscode.env.openExternal(vscode.Uri.parse('https://www.teamwork.com/launchpad/login?state=VSCODE&redirect_uri=vscode://teamwork.twp/loginData'));
}else{
const result = await vscode.window.showInputBox({
const url = await vscode.window.showInputBox({
value: '',
valueSelection: [2, 4],
placeHolder: 'https://<yourdomain>.teamwork.com',
validateInput: text => {
return Utilities.IsValidUrl(text) ? text : null;
return Utilities.IsValidUrl(text) ? undefined : text;
}
});

if( !isNullOrUndefined(url)){
const ApiKey = await vscode.window.showInputBox({
value: '',
placeHolder: '<Your Legacy API Key'},);

this.FinishLogin(this.context,"",true,url,ApiKey);
}
}

return true;
}

public async FinishLogin(context: vscode.ExtensionContext, code: string, isLinux: boolean = false) : Promise<TeamworkAccount>{
var userData = await this.API.getLoginData(context,code);
public async FinishLogin(context: vscode.ExtensionContext, code: string, isLinux: boolean = false, root: string = "", apikey: string = "") : Promise<TeamworkAccount>{

var userData: TeamworkAccount;
if( !isLinux ){
userData = await this.API.getLoginData(context,code);
}
else{
userData = await this.API.getLoginDataLegacy(context,root,apikey);
}

await context.workspaceState.update("twp.data.activeAccount", undefined);
await context.workspaceState.update("twp.data.activeAccount", userData);
Expand Down
75 changes: 72 additions & 3 deletions src/teamworkProjectsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,25 @@ export class TeamworkProjectsApi{
}
}

if(userData.useApiKey){
this.axios.defaults.headers.common = {
'User-Agent': `tw-vscode (${process.platform} | ${vscode.extensions.getExtension('teamwork.twp').packageJSON.version})`
};
this.axios.auth = {
username: token,
password: 'xxxxxxxxxxxxx'
};
this.axios.defaults.auth = {
username: token,
password: 'xxxxxxxxxxxxx'
};
}else{
this.axios.defaults.headers.common = {
'User-Agent': `tw-vscode (${process.platform} | ${vscode.extensions.getExtension('teamwork.twp').packageJSON.version})`,
'Authorization': `Bearer ${token}`};
}


this.axios.defaults.headers.common = {
'User-Agent': `tw-vscode (${process.platform} | ${vscode.extensions.getExtension('teamwork.twp').packageJSON.version})`,
'Authorization': `Bearer ${token}`};

this.isConfigured = true;
}
Expand Down Expand Up @@ -469,6 +484,60 @@ export class TeamworkProjectsApi{
return user;
}

public async getLoginDataLegacy(context: vscode.ExtensionContext, root: string, apiKey: string): Promise<TeamworkAccount> {

var loginaxios = require("axios");

let url = root + '/account.json';

let json = await loginaxios({
method:'get',
url,
auth: {
username: apiKey,
password: 'xxxxxxxxxxxxx'
}
})
.catch(function (error) {
console.log(error);
vscode.window.showErrorMessage("Getting details failed, are you sure your APIKey is correct?");
return null;
});

let loginData = json["data"];

let user = new TeamworkAccount(
loginData.account.id,
0,
"",
"",
apiKey,
root,true);
url = root + '/me.json',
json = await loginaxios({
method:'get',
url,
auth: {
username: apiKey,
password: 'xxxxxxxxxxxxx'
}
})
.catch(function (error) {
console.log(error);
vscode.window.showErrorMessage("Getting details failed, are you sure your APIKey is correct?");
return null;
});

let meData = json["data"];

user.userId = meData.person.id;
user.userName = meData.person["user-name"]



return user;
}


public async timeConvert(n) : Promise<string> {
var num = n;
Expand Down
5 changes: 4 additions & 1 deletion src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
import * as URL from 'url';
import { isNullOrUndefined } from 'util';

export class Utilities{

Expand All @@ -21,7 +22,9 @@ export class Utilities{

public static IsValidUrl(url: string) : boolean {
try {
URL.parse(url);
if(url.length < 3) { return false };
var urlparsed = URL.parse(url);
if( isNullOrUndefined(urlparsed.hostname)) { return false; }
return true;
} catch (err) {
return false;
Expand Down

0 comments on commit cefdd58

Please sign in to comment.