-
Notifications
You must be signed in to change notification settings - Fork 24
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
Showing
24 changed files
with
430 additions
and
63 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
// use `US.AUTH.<name>` prefix for most cases | ||
|
||
export const AUTH_ERRORS = { | ||
UNAUTHORIZED_ACCESS: 'US.AUTH.UNAUTHORIZED_ACCESS', | ||
}; |
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,7 @@ | ||
export enum UserRole { | ||
Editor = 'datalens.editor', | ||
Admin = 'datalens.admin', | ||
Viewer = 'datalens.viewer', | ||
Visitor = 'datalens.visitor', | ||
Creator = 'datalens.creator', | ||
} |
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,52 @@ | ||
import {NextFunction, Request, Response} from '@gravity-ui/expresskit'; | ||
import jwt, {type Algorithm} from 'jsonwebtoken'; | ||
|
||
import {AUTHORIZATION_HEADER, DL_AUTH_HEADER_KEY} from '../../../const/common'; | ||
import {AUTH_ERRORS} from '../constants/error-constants'; | ||
import type {AccessTokenPayload} from '../types/token'; | ||
|
||
const ALGORITHMS: Algorithm[] = ['RS256']; | ||
|
||
export const appAuth = async (req: Request, res: Response, next: NextFunction) => { | ||
req.ctx.log('AUTH'); | ||
|
||
const authorization = req.headers[AUTHORIZATION_HEADER]; | ||
|
||
if (authorization) { | ||
const accessToken = authorization.slice(DL_AUTH_HEADER_KEY.length + 1); | ||
|
||
if (accessToken) { | ||
try { | ||
req.ctx.log('CHECK_ACCESS_TOKEN'); | ||
|
||
const {userId, sessionId, roles} = jwt.verify( | ||
accessToken, | ||
req.ctx.config.authTokenPublicKey || '', | ||
{ | ||
algorithms: ALGORITHMS, | ||
}, | ||
) as AccessTokenPayload; | ||
|
||
req.originalContext.set('user', { | ||
userId, | ||
sessionId, | ||
accessToken, | ||
roles, | ||
}); | ||
|
||
// for ctx info | ||
res.locals.userId = userId; | ||
res.locals.login = userId; | ||
|
||
req.ctx.log('CHECK_ACCESS_TOKEN_SUCCESS'); | ||
|
||
next(); | ||
return; | ||
} catch (err) { | ||
req.ctx.logError('CHECK_ACCESS_TOKEN_ERROR', err); | ||
} | ||
} | ||
} | ||
|
||
res.status(401).send({code: AUTH_ERRORS.UNAUTHORIZED_ACCESS, message: 'Unauthorized access'}); | ||
}; |
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,12 @@ | ||
import type {UserRole} from '../constants/role'; | ||
|
||
export interface ExpirableTokenPayload { | ||
iat: number; | ||
exp: number; | ||
} | ||
|
||
export interface AccessTokenPayload extends ExpirableTokenPayload { | ||
userId: string; | ||
sessionId: string; | ||
roles: `${UserRole}`[]; | ||
} |
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,8 @@ | ||
import type {UserRole} from '../constants/role'; | ||
|
||
export interface CtxUser { | ||
userId: string; | ||
sessionId: string; | ||
accessToken: string; | ||
roles: `${UserRole}`[]; | ||
} |
Oops, something went wrong.