-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1531adb
commit 1eabd0d
Showing
28 changed files
with
641 additions
and
95 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from "./login"; | ||
export * from "./loginCallback"; | ||
export * from "./logout"; | ||
export * from "./logoutCallback"; | ||
export * from "./token"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { Request, Response } from "express"; | ||
import { getLoginURL } from "@bcgov/citz-imb-sso-js-core"; | ||
import type { SSOEnvironment, SSOProtocol } from "@bcgov/citz-imb-sso-js-core"; | ||
import { ENV } from "src/config"; | ||
import { errorWrapper } from "@bcgov/citz-imb-express-utilities"; | ||
|
||
const { SSO_ENVIRONMENT, SSO_REALM, SSO_PROTOCOL, SSO_CLIENT_ID, BACKEND_URL } = | ||
ENV; | ||
|
||
export const login = errorWrapper(async (req: Request, res: Response) => { | ||
try { | ||
const redirectURL = getLoginURL({ | ||
idpHint: "idir", | ||
clientID: SSO_CLIENT_ID, | ||
redirectURI: `${BACKEND_URL}/auth/login/callback`, | ||
ssoEnvironment: SSO_ENVIRONMENT as SSOEnvironment, | ||
ssoRealm: SSO_REALM, | ||
ssoProtocol: SSO_PROTOCOL as SSOProtocol, | ||
}); | ||
|
||
// Redirect the user to the SSO login page | ||
res.redirect(redirectURL); | ||
} catch (error) { | ||
res.status(500).json({ | ||
success: false, | ||
error: | ||
error instanceof Error | ||
? error.message | ||
: "An unknown error occurred during login.", | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { Request, Response } from "express"; | ||
import { getTokens } from "@bcgov/citz-imb-sso-js-core"; | ||
import type { SSOEnvironment, SSOProtocol } from "@bcgov/citz-imb-sso-js-core"; | ||
import { ENV } from "src/config"; | ||
import { errorWrapper } from "@bcgov/citz-imb-express-utilities"; | ||
|
||
const { SSO_ENVIRONMENT, SSO_REALM, SSO_PROTOCOL, SSO_CLIENT_ID, SSO_CLIENT_SECRET, BACKEND_URL } = | ||
ENV; | ||
|
||
export const loginCallback = errorWrapper(async (req: Request, res: Response) => { | ||
try { | ||
const { code } = req.query; | ||
|
||
const tokens = await getTokens({ | ||
code: code as string, | ||
clientID: SSO_CLIENT_ID, | ||
clientSecret: SSO_CLIENT_SECRET, | ||
redirectURI: `${BACKEND_URL}/auth/login/callback`, | ||
ssoEnvironment: SSO_ENVIRONMENT as SSOEnvironment, | ||
ssoRealm: SSO_REALM, | ||
ssoProtocol: SSO_PROTOCOL as SSOProtocol, | ||
}); | ||
|
||
// Sets tokens | ||
res | ||
.cookie("refresh_token", tokens.refresh_token, { | ||
httpOnly: true, | ||
secure: true, | ||
sameSite: "none", | ||
}) | ||
.cookie("access_token", tokens.access_token, { | ||
secure: true, | ||
sameSite: "none", | ||
}) | ||
.cookie("id_token", tokens.id_token, { | ||
secure: true, | ||
sameSite: "none", | ||
}) | ||
.cookie("expires_in", tokens.expires_in, { | ||
secure: true, | ||
sameSite: "none", | ||
}) | ||
.cookie("refresh_expires_in", tokens.refresh_expires_in, { | ||
secure: true, | ||
sameSite: "none", | ||
}) | ||
.status(200) | ||
.json(tokens); | ||
} catch (error) { | ||
res.status(500).json({ | ||
success: false, | ||
error: | ||
error instanceof Error ? error.message : "An unknown error occurred during login callback.", | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { Request, Response } from "express"; | ||
import { getLogoutURL } from "@bcgov/citz-imb-sso-js-core"; | ||
import type { SSOEnvironment, SSOProtocol } from "@bcgov/citz-imb-sso-js-core"; | ||
import { ENV } from "src/config"; | ||
import { errorWrapper } from "@bcgov/citz-imb-express-utilities"; | ||
|
||
const { SSO_ENVIRONMENT, SSO_REALM, SSO_PROTOCOL, BACKEND_URL } = ENV; | ||
|
||
export const logout = errorWrapper(async (req: Request, res: Response) => { | ||
try { | ||
const { id_token } = req.query; | ||
|
||
const redirectURL = getLogoutURL({ | ||
idToken: id_token as string, | ||
postLogoutRedirectURI: `${BACKEND_URL}/auth/logout/callback`, | ||
ssoEnvironment: SSO_ENVIRONMENT as SSOEnvironment, | ||
ssoProtocol: SSO_PROTOCOL as SSOProtocol, | ||
ssoRealm: SSO_REALM, | ||
}); | ||
|
||
res.redirect(redirectURL); | ||
} catch (error) { | ||
res.status(500).json({ | ||
success: false, | ||
error: | ||
error instanceof Error | ||
? error.message | ||
: "An unknown error occurred during logout.", | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { errorWrapper } from "@bcgov/citz-imb-express-utilities"; | ||
import type { Request, Response } from "express"; | ||
|
||
// This endpoint is only needed because SSO needs to redirect somewhere | ||
// and the desktop application does not have a URL to redirect to. | ||
export const logoutCallback = errorWrapper( | ||
async (req: Request, res: Response) => { | ||
try { | ||
res.status(204).send("Logged out."); | ||
} catch (error) { | ||
res.status(500).json({ | ||
success: false, | ||
error: | ||
error instanceof Error | ||
? error.message | ||
: "An unknown error occurred during logout.", | ||
}); | ||
} | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { Request, Response } from "express"; | ||
import { getNewTokens } from "@bcgov/citz-imb-sso-js-core"; | ||
import type { SSOEnvironment, SSOProtocol } from "@bcgov/citz-imb-sso-js-core"; | ||
import { ENV } from "src/config"; | ||
import { errorWrapper } from "@bcgov/citz-imb-express-utilities"; | ||
|
||
const { SSO_ENVIRONMENT, SSO_REALM, SSO_PROTOCOL, SSO_CLIENT_ID, SSO_CLIENT_SECRET } = ENV; | ||
|
||
export const token = errorWrapper(async (req: Request, res: Response) => { | ||
try { | ||
const refresh_token = req.cookies.refresh_token; | ||
|
||
if (!refresh_token) | ||
return res.status(401).json({ | ||
success: false, | ||
message: "Refresh token is missing. Please log in again.", | ||
}); | ||
|
||
const tokens = await getNewTokens({ | ||
refreshToken: refresh_token as string, | ||
clientID: SSO_CLIENT_ID, | ||
clientSecret: SSO_CLIENT_SECRET, | ||
ssoEnvironment: SSO_ENVIRONMENT as SSOEnvironment, | ||
ssoRealm: SSO_REALM, | ||
ssoProtocol: SSO_PROTOCOL as SSOProtocol, | ||
}); | ||
|
||
if (!tokens) return res.status(401).json({ success: false, message: "Invalid token." }); | ||
|
||
// Set token | ||
res | ||
.cookie("access_token", tokens.access_token, { | ||
secure: true, | ||
sameSite: "none", | ||
}) | ||
.status(200) | ||
.json(tokens); | ||
} catch (error) { | ||
res.status(500).json({ | ||
success: false, | ||
error: | ||
error instanceof Error | ||
? error.message | ||
: "An unknown error occurred while refreshing tokens.", | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as router } from "./router"; |
Oops, something went wrong.