package dp.kotul.starter.service

import dp.kotul.jwt.model.User
import dp.kotul.starter.filter.AUTHORIZATION_HEADER
import dp.kotul.starter.filter.RequestAttr
import org.springframework.stereotype.Service
import javax.servlet.http.HttpServletRequest

interface AuthorizationService {
    fun getUser(): User
    fun getValidAuthorizationHeader(): String
}

@Suppress("UNCHECKED_CAST")
@Service
class AuthorizationServiceImpl(val request: HttpServletRequest) : AuthorizationService {

    override fun getUser(): User {
        return request.getAttribute(RequestAttr.USER.key) as User
    }

    override fun getValidAuthorizationHeader(): String {
        getUser()
        return request.getHeader(AUTHORIZATION_HEADER)
    }
}
