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

feat(auth): Added username and password based basic authentication #286

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export default function UserAdminstration() {
])
},
total: (data: EmbeddedUsers) => data.page.totalElements,
headers: { Authorization: `Bearer ${status === 'authenticated' ? session.user.access_token : ''}` },
headers: { Authorization: `${status === 'authenticated' ? session.user.access_token : ''}` },
}

const advancedSearch = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default function VendorsList() {
])
},
total: (data: EmbeddedVendors) => data.page.totalElements,
headers: { Authorization: `Bearer ${status === 'authenticated' ? session.user.access_token : ''}` },
headers: { Authorization: `${status === 'authenticated' ? session.user.access_token : ''}` },
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function ComponentsTable({ setNumberOfComponent }: Props) {
])
},
total: (data: Embedded<Component, 'sw360:components'>) => data.page.totalElements,
headers: { Authorization: `Bearer ${session.user.access_token}` },
headers: { Authorization: `${session.user.access_token}` },
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/[locale]/ecc/components/ECC.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default function ECC() {
])
},
total: (data: EmbeddedECC) => data.page.totalElements,
headers: { Authorization: `Bearer ${status === 'authenticated' ? session.user.access_token : ''}` },
headers: { Authorization: `${status === 'authenticated' ? session.user.access_token : ''}` },
}

return (
Expand Down
2 changes: 1 addition & 1 deletion src/app/[locale]/licenses/components/LicensePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function LicensePage() {
])
},
total: (data: Embedded<LicensePayload, 'sw360:licenses'>) => data.page.totalElements,
headers: { Authorization: `Bearer ${status === 'authenticated' ? session.user.access_token : ''}` },
headers: { Authorization: `${status === 'authenticated' ? session.user.access_token : ''}` },
}

const columns = [
Expand Down
2 changes: 1 addition & 1 deletion src/app/[locale]/projects/components/Projects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ function Project() {
])
},
total: (data: EmbeddedProjects) => data.page.totalElements,
headers: { Authorization: `Bearer ${session.user.access_token}` },
headers: { Authorization: `${session.user.access_token}` },
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/[locale]/projects/detail/[id]/components/Ecc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default function EccDetails({ projectId }: { projectId: string }) {
])
},
total: (data: EmbeddedProjectReleaseEcc) => data.page.totalElements,
headers: { Authorization: `Bearer ${status === 'authenticated' ? session.user.access_token : ''}` },
headers: { Authorization: `${status === 'authenticated' ? session.user.access_token : ''}` },
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export default function LicenseObligation({ projectId }: { projectId: string })
return tableRows
},
total: (data: ProjectObligationsList) => data.page.totalElements,
headers: { Authorization: `Bearer ${session.user.access_token}` },
headers: { Authorization: `${session.user.access_token}` },
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export default function VulnerabilityTab({ projectData }: { projectData: Project
])
},
total: (data: EmbeddedProjectVulnerabilities) => data.page.totalElements,
headers: { Authorization: `Bearer ${status === 'authenticated' ? session.user.access_token : ''}` },
headers: { Authorization: `${status === 'authenticated' ? session.user.access_token : ''}` },
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default function VulnerabilityTrackingStatusComponent({ projectData }: {
])
},
total: (data: ProjectVulnerabilityTrackingStatus) => data.page.totalElements,
headers: { Authorization: `Bearer ${status === 'authenticated' ? session.user.access_token : ''}` },
headers: { Authorization: `${status === 'authenticated' ? session.user.access_token : ''}` },
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function Vulnerabilities() {
}
},
total: (data: EmbeddedVulnerabilities) => data.page.totalElements,
headers: { Authorization: `Bearer ${session.user.access_token}` },
headers: { Authorization: `${session.user.access_token}` },
}
}

Expand Down
29 changes: 28 additions & 1 deletion src/app/api/auth/[...nextauth]/authOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,34 @@ export const authOptions: NextAuthOptions = {
username: username,
password: password,
}

const authToken = await AuthService.generateBasicToken(userCredential)

if (authToken === null) throw new Error('Error while fetching Auth Token')

const response = await ApiUtils.GET(`users/${username}`, authToken)
if (response.status !== HttpStatus.OK) {
throw new Error('Error while fetching User Group')
}
const data = await response.json()
return { access_token: authToken, userGroup: data.userGroup, email: username} as any
} catch (e) {
console.error(e)
return null
}
},
}),
CredentialsProvider({
name: CREDENTIAL_PROVIDER,
credentials: {},
async authorize(credentials) {
try {
const { username, password } = credentials as any
const userCredential: UserCredentialInfo = {
username: username,
password: password,
}

const authToken = await AuthService.generateToken(userCredential)

if (authToken === null) throw new Error('Error while fetching Auth Token')
Expand Down Expand Up @@ -61,7 +89,6 @@ export const authOptions: NextAuthOptions = {
async session({ session, token }) {
// Send properties to the client, like an access_token from a provider.
session.user = token

return session
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function SelectAttachment({
method: 'POST',
body: formData,
headers: {
Authorization: `Bearer ${session.user.access_token}`,
Authorization: `${session.user.access_token}`,
},
})
.then((res) => res.json())
Expand Down
9 changes: 8 additions & 1 deletion src/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,15 @@ const generateToken = async (userData: UserCredentialInfo) => {
return sw360token
}

const generateBasicToken = async (userData: UserCredentialInfo) => {
const credentials: string = Buffer.from(`${userData.username}:${userData.password}`).toString('base64')
const sw360token = `Basic ${credentials}`
return sw360token
}

const AuthService = {
generateToken,
generateBasicToken,
}

export default AuthService
export default AuthService
2 changes: 1 addition & 1 deletion src/utils/api/api.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function send({
}

if (token) {
request_content.headers['Authorization'] = `Bearer ${token}`
request_content.headers['Authorization'] = `${token}`
}

if (signal) {
Expand Down
Loading