-
-
Notifications
You must be signed in to change notification settings - Fork 246
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: oidc-integration #385
Open
er-santosh
wants to merge
14
commits into
bigcapitalhq:develop
Choose a base branch
from
er-santosh:feat/oidc-integration
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
adbb328
feat: oidc-integration
er-santosh e90baac
revert: reverted sorted dependencies from server package json
er-santosh d76c8de
changes: added OIDC environment in production docker compose
er-santosh ebe6f05
feat: added introspection for oidc access token
er-santosh fbb7786
revert: reverted ar lang indentation
er-santosh 1b7dfa5
feat: verified oidc access token in jwt auth middleware
er-santosh 16323e9
feat: show loading overlay when user logs out
er-santosh be546b2
feat/get-tenant-of-authenticated-user
er-santosh 086c27a
Merge branch 'develop' into feat/oidc-integration
er-santosh d4ef1e5
Merge branch 'feat/oidc-integration' into feat/get-tenant-of-authenti…
er-santosh fcbe117
Merge branch 'develop' into feat/get-tenant-of-authenticated-user
er-santosh e598c49
fixed issue after merge
er-santosh eaac2e5
Merge pull request #1 from er-santosh/feat/get-tenant-of-authenticate…
er-santosh a18ff8e
Merge branch 'develop' into feat/oidc-integration
er-santosh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,101 @@ | ||
import BaseController from '@/api/controllers/BaseController'; | ||
import AttachCurrentTenantUser from '@/api/middleware/AttachCurrentTenantUser'; | ||
import TenancyMiddleware from '@/api/middleware/TenancyMiddleware'; | ||
import JWTAuth from '@/api/middleware/jwtAuth'; | ||
import { OidcService } from '@/services/Oidc'; | ||
import { NextFunction, Request, Response, Router } from 'express'; | ||
import { Inject, Service } from 'typedi'; | ||
@Service() | ||
export default class OidcController extends BaseController { | ||
@Inject() | ||
private oidcService: OidcService; | ||
|
||
/** | ||
* Router constructor method. | ||
*/ | ||
public router() { | ||
const router = Router(); | ||
|
||
router.post('/authorize', this.authorize); | ||
|
||
router.post('/login', this.oidcLogin); | ||
|
||
router.use(JWTAuth); | ||
router.use(AttachCurrentTenantUser); | ||
router.use(TenancyMiddleware); | ||
|
||
router.post('/logout', this.oidcLogout); | ||
|
||
return router; | ||
} | ||
|
||
/** | ||
* Authentication Oidc authorize. | ||
* @param {Request} req - | ||
* @param {Response} res - | ||
* @param {NextFunction} next - | ||
*/ | ||
private authorize = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
try { | ||
const authorizationUrl = this.oidcService.generateAuthorizationUrl(); | ||
|
||
res.json({ authorizationUrl }); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
/** | ||
* Authentication oidc login. | ||
* @param {Request} req - | ||
* @param {Response} res - | ||
* @param {NextFunction} next - | ||
*/ | ||
private oidcLogin = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
try { | ||
const code = req.body.code; | ||
|
||
const loginResponse = await this.oidcService.loginUser(code); | ||
|
||
return res.status(200).send(loginResponse); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
/** | ||
* Authentication oidc logout. | ||
* @param {Request} req - | ||
* @param {Response} res - | ||
* @param {NextFunction} next - | ||
*/ | ||
private oidcLogout = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
try { | ||
const { token } = req; | ||
|
||
const oidcIdToken = token.oidc_id_token; | ||
const oidcAccessToken = token.oidc_access_token; | ||
|
||
const logoutUrl = await this.oidcService.generateEndSessionUrl({ | ||
oidcIdToken, | ||
oidcAccessToken, | ||
}); | ||
|
||
return res.status(200).send({ logoutUrl }); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
} |
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,27 @@ | ||
import { oidcClient } from '@/lib/Oidc/OidcClient'; | ||
import { NextFunction, Request, Response } from 'express'; | ||
|
||
const oidcSessionMiddleware = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
const token = req.token; | ||
|
||
try { | ||
const oidcAccessToken = token.oidc_access_token; | ||
|
||
if (oidcAccessToken) { | ||
const oidcUser = await oidcClient.userinfo(oidcAccessToken); | ||
|
||
if (!oidcUser) { | ||
return res.boom.unauthorized(); | ||
} | ||
} | ||
|
||
return next(); | ||
} catch (error) { | ||
return next(error); | ||
} | ||
}; | ||
export default oidcSessionMiddleware; |
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,50 @@ | ||
import { | ||
AuthorizationParameters, | ||
ClientMetadata, | ||
GrantBody, | ||
IssuerMetadata, | ||
} from 'openid-client'; | ||
|
||
export const oidcConfig = { | ||
OIDC_CLIENT_ID: process.env.OIDC_CLIENT_ID, | ||
OIDC_CLIENT_SECRET: process.env.OIDC_CLIENT_SECRET, | ||
OIDC_ISSUER: process.env.OIDC_ISSUER, | ||
OIDC_AUTHORIZATION_ENDPOINT: process.env.OIDC_AUTHORIZATION_ENDPOINT, | ||
OIDC_TOKEN_ENDPOINT: process.env.OIDC_TOKEN_ENDPOINT, | ||
OIDC_USERINFO_ENDPOINT: process.env.OIDC_USERINFO_ENDPOINT, | ||
OIDC_ENDSESSION_ENDPOINT: process.env.OIDC_ENDSESSION_ENDPOINT, | ||
OIDC_REVOCATION_ENDPOINT: process.env.OIDC_REVOCATION_ENDPOINT, | ||
OIDC_REDIRECT_URI: process.env.OIDC_REDIRECT_URI, | ||
OIDC_SCOPE: process.env.OIDC_SCOPE, | ||
OIDC_JWK_URI: process.env.OIDC_JWK_URI, | ||
}; | ||
|
||
export const issuerMetadata: IssuerMetadata = { | ||
issuer: oidcConfig.OIDC_ISSUER, | ||
authorization_endpoint: oidcConfig.OIDC_AUTHORIZATION_ENDPOINT, | ||
token_endpoint: oidcConfig.OIDC_TOKEN_ENDPOINT, | ||
userinfo_endpoint: oidcConfig.OIDC_USERINFO_ENDPOINT, | ||
end_session_endpoint: oidcConfig.OIDC_ENDSESSION_ENDPOINT, | ||
revocation_endpoint: oidcConfig.OIDC_REVOCATION_ENDPOINT, | ||
jwks_uri: oidcConfig.OIDC_JWK_URI, | ||
}; | ||
|
||
export const clientMetadata: ClientMetadata = { | ||
client_id: oidcConfig.OIDC_CLIENT_ID, | ||
client_secret: oidcConfig.OIDC_CLIENT_SECRET, | ||
redirect_uris: [oidcConfig.OIDC_REDIRECT_URI], | ||
token_endpoint_auth_method: 'client_secret_basic', | ||
id_token_signed_response_alg: 'RS256', | ||
post_logout_redirect_uris: [oidcConfig.OIDC_REDIRECT_URI], | ||
response_types: ['code', 'token', 'id_token'], | ||
}; | ||
|
||
export const authorizationUrlParameters: AuthorizationParameters = { | ||
scope: oidcConfig.OIDC_SCOPE, | ||
response_type: 'code', | ||
}; | ||
|
||
export const tokenGrantBody: GrantBody = { | ||
grant_type: 'authorization_code', | ||
redirect_uri: oidcConfig.OIDC_REDIRECT_URI, | ||
}; |
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,6 @@ | ||
import { clientMetadata, issuerMetadata } from '@/config/oidcConfig'; | ||
import { Issuer } from 'openid-client'; | ||
|
||
const issuer = new Issuer(issuerMetadata); | ||
|
||
export const oidcClient = new issuer.Client(clientMetadata); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abouolia I couldnot build server due to this syntax error here. So i removed it to get it working.