Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PB138 - Film Database Group Project
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Package Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tomáš Havlíček
PB138 - Film Database Group Project
Commits
9f764814
There was an error fetching the commit references. Please try again later.
Commit
9f764814
authored
2 years ago
by
Martin Korec
Browse files
Options
Downloads
Patches
Plain Diff
fix: implemented AuthorizationGuard
parent
9d6bc7d4
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
backend/src/authorization/authorization.guard.ts
+40
-3
40 additions, 3 deletions
backend/src/authorization/authorization.guard.ts
with
40 additions
and
3 deletions
backend/src/authorization/authorization.guard.ts
+
40
−
3
View file @
9f764814
import
{
CanActivate
,
ExecutionContext
,
Injectable
}
from
'
@nestjs/common
'
;
import
{
CanActivate
,
ExecutionContext
,
Injectable
,
UnauthorizedException
,
}
from
'
@nestjs/common
'
;
import
{
expressJwtSecret
}
from
'
jwks-rsa
'
;
import
{
promisify
}
from
'
util
'
;
import
{
ConfigService
}
from
'
@nestjs/config
'
;
@
Injectable
()
export
class
AuthorizationGuard
implements
CanActivate
{
canActivate
(
context
:
ExecutionContext
):
boolean
|
Promise
<
boolean
>
{
return
true
;
private
readonly
AUTH_AUDIENCE
:
string
;
private
readonly
AUTH_DOMAIN
:
string
;
constructor
(
private
configService
:
ConfigService
)
{
this
.
AUTH_AUDIENCE
=
this
.
configService
.
get
(
'
AUTH_AUDIENCE
'
);
this
.
AUTH_DOMAIN
=
this
.
configService
.
get
(
'
AUTH_DOMAIN
'
);
}
async
canActivate
(
context
:
ExecutionContext
):
Promise
<
boolean
>
{
const
{
expressjwt
:
jwt
}
=
require
(
"
express-jwt
"
);
const
req
=
context
.
getArgByIndex
(
0
);
const
res
=
context
.
getArgByIndex
(
1
);
const
checkJwt
=
promisify
(
jwt
({
secret
:
expressJwtSecret
({
cache
:
true
,
rateLimit
:
true
,
jwksRequestsPerMinute
:
5
,
jwksUri
:
`
${
this
.
AUTH_DOMAIN
}
.well-known/jwks.json`
,
}),
audience
:
this
.
AUTH_AUDIENCE
,
issuer
:
this
.
AUTH_DOMAIN
,
algorithms
:
[
'
RS256
'
],
}),
);
try
{
await
checkJwt
(
req
,
res
);
return
true
;
}
catch
(
error
)
{
throw
new
UnauthorizedException
(
error
);
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment