Skip to content

Commit

Permalink
feat: token & cookie auth
Browse files Browse the repository at this point in the history
  • Loading branch information
jersou committed Apr 13, 2022
1 parent 7f036ae commit 37e8fd1
Show file tree
Hide file tree
Showing 22 changed files with 81,477 additions and 22,592 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@
/bin
/tmp
/frontend/.eslintcache
/.idea/highlightedFiles.xml
/.idea/markdown.xml
/.idea/httpRequests/
/.idea/google-java-format.xml
/.idea/checkstyle-idea.xml
/frontend/.idea/checkstyle-idea.xml
/frontend/.idea/google-java-format.xml
1 change: 0 additions & 1 deletion .idea/Jira-Work-Logger.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,16 @@ git clone https://github.com/jersou/jira-work-logger.git
cd jira-work-logger/frontend
yarn install
yarn build
cd ../bundler/
deno run --unstable --allow-read --allow-write filesContentGenerator.ts
cd ..
deno run --unstable --allow-read --allow-write bundler/filesContentGenerator.ts
deno bundle --unstable backend/server.ts dist/server.js
deno run --unstable --allow-net --allow-run dist/server.js
```

# Make the binary file

```
deno compile --unstable --allow-net --allow-run --target x86_64-unknown-linux-gnu --output bin/Linux/Jira-Work-Logger --lite backend/server.ts --wait-and-close
deno compile --unstable --allow-net --allow-run --target x86_64-pc-windows-msvc --output bin/Windows/Jira-Work-Logger.exe --lite backend/server.ts --wait-and-close
deno compile --unstable --allow-net --allow-run --target x86_64-unknown-linux-gnu --output bin/Linux/Jira-Work-Logger backend/server.ts --wait-and-close
deno compile --unstable --allow-net --allow-run --target x86_64-pc-windows-msvc --output bin/Windows/Jira-Work-Logger.exe backend/server.ts --wait-and-close
```
26 changes: 24 additions & 2 deletions backend/jira.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ConfigData, ToLogElement } from "../frontend/src/types.ts";

// FIXME
const useCookies = true;

export type jiraApiOptions = {
method?: string;
body?: string;
Expand All @@ -10,11 +13,30 @@ export async function jiraApi(config: ConfigData, query: string, options?: jiraA
throw new Error("bad jira url configuration");
}
const headers = new Headers();
if (config.token) {
headers.append("Authorization", "Bearer " + config.token);
} else {
if (useCookies) {
const resp = await fetch(`${config.jiraUrl.replace(/\/$/, "")}/login.jsp`, {
method: "POST",
headers: { "content-type": "application/x-www-form-urlencoded" },
body: `os_username=${encodeURI(config.username)}&os_password=${encodeURI(
config.password
)}&os_destination=&user_role=&atl_token=&login=Log+In`,
redirect: "manual",
});
if (resp.headers.get("x-seraph-loginreason") !== "OK") {
throw new Error("auth KO");
}
headers.append("cookie", resp.headers.get("set-cookie") || "");
} else {
headers.append("Authorization", "Basic " + btoa(`${config.username}:${config.password}`));
}
}
if (options?.body) {
headers.append("Content-Type", "application/json");
}
const url = `${config.jiraUrl}/rest/api/2/${query}`;
const url = `${config.jiraUrl.replace(/\/$/, "")}/rest/api/2/${query}`;
const response = await fetch(url, { ...options, headers });
console.log(`%c[jiraApi] ${response.status} ${options?.method || "GET"} ${url}`, "color:green");
return await response.json();
Expand All @@ -24,7 +46,7 @@ export async function jiraJql(config: ConfigData, jql: string): Promise<string>
return await jiraApi(config, `search?fields=summary,worklog&maxResults=20&jql=${jql.replace(/\s+/g, " ")}`);
}

export async function myLastIssues(config: ConfigData): Promise<string> {
export function myLastIssues(config: ConfigData): Promise<string> {
const jql = `
(
assignee = currentUser()
Expand Down
4 changes: 2 additions & 2 deletions backend/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export function addJiraRoutes(router: Router) {
// sequence POSTs → otherwise Jira bugs on remaining and total logged (not updated)
for (const log of toLog) {
await logElement(config, log);
// sleep 200 ms
await new Promise((resolve) => setTimeout(resolve, 200));
// sleep 800 ms
await new Promise((resolve) => setTimeout(resolve, 800));
}

ctx.response.body = '{"status":"OK"}';
Expand Down
2 changes: 1 addition & 1 deletion backend/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const controller = new AbortController();

const oakApp = new Application();
oakApp.addEventListener("error", (evt) => console.log(evt.error));
oakApp.addEventListener("listen", async () => {
oakApp.addEventListener("listen", () => {
console.log(`Listening on: http://localhost:${httpPort}`);
opn(`http://localhost:${httpPort}`, { checkDenoPermission: true }).catch(() =>
console.log(
Expand Down
4 changes: 2 additions & 2 deletions backend/websocket.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { WebSocket, WebSocketServer, deferred } from "../deps.ts";
import { WebSocketClient, WebSocketServer, deferred } from "../deps.ts";

// on client: new WebSocket("ws://127.0.0.1:8001")
export function runWebsocketServerAndWaitClose() {
const exitDeferred = deferred<number>();
const wss = new WebSocketServer(8001);

wss.on("connection", (ws: WebSocket) => {
wss.on("connection", (ws: WebSocketClient) => {
console.log("[WebSocketServer] connection");
ws.on("close", () => {
exitDeferred.resolve();
Expand Down
Loading

0 comments on commit 37e8fd1

Please sign in to comment.