-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gh-2: adds access control and users controller
- Loading branch information
Showing
17 changed files
with
479 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 42 additions & 1 deletion
43
code/tic-tac-tow-service/src/main/kotlin/pt/isel/daw/tictactow/TicTacTowApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
code/tic-tac-tow-service/src/main/kotlin/pt/isel/daw/tictactow/domain/UserLogic.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
code/tic-tac-tow-service/src/main/kotlin/pt/isel/daw/tictactow/http/UsersController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package pt.isel.daw.tictactow.http | ||
|
||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RestController | ||
import pt.isel.daw.tictactow.Either | ||
import pt.isel.daw.tictactow.domain.User | ||
import pt.isel.daw.tictactow.http.model.Problem | ||
import pt.isel.daw.tictactow.http.model.UserCreateInputModel | ||
import pt.isel.daw.tictactow.http.model.UserCreateTokenInputModel | ||
import pt.isel.daw.tictactow.http.model.UserHomeOutputModel | ||
import pt.isel.daw.tictactow.http.model.UserTokenCreateOutputModel | ||
import pt.isel.daw.tictactow.service.TokenCreationError | ||
import pt.isel.daw.tictactow.service.UserCreationError | ||
import pt.isel.daw.tictactow.service.UsersService | ||
|
||
@RestController | ||
class UsersController( | ||
private val userService: UsersService | ||
) { | ||
|
||
@PostMapping(Uris.USERS_CREATE) | ||
fun create(@RequestBody input: UserCreateInputModel): ResponseEntity<*> { | ||
val res = userService.createUser(input.username, input.password) | ||
return when (res) { | ||
is Either.Right -> ResponseEntity.status(201) | ||
.header( | ||
"Location", | ||
Uris.userById(res.value).toASCIIString() | ||
).build<Unit>() | ||
is Either.Left -> when (res.value) { | ||
UserCreationError.InsecurePassword -> Problem.response(400, Problem.insecurePassword) | ||
UserCreationError.UserAlreadyExists -> Problem.response(400, Problem.userAlreadyExists) | ||
} | ||
} | ||
} | ||
|
||
@PostMapping(Uris.USERS_TOKEN) | ||
fun token(@RequestBody input: UserCreateTokenInputModel): ResponseEntity<*> { | ||
val res = userService.createToken(input.username, input.password) | ||
return when (res) { | ||
is Either.Right -> ResponseEntity.status(200) | ||
.body(UserTokenCreateOutputModel(res.value)) | ||
is Either.Left -> when (res.value) { | ||
TokenCreationError.UserOrPasswordAreInvalid -> Problem.response(400, Problem.userOrPasswordAreInvalid) | ||
} | ||
} | ||
} | ||
|
||
@GetMapping(Uris.USERS_GET_BY_ID) | ||
fun getById(@PathVariable id: String) { | ||
TODO("TODO") | ||
} | ||
|
||
@GetMapping(Uris.USER_HOME) | ||
fun getUserHome(user: User): UserHomeOutputModel { | ||
return UserHomeOutputModel( | ||
id = user.id.toString(), | ||
username = user.username, | ||
) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
code/tic-tac-tow-service/src/main/kotlin/pt/isel/daw/tictactow/http/model/Problem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package pt.isel.daw.tictactow.http.model | ||
|
||
import org.springframework.http.ResponseEntity | ||
import java.net.URI | ||
|
||
class Problem( | ||
typeUri: URI | ||
) { | ||
val type = typeUri.toASCIIString() | ||
|
||
companion object { | ||
const val MEDIA_TYPE = "application/problem+json" | ||
fun response(status: Int, problem: Problem) = ResponseEntity | ||
.status(status) | ||
.header("Content-Type", MEDIA_TYPE) | ||
.body(problem) | ||
|
||
val userAlreadyExists = Problem( | ||
URI( | ||
"https://github.com/isel-leic-daw/s2223i-51d-51n-public/tree/main/code/tic-tac-tow-service/" + | ||
"docs/problems/user-already-exists" | ||
) | ||
) | ||
val insecurePassword = Problem( | ||
URI( | ||
"https://github.com/isel-leic-daw/s2223i-51d-51n-public/tree/main/code/tic-tac-tow-service/" + | ||
"docs/problems/insecure-password" | ||
) | ||
) | ||
|
||
val userOrPasswordAreInvalid = Problem( | ||
URI( | ||
"https://github.com/isel-leic-daw/s2223i-51d-51n-public/tree/main/code/tic-tac-tow-service/" + | ||
"docs/problems/user-or-password-are-invalid" | ||
) | ||
) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
code/tic-tac-tow-service/src/main/kotlin/pt/isel/daw/tictactow/http/model/UserInputModels.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package pt.isel.daw.tictactow.http.model | ||
|
||
data class UserCreateInputModel( | ||
val username: String, | ||
val password: String, | ||
) | ||
|
||
data class UserCreateTokenInputModel( | ||
val username: String, | ||
val password: String, | ||
) |
10 changes: 10 additions & 0 deletions
10
.../tic-tac-tow-service/src/main/kotlin/pt/isel/daw/tictactow/http/model/UserOutputModels.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package pt.isel.daw.tictactow.http.model | ||
|
||
class UserTokenCreateOutputModel( | ||
val token: String | ||
) | ||
|
||
class UserHomeOutputModel( | ||
val id: String, | ||
val username: String, | ||
) |
39 changes: 39 additions & 0 deletions
39
...-service/src/main/kotlin/pt/isel/daw/tictactow/http/pipeline/AuthenticationInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package pt.isel.daw.tictactow.http.pipeline | ||
|
||
import org.slf4j.LoggerFactory | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.method.HandlerMethod | ||
import org.springframework.web.servlet.HandlerInterceptor | ||
import pt.isel.daw.tictactow.domain.User | ||
import javax.servlet.http.HttpServletRequest | ||
import javax.servlet.http.HttpServletResponse | ||
|
||
@Component | ||
class AuthenticationInterceptor( | ||
private val authorizationHeaderProcessor: AuthorizationHeaderProcessor | ||
) : HandlerInterceptor { | ||
|
||
override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean { | ||
if (handler is HandlerMethod && handler.methodParameters.any { it.parameterType == User::class.java } | ||
) { | ||
// enforce authentication | ||
val user = authorizationHeaderProcessor.process(request.getHeader(NAME_AUTHORIZATION_HEADER)) | ||
if (user == null) { | ||
response.status = 401 | ||
response.addHeader(NAME_WWW_AUTHENTICATE_HEADER, AuthorizationHeaderProcessor.SCHEME) | ||
return false | ||
} else { | ||
UserArgumentResolver.addUserTo(user, request) | ||
return true | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
companion object { | ||
private val logger = LoggerFactory.getLogger(AuthenticationInterceptor::class.java) | ||
private const val NAME_AUTHORIZATION_HEADER = "Authorization" | ||
private const val NAME_WWW_AUTHENTICATE_HEADER = "WWW-Authenticate" | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...rvice/src/main/kotlin/pt/isel/daw/tictactow/http/pipeline/AuthorizationHeaderProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package pt.isel.daw.tictactow.http.pipeline | ||
|
||
import org.slf4j.LoggerFactory | ||
import org.springframework.stereotype.Component | ||
import pt.isel.daw.tictactow.domain.User | ||
import pt.isel.daw.tictactow.service.UsersService | ||
|
||
@Component | ||
class AuthorizationHeaderProcessor( | ||
val usersService: UsersService | ||
) { | ||
|
||
fun process(authorizationValue: String?): User? { | ||
if (authorizationValue == null) { | ||
return null | ||
} | ||
val parts = authorizationValue.trim().split(" ") | ||
if (parts.size != 2) { | ||
return null | ||
} | ||
if (parts[0].lowercase() != SCHEME) { | ||
return null | ||
} | ||
return usersService.getUserByToken(parts[1]) | ||
} | ||
|
||
companion object { | ||
private val logger = LoggerFactory.getLogger(AuthorizationHeaderProcessor::class.java) | ||
const val SCHEME = "bearer" | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...c-tow-service/src/main/kotlin/pt/isel/daw/tictactow/http/pipeline/UserArgumentResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package pt.isel.daw.tictactow.http.pipeline | ||
|
||
import org.springframework.core.MethodParameter | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.bind.support.WebDataBinderFactory | ||
import org.springframework.web.context.request.NativeWebRequest | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver | ||
import org.springframework.web.method.support.ModelAndViewContainer | ||
import pt.isel.daw.tictactow.domain.User | ||
import javax.servlet.http.HttpServletRequest | ||
|
||
@Component | ||
class UserArgumentResolver : HandlerMethodArgumentResolver { | ||
|
||
override fun supportsParameter(parameter: MethodParameter) = parameter.parameterType == User::class.java | ||
|
||
override fun resolveArgument( | ||
parameter: MethodParameter, | ||
mavContainer: ModelAndViewContainer?, | ||
webRequest: NativeWebRequest, | ||
binderFactory: WebDataBinderFactory? | ||
): Any? { | ||
val request = webRequest.getNativeRequest(HttpServletRequest::class.java) | ||
?: throw IllegalStateException("TODO") | ||
return getUserFrom(request) ?: throw IllegalStateException("TODO") | ||
} | ||
|
||
companion object { | ||
private const val KEY = "UserArgumentResolver" | ||
|
||
fun addUserTo(user: User, request: HttpServletRequest) { | ||
return request.setAttribute(KEY, user) | ||
} | ||
|
||
fun getUserFrom(request: HttpServletRequest): User? { | ||
return request.getAttribute(KEY)?.let { | ||
it as? User | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...w-service/src/main/kotlin/pt/isel/daw/tictactow/repository/jdbi/JdbiTransactionManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.