Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Basic Authentication (Issue #2) #10

Merged
merged 3 commits into from
Jun 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ It is automatically enabled if you have a `.jenkins` file in the root folder of

```json
{
"url": "http://127.0.0.1:8080/job/myproject"
"url": "http://127.0.0.1:8080/job/myproject",
"username": "jenkinsuser",
"password": "jenkinspassword"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should also specify that we can also use a token.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed; I'll make the change. Something like "jenkins_user" and "jenkins_password_or_token" seem reasonable?

}
```

Expand Down
12 changes: 9 additions & 3 deletions src/Jenkins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function getConnectionStatusName(status: ConnectionStatus): string {
export class Jenkins {


public getStatus(url: string) {
public getStatus(url: string, username: string, password: string) {

return new Promise<JenkinsStatus>((resolve, reject) => {

Expand All @@ -88,7 +88,12 @@ export class Jenkins {
let result: JenkinsStatus;

request
.get(url + '/api/json')
.get(url + '/api/json', {
'auth': {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should do more tests for this because I couldn't get this work that way. But It worked my way like a I wrote (see #3). It looks like it depends on the version of Jenkins people are using.
I'm gonna check this again to be sure

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forget this. It works also on my side (idk why it didn't work the first time). it should be ok on every version I guess. But I've you tried it on the v1.647 but secured (it could have been some changes in the api between v1 and v2) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@umens Thanks for checking this out.

I didn't test against 1.647 secured; that's a good idea. I will secure my 1.647 instance and test against it later this morning. I'll report back once I've done so.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I secured my 1.647 instance and tested. All scenarios perform as expected, whether providing a password or a token.

'user': username,
'pass': password
}
})
.on('response', function(response) {
statusCode = response.statusCode;
})
Expand All @@ -111,9 +116,10 @@ export class Jenkins {
resolve(result);
break;

case 401:
case 403:
result = {
jobName: 'AUTENTICATION NEEDED',
jobName: 'AUTHENTICATION NEEDED',
url: url,
status: BuildStatus.Disabled,
statusName: 'Disabled',
Expand Down
8 changes: 6 additions & 2 deletions src/JenkinsIndicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ export class JenkinsIndicator {


let url: string;
let user: string;
let pw: string;
let settings = JSON.parse(fs.readFileSync(path.join(vscode.workspace.rootPath, '.jenkins')).toString());
url = settings.url;

user = settings.username ? settings.username : "";
pw = settings.password ? settings.password : "";

// invalid URL
if (!url) {
this.statusBarItem.tooltip = 'No URL Defined';
Expand All @@ -40,7 +44,7 @@ export class JenkinsIndicator {
return;
}

jjj.getStatus(url)
jjj.getStatus(url, user, pw)
.then((status) => {

let icon: string;
Expand Down