Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
MKCLOS
Core Development Platform
corepkgv2
Commits
b83dc523
Commit
b83dc523
authored
4 years ago
by
Akshay Bharambe
Browse files
Options
Downloads
Patches
Plain Diff
Add: Session checking on jwt decode
parent
d3fab005
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!210
Staging mepdeployment05072020
,
!200
Add: Session control
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
authmdl/jwtmdl/jwtmdl_fasthttp.go
+15
-0
authmdl/jwtmdl/jwtmdl_fasthttp.go
authmdl/jwtmdl/jwtmdl_http.go
+15
-0
authmdl/jwtmdl/jwtmdl_http.go
with
30 additions
and
0 deletions
authmdl/jwtmdl/jwtmdl_fasthttp.go
+
15
−
0
View file @
b83dc523
...
...
@@ -8,6 +8,7 @@ import (
"github.com/valyala/fasthttp"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/authmdl/sessionmdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
jwt
"github.com/dgrijalva/jwt-go"
...
...
@@ -15,6 +16,12 @@ import (
// DecodeTokenWithJWTKey decode token
func
DecodeTokenWithJWTKey
(
req
*
fasthttp
.
Request
,
jwtKey
string
)
(
jwt
.
MapClaims
,
error
)
{
// check for instance header.
if
sessionmdl
.
ValidateSession
&&
req
.
Header
.
Peek
(
sessionmdl
.
InstanceHeader
)
!=
sessionmdl
.
SessionInstance
{
return
nil
,
sessionmdl
.
ErrInvalidSessionInstance
}
tokenFromRequest
:=
string
(
req
.
Header
.
Peek
(
"Authorization"
))
tokenArray
:=
strings
.
Split
(
tokenFromRequest
,
"Bearer"
)
if
len
(
tokenArray
)
<=
1
{
...
...
@@ -42,6 +49,14 @@ func DecodeTokenWithJWTKey(req *fasthttp.Request, jwtKey string) (jwt.MapClaims,
return
nil
,
errormdl
.
Wrap
(
"Error while getting claims"
)
}
// validate user session from session id present in token
if
sessionmdl
.
ValidateSession
{
if
err
:=
sessionmdl
.
ValidateSessionFromToken
(
claims
);
err
!=
nil
{
loggermdl
.
LogError
(
"session validation failed with err:"
,
err
)
return
nil
,
sessionmdl
.
ErrSessionValidationFailed
}
}
return
claims
,
nil
}
...
...
This diff is collapsed.
Click to expand it.
authmdl/jwtmdl/jwtmdl_http.go
+
15
−
0
View file @
b83dc523
...
...
@@ -7,6 +7,7 @@ import (
"github.com/dgrijalva/jwt-go/request"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/authmdl/sessionmdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
jwt
"github.com/dgrijalva/jwt-go"
...
...
@@ -14,6 +15,12 @@ import (
// DecodeTokenWithJWTKey decode token
func
DecodeTokenWithJWTKey
(
req
*
http
.
Request
,
jwtKey
string
)
(
jwt
.
MapClaims
,
error
)
{
// check for instance header.
if
sessionmdl
.
ValidateSession
&&
req
.
Header
.
Get
(
sessionmdl
.
InstanceHeader
)
!=
sessionmdl
.
SessionInstance
{
return
nil
,
sessionmdl
.
ErrInvalidSessionInstance
}
token
,
err
:=
request
.
ParseFromRequest
(
req
,
request
.
OAuth2Extractor
,
func
(
token
*
jwt
.
Token
)
(
interface
{},
error
)
{
b
:=
([]
byte
(
jwtKey
))
return
b
,
nil
...
...
@@ -29,6 +36,14 @@ func DecodeTokenWithJWTKey(req *http.Request, jwtKey string) (jwt.MapClaims, err
return
nil
,
errormdl
.
Wrap
(
"Error while getting claims"
)
}
// validate user session from session id present in token
if
sessionmdl
.
ValidateSession
{
if
err
:=
sessionmdl
.
ValidateSessionFromToken
(
claims
);
err
!=
nil
{
loggermdl
.
LogError
(
"session validation failed with err:"
,
err
)
return
nil
,
sessionmdl
.
ErrSessionValidationFailed
}
}
return
claims
,
nil
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
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
Menu
Explore
Projects
Groups
Topics
Snippets