Skip to content

Commit

Permalink
if token not authorized remove it
Browse files Browse the repository at this point in the history
  • Loading branch information
kayra1 committed Jul 22, 2024
1 parent dafa6b6 commit 981a891
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ui/src/app/auth/authContext.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use client"

import { createContext, useContext, useState, useEffect } from 'react';
import { Dispatch, SetStateAction } from 'react';
import { User } from '../types';
import { useCookies } from 'react-cookie';
import { jwtDecode } from 'jwt-decode';
Expand All @@ -24,6 +23,7 @@ export const AuthProvider = ({ children }: Readonly<{ children: React.ReactNode
let userObject = jwtDecode(cookies.user_token) as User
setUser(userObject);
} else {
setUser(null)
router.push('/login');
}
}, [cookies.user_token, router]);
Expand Down
7 changes: 6 additions & 1 deletion ui/src/app/certificate_requests/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ export default function CertificateRequests() {
queryFn: () => getCertificateRequests({ authToken: cookies.user_token })
})
if (query.status == "loading") { return <Loading /> }
if (query.status == "error") { return <Error msg={query.error.message} /> }
if (query.status == "error") {
if (query.error.message.includes("401")) {
removeCookie("user_token")
}
return <Error msg={query.error.message} />
}
const csrs = Array.from(query.data ? query.data : [])
return (
<CertificateRequestsTable csrs={csrs} />
Expand Down
21 changes: 11 additions & 10 deletions ui/src/app/queries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CSREntry } from "./types"
import { HTTPStatus } from "./utils"

export type RequiredParams = {
id: string
Expand All @@ -12,12 +13,12 @@ export async function getCertificateRequests(params: { authToken: string }): Pro
headers: { "Authorization": "Bearer " + params.authToken }
})
if (!response.ok) {
throw new Error('Network response was not ok')
throw new Error(`${response.status} ${HTTPStatus(response.status)}`)
}
return response.json()
}

export async function postCSR(params: {authToken: string, csr: string}) {
export async function postCSR(params: { authToken: string, csr: string }) {
if (!params.csr) {
throw new Error('CSR not provided')
}
Expand All @@ -30,7 +31,7 @@ export async function postCSR(params: {authToken: string, csr: string}) {
body: params.csr.trim()
})
if (!response.ok) {
throw new Error('Network response was not ok')
throw new Error(`${response.status} ${HTTPStatus(response.status)}`)
}
return response.json()
}
Expand All @@ -48,7 +49,7 @@ export async function postCertToID(params: RequiredParams) {
body: params.cert.trim()
})
if (!response.ok) {
throw new Error('Network response was not ok')
throw new Error(`${response.status} ${HTTPStatus(response.status)}`)
}
return response.json()
}
Expand All @@ -61,7 +62,7 @@ export async function deleteCSR(params: RequiredParams) {
}
})
if (!response.ok) {
throw new Error('Network response was not ok')
throw new Error(`${response.status} ${HTTPStatus(response.status)}`)
}
return response.json()
}
Expand All @@ -74,7 +75,7 @@ export async function rejectCSR(params: RequiredParams) {
}
})
if (!response.ok) {
throw new Error('Network response was not ok')
throw new Error(`${response.status} ${HTTPStatus(response.status)}`)
}
return response.json()
}
Expand All @@ -87,7 +88,7 @@ export async function revokeCertificate(params: RequiredParams) {
}
})
if (!response.ok) {
throw new Error('Network response was not ok')
throw new Error(`${response.status} ${HTTPStatus(response.status)}`)
}
return response.json()
}
Expand All @@ -97,9 +98,9 @@ export async function login(userForm: { username: string, password: string }) {
method: "POST",
body: JSON.stringify({ "username": userForm.username, "password": userForm.password })
})
const responseText = await response.text()
if (!response.ok) {
const responseText = await response.text()
throw new Error(responseText)
throw new Error(`${response.status} ${HTTPStatus(response.status)}. ${responseText}`)
}
return response.text()
return responseText
}
12 changes: 11 additions & 1 deletion ui/src/app/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CertificationRequest, Certificate, Extension, Extensions, GeneralName, GeneralNames } from "pkijs";
import { CertificationRequest, Certificate, Extensions } from "pkijs";
import { fromBER } from "asn1js";
import * as pvutils from "pvutils";

Expand Down Expand Up @@ -233,4 +233,14 @@ export const csrMatchesCertificate = (csrPemString: string, certPemString: strin
const csrPKbytes = csr.subjectPublicKeyInfo.subjectPublicKey.valueBeforeDecodeView
const certPKbytes = cert.subjectPublicKeyInfo.subjectPublicKey.valueBeforeDecodeView
return csrPKbytes.toString() == certPKbytes.toString()
}

export const HTTPStatus = (code: number): string => {
const map: { [key: number]: string } = {
401: "Unauthorized",
}
if (!(code in map)) {
throw new Error("code not recognized: " + code)
}
return map[code]
}

0 comments on commit 981a891

Please sign in to comment.