Skip to content

Commit

Permalink
introduce PAT support (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
PacoVK authored Apr 18, 2024
1 parent b787f18 commit 5be4bc7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,17 @@ output:

### Authentication

The plugin uses the `CONFLUENCE_USERNAME` and `CONFLUENCE_PASSWORD` to authenticate with the Confluence API. You can set these values in the environment variables.
You can authenticate with the Confluence API using the following methods:

#### Personal Access Token (PAT)

The plugin uses `CONFLUENCE_PAT` to authenticate with the Confluence API via token. You can set this value in the environment variables.

#### Basic Authentication

The plugin uses `CONFLUENCE_USERNAME` and `CONFLUENCE_PASSWORD` to authenticate with the Confluence API via `basic auth`. You can set these values in the environment variables.

> Info: The order above is the priority. If you set both, the plugin will use the PAT.

### Disable this plugin at runtime

Expand Down
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[semantic version

### added

- [Add support for Confluence authorization via PAT #23](https://github.com/PacoVK/antora-confluence/issues/23)

### changed

- [Use node URL to construct API URL #19](https://github.com/PacoVK/antora-confluence/issues/19)
Expand Down
11 changes: 7 additions & 4 deletions lib/client/ConfluenceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export abstract class ConfluenceClient {
readonly API_V2_PATH;
readonly EDITOR_VERSION;
readonly BASE_URL;
readonly CREDENTIALS;
readonly AUTHORIZATION_HEADER;
readonly ANCESTOR_ID;

constructor(config: ConfluenceClientOptions) {
Expand All @@ -40,7 +40,7 @@ export abstract class ConfluenceClient {
const apiContext = this.constructApiContext(config.baseUrl);
this.API_V1_PATH = apiContext + this.API_V1_IDENTIFIER;
this.API_V2_PATH = apiContext + this.API_V2_IDENTIFIER;
this.CREDENTIALS = this.encodeCredentials();
this.AUTHORIZATION_HEADER = this.buildAuthHeader();
this.ANCESTOR_ID = config.ancestorId;
}

Expand Down Expand Up @@ -87,9 +87,12 @@ export abstract class ConfluenceClient {
return "";
}

encodeCredentials() {
buildAuthHeader() {
if (process.env.CONFLUENCE_PAT) {
return `Bearer ${process.env.CONFLUENCE_PAT}`;
}
const credentials = `${process.env.CONFLUENCE_USERNAME}:${process.env.CONFLUENCE_PASSWORD}`;
return Buffer.from(credentials).toString("base64");
return `Basic ${Buffer.from(credentials).toString("base64")}`;
}

public abstract createPage(
Expand Down
14 changes: 7 additions & 7 deletions lib/client/ConfluenceClientV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class ConfluenceClientV1 extends ConfluenceClient {
headers: {
"Content-Type": "application/json",
"Request-Content-Type": "application/json",
Authorization: `Basic ${this.CREDENTIALS}`,
Authorization: this.AUTHORIZATION_HEADER,
},
body: JSON.stringify({
type: "page",
Expand Down Expand Up @@ -76,7 +76,7 @@ export class ConfluenceClientV1 extends ConfluenceClient {
headers: {
"Content-Type": "application/json",
"Request-Content-Type": "application/json",
Authorization: `Basic ${this.CREDENTIALS}`,
Authorization: this.AUTHORIZATION_HEADER,
},
body: JSON.stringify({
type: "page",
Expand Down Expand Up @@ -125,7 +125,7 @@ export class ConfluenceClientV1 extends ConfluenceClient {
...formData.getHeaders(),
Accept: "application/json",
"X-Atlassian-Token": "nocheck",
Authorization: `Basic ${this.CREDENTIALS}`,
Authorization: this.AUTHORIZATION_HEADER,
},
});
}
Expand All @@ -144,7 +144,7 @@ export class ConfluenceClientV1 extends ConfluenceClient {
headers: {
"Content-Type": "application/json",
"Request-Content-Type": "application/json",
Authorization: `Basic ${this.CREDENTIALS}`,
Authorization: this.AUTHORIZATION_HEADER,
},
});
}
Expand All @@ -158,7 +158,7 @@ export class ConfluenceClientV1 extends ConfluenceClient {
headers: {
"Content-Type": "application/json",
"Request-Content-Type": "application/json",
Authorization: `Basic ${this.CREDENTIALS}`,
Authorization: this.AUTHORIZATION_HEADER,
},
});
}
Expand All @@ -173,7 +173,7 @@ export class ConfluenceClientV1 extends ConfluenceClient {
headers: {
"Content-Type": "application/json",
"Request-Content-Type": "application/json",
Authorization: `Basic ${this.CREDENTIALS}`,
Authorization: this.AUTHORIZATION_HEADER,
},
});
}
Expand All @@ -195,7 +195,7 @@ export class ConfluenceClientV1 extends ConfluenceClient {
...formData.getHeaders(),
Accept: "application/json",
"X-Atlassian-Token": "nocheck",
Authorization: `Basic ${this.CREDENTIALS}`,
Authorization: this.AUTHORIZATION_HEADER,
},
});
}
Expand Down

0 comments on commit 5be4bc7

Please sign in to comment.