Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
ApiCommons
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Coscine
backend
libraries
ApiCommons
Commits
1f5467be
Commit
1f5467be
authored
3 years ago
by
Petar Hristov
Browse files
Options
Downloads
Patches
Plain Diff
Fix: Authorization error
parent
d946f3c0
Branches
Sprint/2021-08
No related tags found
1 merge request
!65
Sprint/2021 08
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/ApiCommons/Middleware/LoggingMiddleware.cs
+25
-17
25 additions, 17 deletions
src/ApiCommons/Middleware/LoggingMiddleware.cs
with
25 additions
and
17 deletions
src/ApiCommons/Middleware/LoggingMiddleware.cs
+
25
−
17
View file @
1f5467be
...
...
@@ -23,6 +23,7 @@ namespace Coscine.ApiCommons.Middleware
public
async
Task
Invoke
(
HttpContext
context
)
{
bool
_authorized
=
false
;
if
(
context
.
Request
.
Path
.
Value
.
Contains
(
"TOS"
))
{
await
_next
(
context
);
...
...
@@ -61,25 +62,33 @@ namespace Coscine.ApiCommons.Middleware
CoscineLoggerMetadata
.
SetUri
(
_uri
);
// Get the User Id
var
authorization
=
context
.
Request
.
Headers
[
"Authorization"
].
ToArray
();
string
bearer
=
null
;
foreach
(
var
line
in
authorization
)
try
{
if
(
line
.
Contains
(
"Bearer"
))
var
authorization
=
context
.
Request
.
Headers
[
"Authorization"
].
ToArray
();
string
bearer
=
null
;
foreach
(
var
line
in
authorization
)
{
bearer
=
line
;
if
(
line
.
Contains
(
"Bearer"
))
{
bearer
=
line
;
}
}
}
if
(!
string
.
IsNullOrWhiteSpace
(
bearer
))
{
bearer
=
bearer
.
Replace
(
"Bearer"
,
""
).
Trim
();
JWTHandler
jwtHandler
=
new
JWTHandler
(
new
ConsulConfiguration
());
var
claims
=
jwtHandler
.
GetContents
(
bearer
);
var
userId
=
Authenticator
.
GetUserId
(
claims
);
if
(
userId
!=
null
)
if
(!
string
.
IsNullOrWhiteSpace
(
bearer
))
{
CoscineLoggerMetadata
.
SetUserId
(
userId
);
bearer
=
bearer
.
Replace
(
"Bearer"
,
""
).
Trim
();
JWTHandler
jwtHandler
=
new
JWTHandler
(
new
ConsulConfiguration
());
var
claims
=
jwtHandler
.
GetContents
(
bearer
);
var
userId
=
Authenticator
.
GetUserId
(
claims
);
if
(
userId
!=
null
)
{
CoscineLoggerMetadata
.
SetUserId
(
userId
);
}
}
_authorized
=
true
;
}
catch
(
Exception
ex
)
{
_authorized
=
false
;
}
// Get the corrolation Id
...
...
@@ -182,11 +191,10 @@ namespace Coscine.ApiCommons.Middleware
{
byte
[]
data
;
if
(
true
)
// ex.GetType() == typeof(...
)
if
(
!
_authorized
)
{
context
.
Response
.
StatusCode
=
StatusCodes
.
Status401Unauthorized
;
data
=
System
.
Text
.
Encoding
.
UTF8
.
GetBytes
(
$"
{
ex
.
GetType
()}
"
);
//byte[] data = System.Text.Encoding.UTF8.GetBytes("Invalid authentication. Please try again.");
data
=
System
.
Text
.
Encoding
.
UTF8
.
GetBytes
(
"Invalid authentication. Please try again."
);
}
else
{
...
...
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