Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
P
PMRMS Notif
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ghitha Dinan
PMRMS Notif
Commits
e88e732d
Commit
e88e732d
authored
Jan 10, 2022
by
Ghitha Dinan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add send email
parent
050d737c
Pipeline
#406
canceled with stages
Changes
4
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
87 additions
and
29 deletions
+87
-29
UrlConstant.kt
...d/go/kemenag/madrasah/pmrms/notif/constant/UrlConstant.kt
+3
-0
EmailController.kt
...emenag/madrasah/pmrms/notif/controller/EmailController.kt
+2
-0
RequestHelpers.kt
...go/kemenag/madrasah/pmrms/notif/helpers/RequestHelpers.kt
+55
-0
TokenInterceptor.kt
...enag/madrasah/pmrms/notif/interceptor/TokenInterceptor.kt
+27
-29
No files found.
src/main/kotlin/id/go/kemenag/madrasah/pmrms/notif/constant/UrlConstant.kt
0 → 100644
View file @
e88e732d
package
id.go.kemenag.madrasah.pmrms.notif.constant
const
val
AUTH_URL
=
"http://localhost:8080"
src/main/kotlin/id/go/kemenag/madrasah/pmrms/notif/controller/EmailController.kt
View file @
e88e732d
...
...
@@ -3,6 +3,7 @@ package id.go.kemenag.madrasah.pmrms.notif.controller
import
id.go.kemenag.madrasah.pmrms.notif.model.request.EmailRequest
import
id.go.kemenag.madrasah.pmrms.notif.model.response.ReturnData
import
id.go.kemenag.madrasah.pmrms.notif.service.EmailService
import
io.swagger.annotations.Api
import
org.springframework.beans.factory.annotation.Autowired
import
org.springframework.http.ResponseEntity
import
org.springframework.web.bind.annotation.PostMapping
...
...
@@ -11,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestMapping
import
org.springframework.web.bind.annotation.RestController
import
javax.validation.Valid
@Api
(
tags
=
[
"Email"
],
description
=
"Email API"
)
@RestController
@RequestMapping
(
path
=
[
"email"
])
class
EmailController
{
...
...
src/main/kotlin/id/go/kemenag/madrasah/pmrms/notif/helpers/RequestHelpers.kt
0 → 100644
View file @
e88e732d
package
id.go.kemenag.madrasah.pmrms.notif.helpers
import
com.fasterxml.jackson.databind.DeserializationFeature
import
com.fasterxml.jackson.databind.ObjectMapper
import
id.go.kemenag.madrasah.pmrms.notif.constant.HEADER_STRING
import
id.go.kemenag.madrasah.pmrms.notif.model.response.ReturnData
import
kong.unirest.Unirest
import
org.apache.http.client.HttpClient
import
org.apache.http.conn.ssl.NoopHostnameVerifier
import
org.apache.http.conn.ssl.TrustSelfSignedStrategy
import
org.apache.http.impl.client.HttpClients
import
org.apache.http.ssl.SSLContextBuilder
import
java.security.cert.X509Certificate
import
javax.net.ssl.SSLContext
class
RequestHelpers
{
companion
object
{
private
fun
configHttpClient
()
{
val
sslContext
:
SSLContext
=
SSLContextBuilder
().
loadTrustMaterial
(
null
,
object
:
TrustSelfSignedStrategy
()
{
override
fun
isTrusted
(
chain
:
Array
<
X509Certificate
?>?,
authType
:
String
?):
Boolean
{
return
true
}
}
).
build
()
val
customHttpClient
:
HttpClient
=
HttpClients
.
custom
().
setSSLContext
(
sslContext
)
.
setSSLHostnameVerifier
(
NoopHostnameVerifier
()).
build
()
Unirest
.
config
().
httpClient
(
customHttpClient
)
}
fun
authDetail
(
authUrl
:
String
,
bearer
:
String
):
ReturnData
?
{
return
try
{
configHttpClient
()
val
objectMapper
=
ObjectMapper
()
objectMapper
.
configure
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
,
false
)
val
reqUrl
=
"$authUrl/detail"
val
response
=
Unirest
.
get
(
reqUrl
)
.
header
(
"Content-Type"
,
"application/json"
)
.
header
(
HEADER_STRING
,
bearer
)
.
asString
()
objectMapper
.
readValue
(
response
.
body
,
ReturnData
::
class
.
java
)
}
catch
(
e
:
Exception
)
{
e
.
printStackTrace
()
null
}
}
}
}
src/main/kotlin/id/go/kemenag/madrasah/pmrms/notif/interceptor/TokenInterceptor.kt
View file @
e88e732d
package
id.go.kemenag.madrasah.pmrms.notif.interceptor
import
com.auth0.jwt.JWT
import
com.auth0.jwt.algorithms.Algorithm
import
com.auth0.jwt.exceptions.JWTCreationException
import
com.auth0.jwt.exceptions.JWTDecodeException
import
com.auth0.jwt.exceptions.JWTVerificationException
import
com.fasterxml.jackson.databind.DeserializationFeature
import
com.fasterxml.jackson.databind.ObjectMapper
import
id.go.kemenag.madrasah.pmrms.notif.model.response.ReturnData
import
id.go.kemenag.madrasah.pmrms.notif.constant.*
import
id.go.kemenag.madrasah.pmrms.notif.helpers.RequestHelpers
import
id.go.kemenag.madrasah.pmrms.notif.model.response.ReturnData
import
id.go.kemenag.madrasah.pmrms.notif.pojo.Users
import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import
org.springframework.security.core.context.SecurityContextHolder
import
org.springframework.web.util.ContentCachingRequestWrapper
...
...
@@ -33,7 +34,6 @@ class TokenInterceptor : Filter {
return
}
var
authentication
:
UsernamePasswordAuthenticationToken
?
=
null
var
successAuth
=
true
...
...
@@ -82,14 +82,25 @@ class TokenInterceptor : Filter {
JWTVerificationException
::
class
,
IllegalArgumentException
::
class
)
private
fun
getAuthentication
(
request
:
HttpServletRequest
):
UsernamePasswordAuthenticationToken
{
try
{
val
token
=
request
.
getHeader
(
HEADER_STRING
)
// parse jwt token from request and validate it internal authentication
val
jwt
=
JWT
.
require
(
Algorithm
.
HMAC512
(
SECRET
.
toByteArray
())).
build
().
verify
(
token
.
replace
(
TOKEN_PREFIX
,
""
))
return
if
(
jwt
.
subject
!=
null
)
{
// return principal username
val
audience
=
jwt
.
audience
?.
get
(
0
)
val
reqAuthDetail
:
ReturnData
=
RequestHelpers
.
authDetail
(
AUTH_URL
,
token
)
?:
throw
Exception
(
"User $VALIDATOR_MSG_NOT_FOUND"
)
reqAuthDetail
.
success
?.
let
{
if
(!
it
)
{
throw
Exception
(
reqAuthDetail
.
message
)
}
}
val
objectMapper
=
ObjectMapper
()
objectMapper
.
configure
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
,
false
)
val
users
:
Users
=
objectMapper
.
readValue
(
objectMapper
.
writeValueAsString
(
reqAuthDetail
.
data
),
Users
::
class
.
java
)
val
audience
=
"user-admin"
var
isAllowed
=
false
AUDIENCE_FILTER_PATH
[
audience
]
?.
forEach
lambda
@
{
...
...
@@ -106,31 +117,18 @@ class TokenInterceptor : Filter {
}
}
if
(
isAllowed
)
{
/*val objectMapper = ObjectMapper()
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
val users: Users = objectMapper.readValue(jwt.subject, Users::class.java)
val servletContext = request.session.servletContext
val webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext)
val repoUser = webApplicationContext!!.getBean(UserRepository::class.java)
val checkUser = repoUser.findByIdAndActive(users.id)
if (checkUser.isPresent) {
return
if
(
isAllowed
)
{
UsernamePasswordAuthenticationToken
(
objectMapper
.writeValueAsString(users),
ObjectMapper
()
.
writeValueAsString
(
users
),
audience
,
ArrayList
()
arrayListOf
()
)
} else {
throw Exception("User $VALIDATOR_MSG_NOT_FOUND")
}*/
throw
Exception
(
"User $VALIDATOR_MSG_NOT_FOUND"
)
}
else
{
throw
Exception
(
"User $VALIDATOR_MSG_NOT_HAVE_ACCESS"
)
}
}
else
{
throw
Exception
(
"Token $VALIDATOR_MSG_NOT_VALID"
)
}
catch
(
e
:
Exception
)
{
e
.
printStackTrace
()
throw
e
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment