Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
P
PMRMS Auth
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 Auth
Commits
992e824b
Commit
992e824b
authored
Jan 11, 2022
by
Ghitha Dinan
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'PMRMS-123' into 'master'
Pmrms 123 See merge request
!5
parents
65c9aade
c9198993
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
275 additions
and
19 deletions
+275
-19
SpringWebConfig.kt
.../go/kemenag/madrasah/pmrms/auth/config/SpringWebConfig.kt
+18
-0
SwaggerConfig.kt
...id/go/kemenag/madrasah/pmrms/auth/config/SwaggerConfig.kt
+67
-0
FileConstant.kt
...d/go/kemenag/madrasah/pmrms/auth/constant/FileConstant.kt
+8
-0
SecurityConstant.kt
.../kemenag/madrasah/pmrms/auth/constant/SecurityConstant.kt
+1
-1
AuthController.kt
.../kemenag/madrasah/pmrms/auth/controller/AuthController.kt
+9
-5
CustomExceptionHandler.kt
.../madrasah/pmrms/auth/controller/CustomExceptionHandler.kt
+1
-1
Helpers.kt
...tlin/id/go/kemenag/madrasah/pmrms/auth/helpers/Helpers.kt
+42
-0
Role.kt
...ain/kotlin/id/go/kemenag/madrasah/pmrms/auth/pojo/Role.kt
+35
-0
Users.kt
...in/kotlin/id/go/kemenag/madrasah/pmrms/auth/pojo/Users.kt
+6
-4
UsersRole.kt
...otlin/id/go/kemenag/madrasah/pmrms/auth/pojo/UsersRole.kt
+36
-0
AuthService.kt
.../id/go/kemenag/madrasah/pmrms/auth/service/AuthService.kt
+15
-0
application.properties
src/main/resources/application.properties
+7
-8
AuthServiceTest.kt
...tlin/id/go/kemenag/madrasah/pmrms/auth/AuthServiceTest.kt
+30
-0
No files found.
src/main/kotlin/id/go/kemenag/madrasah/pmrms/auth/config/SpringWebConfig.kt
View file @
992e824b
...
...
@@ -6,6 +6,8 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean
import
org.springframework.context.annotation.Bean
import
org.springframework.context.annotation.Configuration
import
org.springframework.web.servlet.config.annotation.CorsRegistry
import
org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import
org.springframework.web.servlet.config.annotation.ViewControllerRegistry
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
...
...
@@ -35,4 +37,20 @@ class SpringWebConfig : WebMvcConfigurer {
registrationBean
.
order
=
1
return
registrationBean
}
override
fun
addViewControllers
(
registry
:
ViewControllerRegistry
)
{
registry
.
addRedirectViewController
(
"/v2/api-docs"
,
"/v2/api-docs"
)
registry
.
addRedirectViewController
(
"/swagger-resources/configuration/ui"
,
"/swagger-resources/configuration/ui"
)
registry
.
addRedirectViewController
(
"/swagger-resources/configuration/security"
,
"/swagger-resources/configuration/security"
)
registry
.
addRedirectViewController
(
"/swagger-resources"
,
"/swagger-resources"
)
}
override
fun
addResourceHandlers
(
registry
:
ResourceHandlerRegistry
)
{
registry
.
addResourceHandler
(
"/swagger-ui.html**"
)
.
addResourceLocations
(
"classpath:/META-INF/resources/swagger-ui.html"
)
registry
.
addResourceHandler
(
"/webjars/**"
).
addResourceLocations
(
"classpath:/META-INF/resources/webjars/"
)
}
}
src/main/kotlin/id/go/kemenag/madrasah/pmrms/auth/config/SwaggerConfig.kt
0 → 100644
View file @
992e824b
package
id.go.kemenag.madrasah.pmrms.auth.config
import
org.springframework.beans.factory.annotation.Value
import
org.springframework.context.annotation.Bean
import
org.springframework.context.annotation.Configuration
import
springfox.documentation.builders.PathSelectors
import
springfox.documentation.builders.RequestHandlerSelectors
import
springfox.documentation.service.*
import
springfox.documentation.spi.DocumentationType
import
springfox.documentation.spi.service.contexts.SecurityContext
import
springfox.documentation.spring.web.plugins.Docket
import
springfox.documentation.swagger2.annotations.EnableSwagger2
import
java.util.*
@Suppress
(
"UNCHECKED_CAST"
,
"DEPRECATION"
)
@Configuration
@EnableSwagger2
class
SwaggerConfig
{
@Value
(
"\${swagger.host}"
)
private
val
host
:
String
?
=
null
@Value
(
"\${swagger.protocol}"
)
private
val
protocol
:
String
?
=
null
@Bean
fun
api
():
Docket
?
{
return
Docket
(
DocumentationType
.
SWAGGER_2
)
.
host
(
host
)
.
select
()
.
apis
(
RequestHandlerSelectors
.
basePackage
(
"id.go.kemenag.madrasah.pmrms.auth.controller"
))
.
paths
(
PathSelectors
.
any
())
.
build
()
.
useDefaultResponseMessages
(
false
)
.
apiInfo
(
apiInfo
())
.
protocols
(
setOf
(
protocol
))
.
securitySchemes
(
listOf
(
apiKey
())
as
List
<
SecurityScheme
>?)
.
securityContexts
(
Collections
.
singletonList
(
securityContext
()))
}
private
fun
apiInfo
():
ApiInfo
{
return
ApiInfo
(
"PMRMS API - Auth"
,
"Auth Service for Application PMRMS"
,
"1.0"
,
"Terms of service"
,
Contact
(
"Application PMRMS"
,
"-"
,
"-"
),
""
,
""
,
Collections
.
emptyList
()
)
}
private
fun
apiKey
():
ApiKey
{
return
ApiKey
(
"Bearer"
,
"Authorization"
,
"header"
)
}
private
fun
securityContext
():
SecurityContext
?
{
return
SecurityContext
.
builder
().
securityReferences
(
defaultAuth
()).
forPaths
(
PathSelectors
.
regex
(
"/.*"
)).
build
()
}
private
fun
defaultAuth
():
List
<
SecurityReference
?>
{
return
listOf
(
SecurityReference
(
"Bearer"
,
arrayOfNulls
(
0
)))
}
}
src/main/kotlin/id/go/kemenag/madrasah/pmrms/auth/constant/FileConstant.kt
0 → 100644
View file @
992e824b
package
id.go.kemenag.madrasah.pmrms.auth.constant
import
java.io.File
const
val
ROOT_DIR
=
"D:\\app-files\\pmrms\\files\\"
val
UPLOAD_DIR
=
"${ROOT_DIR}uploads${File.separator}"
val
UPLOAD_USER_DIR
=
"${UPLOAD_DIR}users"
src/main/kotlin/id/go/kemenag/madrasah/pmrms/auth/constant/SecurityConstant.kt
View file @
992e824b
...
...
@@ -14,5 +14,5 @@ val USER_ADMIN_ALLOWED_PATH =
)
val
AUDIENCE_FILTER_PATH
=
mapOf
(
"admin"
to
USER_ADMIN_ALLOWED_PATH
"
user-
admin"
to
USER_ADMIN_ALLOWED_PATH
)
src/main/kotlin/id/go/kemenag/madrasah/pmrms/auth/contoller/AuthController.kt
→
src/main/kotlin/id/go/kemenag/madrasah/pmrms/auth/cont
r
oller/AuthController.kt
View file @
992e824b
package
id.go.kemenag.madrasah.pmrms.auth.contoller
package
id.go.kemenag.madrasah.pmrms.auth.cont
r
oller
import
id.go.kemenag.madrasah.pmrms.auth.model.request.auth.LoginRequest
import
id.go.kemenag.madrasah.pmrms.auth.model.response.ReturnData
import
id.go.kemenag.madrasah.pmrms.auth.service.AuthService
import
io.swagger.annotations.Api
import
org.springframework.beans.factory.annotation.Autowired
import
org.springframework.http.ResponseEntity
import
org.springframework.web.bind.annotation.PostMapping
import
org.springframework.web.bind.annotation.RequestBody
import
org.springframework.web.bind.annotation.RequestMapping
import
org.springframework.web.bind.annotation.RestController
import
org.springframework.web.bind.annotation.*
import
javax.validation.Valid
@Api
(
tags
=
[
"Auth"
],
description
=
"Auth API"
)
@RestController
@RequestMapping
(
path
=
[
"auth"
])
class
AuthController
{
...
...
@@ -22,4 +21,9 @@ class AuthController {
fun
login
(
@Valid
@RequestBody
request
:
LoginRequest
):
ResponseEntity
<
ReturnData
>
{
return
service
.
login
(
request
)
}
@GetMapping
(
value
=
[
"detail"
],
produces
=
[
"application/json"
])
fun
detail
():
ResponseEntity
<
ReturnData
>
{
return
service
.
detail
()
}
}
src/main/kotlin/id/go/kemenag/madrasah/pmrms/auth/contoller/CustomExceptionHandler.kt
→
src/main/kotlin/id/go/kemenag/madrasah/pmrms/auth/cont
r
oller/CustomExceptionHandler.kt
View file @
992e824b
package
id.go.kemenag.madrasah.pmrms.auth.contoller
package
id.go.kemenag.madrasah.pmrms.auth.cont
r
oller
import
id.go.kemenag.madrasah.pmrms.auth.exception.BaseException
import
id.go.kemenag.madrasah.pmrms.auth.model.response.ErrorMessage
...
...
src/main/kotlin/id/go/kemenag/madrasah/pmrms/auth/helpers/Helpers.kt
0 → 100644
View file @
992e824b
package
id.go.kemenag.madrasah.pmrms.auth.helpers
import
com.fasterxml.jackson.databind.DeserializationFeature
import
com.fasterxml.jackson.databind.ObjectMapper
import
id.go.kemenag.madrasah.pmrms.auth.pojo.Users
import
org.springframework.security.core.context.SecurityContextHolder
import
java.net.MalformedURLException
import
java.net.URL
import
javax.servlet.http.HttpServletRequest
fun
getUserLogin
():
Users
?
{
return
try
{
val
principal
=
SecurityContextHolder
.
getContext
().
authentication
.
principal
as
Any
val
objectMapper
=
ObjectMapper
()
objectMapper
.
configure
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
,
false
)
objectMapper
.
readValue
(
principal
.
toString
(),
Users
::
class
.
java
)
}
catch
(
e
:
Exception
)
{
null
}
}
@Throws
(
MalformedURLException
::
class
)
fun
getBaseUrl
(
request
:
HttpServletRequest
):
String
{
try
{
val
requestURL
=
URL
(
request
.
requestURL
.
toString
())
val
port
=
if
(
requestURL
.
port
==
-
1
)
""
else
":"
+
requestURL
.
port
return
requestURL
.
protocol
+
"://"
+
requestURL
.
host
+
port
+
request
.
contextPath
+
"/"
}
catch
(
e
:
Exception
)
{
throw
e
}
}
fun
getFullUrl
(
request
:
HttpServletRequest
):
String
{
val
requestURL
=
StringBuilder
(
request
.
requestURL
.
toString
())
val
queryString
=
request
.
queryString
return
if
(
queryString
==
null
)
{
requestURL
.
toString
()
}
else
{
requestURL
.
append
(
'?'
).
append
(
queryString
).
toString
()
}
}
src/main/kotlin/id/go/kemenag/madrasah/pmrms/auth/pojo/Role.kt
0 → 100644
View file @
992e824b
package
id.go.kemenag.madrasah.pmrms.auth.pojo
import
com.fasterxml.jackson.annotation.JsonFormat
import
com.fasterxml.jackson.annotation.JsonIgnore
import
id.go.kemenag.madrasah.pmrms.auth.constant.VALIDATOR_MSG_REQUIRED
import
java.util.*
import
javax.persistence.Column
import
javax.persistence.Entity
import
javax.persistence.Id
import
javax.persistence.Table
import
javax.validation.constraints.NotEmpty
@Entity
@Table
(
name
=
"role"
,
schema
=
"auth"
)
data class
Role
(
@Id
@Column
(
name
=
"id"
)
var
id
:
String
?
=
UUID
.
randomUUID
().
toString
(),
@Column
(
name
=
"name"
)
@field
:
NotEmpty
(
message
=
"Nama $VALIDATOR_MSG_REQUIRED"
)
var
name
:
String
?
=
null
,
@Column
(
name
=
"created_at"
)
@get
:
JsonFormat
(
shape
=
JsonFormat
.
Shape
.
STRING
,
pattern
=
"dd-MM-yyyy HH:mm:ss"
,
timezone
=
"GMT+7"
)
var
createdAt
:
Date
?
=
Date
(),
@Column
(
name
=
"updated_at"
)
@get
:
JsonFormat
(
shape
=
JsonFormat
.
Shape
.
STRING
,
pattern
=
"dd-MM-yyyy HH:mm:ss"
,
timezone
=
"GMT+7"
)
var
updatedAt
:
Date
?
=
Date
(),
@Column
(
name
=
"active"
)
@JsonIgnore
var
active
:
Boolean
?
=
true
)
src/main/kotlin/id/go/kemenag/madrasah/pmrms/auth/pojo/Users.kt
View file @
992e824b
...
...
@@ -2,11 +2,9 @@ package id.go.kemenag.madrasah.pmrms.auth.pojo
import
com.fasterxml.jackson.annotation.JsonFormat
import
com.fasterxml.jackson.annotation.JsonIgnore
import
org.hibernate.annotations.Where
import
java.util.*
import
javax.persistence.Column
import
javax.persistence.Entity
import
javax.persistence.Id
import
javax.persistence.Table
import
javax.persistence.*
@Entity
@Table
(
name
=
"users"
,
schema
=
"auth"
)
...
...
@@ -28,6 +26,10 @@ data class Users(
@Column
(
name
=
"last_name"
)
var
lastName
:
String
?
=
null
,
@OneToMany
(
mappedBy
=
"userId"
)
@Where
(
clause
=
"active = true"
)
var
roles
:
MutableSet
<
UsersRole
>?
=
null
,
@Column
(
name
=
"created_at"
)
@get
:
JsonFormat
(
shape
=
JsonFormat
.
Shape
.
STRING
,
pattern
=
"dd-MM-yyyy HH:mm:ss"
,
timezone
=
"GMT+7"
)
var
createdAt
:
Date
?
=
Date
(),
...
...
src/main/kotlin/id/go/kemenag/madrasah/pmrms/auth/pojo/UsersRole.kt
0 → 100644
View file @
992e824b
package
id.go.kemenag.madrasah.pmrms.auth.pojo
import
com.fasterxml.jackson.annotation.JsonFormat
import
com.fasterxml.jackson.annotation.JsonIgnore
import
java.util.*
import
javax.persistence.*
@Entity
@Table
(
name
=
"users_role"
,
schema
=
"auth"
)
data class
UsersRole
(
@Id
@Column
(
name
=
"id"
)
var
id
:
String
?
=
UUID
.
randomUUID
().
toString
(),
@Column
(
name
=
"user_id"
)
var
userId
:
String
?
=
null
,
@Column
(
name
=
"role_id"
)
var
roleId
:
String
?
=
null
,
@ManyToOne
@JoinColumn
(
name
=
"role_id"
,
insertable
=
false
,
updatable
=
false
,
nullable
=
true
)
var
role
:
Role
?
=
null
,
@Column
(
name
=
"created_at"
)
@get
:
JsonFormat
(
shape
=
JsonFormat
.
Shape
.
STRING
,
pattern
=
"dd-MM-yyyy HH:mm:ss"
,
timezone
=
"GMT+7"
)
var
createdAt
:
Date
?
=
Date
(),
@Column
(
name
=
"updated_at"
)
@get
:
JsonFormat
(
shape
=
JsonFormat
.
Shape
.
STRING
,
pattern
=
"dd-MM-yyyy HH:mm:ss"
,
timezone
=
"GMT+7"
)
var
updatedAt
:
Date
?
=
Date
(),
@Column
(
name
=
"active"
)
@JsonIgnore
var
active
:
Boolean
?
=
true
)
src/main/kotlin/id/go/kemenag/madrasah/pmrms/auth/service/AuthService.kt
View file @
992e824b
...
...
@@ -7,7 +7,10 @@ import com.fasterxml.jackson.databind.ObjectMapper
import
id.go.kemenag.madrasah.pmrms.auth.constant.EXPIRATION_TIME
import
id.go.kemenag.madrasah.pmrms.auth.constant.SECRET
import
id.go.kemenag.madrasah.pmrms.auth.constant.TOKEN_PREFIX
import
id.go.kemenag.madrasah.pmrms.auth.constant.VALIDATOR_MSG_NOT_FOUND
import
id.go.kemenag.madrasah.pmrms.auth.exception.BadRequestException
import
id.go.kemenag.madrasah.pmrms.auth.helpers.getUserLogin
import
id.go.kemenag.madrasah.pmrms.auth.helpers.responseNotFound
import
id.go.kemenag.madrasah.pmrms.auth.helpers.responseSuccess
import
id.go.kemenag.madrasah.pmrms.auth.model.request.auth.LoginRequest
import
id.go.kemenag.madrasah.pmrms.auth.model.response.ReturnData
...
...
@@ -62,4 +65,16 @@ class AuthService {
return
jwtres
}
fun
detail
():
ResponseEntity
<
ReturnData
>
{
try
{
val
data
=
repoUser
.
findByIdAndActive
(
getUserLogin
()
?.
id
)
if
(
data
.
isPresent
)
{
return
responseSuccess
(
data
=
data
.
get
())
}
return
responseNotFound
(
"User $VALIDATOR_MSG_NOT_FOUND"
)
}
catch
(
e
:
Exception
)
{
throw
e
}
}
}
src/main/resources/application.properties
View file @
992e824b
# SERVER CONFIG
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS
=
false
spring.profiles.active
=
development
server.port
=
8080
#server.port=8080
server.port
=
8081
# DATABASE CONFIG
# local
...
...
@@ -28,13 +29,6 @@ spring.jpa.properties.hibernate.format_sql=true
spring.servlet.multipart.max-file-size
=
500MB
spring.servlet.multipart.max-request-size
=
500MB
# MAIl CONFIG
spring.mail.host
=
mail.smtp2go.com
spring.mail.port
=
2525
spring.mail.username
=
kominfo-05
spring.mail.password
=
eXFwenN6cm4yYmMw
spring.mail.from
=
no-reply@layanan.go.id
spring.mail.properties.mail.smtp.auth
=
true
spring.mail.properties.mail.smtp.connectiontimeout
=
25000
spring.mail.properties.mail.smtp.timeout
=
25000
...
...
@@ -45,3 +39,8 @@ spring.mail.properties.mail.smtp.starttls.enable=true
app.name
=
PMRMS
server.max-http-header-size
=
10000KB
# Swagger
swagger.host
=
localhost:8081
swagger.protocol
=
http
spring.main.allow-circular-references
=
true
spring.mvc.pathmatch.matching-strategy
=
ant_path_matcher
src/test/kotlin/id/go/kemenag/madrasah/pmrms/auth/AuthServiceTest.kt
0 → 100644
View file @
992e824b
package
id.go.kemenag.madrasah.pmrms.auth
import
id.go.kemenag.madrasah.pmrms.auth.model.request.auth.LoginRequest
import
id.go.kemenag.madrasah.pmrms.auth.model.response.ReturnData
import
id.go.kemenag.madrasah.pmrms.auth.service.AuthService
import
org.assertj.core.api.Assertions
import
org.junit.jupiter.api.Test
import
org.springframework.beans.factory.annotation.Autowired
import
org.springframework.boot.test.context.SpringBootTest
import
org.springframework.http.HttpStatus
import
org.springframework.http.ResponseEntity
@SpringBootTest
class
AuthServiceTest
{
@Autowired
private
lateinit
var
service
:
AuthService
@Test
fun
testLogin
()
{
val
test
:
ResponseEntity
<
ReturnData
>
=
service
.
login
(
LoginRequest
(
"admin@madrasah.kemenag.go.id"
,
"123456"
))
Assertions
.
assertThat
(
test
.
statusCode
).
isNotEqualTo
(
HttpStatus
.
INTERNAL_SERVER_ERROR
)
}
@Test
fun
testDetail
()
{
val
test
:
ResponseEntity
<
ReturnData
>
=
service
.
detail
()
Assertions
.
assertThat
(
test
.
statusCode
).
isNotEqualTo
(
HttpStatus
.
INTERNAL_SERVER_ERROR
)
}
}
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